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