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