[TASK] Update doctrine/dbal to ~2.8.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / DefaultTcaSchemaTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\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\Column;
20 use Doctrine\DBAL\Schema\Index;
21 use Doctrine\DBAL\Schema\SchemaException;
22 use Doctrine\DBAL\Schema\Table;
23 use Doctrine\DBAL\Types\Type;
24 use TYPO3\CMS\Core\Database\Schema\DefaultTcaSchema;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class DefaultTcaSchemaTest extends UnitTestCase
31 {
32 /**
33 * @var DefaultTcaSchema
34 */
35 protected $subject;
36
37 public function setUp()
38 {
39 parent::setUp();
40 $this->subject = $this->getAccessibleMock(DefaultTcaSchema::class, ['tableRunsOnSqlite']);
41 $this->subject->method('tableRunsOnSqlite')->willReturn(false);
42 }
43
44 /**
45 * @test
46 */
47 public function enrichKeepsGivenTablesArrayWithEmptyTca()
48 {
49 $GLOBALS['TCA'] = [];
50 $this->assertEquals([], $this->subject->enrich([]));
51 }
52
53 /**
54 * @test
55 */
56 public function enrichDoesNotAddColumnIfExists()
57 {
58 $GLOBALS['TCA']['aTable']['ctrl'] = [];
59
60 $table = new Table('aTable');
61 $table->addColumn('uid', 'integer');
62 $table->addColumn('pid', 'integer');
63 $input[] = $table;
64
65 $table = new Table('aTable');
66 $table->addColumn('uid', 'integer');
67 $table->addColumn('pid', 'integer');
68 $expected[] = $table;
69
70 $this->assertEquals($expected, $this->subject->enrich($input));
71 }
72
73 /**
74 * @test
75 */
76 public function enrichDoesNotAddColumnIfTableExistsMultipleTimesAndUidExists()
77 {
78 $GLOBALS['TCA']['aTable']['ctrl'] = [];
79
80 $table = new Table('aTable');
81 $table->addColumn('foo', 'integer');
82 $input[] = $table;
83 $table = new Table('aTable');
84 $table->addColumn('uid', 'integer');
85 $table->addColumn('pid', 'integer');
86 $input[] = $table;
87
88 $table = new Table('aTable');
89 $table->addColumn('foo', 'integer');
90 $expected[] = $table;
91 $table = new Table('aTable');
92 $table->addColumn('uid', 'integer');
93 $table->addColumn('pid', 'integer');
94 $expected[] = $table;
95
96 $this->assertEquals($expected, $this->subject->enrich($input));
97 }
98
99 /**
100 * @test
101 */
102 public function enrichAddsFieldToFirstTableDefinitionOfThatName()
103 {
104 $GLOBALS['TCA']['aTable']['ctrl'] = [];
105
106 $table = new Table('aTable');
107 $table->addColumn('foo', 'integer');
108 $input[] = $table;
109 $table = new Table('aTable');
110 $table->addColumn('bar', 'integer');
111 $input[] = $table;
112
113 $result = $this->subject->enrich($input);
114
115 $this->assertInstanceOf(Column::class, $result[0]->getColumn('uid'));
116 }
117
118 /**
119 * @test
120 */
121 public function enrichAddsUidAndPrimaryKey()
122 {
123 $GLOBALS['TCA']['aTable']['ctrl'] = [];
124 $result = $this->subject->enrich([]);
125 $expectedUidColumn = new Column(
126 'uid',
127 Type::getType('integer'),
128 [
129 'notnull' => true,
130 'unsigned' => true,
131 'autoincrement' => true,
132 ]
133 );
134 $expectedPrimaryKey = new Index('primary', ['uid'], true, true);
135 $this->assertEquals($expectedUidColumn, $result[0]->getColumn('uid'));
136 $this->assertEquals($expectedPrimaryKey, $result[0]->getPrimaryKey());
137 }
138
139 /**
140 * @test
141 */
142 public function enrichAddsPid()
143 {
144 $GLOBALS['TCA']['aTable']['ctrl'] = [];
145 $result = $this->subject->enrich([]);
146 $expectedPidColumn = new Column(
147 'pid',
148 Type::getType('integer'),
149 [
150 'default' => 0,
151 'notnull' => true,
152 'unsigned' => true,
153 ]
154 );
155 $this->assertEquals($expectedPidColumn, $result[0]->getColumn('pid'));
156 }
157
158 /**
159 * @test
160 */
161 public function enrichAddsSignedPidWithEnabledWorkspace()
162 {
163 $GLOBALS['TCA']['aTable']['ctrl'] = [
164 'versioningWS' => true,
165 ];
166 $result = $this->subject->enrich([]);
167 $expectedPidColumn = new Column(
168 'pid',
169 Type::getType('integer'),
170 [
171 'default' => 0,
172 'notnull' => true,
173 'unsigned' => false,
174 ]
175 );
176 $this->assertEquals($expectedPidColumn, $result[0]->getColumn('pid'));
177 }
178
179 /**
180 * @test
181 */
182 public function enrichAddsTstamp()
183 {
184 $GLOBALS['TCA']['aTable']['ctrl'] = [
185 'tstamp' => 'updatedon',
186 ];
187 $result = $this->subject->enrich([]);
188 $expectedColumn = new Column(
189 '`updatedon`',
190 Type::getType('integer'),
191 [
192 'default' => 0,
193 'notnull' => true,
194 'unsigned' => true,
195 ]
196 );
197 $this->assertEquals($expectedColumn, $result[0]->getColumn('updatedon'));
198 }
199
200 /**
201 * @test
202 */
203 public function enrichAddsCrdate()
204 {
205 $GLOBALS['TCA']['aTable']['ctrl'] = [
206 'crdate' => 'createdon',
207 ];
208 $result = $this->subject->enrich([]);
209 $expectedColumn = new Column(
210 '`createdon`',
211 Type::getType('integer'),
212 [
213 'default' => 0,
214 'notnull' => true,
215 'unsigned' => true,
216 ]
217 );
218 $this->assertEquals($expectedColumn, $result[0]->getColumn('createdon'));
219 }
220
221 /**
222 * @test
223 */
224 public function enrichAddsCruserid()
225 {
226 $GLOBALS['TCA']['aTable']['ctrl'] = [
227 'cruser_id' => 'createdby',
228 ];
229 $result = $this->subject->enrich([]);
230 $expectedColumn = new Column(
231 '`createdby`',
232 Type::getType('integer'),
233 [
234 'default' => 0,
235 'notnull' => true,
236 'unsigned' => true,
237 ]
238 );
239 $this->assertEquals($expectedColumn, $result[0]->getColumn('createdby'));
240 }
241
242 /**
243 * @test
244 */
245 public function enrichAddsDeleted()
246 {
247 $GLOBALS['TCA']['aTable']['ctrl'] = [
248 'delete' => 'deleted',
249 ];
250 $result = $this->subject->enrich([]);
251 $expectedColumn = new Column(
252 '`deleted`',
253 Type::getType('smallint'),
254 [
255 'default' => 0,
256 'notnull' => true,
257 'unsigned' => true,
258 ]
259 );
260 $this->assertEquals($expectedColumn, $result[0]->getColumn('deleted'));
261 }
262
263 /**
264 * @test
265 */
266 public function enrichAddsDisabled()
267 {
268 $GLOBALS['TCA']['aTable']['ctrl'] = [
269 'enablecolumns' => [
270 'disabled' => 'disabled',
271 ]
272 ];
273 $result = $this->subject->enrich([]);
274 $expectedColumn = new Column(
275 '`disabled`',
276 Type::getType('smallint'),
277 [
278 'default' => 0,
279 'notnull' => true,
280 'unsigned' => true,
281 ]
282 );
283 $this->assertEquals($expectedColumn, $result[0]->getColumn('disabled'));
284 }
285
286 /**
287 * @test
288 */
289 public function enrichAddsStarttime()
290 {
291 $GLOBALS['TCA']['aTable']['ctrl'] = [
292 'enablecolumns' => [
293 'starttime' => 'starttime',
294 ]
295 ];
296 $result = $this->subject->enrich([]);
297 $expectedColumn = new Column(
298 '`starttime`',
299 Type::getType('integer'),
300 [
301 'default' => 0,
302 'notnull' => true,
303 'unsigned' => true,
304 ]
305 );
306 $this->assertEquals($expectedColumn, $result[0]->getColumn('starttime'));
307 }
308
309 /**
310 * @test
311 */
312 public function enrichAddsEndtime()
313 {
314 $GLOBALS['TCA']['aTable']['ctrl'] = [
315 'enablecolumns' => [
316 'endtime' => 'endtime',
317 ]
318 ];
319 $result = $this->subject->enrich([]);
320 $expectedColumn = new Column(
321 '`endtime`',
322 Type::getType('integer'),
323 [
324 'default' => 0,
325 'notnull' => true,
326 'unsigned' => true,
327 ]
328 );
329 $this->assertEquals($expectedColumn, $result[0]->getColumn('endtime'));
330 }
331
332 /**
333 * @test
334 */
335 public function enrichAddsFegroup()
336 {
337 $GLOBALS['TCA']['aTable']['ctrl'] = [
338 'enablecolumns' => [
339 'fe_group' => 'fe_group',
340 ]
341 ];
342 $result = $this->subject->enrich([]);
343 $expectedColumn = new Column(
344 '`fe_group`',
345 Type::getType('string'),
346 [
347 'default' => '0',
348 'notnull' => true,
349 'length' => 255,
350 ]
351 );
352 $this->assertEquals($expectedColumn, $result[0]->getColumn('fe_group'));
353 }
354
355 /**
356 * @test
357 */
358 public function enrichAddsSorting()
359 {
360 $GLOBALS['TCA']['aTable']['ctrl'] = [
361 'sortby' => 'sorting',
362 ];
363 $result = $this->subject->enrich([]);
364 $expectedColumn = new Column(
365 '`sorting`',
366 Type::getType('integer'),
367 [
368 'default' => '0',
369 'notnull' => true,
370 'unsigned' => false,
371 ]
372 );
373 $this->assertEquals($expectedColumn, $result[0]->getColumn('sorting'));
374 }
375
376 /**
377 * @test
378 */
379 public function enrichAddsParentKey()
380 {
381 $GLOBALS['TCA']['aTable']['ctrl'] = [];
382 $result = $this->subject->enrich([]);
383 $expectedIndex = new Index('parent', ['pid']);
384 $this->assertEquals($expectedIndex, $result[0]->getIndex('parent'));
385 }
386
387 /**
388 * @test
389 */
390 public function enrichAddsParentKeyWithDelete()
391 {
392 $GLOBALS['TCA']['aTable']['ctrl'] = [
393 'delete' => 'deleted',
394 ];
395 $result = $this->subject->enrich([]);
396 $expectedIndex = new Index('parent', ['pid', 'deleted']);
397 $this->assertEquals($expectedIndex, $result[0]->getIndex('parent'));
398 }
399
400 /**
401 * @test
402 */
403 public function enrichAddsParentKeyWithDisabled()
404 {
405 $GLOBALS['TCA']['aTable']['ctrl'] = [
406 'enablecolumns' => [
407 'disabled' => 'disabled',
408 ],
409 ];
410 $result = $this->subject->enrich([]);
411 $expectedIndex = new Index('parent', ['pid', 'disabled']);
412 $this->assertEquals($expectedIndex, $result[0]->getIndex('parent'));
413 }
414
415 /**
416 * @test
417 */
418 public function enrichAddsParentKeyInCorrectOrder()
419 {
420 $GLOBALS['TCA']['aTable']['ctrl'] = [
421 'delete' => 'deleted',
422 'enablecolumns' => [
423 'disabled' => 'disabled',
424 ],
425 ];
426 $result = $this->subject->enrich([]);
427 $expectedIndex = new Index('parent', ['pid', 'deleted', 'disabled']);
428 $this->assertEquals($expectedIndex, $result[0]->getIndex('parent'));
429 }
430
431 /**
432 * @test
433 */
434 public function enrichAddsSysLanguageUid()
435 {
436 $GLOBALS['TCA']['aTable']['ctrl'] = [
437 'languageField' => 'sys_language_uid',
438 ];
439 $result = $this->subject->enrich([]);
440 $expectedColumn = new Column(
441 '`sys_language_uid`',
442 Type::getType('integer'),
443 [
444 'default' => 0,
445 'notnull' => true,
446 'unsigned' => false,
447 ]
448 );
449 $this->assertEquals($expectedColumn, $result[0]->getColumn('sys_language_uid'));
450 }
451
452 /**
453 * @test
454 */
455 public function enrichAddsL10nParent()
456 {
457 $GLOBALS['TCA']['aTable']['ctrl'] = [
458 'languageField' => 'sys_language_uid',
459 'transOrigPointerField' => 'l10n_parent',
460 ];
461 $result = $this->subject->enrich([]);
462 $expectedColumn = new Column(
463 '`l10n_parent`',
464 Type::getType('integer'),
465 [
466 'default' => 0,
467 'notnull' => true,
468 'unsigned' => true,
469 ]
470 );
471 $this->assertEquals($expectedColumn, $result[0]->getColumn('l10n_parent'));
472 }
473
474 /**
475 * @test
476 */
477 public function enrichDoesNotAddL10nParentIfLanguageFieldIsNotDefined()
478 {
479 $GLOBALS['TCA']['aTable']['ctrl'] = [
480 'transOrigPointerField' => 'l10n_parent',
481 ];
482 $result = $this->subject->enrich([]);
483 $this->expectException(SchemaException::class);
484 $result[0]->getColumn('l10n_parent');
485 }
486
487 /**
488 * @test
489 */
490 public function enrichAddsDescription()
491 {
492 $GLOBALS['TCA']['aTable']['ctrl'] = [
493 'descriptionColumn' => 'rowDescription',
494 ];
495 $result = $this->subject->enrich([]);
496 $expectedColumn = new Column(
497 '`rowDescription`',
498 Type::getType('text'),
499 [
500 'notnull' => false,
501 'length' => 65535,
502 ]
503 );
504 $this->assertEquals($expectedColumn, $result[0]->getColumn('rowDescription'));
505 }
506
507 /**
508 * @test
509 */
510 public function enrichAddsEditlock()
511 {
512 $GLOBALS['TCA']['aTable']['ctrl'] = [
513 'editlock' => 'editlock'
514 ];
515 $result = $this->subject->enrich([]);
516 $expectedColumn = new Column(
517 '`editlock`',
518 Type::getType('smallint'),
519 [
520 'default' => 0,
521 'notnull' => true,
522 'unsigned' => true,
523 ]
524 );
525 $this->assertEquals($expectedColumn, $result[0]->getColumn('editlock'));
526 }
527
528 /**
529 * @test
530 */
531 public function enrichAddsL10nSource()
532 {
533 $GLOBALS['TCA']['aTable']['ctrl'] = [
534 'languageField' => 'sys_language_uid',
535 'translationSource' => 'l10n_source',
536 ];
537 $result = $this->subject->enrich([]);
538 $expectedColumn = new Column(
539 '`l10n_source`',
540 Type::getType('integer'),
541 [
542 'default' => 0,
543 'notnull' => true,
544 'unsigned' => true,
545 ]
546 );
547 $this->assertEquals($expectedColumn, $result[0]->getColumn('l10n_source'));
548 }
549
550 /**
551 * @test
552 */
553 public function enrichDoesNotAddL10nSourceIfLanguageFieldIsNotDefined()
554 {
555 $GLOBALS['TCA']['aTable']['ctrl'] = [
556 'translationSource' => 'l10n_source',
557 ];
558 $result = $this->subject->enrich([]);
559 $this->expectException(SchemaException::class);
560 $result[0]->getColumn('l10n_source');
561 }
562
563 /**
564 * @test
565 */
566 public function enrichAddsL10nState()
567 {
568 $GLOBALS['TCA']['aTable']['ctrl'] = [
569 'languageField' => 'sys_language_uid',
570 'transOrigPointerField' => 'l10n_parent',
571 ];
572 $result = $this->subject->enrich([]);
573 $expectedColumn = new Column(
574 '`l10n_state`',
575 Type::getType('text'),
576 [
577 'notnull' => false,
578 'length' => 65535,
579 ]
580 );
581 $this->assertEquals($expectedColumn, $result[0]->getColumn('l10n_state'));
582 }
583
584 /**
585 * @test
586 */
587 public function enrichDoesNotAddL10nStateIfLanguageFieldIsNotDefined()
588 {
589 $GLOBALS['TCA']['aTable']['ctrl'] = [
590 'transOrigPointerField' => 'l10n_parent',
591 ];
592 $result = $this->subject->enrich([]);
593 $this->expectException(SchemaException::class);
594 $result[0]->getColumn('l10n_state');
595 }
596
597 /**
598 * @test
599 */
600 public function enrichDoesNotAddL10nStateIfTransOrigPointerFieldIsNotDefined()
601 {
602 $GLOBALS['TCA']['aTable']['ctrl'] = [
603 'languageField' => 'sys_language_uid',
604 ];
605 $result = $this->subject->enrich([]);
606 $this->expectException(SchemaException::class);
607 $result[0]->getColumn('l10n_state');
608 }
609
610 /**
611 * @test
612 */
613 public function enrichAddsT3origUid()
614 {
615 $GLOBALS['TCA']['aTable']['ctrl'] = [
616 'origUid' => 't3_origuid',
617 ];
618 $result = $this->subject->enrich([]);
619 $expectedColumn = new Column(
620 '`t3_origuid`',
621 Type::getType('integer'),
622 [
623 'default' => 0,
624 'notnull' => true,
625 'unsigned' => true,
626 ]
627 );
628 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3_origuid'));
629 }
630
631 /**
632 * @test
633 */
634 public function enrichAddsL10nDiffsource()
635 {
636 $GLOBALS['TCA']['aTable']['ctrl'] = [
637 'transOrigDiffSourceField' => 'l18n_diffsource',
638 ];
639 $result = $this->subject->enrich([]);
640 $expectedColumn = new Column(
641 '`l18n_diffsource`',
642 Type::getType('blob'),
643 [
644 'length' => 16777215,
645 'notnull' => false,
646 ]
647 );
648 $this->assertEquals($expectedColumn, $result[0]->getColumn('l18n_diffsource'));
649 }
650
651 /**
652 * @test
653 */
654 public function enrichAddsT3verOid()
655 {
656 $GLOBALS['TCA']['aTable']['ctrl'] = [
657 'versioningWS' => true,
658 ];
659 $result = $this->subject->enrich([]);
660 $expectedColumn = new Column(
661 '`t3ver_oid`',
662 Type::getType('integer'),
663 [
664 'default' => 0,
665 'notnull' => true,
666 'unsigned' => true,
667 ]
668 );
669 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_oid'));
670 }
671
672 /**
673 * @test
674 */
675 public function enrichAddsT3verId()
676 {
677 $GLOBALS['TCA']['aTable']['ctrl'] = [
678 'versioningWS' => true,
679 ];
680 $result = $this->subject->enrich([]);
681 $expectedColumn = new Column(
682 '`t3ver_id`',
683 Type::getType('integer'),
684 [
685 'default' => 0,
686 'notnull' => true,
687 'unsigned' => true,
688 ]
689 );
690 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_id'));
691 }
692
693 /**
694 * @test
695 */
696 public function enrichAddsT3verWsid()
697 {
698 $GLOBALS['TCA']['aTable']['ctrl'] = [
699 'versioningWS' => true,
700 ];
701 $result = $this->subject->enrich([]);
702 $expectedColumn = new Column(
703 '`t3ver_wsid`',
704 Type::getType('integer'),
705 [
706 'default' => 0,
707 'notnull' => true,
708 'unsigned' => true,
709 ]
710 );
711 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_wsid'));
712 }
713
714 /**
715 * @test
716 */
717 public function enrichAddsT3verState()
718 {
719 $GLOBALS['TCA']['aTable']['ctrl'] = [
720 'versioningWS' => true,
721 ];
722 $result = $this->subject->enrich([]);
723 $expectedColumn = new Column(
724 '`t3ver_state`',
725 Type::getType('smallint'),
726 [
727 'default' => 0,
728 'notnull' => true,
729 'unsigned' => false,
730 ]
731 );
732 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_state'));
733 }
734
735 /**
736 * @test
737 */
738 public function enrichAddsT3verStage()
739 {
740 $GLOBALS['TCA']['aTable']['ctrl'] = [
741 'versioningWS' => true,
742 ];
743 $result = $this->subject->enrich([]);
744 $expectedColumn = new Column(
745 '`t3ver_stage`',
746 Type::getType('integer'),
747 [
748 'default' => 0,
749 'notnull' => true,
750 'unsigned' => false,
751 ]
752 );
753 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_stage'));
754 }
755
756 /**
757 * @test
758 */
759 public function enrichAddsT3verCount()
760 {
761 $GLOBALS['TCA']['aTable']['ctrl'] = [
762 'versioningWS' => true,
763 ];
764 $result = $this->subject->enrich([]);
765 $expectedColumn = new Column(
766 '`t3ver_count`',
767 Type::getType('integer'),
768 [
769 'default' => 0,
770 'notnull' => true,
771 'unsigned' => true,
772 ]
773 );
774 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_count'));
775 }
776
777 /**
778 * @test
779 */
780 public function enrichAddsT3verTstamp()
781 {
782 $GLOBALS['TCA']['aTable']['ctrl'] = [
783 'versioningWS' => true,
784 ];
785 $result = $this->subject->enrich([]);
786 $expectedColumn = new Column(
787 '`t3ver_tstamp`',
788 Type::getType('integer'),
789 [
790 'default' => 0,
791 'notnull' => true,
792 'unsigned' => true,
793 ]
794 );
795 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_tstamp'));
796 }
797
798 /**
799 * @test
800 */
801 public function enrichAddsT3verMoveId()
802 {
803 $GLOBALS['TCA']['aTable']['ctrl'] = [
804 'versioningWS' => true,
805 ];
806 $result = $this->subject->enrich([]);
807 $expectedColumn = new Column(
808 '`t3ver_move_id`',
809 Type::getType('integer'),
810 [
811 'default' => 0,
812 'notnull' => true,
813 'unsigned' => true,
814 ]
815 );
816 $this->assertEquals($expectedColumn, $result[0]->getColumn('t3ver_move_id'));
817 }
818
819 /**
820 * @test
821 */
822 public function enrichAddsT3verOidIndex()
823 {
824 $GLOBALS['TCA']['aTable']['ctrl'] = [
825 'versioningWS' => true,
826 ];
827 $result = $this->subject->enrich([]);
828 $expectedIndex = new Index('t3ver_oid', ['t3ver_oid', 't3ver_wsid']);
829 $this->assertEquals($expectedIndex, $result[0]->getIndex('t3ver_oid'));
830 }
831 }