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