[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / SqlSchemaMigrationServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Install\Service\SqlSchemaMigrationService;
19
20 /**
21 * Test case
22 */
23 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
25 /**
26 * Get a SchemaService instance with mocked DBAL enable database connection, DBAL not enabled
27 *
28 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
29 */
30 protected function getSqlSchemaMigrationService()
31 {
32 /** @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $databaseConnection */
33 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', false);
34 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(false));
35
36 return $subject;
37 }
38
39 /**
40 * Get a SchemaService instance with mocked DBAL enable database connection, DBAL enabled
41 *
42 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
43 */
44 protected function getDbalEnabledSqlSchemaMigrationService()
45 {
46 /** @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $databaseConnection */
47 $databaseConnection = $this->getAccessibleMock(\TYPO3\CMS\Dbal\Database\DatabaseConnection::class, array('dummy'), array(), '', false);
48 $databaseConnection->_set('dbmsSpecifics', GeneralUtility::makeInstance(\TYPO3\CMS\Dbal\Database\Specifics\PostgresSpecifics::class));
49
50 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled', 'getDatabaseConnection'), array(), '', false);
51 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(true));
52 $subject->expects($this->any())->method('getDatabaseConnection')->will($this->returnValue($databaseConnection));
53
54 return $subject;
55 }
56
57 /**
58 * @test
59 */
60 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions()
61 {
62 $subject = $this->getSqlSchemaMigrationService();
63 // Multiple whitespaces and tabs in field definition
64 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
65 $result = $subject->getFieldDefinitions_fileContent($inputString);
66
67 $this->assertEquals(
68 array(
69 'atable' => array(
70 'fields' => array(
71 'aFieldName' => 'int(11) unsigned default \'0\'',
72 ),
73 'extra' => array(
74 'COLLATE' => '',
75 ),
76 ),
77 ),
78 $result
79 );
80 }
81
82 /**
83 * @test
84 */
85 public function getDatabaseExtraFindsChangedFields()
86 {
87 $subject = $this->getSqlSchemaMigrationService();
88 $differenceArray = $subject->getDatabaseExtra(
89 array(
90 'tx_foo' => array(
91 'fields' => array(
92 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
93 )
94 )
95 ),
96 array(
97 'tx_foo' => array(
98 'fields' => array(
99 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
100 )
101 )
102 )
103 );
104
105 $this->assertEquals(
106 $differenceArray,
107 array(
108 'extra' => array(),
109 'diff' => array(
110 'tx_foo' => array(
111 'fields' => array(
112 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
113 )
114 )
115 ),
116 'diff_currentValues' => array(
117 'tx_foo' => array(
118 'fields' => array(
119 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
120 )
121 )
122 )
123 )
124 );
125 }
126
127 /**
128 * @test
129 */
130 public function getDatabaseExtraFindsChangedFieldsIncludingNull()
131 {
132 $subject = $this->getSqlSchemaMigrationService();
133 $differenceArray = $subject->getDatabaseExtra(
134 array(
135 'tx_foo' => array(
136 'fields' => array(
137 'foo' => 'varchar(999) NULL'
138 )
139 )
140 ),
141 array(
142 'tx_foo' => array(
143 'fields' => array(
144 'foo' => 'varchar(255) NULL'
145 )
146 )
147 )
148 );
149
150 $this->assertEquals(
151 $differenceArray,
152 array(
153 'extra' => array(),
154 'diff' => array(
155 'tx_foo' => array(
156 'fields' => array(
157 'foo' => 'varchar(999) NULL'
158 )
159 )
160 ),
161 'diff_currentValues' => array(
162 'tx_foo' => array(
163 'fields' => array(
164 'foo' => 'varchar(255) NULL'
165 )
166 )
167 )
168 )
169 );
170 }
171
172 /**
173 * @test
174 */
175 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull()
176 {
177 $subject = $this->getSqlSchemaMigrationService();
178 $differenceArray = $subject->getDatabaseExtra(
179 array(
180 'tx_foo' => array(
181 'fields' => array(
182 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
183 )
184 )
185 ),
186 array(
187 'tx_foo' => array(
188 'fields' => array(
189 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
190 )
191 )
192 ),
193 '',
194 true
195 );
196
197 $this->assertEquals(
198 $differenceArray,
199 array(
200 'extra' => array(),
201 'diff' => array(
202 'tx_foo' => array(
203 'fields' => array(
204 'foo' => 'varchar(999) DEFAULT \'0\''
205 )
206 )
207 ),
208 'diff_currentValues' => array(
209 'tx_foo' => array(
210 'fields' => array(
211 'foo' => 'varchar(255) DEFAULT \'0\''
212 )
213 )
214 )
215 )
216 );
217 }
218
219 /**
220 * @test
221 */
222 public function getDatabaseExtraIgnoresCaseDifference()
223 {
224 $subject = $this->getSqlSchemaMigrationService();
225 $differenceArray = $subject->getDatabaseExtra(
226 array(
227 'tx_foo' => array(
228 'fields' => array(
229 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
230 )
231 )
232 ),
233 array(
234 'tx_foo' => array(
235 'fields' => array(
236 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
237 )
238 )
239 )
240 );
241
242 $this->assertEquals(
243 $differenceArray,
244 array(
245 'extra' => array(),
246 'diff' => array(),
247 'diff_currentValues' => null,
248 )
249 );
250 }
251
252 /**
253 * @test
254 */
255 public function getDatabaseExtraIgnoresCaseDifferenceButKeepsCaseInSetIntact()
256 {
257 $subject = $this->getSqlSchemaMigrationService();
258 $differenceArray = $subject->getDatabaseExtra(
259 array(
260 'tx_foo' => array(
261 'fields' => array(
262 'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
263 )
264 )
265 ),
266 array(
267 'tx_foo' => array(
268 'fields' => array(
269 'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
270 )
271 )
272 )
273 );
274
275 $this->assertEquals(
276 $differenceArray,
277 array(
278 'extra' => array(),
279 'diff' => array(),
280 'diff_currentValues' => null,
281 )
282 );
283 }
284
285 /**
286 * @test
287 */
288 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison()
289 {
290 $subject = $this->getSqlSchemaMigrationService();
291 $differenceArray = $subject->getDatabaseExtra(
292 array(
293 'tx_foo' => array(
294 'fields' => array(
295 'PRIMARY KEY (md5hash)',
296 )
297 )
298 ),
299 array(
300 'tx_foo' => array(
301 'fields' => array(
302 'PRIMARY KEY (md5hash)'),
303 )
304 )
305 );
306
307 $this->assertEquals(
308 $differenceArray,
309 array(
310 'extra' => array(),
311 'diff' => array(),
312 'diff_currentValues' => null,
313 )
314 );
315 }
316
317 /**
318 * @test
319 */
320 public function getDatabaseExtraFindsNewSpatialKeys()
321 {
322 $subject = $this->getSqlSchemaMigrationService();
323 $differenceArray = $subject->getDatabaseExtra(
324 array(
325 'tx_foo' => array(
326 'keys' => array(
327 'foo' => 'SPATIAL foo (foo)'
328 )
329 )
330 ),
331 array(
332 'tx_foo' => array(
333 'keys' => array()
334 )
335 )
336 );
337
338 $this->assertEquals(
339 $differenceArray,
340 array(
341 'extra' => array(
342 'tx_foo' => array(
343 'keys' => array(
344 'foo' => 'SPATIAL foo (foo)'
345 )
346 )
347 ),
348 'diff' => array(),
349 'diff_currentValues' => null
350 )
351 );
352 }
353
354 /**
355 * @test
356 */
357 public function checkColumnDefinitionIfCommentIsSupplied()
358 {
359 $subject = $this->getSqlSchemaMigrationService();
360 $fieldDefinition = $subject->assembleFieldDefinition(
361 array(
362 'Field' => 'uid',
363 'Type' => 'int(11)',
364 'Null' => 'NO',
365 'Key' => 'PRI',
366 'Default' => null,
367 'Extra' => 'auto_increment',
368 'Comment' => 'I am a comment',
369 )
370 );
371
372 $this->assertSame(
373 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
374 $fieldDefinition
375 );
376 }
377
378 /**
379 * @test
380 */
381 public function checkColumnDefinitionIfNoCommentIsSupplied()
382 {
383 $subject = $this->getSqlSchemaMigrationService();
384 $fieldDefinition = $subject->assembleFieldDefinition(
385 array(
386 'Field' => 'uid',
387 'Type' => 'int(11)',
388 'Null' => 'NO',
389 'Key' => 'PRI',
390 'Default' => null,
391 'Extra' => 'auto_increment',
392 )
393 );
394
395 $this->assertSame(
396 'int(11) NOT NULL auto_increment',
397 $fieldDefinition
398 );
399 }
400
401 /**
402 * @test
403 */
404 public function getDatabaseExtraIncludesEngineIfMySQLIsUsed()
405 {
406 $subject = $this->getSqlSchemaMigrationService();
407 $differenceArray = $subject->getDatabaseExtra(
408 array(
409 'tx_foo' => array(
410 'fields' => array(
411 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
412 ),
413 'extra' => array(
414 'ENGINE' => 'InnoDB'
415 )
416 )
417 ),
418 array(
419 'tx_foo' => array(
420 'fields' => array(
421 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
422 ),
423 'extra' => array(
424 'ENGINE' => 'InnoDB'
425 )
426 )
427 )
428 );
429
430 $this->assertSame(
431 $differenceArray,
432 array(
433 'extra' => array(),
434 'diff' => array(),
435 'diff_currentValues' => null,
436 )
437 );
438 }
439
440 /**
441 * @test
442 */
443 public function getDatabaseExtraExcludesEngineIfDbalIsUsed()
444 {
445 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
446 $differenceArray = $subject->getDatabaseExtra(
447 array(
448 'tx_foo' => array(
449 'fields' => array(
450 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
451 ),
452 'extra' => array(
453 'ENGINE' => 'InnoDB'
454 )
455 )
456 ),
457 array(
458 'tx_foo' => array(
459 'fields' => array(
460 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
461 ),
462 'extra' => array()
463 )
464 )
465 );
466
467 $this->assertSame(
468 $differenceArray,
469 array(
470 'extra' => array(),
471 'diff' => array(),
472 'diff_currentValues' => null,
473 )
474 );
475 }
476
477 /**
478 * @test
479 */
480 public function getDatabaseExtraIncludesUnsignedAttributeIfMySQLIsUsed()
481 {
482 $subject = $this->getSqlSchemaMigrationService();
483 $differenceArray = $subject->getDatabaseExtra(
484 array(
485 'tx_foo' => array(
486 'fields' => array(
487 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
488 ),
489 'extra' => array(
490 'ENGINE' => 'InnoDB'
491 )
492 )
493 ),
494 array(
495 'tx_foo' => array(
496 'fields' => array(
497 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
498 ),
499 'extra' => array(
500 'ENGINE' => 'InnoDB'
501 )
502 )
503 )
504 );
505
506 $this->assertSame(
507 $differenceArray,
508 array(
509 'extra' => array(),
510 'diff' => array(
511 'tx_foo' => array(
512 'fields' => array(
513 'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
514 ),
515 )
516 ),
517 'diff_currentValues' => array(
518 'tx_foo' => array(
519 'fields' => array(
520 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
521 ),
522 )
523 )
524 )
525 );
526 }
527
528 /**
529 * @test
530 */
531 public function getDatabaseExtraExcludesUnsignedAttributeIfDbalIsUsed()
532 {
533 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
534 $differenceArray = $subject->getDatabaseExtra(
535 array(
536 'tx_foo' => array(
537 'fields' => array(
538 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
539 ),
540 'extra' => array(
541 'ENGINE' => 'InnoDB'
542 )
543 )
544 ),
545 array(
546 'tx_foo' => array(
547 'fields' => array(
548 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
549 ),
550 'extra' => array(
551 'ENGINE' => 'InnoDB'
552 )
553 )
554 )
555 );
556
557 $this->assertSame(
558 $differenceArray,
559 array(
560 'extra' => array(),
561 'diff' => array(),
562 'diff_currentValues' => null
563 )
564 );
565 }
566
567 /**
568 * @test
569 */
570 public function getDatabaseExtraIgnoresIndexPrefixLengthIfDbalIsUsed()
571 {
572 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
573 $differenceArray = $subject->getDatabaseExtra(
574 array(
575 'tx_foo' => array(
576 'keys' => array(
577 'foo' => 'KEY foo (foo(199))'
578 )
579 )
580 ),
581 array(
582 'tx_foo' => array(
583 'keys' => array(
584 'foo' => 'KEY foo (foo)'
585 )
586 )
587 )
588 );
589
590 $this->assertSame(
591 $differenceArray,
592 array(
593 'extra' => array(),
594 'diff' => array(),
595 'diff_currentValues' => null,
596 )
597 );
598 }
599
600 /**
601 * @test
602 */
603 public function getDatabaseExtraComparesIndexPrefixLengthIfMySQLIsUsed()
604 {
605 $subject = $this->getSqlSchemaMigrationService();
606 $differenceArray = $subject->getDatabaseExtra(
607 array(
608 'tx_foo' => array(
609 'keys' => array(
610 'foo' => 'KEY foo (foo(199))'
611 )
612 )
613 ),
614 array(
615 'tx_foo' => array(
616 'keys' => array(
617 'foo' => 'KEY foo (foo)'
618 )
619 )
620 )
621 );
622
623 $this->assertSame(
624 $differenceArray,
625 array(
626 'extra' => array(),
627 'diff' => array(
628 'tx_foo' => array(
629 'keys' => array(
630 'foo' => 'KEY foo (foo(199))'
631 )
632 )
633 ),
634 'diff_currentValues' => array(
635 'tx_foo' => array(
636 'keys' => array(
637 'foo' => 'KEY foo (foo)'
638 )
639 )
640 )
641 )
642 );
643 }
644 }