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