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