[BUGFIX] Notice free FormEngine testing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseRowInitializeNewTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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\Backend\Form\FormDataProvider\DatabaseRowInitializeNew;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Test case
22 */
23 class DatabaseRowInitializeNewTest extends UnitTestCase
24 {
25 /**
26 * @test
27 */
28 public function addDataReturnSameDataIfCommandIsEdit()
29 {
30 $input = [
31 'command' => 'edit',
32 'tableName' => 'aTable',
33 'vanillaUid' => 23,
34 'databaseRow' => [
35 'uid' => 42,
36 ],
37 'userTsConfig' => [
38 'TCAdefaults.' => [
39 'aTable.' => [
40 'uid' => 23,
41 ],
42 ],
43 ],
44 ];
45 $this->assertSame($input, (new DatabaseRowInitializeNew)->addData($input));
46 }
47
48 /**
49 * @test
50 */
51 public function addDataThrowsExceptionIfDatabaseRowIsNotArray()
52 {
53 $input = [
54 'tableName' => 'aTable',
55 'command' => 'new',
56 'databaseRow' => 'not-an-array',
57 ];
58 $this->expectException(\UnexpectedValueException::class);
59 $this->expectExceptionCode(1444431128);
60 (new DatabaseRowInitializeNew)->addData($input);
61 }
62
63 /**
64 * @test
65 */
66 public function addDataKeepsGivenDefaultsIfCommandIsNew()
67 {
68 $input = [
69 'command' => 'new',
70 'tableName' => 'aTable',
71 'vanillaUid' => 23,
72 'neighborRow' => null,
73 'inlineChildChildUid' => null,
74 'isInlineChild' => false,
75 'databaseRow' => [
76 'aField' => 42,
77 ],
78 ];
79 $expected = $input;
80 $expected['databaseRow']['pid'] = 23;
81 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
82 }
83
84 /**
85 * @test
86 */
87 public function addDataSetsDefaultDataFromUserTsIfColumnIsDefinedInTca()
88 {
89 $input = [
90 'command' => 'new',
91 'tableName' => 'aTable',
92 'vanillaUid' => 23,
93 'neighborRow' => null,
94 'inlineChildChildUid' => null,
95 'isInlineChild' => false,
96 'databaseRow' => [],
97 'userTsConfig' => [
98 'TCAdefaults.' => [
99 'aTable.' => [
100 'aField' => 'userTsValue',
101 ],
102 ],
103 ],
104 'processedTca' => [
105 'columns' => [
106 'aField' => [],
107 ],
108 ]
109 ];
110 $expected = [
111 'aField' => 'userTsValue',
112 'pid' => 23,
113 ];
114 $result = (new DatabaseRowInitializeNew)->addData($input);
115 $this->assertSame($expected, $result['databaseRow']);
116 }
117
118 /**
119 * @test
120 */
121 public function addDataDoesNotSetDefaultDataFromUserTsIfColumnIsMissingInTca()
122 {
123 $input = [
124 'command' => 'new',
125 'tableName' => 'aTable',
126 'vanillaUid' => 23,
127 'neighborRow' => null,
128 'inlineChildChildUid' => null,
129 'isInlineChild' => false,
130 'databaseRow' => [],
131 'userTsConfig' => [
132 'TCAdefaults.' => [
133 'aTable.' => [
134 'aField' => 'userTsValue',
135 ],
136 ],
137 ],
138 'processedTca' => [
139 'columns' => [],
140 ]
141 ];
142 $expected = [
143 'pid' => 23,
144 ];
145 $result = (new DatabaseRowInitializeNew)->addData($input);
146 $this->assertSame($expected, $result['databaseRow']);
147 }
148
149 /**
150 * @test
151 */
152 public function addDataSetsDefaultDataFromPageTsIfColumnIsDefinedInTca()
153 {
154 $input = [
155 'command' => 'new',
156 'tableName' => 'aTable',
157 'vanillaUid' => 23,
158 'neighborRow' => null,
159 'inlineChildChildUid' => null,
160 'isInlineChild' => false,
161 'databaseRow' => [],
162 'pageTsConfig' => [
163 'TCAdefaults.' => [
164 'aTable.' => [
165 'aField' => 'pageTsValue',
166 ],
167 ],
168 ],
169 'processedTca' => [
170 'columns' => [
171 'aField' => [],
172 ],
173 ]
174 ];
175 $expected = [
176 'aField' => 'pageTsValue',
177 'pid' => 23,
178 ];
179 $result = (new DatabaseRowInitializeNew)->addData($input);
180 $this->assertSame($expected, $result['databaseRow']);
181 }
182
183 /**
184 * @test
185 */
186 public function addDataDoesNotSetDefaultDataFromPageTsIfColumnIsMissingInTca()
187 {
188 $input = [
189 'command' => 'new',
190 'tableName' => 'aTable',
191 'vanillaUid' => 23,
192 'neighborRow' => null,
193 'inlineChildChildUid' => null,
194 'isInlineChild' => false,
195 'databaseRow' => [],
196 'pageTsConfig' => [
197 'TCAdefaults.' => [
198 'aTable.' => [
199 'aField' => 'pageTsValue',
200 ],
201 ],
202 ],
203 'processedTca' => [
204 'columns' => [],
205 ]
206 ];
207 $expected = [
208 'pid' => 23,
209 ];
210 $result = (new DatabaseRowInitializeNew)->addData($input);
211 $this->assertSame($expected, $result['databaseRow']);
212 }
213
214 /**
215 * @test
216 */
217 public function addDataSetsDefaultDataOverrulingFromPageTs()
218 {
219 $input = [
220 'command' => 'new',
221 'tableName' => 'aTable',
222 'vanillaUid' => 23,
223 'neighborRow' => null,
224 'inlineChildChildUid' => null,
225 'isInlineChild' => false,
226 'databaseRow' => [],
227 'pageTsConfig' => [
228 'TCAdefaults.' => [
229 'aTable.' => [
230 'aField' => 'pageTsValue',
231 ],
232 ],
233 ],
234 'userTsConfig' => [
235 'TCAdefaults.' => [
236 'aTable.' => [
237 'aField' => 'userTsValue',
238 ],
239 ],
240 ],
241 'processedTca' => [
242 'columns' => [
243 'aField' => [],
244 ],
245 ]
246 ];
247 $expected = [
248 'aField' => 'pageTsValue',
249 'pid' => 23,
250 ];
251 $result = (new DatabaseRowInitializeNew)->addData($input);
252 $this->assertSame($expected, $result['databaseRow']);
253 }
254
255 /**
256 * @test
257 */
258 public function addDataSetsDefaultFromNeighborRow()
259 {
260 $input = [
261 'command' => 'new',
262 'tableName' => 'aTable',
263 'vanillaUid' => 23,
264 'inlineChildChildUid' => null,
265 'isInlineChild' => false,
266 'databaseRow' => [],
267 'neighborRow' => [
268 'aField' => 'valueFromNeighbor',
269 ],
270 'processedTca' => [
271 'ctrl' => [
272 'useColumnsForDefaultValues' => 'aField',
273 ],
274 'columns' => [
275 'aField' => [],
276 ],
277 ],
278 ];
279 $expected = [
280 'aField' => 'valueFromNeighbor',
281 'pid' => 23,
282 ];
283 $result = (new DatabaseRowInitializeNew)->addData($input);
284 $this->assertSame($expected, $result['databaseRow']);
285 }
286
287 /**
288 * @test
289 */
290 public function addDataSetsDefaultDataOverrulingFromNeighborRow()
291 {
292 $input = [
293 'command' => 'new',
294 'tableName' => 'aTable',
295 'vanillaUid' => 23,
296 'inlineChildChildUid' => null,
297 'isInlineChild' => false,
298 'databaseRow' => [],
299 'neighborRow' => [
300 'aField' => 'valueFromNeighbor',
301 ],
302 'pageTsConfig' => [
303 'TCAdefaults.' => [
304 'aTable.' => [
305 'aField' => 'pageTsValue',
306 ],
307 ],
308 ],
309 'userTsConfig' => [
310 'TCAdefaults.' => [
311 'aTable.' => [
312 'aField' => 'userTsValue',
313 ],
314 ],
315 ],
316 'processedTca' => [
317 'ctrl' => [
318 'useColumnsForDefaultValues' => 'aField',
319 ],
320 'columns' => [
321 'aField' => [],
322 ],
323 ],
324 ];
325 $expected = [
326 'aField' => 'valueFromNeighbor',
327 'pid' => 23,
328 ];
329 $result = (new DatabaseRowInitializeNew)->addData($input);
330 $this->assertSame($expected, $result['databaseRow']);
331 }
332
333 /**
334 * @test
335 */
336 public function addDataSetsDefaultDataFromGetIfColumnIsDefinedInTca()
337 {
338 $input = [
339 'command' => 'new',
340 'tableName' => 'aTable',
341 'vanillaUid' => 23,
342 'neighborRow' => null,
343 'inlineChildChildUid' => null,
344 'isInlineChild' => false,
345 'databaseRow' => [],
346 'processedTca' => [
347 'columns' => [
348 'aField' => [],
349 ],
350 ]
351 ];
352 $GLOBALS['_GET'] = [
353 'defVals' => [
354 'aTable' => [
355 'aField' => 'getValue',
356 ],
357 ],
358 ];
359 $expected = [
360 'aField' => 'getValue',
361 'pid' => 23,
362 ];
363 $result = (new DatabaseRowInitializeNew)->addData($input);
364 $this->assertSame($expected, $result['databaseRow']);
365 }
366
367 /**
368 * @test
369 */
370 public function addDataSetsDefaultDataFromPostIfColumnIsDefinedInTca()
371 {
372 $input = [
373 'command' => 'new',
374 'tableName' => 'aTable',
375 'vanillaUid' => 23,
376 'neighborRow' => null,
377 'inlineChildChildUid' => null,
378 'isInlineChild' => false,
379 'databaseRow' => [],
380 'processedTca' => [
381 'columns' => [
382 'aField' => [],
383 ],
384 ]
385 ];
386 $GLOBALS['_POST'] = [
387 'defVals' => [
388 'aTable' => [
389 'aField' => 'postValue',
390 ],
391 ],
392 ];
393 $expected = [
394 'aField' => 'postValue',
395 'pid' => 23,
396 ];
397 $result = (new DatabaseRowInitializeNew)->addData($input);
398 $this->assertSame($expected, $result['databaseRow']);
399 }
400
401 /**
402 * @test
403 */
404 public function addDataSetsPrioritizesDefaultPostOverDefaultGet()
405 {
406 $input = [
407 'command' => 'new',
408 'tableName' => 'aTable',
409 'vanillaUid' => 23,
410 'neighborRow' => null,
411 'inlineChildChildUid' => null,
412 'isInlineChild' => false,
413 'databaseRow' => [],
414 'processedTca' => [
415 'columns' => [
416 'aField' => [],
417 ],
418 ]
419 ];
420 $GLOBALS['_GET'] = [
421 'defVals' => [
422 'aTable' => [
423 'aField' => 'getValue',
424 ],
425 ],
426 ];
427 $GLOBALS['_POST'] = [
428 'defVals' => [
429 'aTable' => [
430 'aField' => 'postValue',
431 ],
432 ],
433 ];
434 $expected = [
435 'aField' => 'postValue',
436 'pid' => 23,
437 ];
438 $result = (new DatabaseRowInitializeNew)->addData($input);
439 $this->assertSame($expected, $result['databaseRow']);
440 }
441
442 /**
443 * @test
444 */
445 public function addDataDoesNotSetDefaultDataFromGetPostIfColumnIsMissingInTca()
446 {
447 $input = [
448 'command' => 'new',
449 'tableName' => 'aTable',
450 'vanillaUid' => 23,
451 'neighborRow' => null,
452 'inlineChildChildUid' => null,
453 'isInlineChild' => false,
454 'databaseRow' => [],
455 'userTsConfig' => [
456 'TCAdefaults.' => [
457 'aTable.' => [
458 'aField' => 'pageTsValue',
459 ],
460 ],
461 ],
462 'processedTca' => [
463 'columns' => [],
464 ]
465 ];
466 $GLOBALS['_GET'] = [
467 'defVals' => [
468 'aTable' => [
469 'aField' => 'getValue',
470 ],
471 ],
472 ];
473 $GLOBALS['_POST'] = [
474 'defVals' => [
475 'aTable' => [
476 'aField' => 'postValue',
477 ],
478 ],
479 ];
480 $expected = [
481 'pid' => 23,
482 ];
483 $result = (new DatabaseRowInitializeNew)->addData($input);
484 $this->assertSame($expected, $result['databaseRow']);
485 }
486
487 /**
488 * @test
489 */
490 public function addDataSetsDefaultDataOverrulingGetPost()
491 {
492 $input = [
493 'command' => 'new',
494 'tableName' => 'aTable',
495 'vanillaUid' => 23,
496 'inlineChildChildUid' => null,
497 'isInlineChild' => false,
498 'databaseRow' => [],
499 'neighborRow' => [
500 'aField' => 'valueFromNeighbor',
501 ],
502 'pageTsConfig' => [
503 'TCAdefaults.' => [
504 'aTable.' => [
505 'aField' => 'pageTsValue',
506 ],
507 ],
508 ],
509 'userTsConfig' => [
510 'TCAdefaults.' => [
511 'aTable.' => [
512 'aField' => 'userTsValue',
513 ],
514 ],
515 ],
516 'processedTca' => [
517 'ctrl' => [
518 'useColumnsForDefaultValues' => 'aField',
519 ],
520 'columns' => [
521 'aField' => [],
522 ],
523 ],
524 ];
525 $GLOBALS['_POST'] = [
526 'defVals' => [
527 'aTable' => [
528 'aField' => 'postValue',
529 ],
530 ],
531 ];
532 $expected = [
533 'aField' => 'postValue',
534 'pid' => 23,
535 ];
536 $result = (new DatabaseRowInitializeNew)->addData($input);
537 $this->assertSame($expected, $result['databaseRow']);
538 }
539
540 /**
541 * @test
542 */
543 public function addDataThrowsExceptionWithGivenChildChildUidButMissingInlineConfig()
544 {
545 $input = [
546 'command' => 'new',
547 'tableName' => 'aTable',
548 'neighborRow' => null,
549 'isInlineChild' => false,
550 'databaseRow' => [],
551 'inlineChildChildUid' => 42,
552 ];
553 $this->expectException(\UnexpectedValueException::class);
554 $this->expectExceptionCode(1444434102);
555 (new DatabaseRowInitializeNew)->addData($input);
556 }
557
558 /**
559 * @test
560 */
561 public function addDataThrowsExceptionWithGivenChildChildUidButIsNotInteger()
562 {
563 $input = [
564 'command' => 'new',
565 'tableName' => 'aTable',
566 'neighborRow' => null,
567 'isInlineChild' => false,
568 'databaseRow' => [],
569 'inlineChildChildUid' => '42',
570 ];
571 $this->expectException(\UnexpectedValueException::class);
572 $this->expectExceptionCode(1444434103);
573 (new DatabaseRowInitializeNew)->addData($input);
574 }
575
576 /**
577 * @test
578 */
579 public function addDataSetsForeignSelectorFieldToValueOfChildChildUid()
580 {
581 $input = [
582 'command' => 'new',
583 'tableName' => 'aTable',
584 'neighborRow' => null,
585 'isInlineChild' => false,
586 'databaseRow' => [],
587 'inlineChildChildUid' => 42,
588 'inlineParentConfig' => [
589 'foreign_selector' => 'theForeignSelectorField',
590 ],
591 'processedTca' => [
592 'columns' => [
593 'theForeignSelectorField' => [
594 'config' => [
595 'type' => 'group',
596 ],
597 ],
598 ],
599 ],
600 'vanillaUid' => 5,
601 ];
602 $expected = $input;
603 $expected['databaseRow']['theForeignSelectorField'] = 42;
604 $expected['databaseRow']['pid'] = 5;
605 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
606 }
607
608 /**
609 * @test
610 */
611 public function addDataThrowsExceptionIfForeignSelectorDoesNotPointToGroupOrSelectField()
612 {
613 $input = [
614 'command' => 'new',
615 'tableName' => 'aTable',
616 'neighborRow' => null,
617 'isInlineChild' => false,
618 'databaseRow' => [],
619 'inlineChildChildUid' => 42,
620 'inlineParentConfig' => [
621 'foreign_selector' => 'theForeignSelectorField',
622 ],
623 'processedTca' => [
624 'columns' => [
625 'theForeignSelectorField' => [
626 'config' => [
627 'type' => 'input',
628 ],
629 ],
630 ],
631 ],
632 ];
633 $this->expectException(\UnexpectedValueException::class);
634 $this->expectExceptionCode(1444434104);
635 (new DatabaseRowInitializeNew)->addData($input);
636 }
637
638 /**
639 * @test
640 */
641 public function addDataThrowsExceptionIfInlineParentLanguageIsNoInteger()
642 {
643 $input = [
644 'command' => 'new',
645 'tableName' => 'aTable',
646 'neighborRow' => null,
647 'inlineChildChildUid' => null,
648 'isInlineChild' => false,
649 'databaseRow' => [],
650 'inlineParentConfig' => [
651 'inline' => [
652 'parentSysLanguageUid' => 'not-an-integer',
653 ],
654 ],
655 'processedTca' => [
656 'ctrl' => [
657 'languageField' => 'sys_language_uid',
658 'transOrigPointerField' => 'l10n_parent',
659 ],
660 'columns' => [],
661 ],
662 ];
663 $this->expectException(\UnexpectedValueException::class);
664 $this->expectExceptionCode(1490360772);
665 (new DatabaseRowInitializeNew)->addData($input);
666 }
667
668 /**
669 * @test
670 */
671 public function addDataSetsSysLanguageUidFromParent()
672 {
673 $input = [
674 'command' => 'new',
675 'tableName' => 'aTable',
676 'neighborRow' => null,
677 'inlineChildChildUid' => null,
678 'isInlineChild' => false,
679 'vanillaUid' => 1,
680 'databaseRow' => [],
681 'inlineParentConfig' => [
682 'inline' => [
683 'parentSysLanguageUid' => '42',
684 ],
685 ],
686 'processedTca' => [
687 'ctrl' => [
688 'languageField' => 'sys_language_uid',
689 'transOrigPointerField' => 'l10n_parent',
690 ],
691 'columns' => [],
692 ],
693 ];
694 $expected = $input;
695 $expected['databaseRow'] = [
696 'sys_language_uid' => 42,
697 'pid' => 1,
698 ];
699 $result = (new DatabaseRowInitializeNew)->addData($input);
700 $this->assertSame($expected, $result);
701 }
702
703 /**
704 * @test
705 */
706 public function addDataSetsPidToVanillaUid()
707 {
708 $input = [
709 'command' => 'new',
710 'tableName' => 'aTable',
711 'vanillaUid' => 23,
712 'databaseRow' => [],
713 'neighborRow' => null,
714 'inlineChildChildUid' => null,
715 'isInlineChild' => false,
716 ];
717 $expected['pid'] = 23;
718 $result = (new DatabaseRowInitializeNew)->addData($input);
719 $this->assertSame($expected, $result['databaseRow']);
720 }
721
722 /**
723 * @test
724 */
725 public function addDataDoesNotUsePageTsValueForPidIfRecordIsNotInlineChild()
726 {
727 $input = [
728 'command' => 'new',
729 'tableName' => 'aTable',
730 'vanillaUid' => 23,
731 'neighborRow' => null,
732 'inlineChildChildUid' => null,
733 'databaseRow' => [],
734 'pageTsConfig' => [
735 'TCAdefaults.' => [
736 'aTable.' => [
737 'pid' => '42',
738 ],
739 ],
740 ],
741 'isInlineChild' => false,
742 ];
743 $expected = $input;
744 $expected['databaseRow']['pid'] = 23;
745 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
746 }
747
748 /**
749 * @test
750 */
751 public function addDataThrowsExceptionIfPageTsConfigPidValueCanNotBeInterpretedAsInteger()
752 {
753 $input = [
754 'command' => 'new',
755 'tableName' => 'aTable',
756 'vanillaUid' => 23,
757 'neighborRow' => null,
758 'inlineChildChildUid' => null,
759 'databaseRow' => [],
760 'pageTsConfig' => [
761 'TCAdefaults.' => [
762 'aTable.' => [
763 'pid' => 'notAnInteger',
764 ],
765 ],
766 ],
767 'isInlineChild' => true,
768 ];
769 $this->expectException(\UnexpectedValueException::class);
770 $this->expectExceptionCode(1461598332);
771 (new DatabaseRowInitializeNew)->addData($input);
772 }
773
774 /**
775 * @test
776 */
777 public function addDataDoesUsePageTsValueForPidIfRecordIsInlineChild()
778 {
779 $input = [
780 'command' => 'new',
781 'tableName' => 'aTable',
782 'vanillaUid' => 23,
783 'neighborRow' => null,
784 'inlineChildChildUid' => null,
785 'databaseRow' => [],
786 'pageTsConfig' => [
787 'TCAdefaults.' => [
788 'aTable.' => [
789 'pid' => '42',
790 ],
791 ],
792 ],
793 'isInlineChild' => true,
794 ];
795 $expected = $input;
796 $expected['databaseRow']['pid'] = 42;
797 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
798 }
799 }