cfb86fca5340890a75d2018b298c3d43630791c0
[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 * @author Mario Rimann <mario.rimann@typo3.org>
23 */
24 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @test
28 */
29 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions() {
30 $subject = new SqlSchemaMigrationService();
31 // Multiple whitespaces and tabs in field definition
32 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
33 $result = $subject->getFieldDefinitions_fileContent($inputString);
34
35 $this->assertEquals(
36 array(
37 'atable' => array(
38 'fields' => array(
39 'aFieldName' => 'int(11) unsigned default \'0\'',
40 ),
41 'extra' => array(
42 'COLLATE' => '',
43 ),
44 ),
45 ),
46 $result
47 );
48 }
49
50 /**
51 * @test
52 */
53 public function getDatabaseExtraFindsChangedFields() {
54 $subject = new SqlSchemaMigrationService();
55 $differenceArray = $subject->getDatabaseExtra(
56 array(
57 'tx_foo' => array(
58 'fields' => array(
59 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
60 )
61 )
62 ),
63 array(
64 'tx_foo' => array(
65 'fields' => array(
66 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
67 )
68 )
69 )
70 );
71
72 $this->assertEquals(
73 $differenceArray,
74 array(
75 'extra' => array(),
76 'diff' => array(
77 'tx_foo' => array(
78 'fields' => array(
79 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
80 )
81 )
82 ),
83 'diff_currentValues' => array(
84 'tx_foo' => array(
85 'fields' => array(
86 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
87 )
88 )
89 )
90 )
91 );
92 }
93
94 /**
95 * @test
96 */
97 public function getDatabaseExtraFindsChangedFieldsIncludingNull() {
98 $subject = new SqlSchemaMigrationService();
99 $differenceArray = $subject->getDatabaseExtra(
100 array(
101 'tx_foo' => array(
102 'fields' => array(
103 'foo' => 'varchar(999) NULL'
104 )
105 )
106 ),
107 array(
108 'tx_foo' => array(
109 'fields' => array(
110 'foo' => 'varchar(255) NULL'
111 )
112 )
113 )
114 );
115
116 $this->assertEquals(
117 $differenceArray,
118 array(
119 'extra' => array(),
120 'diff' => array(
121 'tx_foo' => array(
122 'fields' => array(
123 'foo' => 'varchar(999) NULL'
124 )
125 )
126 ),
127 'diff_currentValues' => array(
128 'tx_foo' => array(
129 'fields' => array(
130 'foo' => 'varchar(255) NULL'
131 )
132 )
133 )
134 )
135 );
136 }
137
138 /**
139 * @test
140 */
141 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull() {
142 $subject = new SqlSchemaMigrationService();
143 $differenceArray = $subject->getDatabaseExtra(
144 array(
145 'tx_foo' => array(
146 'fields' => array(
147 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
148 )
149 )
150 ),
151 array(
152 'tx_foo' => array(
153 'fields' => array(
154 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
155 )
156 )
157 ),
158 '',
159 TRUE
160 );
161
162 $this->assertEquals(
163 $differenceArray,
164 array(
165 'extra' => array(),
166 'diff' => array(
167 'tx_foo' => array(
168 'fields' => array(
169 'foo' => 'varchar(999) DEFAULT \'0\''
170 )
171 )
172 ),
173 'diff_currentValues' => array(
174 'tx_foo' => array(
175 'fields' => array(
176 'foo' => 'varchar(255) DEFAULT \'0\''
177 )
178 )
179 )
180 )
181 );
182 }
183
184 /**
185 * @test
186 */
187 public function getDatabaseExtraIgnoresCaseDifference() {
188 $subject = new SqlSchemaMigrationService();
189 $differenceArray = $subject->getDatabaseExtra(
190 array(
191 'tx_foo' => array(
192 'fields' => array(
193 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
194 )
195 )
196 ),
197 array(
198 'tx_foo' => array(
199 'fields' => array(
200 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
201 )
202 )
203 )
204 );
205
206
207 $this->assertEquals(
208 $differenceArray,
209 array(
210 'extra' => array(),
211 'diff' => array(),
212 'diff_currentValues' => NULL,
213 )
214 );
215 }
216
217 /**
218 * @test
219 */
220 public function getDatabaseExtraIgnoresCaseDifferenceButKeepsCaseInSetIntact() {
221 $subject = new SqlSchemaMigrationService();
222 $differenceArray = $subject->getDatabaseExtra(
223 array(
224 'tx_foo' => array(
225 'fields' => array(
226 'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
227 )
228 )
229 ),
230 array(
231 'tx_foo' => array(
232 'fields' => array(
233 'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
234 )
235 )
236 )
237 );
238
239 $this->assertEquals(
240 $differenceArray,
241 array(
242 'extra' => array(),
243 'diff' => array(),
244 'diff_currentValues' => NULL,
245 )
246 );
247 }
248
249 /**
250 * @test
251 */
252 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() {
253 $subject = new SqlSchemaMigrationService();
254 $differenceArray = $subject->getDatabaseExtra(
255 array(
256 'tx_foo' => array(
257 'fields' => array(
258 'PRIMARY KEY (md5hash)',
259 )
260 )
261 ),
262 array(
263 'tx_foo' => array(
264 'fields' => array(
265 'PRIMARY KEY (md5hash)'),
266 )
267 )
268 );
269
270
271 $this->assertEquals(
272 $differenceArray,
273 array(
274 'extra' => array(),
275 'diff' => array(),
276 'diff_currentValues' => NULL,
277 )
278 );
279 }
280
281 /**
282 * @test
283 */
284 public function getDatabaseExtraFindsNewSpatialKeys() {
285 $subject = new SqlSchemaMigrationService();
286 $differenceArray = $subject->getDatabaseExtra(
287 array(
288 'tx_foo' => array(
289 'keys' => array(
290 'foo' => 'SPATIAL foo (foo)'
291 )
292 )
293 ),
294 array(
295 'tx_foo' => array(
296 'keys' => array()
297 )
298 )
299 );
300
301 $this->assertEquals(
302 $differenceArray,
303 array(
304 'extra' => array(
305 'tx_foo' => array(
306 'keys' => array(
307 'foo' => 'SPATIAL foo (foo)'
308 )
309 )
310 ),
311 'diff' => array(),
312 'diff_currentValues' => NULL
313 )
314 );
315 }
316
317 /**
318 * @test
319 */
320 public function checkColumnDefinitionIfCommentIsSupplied() {
321 $subject = new SqlSchemaMigrationService();
322 $fieldDefinition = $subject->assembleFieldDefinition(
323 array(
324 'Field' => 'uid',
325 'Type' => 'int(11)',
326 'Null' => 'NO',
327 'Key' => 'PRI',
328 'Default' => NULL,
329 'Extra' => 'auto_increment',
330 'Comment' => 'I am a comment',
331 )
332 );
333
334
335 $this->assertSame(
336 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
337 $fieldDefinition
338 );
339 }
340
341 /**
342 * @test
343 */
344 public function checkColumnDefinitionIfNoCommentIsSupplied() {
345 $subject = new SqlSchemaMigrationService();
346 $fieldDefinition = $subject->assembleFieldDefinition(
347 array(
348 'Field' => 'uid',
349 'Type' => 'int(11)',
350 'Null' => 'NO',
351 'Key' => 'PRI',
352 'Default' => NULL,
353 'Extra' => 'auto_increment',
354 )
355 );
356
357
358 $this->assertSame(
359 'int(11) NOT NULL auto_increment',
360 $fieldDefinition
361 );
362 }
363
364 /**
365 * @test
366 */
367 public function getDatabaseExtraIncludesEngineIfMySQLIsUsed() {
368 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
369 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(FALSE));
370 $differenceArray = $subject->getDatabaseExtra(
371 array(
372 'tx_foo' => array(
373 'fields' => array(
374 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
375 ),
376 'extra' => array(
377 'ENGINE' => 'InnoDB'
378 )
379 )
380 ),
381 array(
382 'tx_foo' => array(
383 'fields' => array(
384 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
385 ),
386 'extra' => array(
387 'ENGINE' => 'InnoDB'
388 )
389 )
390 )
391 );
392
393 $this->assertSame(
394 $differenceArray,
395 array(
396 'extra' => array(),
397 'diff' => array(),
398 'diff_currentValues' => NULL,
399 )
400 );
401 }
402
403 /**
404 * @test
405 */
406 public function getDatabaseExtraExcludesEngineIfDbalIsUsed() {
407 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', FALSE);
408 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(TRUE));
409 $differenceArray = $subject->getDatabaseExtra(
410 array(
411 'tx_foo' => array(
412 'fields' => array(
413 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
414 ),
415 'extra' => array(
416 'ENGINE' => 'InnoDB'
417 )
418 )
419 ),
420 array(
421 'tx_foo' => array(
422 'fields' => array(
423 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
424 ),
425 'extra' => array()
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 }