[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Database / Schema / SchemaMigratorTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Core\Tests\Functional\Database\Schema;
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\AbstractSchemaManager;
20 use Doctrine\DBAL\Schema\Table;
21 use Doctrine\DBAL\Types\BigIntType;
22 use Doctrine\DBAL\Types\IntegerType;
23 use TYPO3\CMS\Core\Database\ConnectionPool;
24 use TYPO3\CMS\Core\Database\Schema\SchemaMigrator;
25 use TYPO3\CMS\Core\Database\Schema\SqlReader;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * Test case for \TYPO3\CMS\Core\Database\Schema\SchemaMigratorTest
30 */
31 class SchemaMigratorTest extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTestCase
32 {
33 /**
34 * @var SqlReader
35 */
36 protected $sqlReader;
37
38 /**
39 * @var ConnectionPool
40 */
41 protected $connectionPool;
42
43 /**
44 * @var AbstractSchemaManager
45 */
46 protected $schemaManager;
47
48 /**
49 * @var \TYPO3\CMS\Core\Database\Schema\SchemaMigrator
50 */
51 protected $subject;
52
53 /**
54 * @var string
55 */
56 protected $tableName = 'a_test_table';
57
58 /**
59 * Sets up this test suite.
60 *
61 * @return void
62 */
63 protected function setUp()
64 {
65 parent::setUp();
66 $this->subject = GeneralUtility::makeInstance(SchemaMigrator::class);
67 $this->sqlReader = GeneralUtility::makeInstance(SqlReader::class);
68 $this->connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
69 $this->schemaManager = $this->connectionPool->getConnectionForTable($this->tableName)->getSchemaManager();
70 $this->prepareTestTable();
71 }
72
73 /**
74 * Tears down this test suite.
75 */
76 protected function tearDown()
77 {
78 parent::tearDown();
79
80 if ($this->schemaManager->tablesExist([$this->tableName])) {
81 $this->schemaManager->dropTable($this->tableName);
82 }
83 if ($this->schemaManager->tablesExist(['zzz_deleted_' . $this->tableName])) {
84 $this->schemaManager->dropTable('zzz_deleted_' . $this->tableName);
85 }
86 if ($this->schemaManager->tablesExist(['another_test_table'])) {
87 $this->schemaManager->dropTable('another_test_table');
88 }
89 }
90
91 /**
92 * @test
93 */
94 public function createNewTable()
95 {
96 if ($this->schemaManager->tablesExist([$this->tableName])) {
97 $this->schemaManager->dropTable($this->tableName);
98 }
99
100 $statements = $this->readFixtureFile('newTable');
101 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
102
103 $this->subject->migrate(
104 $statements,
105 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['create_table']
106 );
107
108 $this->assertCount(5, $this->getTableDetails()->getColumns());
109 }
110
111 /**
112 * @test
113 */
114 public function createNewTableIfNotExists()
115 {
116 $statements = $this->readFixtureFile('ifNotExists');
117 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
118
119 $this->subject->migrate(
120 $statements,
121 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['create_table']
122 );
123
124 $this->assertTrue($this->schemaManager->tablesExist(['another_test_table']));
125 }
126
127 /**
128 * @test
129 */
130 public function addNewColumns()
131 {
132 $statements = $this->readFixtureFile('addColumnsToTable');
133 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
134
135 $this->subject->migrate(
136 $statements,
137 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
138 );
139
140 $this->assertCount(7, $this->getTableDetails()->getColumns());
141 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
142 $this->assertTrue($this->getTableDetails()->hasColumn('description'));
143 }
144
145 /**
146 * @test
147 */
148 public function changeExistingColumn()
149 {
150 $statements = $this->readFixtureFile('changeExistingColumn');
151 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
152
153 $this->assertInstanceOf(IntegerType::class, $this->getTableDetails()->getColumn('uid')->getType());
154 $this->assertTrue($this->getTableDetails()->getColumn('uid')->getUnsigned());
155
156 $this->subject->migrate(
157 $statements,
158 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
159 );
160
161 $this->assertInstanceOf(BigIntType::class, $this->getTableDetails()->getColumn('uid')->getType());
162 $this->assertFalse($this->getTableDetails()->getColumn('uid')->getUnsigned());
163 }
164
165 /**
166 * @test
167 */
168 public function notNullWithoutDefaultValue()
169 {
170 $statements = $this->readFixtureFile('notNullWithoutDefaultValue');
171 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
172
173 $this->subject->migrate(
174 $statements,
175 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
176 );
177
178 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
179 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
180 $this->assertTrue($this->getTableDetails()->getColumn('aTestField')->getNotnull());
181 }
182
183 /**
184 * @test
185 */
186 public function defaultNullWithoutNotNull()
187 {
188 $statements = $this->readFixtureFile('defaultNullWithoutNotNull');
189 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
190
191 $this->subject->migrate(
192 $statements,
193 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
194 );
195
196 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
197 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
198 $this->assertFalse($this->getTableDetails()->getColumn('aTestField')->getNotnull());
199 $this->assertNull($this->getTableDetails()->getColumn('aTestField')->getDefault());
200 }
201
202 /**
203 * @test
204 */
205 public function renameUnusedField()
206 {
207 $statements = $this->readFixtureFile('unusedColumn');
208 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
209
210 $this->subject->migrate(
211 $statements,
212 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
213 );
214
215 $this->assertFalse($this->getTableDetails()->hasColumn('hidden'));
216 $this->assertTrue($this->getTableDetails()->hasColumn('zzz_deleted_hidden'));
217 }
218
219 /**
220 * @test
221 */
222 public function renameUnusedTable()
223 {
224 $statements = $this->readFixtureFile('unusedTable');
225 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
226
227 $this->subject->migrate(
228 $statements,
229 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change_table']
230 );
231
232 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
233 $this->assertContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
234 }
235
236 /**
237 * @test
238 */
239 public function dropUnusedField()
240 {
241 $connection = $this->connectionPool->getConnectionForTable($this->tableName);
242 $fromSchema = $this->schemaManager->createSchema();
243 $toSchema = clone $fromSchema;
244 $toSchema->getTable($this->tableName)->addColumn('zzz_deleted_testfield', 'integer');
245 $statements = $fromSchema->getMigrateToSql(
246 $toSchema,
247 $connection->getDatabasePlatform()
248 );
249 $connection->executeUpdate($statements[0]);
250 $this->assertTrue($this->getTableDetails()->hasColumn('zzz_deleted_testfield'));
251
252 $statements = $this->readFixtureFile('newTable');
253 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
254 $this->subject->migrate(
255 $statements,
256 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['drop']
257 );
258
259 $this->assertFalse($this->getTableDetails()->hasColumn('zzz_deleted_testfield'));
260 }
261
262 /**
263 * @test
264 */
265 public function dropUnusedTable()
266 {
267 $this->schemaManager->renameTable($this->tableName, 'zzz_deleted_' . $this->tableName);
268 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
269 $this->assertContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
270
271 $statements = $this->readFixtureFile('newTable');
272 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
273 $this->subject->migrate(
274 $statements,
275 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['drop_table']
276 );
277
278 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
279 $this->assertNotContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
280 }
281
282 /**
283 * @test
284 */
285 public function installPerformsOnlyAddAndCreateOperations()
286 {
287 $statements = $this->readFixtureFile('addCreateChange');
288 $this->subject->install($statements, true);
289
290 $this->assertContains('another_test_table', $this->schemaManager->listTableNames());
291 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
292 $this->assertTrue($this->getTableDetails()->hasIndex('title'));
293 $this->assertTrue($this->getTableDetails()->getIndex('title')->isUnique());
294 $this->assertNotInstanceOf(BigIntType::class, $this->getTableDetails()->getColumn('pid')->getType());
295 }
296
297 /**
298 * @test
299 */
300 public function installCanPerformChangeOperations()
301 {
302 $statements = $this->readFixtureFile('addCreateChange');
303 $this->subject->install($statements);
304
305 $this->assertContains('another_test_table', $this->schemaManager->listTableNames());
306 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
307 $this->assertTrue($this->getTableDetails()->hasIndex('title'));
308 $this->assertTrue($this->getTableDetails()->getIndex('title')->isUnique());
309 $this->assertInstanceOf(BigIntType::class, $this->getTableDetails()->getColumn('pid')->getType());
310 }
311
312 /**
313 * @test
314 */
315 public function importStaticDataInsertsRecords()
316 {
317 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'importStaticData.sql']));
318 $connection = $this->connectionPool->getConnectionForTable($this->tableName);
319 $statements = $this->sqlReader->getInsertStatementArray($sqlCode);
320 $this->subject->importStaticData($statements);
321
322 $this->assertEquals(2, $connection->count('*', $this->tableName, []));
323 }
324
325 /**
326 * @test
327 */
328 public function importStaticDataIgnoresTableDefinitions()
329 {
330 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'importStaticData.sql']));
331 $statements = $this->sqlReader->getStatementArray($sqlCode);
332 $this->subject->importStaticData($statements);
333
334 $this->assertNotContains('another_test_table', $this->schemaManager->listTableNames());
335 }
336
337 /**
338 * @test
339 */
340 public function changeTableEngine()
341 {
342 $statements = $this->readFixtureFile('alterTableEngine');
343 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
344
345 $index = array_keys($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change'])[0];
346 $this->assertStringEndsWith(
347 'ENGINE = MyISAM',
348 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change'][$index]
349 );
350
351 $this->subject->migrate(
352 $statements,
353 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
354 );
355
356 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
357 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
358 }
359
360 /**
361 * Create the base table for all migration tests
362 */
363 protected function prepareTestTable()
364 {
365 $statements = $this->readFixtureFile('newTable');
366 $this->subject->install($statements, true);
367 }
368
369 /**
370 * Helper to return the Doctrine Table object for the test table
371 *
372 * @return \Doctrine\DBAL\Schema\Table
373 */
374 protected function getTableDetails(): Table
375 {
376 return $this->schemaManager->listTableDetails($this->tableName);
377 }
378
379 /**
380 * Helper to read a fixture SQL file and convert it into a statement array.
381 *
382 * @param string $fixtureName
383 * @return array
384 */
385 protected function readFixtureFile(string $fixtureName): array
386 {
387 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', $fixtureName]) . '.sql');
388
389 return $this->sqlReader->getCreateTableStatementArray($sqlCode);
390 }
391 }