‪TYPO3CMS  11.5
TcaRadioItemsTest.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 
33 class ‪TcaRadioItemsTest extends UnitTestCase
34 {
35  use ProphecyTrait;
36 
40  protected function ‪tearDown(): void
41  {
42  GeneralUtility::purgeInstances();
43  parent::tearDown();
44  }
45 
50  {
51  $input = [
52  'tableName' => 'aTable',
53  'processedTca' => [
54  'columns' => [
55  'aField' => [
56  'config' => [
57  'type' => 'radio',
58  ],
59  ],
60  ],
61  ],
62  ];
63 
64  $languageService = $this->prophesize(LanguageService::class);
65  ‪$GLOBALS['LANG'] = $languageService->reveal();
66  $languageService->sL(Argument::cetera())->willReturnArgument(0);
67 
68  $this->expectException(\UnexpectedValueException::class);
69  $this->expectExceptionCode(1438594829);
70  (new ‪TcaRadioItems())->addData($input);
71  }
72 
76  public function ‪addDataKeepExistingItems(): void
77  {
78  $input = [
79  'tableName' => 'aTable',
80  'processedTca' => [
81  'columns' => [
82  'aField' => [
83  'config' => [
84  'type' => 'radio',
85  'items' => [
86  0 => [
87  'foo',
88  'bar',
89  ],
90  ],
91  ],
92  ],
93  ],
94  ],
95  ];
96  $languageService = $this->prophesize(LanguageService::class);
97  ‪$GLOBALS['LANG'] = $languageService->reveal();
98  $languageService->sL(Argument::cetera())->willReturnArgument(0);
99 
100  $expected = $input;
101  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
102  }
103 
107  public function ‪addDataThrowsExceptionIfItemsAreNoArray(): void
108  {
109  $input = [
110  'tableName' => 'aTable',
111  'processedTca' => [
112  'columns' => [
113  'aField' => [
114  'config' => [
115  'type' => 'radio',
116  'items' => [
117  0 => 'aoeu',
118  ],
119  ],
120  ],
121  ],
122  ],
123  ];
124 
125  $languageService = $this->prophesize(LanguageService::class);
126  ‪$GLOBALS['LANG'] = $languageService->reveal();
127  $languageService->sL(Argument::cetera())->willReturnArgument(0);
128 
129  $this->expectException(\UnexpectedValueException::class);
130  $this->expectExceptionCode(1438607163);
131  (new ‪TcaRadioItems())->addData($input);
132  }
133 
137  public function ‪addDataThrowsExceptionIfItemLabelIsNotSet(): void
138  {
139  $input = [
140  'tableName' => 'aTable',
141  'processedTca' => [
142  'columns' => [
143  'aField' => [
144  'config' => [
145  'type' => 'radio',
146  'items' => [
147  0 => [
148  'funnyKey' => 'funnyValue',
149  ],
150  ],
151  ],
152  ],
153  ],
154  ],
155  ];
156 
157  $languageService = $this->prophesize(LanguageService::class);
158  ‪$GLOBALS['LANG'] = $languageService->reveal();
159  $languageService->sL(Argument::cetera())->willReturnArgument(0);
160 
161  $this->expectException(\UnexpectedValueException::class);
162  $this->expectExceptionCode(1438607164);
163  (new ‪TcaRadioItems())->addData($input);
164  }
165 
169  public function ‪addDataThrowsExceptionIfItemValueIsNotSet(): void
170  {
171  $input = [
172  'tableName' => 'aTable',
173  'processedTca' => [
174  'columns' => [
175  'aField' => [
176  'config' => [
177  'type' => 'radio',
178  'items' => [
179  0 => [
180  0 => 'aLabel',
181  ],
182  ],
183  ],
184  ],
185  ],
186  ],
187  ];
188 
189  $languageService = $this->prophesize(LanguageService::class);
190  ‪$GLOBALS['LANG'] = $languageService->reveal();
191  $languageService->sL(Argument::cetera())->willReturnArgument(0);
192 
193  $this->expectException(\UnexpectedValueException::class);
194  $this->expectExceptionCode(1438607165);
195  (new ‪TcaRadioItems())->addData($input);
196  }
197 
201  public function ‪addDataTranslatesItemLabels(): void
202  {
203  $input = [
204  'tableName' => 'aTable',
205  'processedTca' => [
206  'columns' => [
207  'aField' => [
208  'config' => [
209  'type' => 'radio',
210  'items' => [
211  0 => [
212  0 => 'aLabel',
213  1 => 'aValue',
214  ],
215  ],
216  ],
217  ],
218  ],
219  ],
220  ];
221 
222  $languageService = $this->prophesize(LanguageService::class);
223  ‪$GLOBALS['LANG'] = $languageService->reveal();
224 
225  $languageService->sL('aLabel')->shouldBeCalled()->willReturn('translated');
226 
227  $expected = $input;
228  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'translated';
229 
230  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
231  (new ‪TcaRadioItems())->addData($input);
232  }
233 
237  public function ‪addDataCallsItemsProcFunc(): void
238  {
239  $input = [
240  'tableName' => 'aTable',
241  'inlineParentUid' => 1,
242  'inlineParentTableName' => 'aTable',
243  'inlineParentFieldName' => 'aField',
244  'inlineParentConfig' => [],
245  'inlineTopMostParentUid' => 1,
246  'inlineTopMostParentTableName' => 'topMostTable',
247  'inlineTopMostParentFieldName' => 'topMostField',
248  'databaseRow' => [],
249  'processedTca' => [
250  'columns' => [
251  'aField' => [
252  'config' => [
253  'type' => 'radio',
254  'items' => [],
255  'itemsProcFunc' => static function (array $parameters, $pObj) {
256  $parameters['items'] = [
257  'foo' => 'bar',
258  ];
259  },
260  ],
261  ],
262  ],
263  ],
264  ];
265 
266  $languageService = $this->prophesize(LanguageService::class);
267  ‪$GLOBALS['LANG'] = $languageService->reveal();
268  $languageService->sL(Argument::cetera())->willReturnArgument(0);
269 
270  $expected = $input;
271  $expected['processedTca']['columns']['aField']['config'] = [
272  'type' => 'radio',
273  'items' => [
274  'foo' => 'bar',
275  ],
276  ];
277  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
278  }
279 
283  public function ‪addDataItemsProcFuncReceivesParameters(): void
284  {
285  $input = [
286  'tableName' => 'aTable',
287  'inlineParentUid' => 1,
288  'inlineParentTableName' => 'aTable',
289  'inlineParentFieldName' => 'aField',
290  'inlineParentConfig' => ['config' => 'someValue'],
291  'inlineTopMostParentUid' => 1,
292  'inlineTopMostParentTableName' => 'topMostTable',
293  'inlineTopMostParentFieldName' => 'topMostField',
294  'databaseRow' => [
295  'aField' => 'aValue',
296  ],
297  'pageTsConfig' => [
298  'TCEFORM.' => [
299  'aTable.' => [
300  'aField.' => [
301  'itemsProcFunc.' => [
302  'itemParamKey' => 'itemParamValue',
303  ],
304  ],
305  ],
306  ],
307  ],
308  'processedTca' => [
309  'columns' => [
310  'aField' => [
311  'config' => [
312  'type' => 'radio',
313  'aKey' => 'aValue',
314  'items' => [
315  0 => [
316  'foo',
317  'bar',
318  ],
319  ],
320  'itemsProcFunc' => static function (array $parameters, $pObj) {
321  if ($parameters['items'] !== [ 0 => [ 'foo', 'bar'] ]
322  || $parameters['config']['aKey'] !== 'aValue'
323  || $parameters['TSconfig'] !== [ 'itemParamKey' => 'itemParamValue' ]
324  || $parameters['table'] !== 'aTable'
325  || $parameters['row'] !== [ 'aField' => 'aValue' ]
326  || $parameters['field'] !== 'aField'
327  || $parameters['inlineParentUid'] !== 1
328  || $parameters['inlineParentTableName'] !== 'aTable'
329  || $parameters['inlineParentFieldName'] !== 'aField'
330  || $parameters['inlineParentConfig'] !== ['config' => 'someValue']
331  || $parameters['inlineTopMostParentUid'] !== 1
332  || $parameters['inlineTopMostParentTableName'] !== 'topMostTable'
333  || $parameters['inlineTopMostParentFieldName'] !== 'topMostField'
334  ) {
335  throw new \UnexpectedValueException('broken', 1476109434);
336  }
337  },
338  ],
339  ],
340  ],
341  ],
342  ];
343 
344  $languageService = $this->prophesize(LanguageService::class);
345  ‪$GLOBALS['LANG'] = $languageService->reveal();
346  $languageService->sL(Argument::cetera())->willReturnArgument(0);
347  $flashMessage = $this->prophesize(FlashMessage::class);
348  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
349  $flashMessageService = $this->prophesize(FlashMessageService::class);
350  GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
351  $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
352  $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
353 
354  // itemsProcFunc must NOT have raised an exception
355  $flashMessageQueue->enqueue($flashMessage)->shouldNotBeCalled();
356 
357  (new ‪TcaRadioItems())->addData($input);
358  }
359 
364  {
365  $input = [
366  'tableName' => 'aTable',
367  'inlineParentUid' => 1,
368  'inlineParentTableName' => 'aTable',
369  'inlineParentFieldName' => 'aField',
370  'inlineParentConfig' => [],
371  'inlineTopMostParentUid' => 1,
372  'inlineTopMostParentTableName' => 'topMostTable',
373  'inlineTopMostParentFieldName' => 'topMostField',
374  'databaseRow' => [
375  'aField' => 'aValue',
376  ],
377  'pageTsConfig' => [
378  'TCEFORM.' => [
379  'aTable.' => [
380  'aField.' => [
381  'itemsProcFunc.' => [
382  'itemParamKey' => 'itemParamValue',
383  ],
384  ],
385  ],
386  ],
387  ],
388  'processedTca' => [
389  'columns' => [
390  'aField' => [
391  'config' => [
392  'type' => 'radio',
393  'aKey' => 'aValue',
394  'items' => [
395  0 => [
396  'foo',
397  'bar',
398  ],
399  ],
400  'itemsProcFunc' => static function (array $parameters, $pObj) {
401  throw new \UnexpectedValueException('anException', 1476109435);
402  },
403  ],
404  ],
405  ],
406  ],
407  ];
408 
409  $languageService = $this->prophesize(LanguageService::class);
410  $languageService->sL(Argument::cetera())->willReturn('');
411  ‪$GLOBALS['LANG'] = $languageService->reveal();
412  $flashMessage = $this->prophesize(FlashMessage::class);
413  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
414  $flashMessageService = $this->prophesize(FlashMessageService::class);
415  GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
416  $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
417  $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
418 
419  $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
420 
421  (new ‪TcaRadioItems())->addData($input);
422  }
423 
427  public function ‪addDataTranslatesItemLabelsFromPageTsConfig(): void
428  {
429  $input = [
430  'tableName' => 'aTable',
431  'processedTca' => [
432  'columns' => [
433  'aField' => [
434  'config' => [
435  'type' => 'radio',
436  'items' => [
437  0 => [
438  0 => 'aLabel',
439  1 => 'aValue',
440  ],
441  ],
442  ],
443  ],
444  ],
445  ],
446  'pageTsConfig' => [
447  'TCEFORM.' => [
448  'aTable.' => [
449  'aField.' => [
450  'altLabels.' => [
451  0 => 'labelOverride',
452  ],
453  ],
454  ],
455  ],
456  ],
457  ];
458 
459  $languageService = $this->prophesize(LanguageService::class);
460  ‪$GLOBALS['LANG'] = $languageService->reveal();
461  $languageService->sL('aLabel')->willReturnArgument(0);
462 
463  $languageService->sL('labelOverride')->shouldBeCalled()->willReturnArgument(0);
464 
465  $expected = $input;
466  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'labelOverride';
467 
468  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
469  (new ‪TcaRadioItems())->addData($input);
470  }
471 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemValueIsNotSet
‪addDataThrowsExceptionIfItemValueIsNotSet()
Definition: TcaRadioItemsTest.php:168
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataTranslatesItemLabels
‪addDataTranslatesItemLabels()
Definition: TcaRadioItemsTest.php:200
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataCallsItemsProcFunc
‪addDataCallsItemsProcFunc()
Definition: TcaRadioItemsTest.php:236
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemsAreNoArray
‪addDataThrowsExceptionIfItemsAreNoArray()
Definition: TcaRadioItemsTest.php:106
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfRadioItemsNotDefined
‪addDataThrowsExceptionIfRadioItemsNotDefined()
Definition: TcaRadioItemsTest.php:48
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataKeepExistingItems
‪addDataKeepExistingItems()
Definition: TcaRadioItemsTest.php:75
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataItemsProcFuncReceivesParameters
‪addDataItemsProcFuncReceivesParameters()
Definition: TcaRadioItemsTest.php:282
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\tearDown
‪tearDown()
Definition: TcaRadioItemsTest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataItemsProcFuncEnqueuesFlashMessageOnException
‪addDataItemsProcFuncEnqueuesFlashMessageOnException()
Definition: TcaRadioItemsTest.php:362
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemLabelIsNotSet
‪addDataThrowsExceptionIfItemLabelIsNotSet()
Definition: TcaRadioItemsTest.php:136
‪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\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataTranslatesItemLabelsFromPageTsConfig
‪addDataTranslatesItemLabelsFromPageTsConfig()
Definition: TcaRadioItemsTest.php:426
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest
Definition: TcaRadioItemsTest.php:34
‪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\Form\FormDataProvider\TcaRadioItems
Definition: TcaRadioItems.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18