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