‪TYPO3CMS  10.4
TcaCheckboxItemsTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
19 use Prophecy\Prophecy\ObjectProphecy;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪TcaCheckboxItemsTest extends UnitTestCase
32 {
36  protected function ‪tearDown(): void
37  {
38  GeneralUtility::purgeInstances();
39  parent::tearDown();
40  }
41 
45  public function ‪checkboxConfigurationDataProvider(): array
46  {
47  return [
48  'simpleCheckboxConfig' => [
49  'input' => [
50  'tableName' => 'foo',
51  'processedTca' => [
52  'columns' => [
53  'aField' => [
54  'config' => [
55  'type' => 'check',
56  'items' => [
57  0 => [
58  'foo',
59  'bar',
60  ],
61  ],
62  ],
63  ],
64  ],
65  ],
66  ],
67  'expected' => [
68  'tableName' => 'foo',
69  'processedTca' => [
70  'columns' => [
71  'aField' => [
72  'config' => [
73  'type' => 'check',
74  'items' => [
75  0 => [
76  'foo', // @todo a followup patch should refactor towards 'label' => 'foo'
77  'bar', // @todo a followup patch should remove this numeric key altogether
78  'invertStateDisplay' => false
79  ],
80  ],
81  ],
82  ],
83  ],
84  ],
85  ]
86  ],
87  'brokenSimpleCheckboxConfig' => [
88  'input' => [
89  'tableName' => 'foo',
90  'processedTca' => [
91  'columns' => [
92  'aField' => [
93  'config' => [
94  'type' => 'check',
95  'items' => [
96  0 => [
97  'foo',
98  'bar',
99  'baz'
100  ],
101  ],
102  ],
103  ],
104  ],
105  ],
106  ],
107  'expected' => [
108  'tableName' => 'foo',
109  'processedTca' => [
110  'columns' => [
111  'aField' => [
112  'config' => [
113  'type' => 'check',
114  'items' => [
115  0 => [
116  'foo',
117  'bar',
118  'invertStateDisplay' => false
119  ],
120  ],
121  ],
122  ],
123  ],
124  ],
125  ]
126  ],
127  'toggleCheckboxConfig' => [
128  'input' => [
129  'tableName' => 'foo',
130  'processedTca' => [
131  'columns' => [
132  'aField' => [
133  'config' => [
134  'type' => 'check',
135  'renderType' => 'checkboxToggle',
136  'items' => [
137  0 => [
138  'foo',
139  'bar',
140  'labelChecked' => 'Enabled',
141  'labelUnchecked' => 'Disabled'
142  ],
143  ],
144  ],
145  ],
146  ],
147  ],
148  ],
149  'expected' => [
150  'tableName' => 'foo',
151  'processedTca' => [
152  'columns' => [
153  'aField' => [
154  'config' => [
155  'type' => 'check',
156  'renderType' => 'checkboxToggle',
157  'items' => [
158  0 => [
159  'foo',
160  'bar',
161  'invertStateDisplay' => false
162  ],
163  ],
164  ],
165  ],
166  ],
167  ],
168  ]
169  ],
170  'inverted_toggleCheckboxConfig' => [
171  'input' => [
172  'tableName' => 'foo',
173  'processedTca' => [
174  'columns' => [
175  'aField' => [
176  'config' => [
177  'type' => 'check',
178  'renderType' => 'checkboxToggle',
179  'items' => [
180  0 => [
181  'foo',
182  'bar',
183  'labelChecked' => 'Enabled',
184  'labelUnchecked' => 'Disabled',
185  'invertStateDisplay' => true
186  ],
187  ],
188  ],
189  ],
190  ],
191  ],
192  ],
193  'expected' => [
194  'tableName' => 'foo',
195  'processedTca' => [
196  'columns' => [
197  'aField' => [
198  'config' => [
199  'type' => 'check',
200  'renderType' => 'checkboxToggle',
201  'items' => [
202  0 => [
203  'foo',
204  'bar',
205  'invertStateDisplay' => true
206  ],
207  ],
208  ],
209  ],
210  ],
211  ],
212  ]
213  ],
214  'labeledCheckboxConfig' => [
215  'input' => [
216  'tableName' => 'foo',
217  'processedTca' => [
218  'columns' => [
219  'aField' => [
220  'config' => [
221  'type' => 'check',
222  'renderType' => 'checkboxLabeledToggle',
223  'items' => [
224  0 => [
225  'foo',
226  'bar',
227  'labelChecked' => 'Enabled',
228  'labelUnchecked' => 'Disabled'
229  ],
230  ],
231  ],
232  ],
233  ],
234  ],
235  ],
236  'expected' => [
237  'tableName' => 'foo',
238  'processedTca' => [
239  'columns' => [
240  'aField' => [
241  'config' => [
242  'type' => 'check',
243  'renderType' => 'checkboxLabeledToggle',
244  'items' => [
245  0 => [
246  'foo',
247  'bar',
248  'labelChecked' => 'Enabled',
249  'labelUnchecked' => 'Disabled',
250  'invertStateDisplay' => false
251  ],
252  ],
253  ],
254  ],
255  ],
256  ],
257  ]
258  ],
259  'inverted_labeledCheckboxConfig' => [
260  'input' => [
261  'tableName' => 'foo',
262  'processedTca' => [
263  'columns' => [
264  'aField' => [
265  'config' => [
266  'type' => 'check',
267  'renderType' => 'checkboxLabeledToggle',
268  'items' => [
269  0 => [
270  'foo',
271  'bar',
272  'labelChecked' => 'Enabled',
273  'labelUnchecked' => 'Disabled',
274  'invertStateDisplay' => true
275  ],
276  ],
277  ],
278  ],
279  ],
280  ],
281  ],
282  'expected' => [
283  'tableName' => 'foo',
284  'processedTca' => [
285  'columns' => [
286  'aField' => [
287  'config' => [
288  'type' => 'check',
289  'renderType' => 'checkboxLabeledToggle',
290  'items' => [
291  0 => [
292  'foo',
293  'bar',
294  'labelChecked' => 'Enabled',
295  'labelUnchecked' => 'Disabled',
296  'invertStateDisplay' => true
297  ],
298  ],
299  ],
300  ],
301  ],
302  ],
303  ]
304  ],
305  'iconCheckboxConfig' => [
306  'input' => [
307  'tableName' => 'foo',
308  'processedTca' => [
309  'columns' => [
310  'aField' => [
311  'config' => [
312  'type' => 'check',
313  'renderType' => 'checkboxIconToggle',
314  'items' => [
315  0 => [
316  'foo',
317  'bar',
318  'labelChecked' => 'Enabled',
319  'labelUnchecked' => 'Disabled',
320  'iconIdentifierChecked' => 'styleguide-icon-toggle-checked',
321  'iconIdentifierUnchecked' => 'styleguide-icon-toggle-checked',
322  ],
323  ],
324  ],
325  ],
326  ],
327  ],
328  ],
329  'expected' => [
330  'tableName' => 'foo',
331  'processedTca' => [
332  'columns' => [
333  'aField' => [
334  'config' => [
335  'type' => 'check',
336  'renderType' => 'checkboxIconToggle',
337  'items' => [
338  0 => [
339  'foo',
340  'bar',
341  'iconIdentifierChecked' => 'styleguide-icon-toggle-checked',
342  'iconIdentifierUnchecked' => 'styleguide-icon-toggle-checked',
343  'invertStateDisplay' => false
344  ],
345  ],
346  ],
347  ],
348  ],
349  ],
350  ]
351  ],
352  'inverted_iconCheckboxConfig' => [
353  'input' => [
354  'tableName' => 'foo',
355  'processedTca' => [
356  'columns' => [
357  'aField' => [
358  'config' => [
359  'type' => 'check',
360  'renderType' => 'checkboxIconToggle',
361  'items' => [
362  0 => [
363  'foo',
364  'bar',
365  'labelChecked' => 'Enabled',
366  'labelUnchecked' => 'Disabled',
367  'iconIdentifierChecked' => 'styleguide-icon-toggle-checked',
368  'iconIdentifierUnchecked' => 'styleguide-icon-toggle-checked',
369  'invertStateDisplay' => true
370  ],
371  ],
372  ],
373  ],
374  ],
375  ],
376  ],
377  'expected' => [
378  'tableName' => 'foo',
379  'processedTca' => [
380  'columns' => [
381  'aField' => [
382  'config' => [
383  'type' => 'check',
384  'renderType' => 'checkboxIconToggle',
385  'items' => [
386  0 => [
387  'foo',
388  'bar',
389  'iconIdentifierChecked' => 'styleguide-icon-toggle-checked',
390  'iconIdentifierUnchecked' => 'styleguide-icon-toggle-checked',
391  'invertStateDisplay' => true
392  ],
393  ],
394  ],
395  ],
396  ],
397  ],
398  ]
399  ],
400  ];
401  }
402 
407  public function ‪addDataKeepExistingItems($input, $expectedResult)
408  {
409  $languageService = $this->prophesize(LanguageService::class);
410  ‪$GLOBALS['LANG'] = $languageService->reveal();
411  $languageService->sL(Argument::cetera())->willReturnArgument(0);
412 
413  self::assertSame($expectedResult, (new ‪TcaCheckboxItems())->addData($input));
414  }
415 
420  {
421  $input = [
422  'processedTca' => [
423  'columns' => [
424  'aField' => [
425  'config' => [
426  'type' => 'check',
427  'items' => [
428  0 => 'aoeu',
429  ],
430  ],
431  ],
432  ],
433  ],
434  'tableName' => 'foo'
435  ];
436 
437  $languageService = $this->prophesize(LanguageService::class);
438  ‪$GLOBALS['LANG'] = $languageService->reveal();
439  $languageService->sL(Argument::cetera())->willReturnArgument(0);
440 
441  $this->expectException(\UnexpectedValueException::class);
442  $this->expectExceptionCode(1440499337);
443  (new ‪TcaCheckboxItems())->addData($input);
444  }
445 
450  {
451  $input = [
452  'processedTca' => [
453  'columns' => [
454  'aField' => [
455  'config' => [
456  'type' => 'check',
457  'items' => [
458  0 => [
459  'funnyKey' => 'funnyValue',
460  ],
461  ],
462  ],
463  ],
464  ],
465  ],
466  'tableName' => 'foo'
467  ];
468 
469  $languageService = $this->prophesize(LanguageService::class);
470  ‪$GLOBALS['LANG'] = $languageService->reveal();
471  $languageService->sL(Argument::cetera())->willReturnArgument(0);
472 
473  $this->expectException(\UnexpectedValueException::class);
474  $this->expectExceptionCode(1440499338);
475  (new ‪TcaCheckboxItems())->addData($input);
476  }
477 
482  {
483  $input = [
484  'processedTca' => [
485  'columns' => [
486  'aField' => [
487  'config' => [
488  'type' => 'check',
489  'items' => [
490  0 => [
491  0 => 'aLabel',
492  1 => 'aValue',
493  ],
494  ],
495  ],
496  ],
497  ],
498  ],
499  'tableName' => 'foo'
500  ];
501 
503  $languageService = $this->prophesize(LanguageService::class);
504  ‪$GLOBALS['LANG'] = $languageService->reveal();
505 
506  $languageService->sL('aLabel')->shouldBeCalled()->willReturn('translated');
507 
508  $expected = $input;
509  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'translated';
510  $expected['processedTca']['columns']['aField']['config']['items'][0]['invertStateDisplay'] = false;
511 
512  self::assertSame($expected, (new ‪TcaCheckboxItems())->addData($input));
513  }
514 
519  {
520  $input = [
521  'tableName' => 'aTable',
522  'inlineParentUid' => 1,
523  'inlineParentTableName' => 'aTable',
524  'inlineParentFieldName' => 'aField',
525  'inlineParentConfig' => [],
526  'inlineTopMostParentUid' => 1,
527  'inlineTopMostParentTableName' => 'topMostTable',
528  'inlineTopMostParentFieldName' => 'topMostField',
529  'databaseRow' => [],
530  'processedTca' => [
531  'columns' => [
532  'aField' => [
533  'config' => [
534  'type' => 'check',
535  'items' => [],
536  'itemsProcFunc' => function (array $parameters, $pObj) {
537  $parameters['items'] = [
538  'foo' => 'bar',
539  ];
540  },
541  ],
542  ],
543  ],
544  ],
545  ];
546 
547  $languageService = $this->prophesize(LanguageService::class);
548  ‪$GLOBALS['LANG'] = $languageService->reveal();
549  $languageService->sL(Argument::cetera())->willReturnArgument(0);
550 
551  $expected = $input;
552  $expected['processedTca']['columns']['aField']['config'] = [
553  'type' => 'check',
554  'items' => [
555  'foo' => 'bar',
556  ],
557  ];
558  self::assertSame($expected, (new ‪TcaCheckboxItems())->addData($input));
559  }
560 
565  {
566  $input = [
567  'tableName' => 'aTable',
568  'inlineParentUid' => 1,
569  'inlineParentTableName' => 'aTable',
570  'inlineParentFieldName' => 'aField',
571  'inlineParentConfig' => ['config' => 'someValue'],
572  'inlineTopMostParentUid' => 1,
573  'inlineTopMostParentTableName' => 'topMostTable',
574  'inlineTopMostParentFieldName' => 'topMostField',
575  'databaseRow' => [
576  'aField' => 'aValue',
577  ],
578  'pageTsConfig' => [
579  'TCEFORM.' => [
580  'aTable.' => [
581  'aField.' => [
582  'itemsProcFunc.' => [
583  'itemParamKey' => 'itemParamValue',
584  ],
585  ]
586  ],
587  ],
588  ],
589  'flexParentDatabaseRow' => [
590  'aParentDatabaseRowFieldName' => 'aParentDatabaseRowFieldValue',
591  ],
592  'processedTca' => [
593  'columns' => [
594  'aField' => [
595  'config' => [
596  'type' => 'check',
597  'aKey' => 'aValue',
598  'items' => [
599  0 => [
600  0 => 'foo',
601  1 => 'bar',
602  'invertedStateDisplay' => false
603  ],
604  ],
605  'itemsProcFunc' => function (array $parameters, $pObj) {
606  if (
607  $parameters['items'] !== [ 0 => [0=>'foo', 1 =>'bar', 'invertStateDisplay' => false]]
608  || $parameters['config']['aKey'] !== 'aValue'
609  || $parameters['TSconfig'] !== [ 'itemParamKey' => 'itemParamValue' ]
610  || $parameters['table'] !== 'aTable'
611  || $parameters['row'] !== [ 'aField' => 'aValue' ]
612  || $parameters['field'] !== 'aField'
613  || $parameters['flexParentDatabaseRow']['aParentDatabaseRowFieldName'] !== 'aParentDatabaseRowFieldValue'
614  || $parameters['inlineParentUid'] !== 1
615  || $parameters['inlineParentTableName'] !== 'aTable'
616  || $parameters['inlineParentFieldName'] !== 'aField'
617  || $parameters['inlineParentConfig'] !== ['config' => 'someValue']
618  || $parameters['inlineTopMostParentUid'] !== 1
619  || $parameters['inlineTopMostParentTableName'] !== 'topMostTable'
620  || $parameters['inlineTopMostParentFieldName'] !== 'topMostField'
621  ) {
622  throw new \UnexpectedValueException('broken', 1476109402);
623  }
624  },
625  ],
626  ],
627  ],
628  ],
629  ];
630 
631  $languageService = $this->prophesize(LanguageService::class);
632  ‪$GLOBALS['LANG'] = $languageService->reveal();
633  $languageService->sL(Argument::cetera())->willReturnArgument(0);
635  $flashMessage = $this->prophesize(FlashMessage::class);
636  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
638  $flashMessageService = $this->prophesize(FlashMessageService::class);
639  GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
641  $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
642  $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
643 
644  // itemsProcFunc must NOT have raised an exception
645  $flashMessageQueue->enqueue($flashMessage)->shouldNotBeCalled();
646 
647  (new ‪TcaCheckboxItems())->addData($input);
648  }
649 
654  {
655  $input = [
656  'tableName' => 'aTable',
657  'inlineParentUid' => 1,
658  'inlineParentTableName' => 'aTable',
659  'inlineParentFieldName' => 'aField',
660  'inlineParentConfig' => [],
661  'inlineTopMostParentUid' => 1,
662  'inlineTopMostParentTableName' => 'topMostTable',
663  'inlineTopMostParentFieldName' => 'topMostField',
664  'databaseRow' => [
665  'aField' => 'aValue',
666  ],
667  'pageTsConfig' => [
668  'TCEFORM.' => [
669  'aTable.' => [
670  'aField.' => [
671  'itemsProcFunc.' => [
672  'itemParamKey' => 'itemParamValue',
673  ],
674  ]
675  ],
676  ],
677  ],
678  'processedTca' => [
679  'columns' => [
680  'aField' => [
681  'config' => [
682  'type' => 'check',
683  'aKey' => 'aValue',
684  'items' => [
685  0 => [
686  'foo',
687  'bar',
688  ],
689  ],
690  'itemsProcFunc' => function (array $parameters, $pObj) {
691  throw new \UnexpectedValueException('anException', 1438604329);
692  },
693  ],
694  ],
695  ],
696  ],
697  ];
698 
699  $languageService = $this->prophesize(LanguageService::class);
700  ‪$GLOBALS['LANG'] = $languageService->reveal();
702  $flashMessage = $this->prophesize(FlashMessage::class);
703  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
705  $flashMessageService = $this->prophesize(FlashMessageService::class);
706  GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
708  $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
709  $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
710 
711  $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
712 
713  (new ‪TcaCheckboxItems())->addData($input);
714  }
715 
720  {
721  $input = [
722  'tableName' => 'aTable',
723  'processedTca' => [
724  'columns' => [
725  'aField' => [
726  'config' => [
727  'type' => 'check',
728  'items' => [
729  0 => [
730  0 => 'aLabel',
731  1 => 'aValue',
732  ],
733  ],
734  ],
735  ],
736  ],
737  ],
738  'pageTsConfig' => [
739  'TCEFORM.' => [
740  'aTable.' => [
741  'aField.' => [
742  'altLabels.' => [
743  0 => 'labelOverride',
744  ],
745  ]
746  ],
747  ],
748  ],
749  ];
750 
752  $languageService = $this->prophesize(LanguageService::class);
753  ‪$GLOBALS['LANG'] = $languageService->reveal();
754  $languageService->sL('aLabel')->willReturnArgument(0);
755 
756  $languageService->sL('labelOverride')->shouldBeCalled()->willReturnArgument(0);
757 
758  $expected = $input;
759  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'labelOverride';
760  $expected['processedTca']['columns']['aField']['config']['items'][0]['invertStateDisplay'] = false;
761 
762  self::assertSame($expected, (new ‪TcaCheckboxItems())->addData($input));
763  }
764 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataTranslatesItemLabelsFromPageTsConfig
‪addDataTranslatesItemLabelsFromPageTsConfig()
Definition: TcaCheckboxItemsTest.php:719
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataKeepExistingItems
‪addDataKeepExistingItems($input, $expectedResult)
Definition: TcaCheckboxItemsTest.php:407
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataThrowsExceptionIfItemsAreNoArray
‪addDataThrowsExceptionIfItemsAreNoArray()
Definition: TcaCheckboxItemsTest.php:419
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest
Definition: TcaCheckboxItemsTest.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataCallsItemsProcFunc
‪addDataCallsItemsProcFunc()
Definition: TcaCheckboxItemsTest.php:518
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataThrowsExceptionIfItemLabelIsNotSet
‪addDataThrowsExceptionIfItemLabelIsNotSet()
Definition: TcaCheckboxItemsTest.php:449
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataItemsProcFuncReceivesParameters
‪addDataItemsProcFuncReceivesParameters()
Definition: TcaCheckboxItemsTest.php:564
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\checkboxConfigurationDataProvider
‪array checkboxConfigurationDataProvider()
Definition: TcaCheckboxItemsTest.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataTranslatesItemLabels
‪addDataTranslatesItemLabels()
Definition: TcaCheckboxItemsTest.php:481
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems
Definition: TcaCheckboxItems.php:24
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\tearDown
‪tearDown()
Definition: TcaCheckboxItemsTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaCheckboxItemsTest\addDataItemsProcFuncEnqueuesFlashMessageOnException
‪addDataItemsProcFuncEnqueuesFlashMessageOnException()
Definition: TcaCheckboxItemsTest.php:653