[TASK] Free FormEngine from _GP usage
[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 addDataSetsDefaultDataFromDefaultValuesIfColumnIsDefinedInTca()
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 'defaultValues' => [
352 'aTable' => [
353 'aField' => 'getValue',
354 ],
355 ],
356 ];
357 $expected = [
358 'aField' => 'getValue',
359 'pid' => 23,
360 ];
361 $result = (new DatabaseRowInitializeNew)->addData($input);
362 $this->assertSame($expected, $result['databaseRow']);
363 }
364
365 /**
366 * @test
367 */
368 public function addDataDoesNotSetDefaultDataFromDefaultValuesIfColumnIsMissingInTca()
369 {
370 $input = [
371 'command' => 'new',
372 'tableName' => 'aTable',
373 'vanillaUid' => 23,
374 'neighborRow' => null,
375 'inlineChildChildUid' => null,
376 'isInlineChild' => false,
377 'databaseRow' => [],
378 'userTsConfig' => [
379 'TCAdefaults.' => [
380 'aTable.' => [
381 'aField' => 'pageTsValue',
382 ],
383 ],
384 ],
385 'processedTca' => [
386 'columns' => [],
387 ],
388 'defaultValues' => [
389 'aTable' => [
390 'aField' => 'getValue',
391 ],
392 ],
393 ];
394 $expected = [
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 addDataSetsDefaultDataOverrulesOtherDefauls()
405 {
406 $input = [
407 'command' => 'new',
408 'tableName' => 'aTable',
409 'vanillaUid' => 23,
410 'inlineChildChildUid' => null,
411 'isInlineChild' => false,
412 'databaseRow' => [],
413 'neighborRow' => [
414 'aField' => 'valueFromNeighbor',
415 ],
416 'pageTsConfig' => [
417 'TCAdefaults.' => [
418 'aTable.' => [
419 'aField' => 'pageTsValue',
420 ],
421 ],
422 ],
423 'userTsConfig' => [
424 'TCAdefaults.' => [
425 'aTable.' => [
426 'aField' => 'userTsValue',
427 ],
428 ],
429 ],
430 'processedTca' => [
431 'ctrl' => [
432 'useColumnsForDefaultValues' => 'aField',
433 ],
434 'columns' => [
435 'aField' => [],
436 ],
437 ],
438 'defaultValues' => [
439 'aTable' => [
440 'aField' => 'postValue',
441 ],
442 ]
443 ];
444 $expected = [
445 'aField' => 'postValue',
446 'pid' => 23,
447 ];
448 $result = (new DatabaseRowInitializeNew)->addData($input);
449 $this->assertSame($expected, $result['databaseRow']);
450 }
451
452 /**
453 * @test
454 */
455 public function addDataThrowsExceptionWithGivenChildChildUidButMissingInlineConfig()
456 {
457 $input = [
458 'command' => 'new',
459 'tableName' => 'aTable',
460 'neighborRow' => null,
461 'isInlineChild' => false,
462 'databaseRow' => [],
463 'inlineChildChildUid' => 42,
464 ];
465 $this->expectException(\UnexpectedValueException::class);
466 $this->expectExceptionCode(1444434102);
467 (new DatabaseRowInitializeNew)->addData($input);
468 }
469
470 /**
471 * @test
472 */
473 public function addDataThrowsExceptionWithGivenChildChildUidButIsNotInteger()
474 {
475 $input = [
476 'command' => 'new',
477 'tableName' => 'aTable',
478 'neighborRow' => null,
479 'isInlineChild' => false,
480 'databaseRow' => [],
481 'inlineChildChildUid' => '42',
482 ];
483 $this->expectException(\UnexpectedValueException::class);
484 $this->expectExceptionCode(1444434103);
485 (new DatabaseRowInitializeNew)->addData($input);
486 }
487
488 /**
489 * @test
490 */
491 public function addDataSetsForeignSelectorFieldToValueOfChildChildUid()
492 {
493 $input = [
494 'command' => 'new',
495 'tableName' => 'aTable',
496 'neighborRow' => null,
497 'isInlineChild' => false,
498 'databaseRow' => [],
499 'inlineChildChildUid' => 42,
500 'inlineParentConfig' => [
501 'foreign_selector' => 'theForeignSelectorField',
502 ],
503 'processedTca' => [
504 'columns' => [
505 'theForeignSelectorField' => [
506 'config' => [
507 'type' => 'group',
508 ],
509 ],
510 ],
511 ],
512 'vanillaUid' => 5,
513 ];
514 $expected = $input;
515 $expected['databaseRow']['theForeignSelectorField'] = 42;
516 $expected['databaseRow']['pid'] = 5;
517 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
518 }
519
520 /**
521 * @test
522 */
523 public function addDataThrowsExceptionIfForeignSelectorDoesNotPointToGroupOrSelectField()
524 {
525 $input = [
526 'command' => 'new',
527 'tableName' => 'aTable',
528 'neighborRow' => null,
529 'isInlineChild' => false,
530 'databaseRow' => [],
531 'inlineChildChildUid' => 42,
532 'inlineParentConfig' => [
533 'foreign_selector' => 'theForeignSelectorField',
534 ],
535 'processedTca' => [
536 'columns' => [
537 'theForeignSelectorField' => [
538 'config' => [
539 'type' => 'input',
540 ],
541 ],
542 ],
543 ],
544 ];
545 $this->expectException(\UnexpectedValueException::class);
546 $this->expectExceptionCode(1444434104);
547 (new DatabaseRowInitializeNew)->addData($input);
548 }
549
550 /**
551 * @test
552 */
553 public function addDataThrowsExceptionIfInlineParentLanguageIsNoInteger()
554 {
555 $input = [
556 'command' => 'new',
557 'tableName' => 'aTable',
558 'neighborRow' => null,
559 'inlineChildChildUid' => null,
560 'isInlineChild' => false,
561 'databaseRow' => [],
562 'inlineParentConfig' => [
563 'inline' => [
564 'parentSysLanguageUid' => 'not-an-integer',
565 ],
566 ],
567 'processedTca' => [
568 'ctrl' => [
569 'languageField' => 'sys_language_uid',
570 'transOrigPointerField' => 'l10n_parent',
571 ],
572 'columns' => [],
573 ],
574 ];
575 $this->expectException(\UnexpectedValueException::class);
576 $this->expectExceptionCode(1490360772);
577 (new DatabaseRowInitializeNew)->addData($input);
578 }
579
580 /**
581 * @test
582 */
583 public function addDataSetsSysLanguageUidFromParent()
584 {
585 $input = [
586 'command' => 'new',
587 'tableName' => 'aTable',
588 'neighborRow' => null,
589 'inlineChildChildUid' => null,
590 'isInlineChild' => false,
591 'vanillaUid' => 1,
592 'databaseRow' => [],
593 'inlineParentConfig' => [
594 'inline' => [
595 'parentSysLanguageUid' => '42',
596 ],
597 ],
598 'processedTca' => [
599 'ctrl' => [
600 'languageField' => 'sys_language_uid',
601 'transOrigPointerField' => 'l10n_parent',
602 ],
603 'columns' => [],
604 ],
605 ];
606 $expected = $input;
607 $expected['databaseRow'] = [
608 'sys_language_uid' => 42,
609 'pid' => 1,
610 ];
611 $result = (new DatabaseRowInitializeNew)->addData($input);
612 $this->assertSame($expected, $result);
613 }
614
615 /**
616 * @test
617 */
618 public function addDataSetsPidToVanillaUid()
619 {
620 $input = [
621 'command' => 'new',
622 'tableName' => 'aTable',
623 'vanillaUid' => 23,
624 'databaseRow' => [],
625 'neighborRow' => null,
626 'inlineChildChildUid' => null,
627 'isInlineChild' => false,
628 ];
629 $expected['pid'] = 23;
630 $result = (new DatabaseRowInitializeNew)->addData($input);
631 $this->assertSame($expected, $result['databaseRow']);
632 }
633
634 /**
635 * @test
636 */
637 public function addDataDoesNotUsePageTsValueForPidIfRecordIsNotInlineChild()
638 {
639 $input = [
640 'command' => 'new',
641 'tableName' => 'aTable',
642 'vanillaUid' => 23,
643 'neighborRow' => null,
644 'inlineChildChildUid' => null,
645 'databaseRow' => [],
646 'pageTsConfig' => [
647 'TCAdefaults.' => [
648 'aTable.' => [
649 'pid' => '42',
650 ],
651 ],
652 ],
653 'isInlineChild' => false,
654 ];
655 $expected = $input;
656 $expected['databaseRow']['pid'] = 23;
657 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
658 }
659
660 /**
661 * @test
662 */
663 public function addDataThrowsExceptionIfPageTsConfigPidValueCanNotBeInterpretedAsInteger()
664 {
665 $input = [
666 'command' => 'new',
667 'tableName' => 'aTable',
668 'vanillaUid' => 23,
669 'neighborRow' => null,
670 'inlineChildChildUid' => null,
671 'databaseRow' => [],
672 'pageTsConfig' => [
673 'TCAdefaults.' => [
674 'aTable.' => [
675 'pid' => 'notAnInteger',
676 ],
677 ],
678 ],
679 'isInlineChild' => true,
680 ];
681 $this->expectException(\UnexpectedValueException::class);
682 $this->expectExceptionCode(1461598332);
683 (new DatabaseRowInitializeNew)->addData($input);
684 }
685
686 /**
687 * @test
688 */
689 public function addDataDoesUsePageTsValueForPidIfRecordIsInlineChild()
690 {
691 $input = [
692 'command' => 'new',
693 'tableName' => 'aTable',
694 'vanillaUid' => 23,
695 'neighborRow' => null,
696 'inlineChildChildUid' => null,
697 'databaseRow' => [],
698 'pageTsConfig' => [
699 'TCAdefaults.' => [
700 'aTable.' => [
701 'pid' => '42',
702 ],
703 ],
704 ],
705 'isInlineChild' => true,
706 ];
707 $expected = $input;
708 $expected['databaseRow']['pid'] = 42;
709 $this->assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
710 }
711 }