[BUGFIX] Respect SQL table names max length in install tool
[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\Schema\SchemaDiff;
20 use Doctrine\DBAL\Schema\Table;
21 use TYPO3\CMS\Core\Database\Connection;
22 use TYPO3\CMS\Core\Database\Schema\ConnectionMigrator;
23 use TYPO3\CMS\Core\Tests\UnitTestCase;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Tests for ConnectionMigrator
28 */
29 class ConnectionMigratorTest extends UnitTestCase
30 {
31
32 /**
33 * @test
34 */
35 public function tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
36 {
37 $ridiculouslyLongTableName = 'table_name_that_is_ridiculously_long_' . random_bytes(200);
38 $tableMock = $this->getAccessibleMock(Table::class, ['getQuotedName'], [$ridiculouslyLongTableName]);
39 $tableMock->expects($this->any())->method('getQuotedName')->withAnyParameters()->will($this->returnValue($ridiculouslyLongTableName));
40
41 $platform = $this->getMockBuilder(\Doctrine\DBAL\Platforms\AbstractPlatform::class)->disableOriginalConstructor()->getMock();
42
43 $connectionMock = $this->getMockBuilder(Connection::class)->setMethods(['getDatabasePlatform'])->disableOriginalConstructor()->getMock();
44 $connectionMock->method('getDatabasePlatform')->willReturn($platform);
45
46 $connectionMigrator = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
47 $connectionMigrator->_set('connection', $connectionMock);
48
49 $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$tableMock]);
50
51 $renamedSchemaDiff = $connectionMigrator->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
52
53 $this->assertStringStartsWith('zzz_deleted_', $renamedSchemaDiff->changedTables[0]->newName);
54 $this->assertLessThanOrEqual(
55 strlen($renamedSchemaDiff->changedTables[0]->newName),
56 $this->maxTableNameLength
57 );
58 }
59 }