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