548592580144ab277c82f4ff564a935d802640ed
[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\Install\Service\SqlSchemaMigrationService;
18
19 /**
20 * Test case
21 */
22 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @test
26 */
27 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions() {
28 $subject = new SqlSchemaMigrationService();
29 // Multiple whitespaces and tabs in field definition
30 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
31 $result = $subject->getFieldDefinitions_fileContent($inputString);
32
33 $this->assertEquals(
34 array(
35 'atable' => array(
36 'fields' => array(
37 'aFieldName' => 'int(11) unsigned default \'0\'',
38 ),
39 'extra' => array(
40 'COLLATE' => '',
41 ),
42 ),
43 ),
44 $result
45 );
46 }
47
48 /**
49 * @test
50 */
51 public function getDatabaseExtraFindsChangedFields() {
52 $subject = new SqlSchemaMigrationService();
53 $differenceArray = $subject->getDatabaseExtra(
54 array(
55 'tx_foo' => array(
56 'fields' => array(
57 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
58 )
59 )
60 ),
61 array(
62 'tx_foo' => array(
63 'fields' => array(
64 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
65 )
66 )
67 )
68 );
69
70 $this->assertEquals(
71 $differenceArray,
72 array(
73 'extra' => array(),
74 'diff' => array(
75 'tx_foo' => array(
76 'fields' => array(
77 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
78 )
79 )
80 ),
81 'diff_currentValues' => array(
82 'tx_foo' => array(
83 'fields' => array(
84 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
85 )
86 )
87 )
88 )
89 );
90 }
91
92 /**
93 * @test
94 */
95 public function getDatabaseExtraFindsChangedFieldsIncludingNull() {
96 $subject = new SqlSchemaMigrationService();
97 $differenceArray = $subject->getDatabaseExtra(
98 array(
99 'tx_foo' => array(
100 'fields' => array(
101 'foo' => 'varchar(999) NULL'
102 )
103 )
104 ),
105 array(
106 'tx_foo' => array(
107 'fields' => array(
108 'foo' => 'varchar(255) NULL'
109 )
110 )
111 )
112 );
113
114 $this->assertEquals(
115 $differenceArray,
116 array(
117 'extra' => array(),
118 'diff' => array(
119 'tx_foo' => array(
120 'fields' => array(
121 'foo' => 'varchar(999) NULL'
122 )
123 )
124 ),
125 'diff_currentValues' => array(
126 'tx_foo' => array(
127 'fields' => array(
128 'foo' => 'varchar(255) NULL'
129 )
130 )
131 )
132 )
133 );
134 }
135
136 /**
137 * @test
138 */
139 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull() {
140 $subject = new SqlSchemaMigrationService();
141 $differenceArray = $subject->getDatabaseExtra(
142 array(
143 'tx_foo' => array(
144 'fields' => array(
145 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
146 )
147 )
148 ),
149 array(
150 'tx_foo' => array(
151 'fields' => array(
152 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
153 )
154 )
155 ),
156 '',
157 TRUE
158 );
159
160 $this->assertEquals(
161 $differenceArray,
162 array(
163 'extra' => array(),
164 'diff' => array(
165 'tx_foo' => array(
166 'fields' => array(
167 'foo' => 'varchar(999) DEFAULT \'0\''
168 )
169 )
170 ),
171 'diff_currentValues' => array(
172 'tx_foo' => array(
173 'fields' => array(
174 'foo' => 'varchar(255) DEFAULT \'0\''
175 )
176 )
177 )
178 )
179 );
180 }
181
182 /**
183 * @test
184 */
185 public function getDatabaseExtraIgnoresCaseDifference() {
186 $subject = new SqlSchemaMigrationService();
187 $differenceArray = $subject->getDatabaseExtra(
188 array(
189 'tx_foo' => array(
190 'fields' => array(
191 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
192 )
193 )
194 ),
195 array(
196 'tx_foo' => array(
197 'fields' => array(
198 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
199 )
200 )
201 )
202 );
203
204
205 $this->assertEquals(
206 $differenceArray,
207 array(
208 'extra' => array(),
209 'diff' => array(),
210 'diff_currentValues' => NULL,
211 )
212 );
213 }
214
215 /**
216 * @test
217 */
218 public function getDatabaseExtraIgnoresCaseDifferenceButKeepsCaseInSetIntact() {
219 $subject = new SqlSchemaMigrationService();
220 $differenceArray = $subject->getDatabaseExtra(
221 array(
222 'tx_foo' => array(
223 'fields' => array(
224 'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
225 )
226 )
227 ),
228 array(
229 'tx_foo' => array(
230 'fields' => array(
231 'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
232 )
233 )
234 )
235 );
236
237 $this->assertEquals(
238 $differenceArray,
239 array(
240 'extra' => array(),
241 'diff' => array(),
242 'diff_currentValues' => NULL,
243 )
244 );
245 }
246
247 /**
248 * @test
249 */
250 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() {
251 $subject = new SqlSchemaMigrationService();
252 $differenceArray = $subject->getDatabaseExtra(
253 array(
254 'tx_foo' => array(
255 'fields' => array(
256 'PRIMARY KEY (md5hash)',
257 )
258 )
259 ),
260 array(
261 'tx_foo' => array(
262 'fields' => array(
263 'PRIMARY KEY (md5hash)'),
264 )
265 )
266 );
267
268
269 $this->assertEquals(
270 $differenceArray,
271 array(
272 'extra' => array(),
273 'diff' => array(),
274 'diff_currentValues' => NULL,
275 )
276 );
277 }
278
279 /**
280 * @test
281 */
282 public function getDatabaseExtraFindsNewSpatialKeys() {
283 $subject = new SqlSchemaMigrationService();
284 $differenceArray = $subject->getDatabaseExtra(
285 array(
286 'tx_foo' => array(
287 'keys' => array(
288 'foo' => 'SPATIAL foo (foo)'
289 )
290 )
291 ),
292 array(
293 'tx_foo' => array(
294 'keys' => array()
295 )
296 )
297 );
298
299 $this->assertEquals(
300 $differenceArray,
301 array(
302 'extra' => array(
303 'tx_foo' => array(
304 'keys' => array(
305 'foo' => 'SPATIAL foo (foo)'
306 )
307 )
308 ),
309 'diff' => array(),
310 'diff_currentValues' => NULL
311 )
312 );
313 }
314
315 /**
316 * @test
317 */
318 public function checkColumnDefinitionIfCommentIsSupplied() {
319 $subject = new SqlSchemaMigrationService();
320 $fieldDefinition = $subject->assembleFieldDefinition(
321 array(
322 'Field' => 'uid',
323 'Type' => 'int(11)',
324 'Null' => 'NO',
325 'Key' => 'PRI',
326 'Default' => NULL,
327 'Extra' => 'auto_increment',
328 'Comment' => 'I am a comment',
329 )
330 );
331
332
333 $this->assertSame(
334 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
335 $fieldDefinition
336 );
337 }
338
339 /**
340 * @test
341 */
342 public function checkColumnDefinitionIfNoCommentIsSupplied() {
343 $subject = new SqlSchemaMigrationService();
344 $fieldDefinition = $subject->assembleFieldDefinition(
345 array(
346 'Field' => 'uid',
347 'Type' => 'int(11)',
348 'Null' => 'NO',
349 'Key' => 'PRI',
350 'Default' => NULL,
351 'Extra' => 'auto_increment',
352 )
353 );
354
355
356 $this->assertSame(
357 'int(11) NOT NULL auto_increment',
358 $fieldDefinition
359 );
360 }
361
362 /**
363 * @test
364 */
365 public function getDatabaseExtraIncludesEngineIfMySQLIsUsed() {
366 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
367 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(FALSE));
368 $differenceArray = $subject->getDatabaseExtra(
369 array(
370 'tx_foo' => array(
371 'fields' => array(
372 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
373 ),
374 'extra' => array(
375 'ENGINE' => 'InnoDB'
376 )
377 )
378 ),
379 array(
380 'tx_foo' => array(
381 'fields' => array(
382 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
383 ),
384 'extra' => array(
385 'ENGINE' => 'InnoDB'
386 )
387 )
388 )
389 );
390
391 $this->assertSame(
392 $differenceArray,
393 array(
394 'extra' => array(),
395 'diff' => array(),
396 'diff_currentValues' => NULL,
397 )
398 );
399 }
400
401 /**
402 * @test
403 */
404 public function getDatabaseExtraExcludesEngineIfDbalIsUsed() {
405 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
406 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(TRUE));
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 )
425 )
426 );
427
428 $this->assertSame(
429 $differenceArray,
430 array(
431 'extra' => array(),
432 'diff' => array(),
433 'diff_currentValues' => NULL,
434 )
435 );
436 }
437
438 /**
439 * @test
440 */
441 public function getDatabaseExtraIncludesUnsignedAttributeIfMySQLIsUsed() {
442 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
443 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(FALSE));
444 $differenceArray = $subject->getDatabaseExtra(
445 array(
446 'tx_foo' => array(
447 'fields' => array(
448 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
449 ),
450 'extra' => array(
451 'ENGINE' => 'InnoDB'
452 )
453 )
454 ),
455 array(
456 'tx_foo' => array(
457 'fields' => array(
458 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
459 ),
460 'extra' => array(
461 'ENGINE' => 'InnoDB'
462 )
463 )
464 )
465 );
466
467 $this->assertSame(
468 $differenceArray,
469 array(
470 'extra' => array(),
471 'diff' => array(
472 'tx_foo' => array(
473 'fields' => array(
474 'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
475 ),
476 )
477 ),
478 'diff_currentValues' => array(
479 'tx_foo' => array(
480 'fields' => array(
481 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
482 ),
483 )
484 )
485 )
486 );
487 }
488
489 /**
490 * @test
491 */
492 public function getDatabaseExtraExcludesUnsignedAttributeIfDbalIsUsed() {
493 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
494 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(TRUE));
495 $differenceArray = $subject->getDatabaseExtra(
496 array(
497 'tx_foo' => array(
498 'fields' => array(
499 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
500 ),
501 'extra' => array(
502 'ENGINE' => 'InnoDB'
503 )
504 )
505 ),
506 array(
507 'tx_foo' => array(
508 'fields' => array(
509 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
510 ),
511 'extra' => array(
512 'ENGINE' => 'InnoDB'
513 )
514 )
515 )
516 );
517
518 $this->assertSame(
519 $differenceArray,
520 array(
521 'extra' => array(),
522 'diff' => array(),
523 'diff_currentValues' => NULL
524 )
525 );
526 }
527 }