[BUGFIX] Process large uid lists in chunks in RelationHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / ConnectionMigratorTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Database;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Doctrine\DBAL\Platforms\MySqlPlatform;
20 use Doctrine\DBAL\Schema\Column;
21 use Doctrine\DBAL\Schema\SchemaDiff;
22 use Doctrine\DBAL\Schema\Table;
23 use Doctrine\DBAL\Types\Type;
24 use Prophecy\Argument;
25 use TYPO3\CMS\Core\Database\Connection;
26 use TYPO3\CMS\Core\Database\Platform\PlatformInformation;
27 use TYPO3\CMS\Core\Database\Schema\ConnectionMigrator;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Tests for ConnectionMigrator
32 */
33 class ConnectionMigratorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
34 {
35 /**
36 * @var \Doctrine\DBAL\Platforms\AbstractPlatform|\Prophecy\Prophecy\ObjectProphecy
37 */
38 protected $platform;
39
40 /**
41 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
42 */
43 protected $subject;
44
45 /**
46 * @var int
47 */
48 protected $maxIdentifierLength = -1;
49
50 /**
51 * Set up the test subject
52 */
53 protected function setUp()
54 {
55 parent::setUp();
56
57 $platformMock = $this->prophesize(MySqlPlatform::class);
58 $platformMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
59 $this->platform = $platformMock->reveal();
60
61 $connectionMock = $this->prophesize(Connection::class);
62 $connectionMock->getDatabasePlatform()->willReturn($this->platform);
63 $connectionMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
64
65 $this->maxIdentifierLength = PlatformInformation::getMaxIdentifierLength($this->platform);
66
67 $this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
68 $this->subject->_set('connection', $connectionMock->reveal());
69 }
70
71 /**
72 * @test
73 */
74 public function tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
75 {
76 $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$this->getTable()]);
77 $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
78
79 $this->assertStringStartsWith('zzz_deleted_', $renamedSchemaDiff->changedTables[0]->newName);
80 $this->assertEquals(
81 $this->maxIdentifierLength,
82 strlen($renamedSchemaDiff->changedTables[0]->newName)
83 );
84 }
85
86 /**
87 * @test
88 */
89 public function columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
90 {
91 $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$this->getTable()]);
92 $originalSchemaDiff->changedTables[0]->removedColumns[] = $this->getColumn();
93 $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedFieldsToRenames', $originalSchemaDiff);
94
95 $this->assertStringStartsWith(
96 'zzz_deleted_',
97 $renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName()
98 );
99 $this->assertEquals(
100 $this->maxIdentifierLength,
101 strlen($renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName())
102 );
103 }
104
105 /**
106 * Utility method to create a table instance with name that exceeds the identifier limits.
107 *
108 * @return Table
109 */
110 protected function getTable(): Table
111 {
112 $tableName = 'table_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
113 $table = GeneralUtility::makeInstance(
114 Table::class,
115 $tableName
116 );
117
118 return $table;
119 }
120
121 /**
122 * Utility method to create a column instance with name that exceeds the identifier limits.
123 *
124 *
125 * @return Column
126 */
127 protected function getColumn(): Column
128 {
129 $columnName = 'column_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
130 $column = GeneralUtility::makeInstance(
131 Column::class,
132 $columnName,
133 Type::getType('string')
134 );
135
136 return $column;
137 }
138 }