‪TYPO3CMS  10.4
TcaRadioItemsTest.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 ‪TcaRadioItemsTest extends UnitTestCase
32 {
36  protected function ‪tearDown(): void
37  {
38  GeneralUtility::purgeInstances();
39  parent::tearDown();
40  }
41 
46  {
47  $input = [
48  'tableName' => 'aTable',
49  'processedTca' => [
50  'columns' => [
51  'aField' => [
52  'config' => [
53  'type' => 'radio',
54  ],
55  ],
56  ],
57  ],
58  ];
59 
60  $languageService = $this->prophesize(LanguageService::class);
61  ‪$GLOBALS['LANG'] = $languageService->reveal();
62  $languageService->sL(Argument::cetera())->willReturnArgument(0);
63 
64  $this->expectException(\UnexpectedValueException::class);
65  $this->expectExceptionCode(1438594829);
66  (new ‪TcaRadioItems())->addData($input);
67  }
68 
72  public function ‪addDataKeepExistingItems()
73  {
74  $input = [
75  'tableName' => 'aTable',
76  'processedTca' => [
77  'columns' => [
78  'aField' => [
79  'config' => [
80  'type' => 'radio',
81  'items' => [
82  0 => [
83  'foo',
84  'bar',
85  ],
86  ],
87  ],
88  ],
89  ],
90  ],
91  ];
92  $languageService = $this->prophesize(LanguageService::class);
93  ‪$GLOBALS['LANG'] = $languageService->reveal();
94  $languageService->sL(Argument::cetera())->willReturnArgument(0);
95 
96  $expected = $input;
97  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
98  }
99 
104  {
105  $input = [
106  'tableName' => 'aTable',
107  'processedTca' => [
108  'columns' => [
109  'aField' => [
110  'config' => [
111  'type' => 'radio',
112  'items' => [
113  0 => 'aoeu',
114  ],
115  ],
116  ],
117  ],
118  ],
119  ];
120 
121  $languageService = $this->prophesize(LanguageService::class);
122  ‪$GLOBALS['LANG'] = $languageService->reveal();
123  $languageService->sL(Argument::cetera())->willReturnArgument(0);
124 
125  $this->expectException(\UnexpectedValueException::class);
126  $this->expectExceptionCode(1438607163);
127  (new ‪TcaRadioItems())->addData($input);
128  }
129 
134  {
135  $input = [
136  'tableName' => 'aTable',
137  'processedTca' => [
138  'columns' => [
139  'aField' => [
140  'config' => [
141  'type' => 'radio',
142  'items' => [
143  0 => [
144  'funnyKey' => 'funnyValue',
145  ],
146  ],
147  ],
148  ],
149  ],
150  ],
151  ];
152 
153  $languageService = $this->prophesize(LanguageService::class);
154  ‪$GLOBALS['LANG'] = $languageService->reveal();
155  $languageService->sL(Argument::cetera())->willReturnArgument(0);
156 
157  $this->expectException(\UnexpectedValueException::class);
158  $this->expectExceptionCode(1438607164);
159  (new ‪TcaRadioItems())->addData($input);
160  }
161 
166  {
167  $input = [
168  'tableName' => 'aTable',
169  'processedTca' => [
170  'columns' => [
171  'aField' => [
172  'config' => [
173  'type' => 'radio',
174  'items' => [
175  0 => [
176  0 => 'aLabel',
177  ],
178  ],
179  ],
180  ],
181  ],
182  ],
183  ];
184 
185  $languageService = $this->prophesize(LanguageService::class);
186  ‪$GLOBALS['LANG'] = $languageService->reveal();
187  $languageService->sL(Argument::cetera())->willReturnArgument(0);
188 
189  $this->expectException(\UnexpectedValueException::class);
190  $this->expectExceptionCode(1438607165);
191  (new ‪TcaRadioItems())->addData($input);
192  }
193 
198  {
199  $input = [
200  'tableName' => 'aTable',
201  'processedTca' => [
202  'columns' => [
203  'aField' => [
204  'config' => [
205  'type' => 'radio',
206  'items' => [
207  0 => [
208  0 => 'aLabel',
209  1 => 'aValue',
210  ],
211  ],
212  ],
213  ],
214  ],
215  ],
216  ];
217 
219  $languageService = $this->prophesize(LanguageService::class);
220  ‪$GLOBALS['LANG'] = $languageService->reveal();
221 
222  $languageService->sL('aLabel')->shouldBeCalled()->willReturn('translated');
223 
224  $expected = $input;
225  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'translated';
226 
227  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
228  (new ‪TcaRadioItems())->addData($input);
229  }
230 
235  {
236  $input = [
237  'tableName' => 'aTable',
238  'inlineParentUid' => 1,
239  'inlineParentTableName' => 'aTable',
240  'inlineParentFieldName' => 'aField',
241  'inlineParentConfig' => [],
242  'inlineTopMostParentUid' => 1,
243  'inlineTopMostParentTableName' => 'topMostTable',
244  'inlineTopMostParentFieldName' => 'topMostField',
245  'databaseRow' => [],
246  'processedTca' => [
247  'columns' => [
248  'aField' => [
249  'config' => [
250  'type' => 'radio',
251  'items' => [],
252  'itemsProcFunc' => function (array $parameters, $pObj) {
253  $parameters['items'] = [
254  'foo' => 'bar',
255  ];
256  },
257  ],
258  ],
259  ],
260  ],
261  ];
262 
263  $languageService = $this->prophesize(LanguageService::class);
264  ‪$GLOBALS['LANG'] = $languageService->reveal();
265  $languageService->sL(Argument::cetera())->willReturnArgument(0);
266 
267  $expected = $input;
268  $expected['processedTca']['columns']['aField']['config'] = [
269  'type' => 'radio',
270  'items' => [
271  'foo' => 'bar',
272  ],
273  ];
274  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
275  }
276 
281  {
282  $input = [
283  'tableName' => 'aTable',
284  'inlineParentUid' => 1,
285  'inlineParentTableName' => 'aTable',
286  'inlineParentFieldName' => 'aField',
287  'inlineParentConfig' => ['config' => 'someValue'],
288  'inlineTopMostParentUid' => 1,
289  'inlineTopMostParentTableName' => 'topMostTable',
290  'inlineTopMostParentFieldName' => 'topMostField',
291  'databaseRow' => [
292  'aField' => 'aValue',
293  ],
294  'pageTsConfig' => [
295  'TCEFORM.' => [
296  'aTable.' => [
297  'aField.' => [
298  'itemsProcFunc.' => [
299  'itemParamKey' => 'itemParamValue',
300  ],
301  ]
302  ],
303  ],
304  ],
305  'processedTca' => [
306  'columns' => [
307  'aField' => [
308  'config' => [
309  'type' => 'radio',
310  'aKey' => 'aValue',
311  'items' => [
312  0 => [
313  'foo',
314  'bar',
315  ],
316  ],
317  'itemsProcFunc' => function (array $parameters, $pObj) {
318  if ($parameters['items'] !== [ 0 => [ 'foo', 'bar'] ]
319  || $parameters['config']['aKey'] !== 'aValue'
320  || $parameters['TSconfig'] !== [ 'itemParamKey' => 'itemParamValue' ]
321  || $parameters['table'] !== 'aTable'
322  || $parameters['row'] !== [ 'aField' => 'aValue' ]
323  || $parameters['field'] !== 'aField'
324  || $parameters['inlineParentUid'] !== 1
325  || $parameters['inlineParentTableName'] !== 'aTable'
326  || $parameters['inlineParentFieldName'] !== 'aField'
327  || $parameters['inlineParentConfig'] !== ['config' => 'someValue']
328  || $parameters['inlineTopMostParentUid'] !== 1
329  || $parameters['inlineTopMostParentTableName'] !== 'topMostTable'
330  || $parameters['inlineTopMostParentFieldName'] !== 'topMostField'
331  ) {
332  throw new \UnexpectedValueException('broken', 1476109434);
333  }
334  },
335  ],
336  ],
337  ],
338  ],
339  ];
340 
341  $languageService = $this->prophesize(LanguageService::class);
342  ‪$GLOBALS['LANG'] = $languageService->reveal();
343  $languageService->sL(Argument::cetera())->willReturnArgument(0);
345  $flashMessage = $this->prophesize(FlashMessage::class);
346  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
348  $flashMessageService = $this->prophesize(FlashMessageService::class);
349  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' => function (array $parameters, $pObj) {
401  throw new \UnexpectedValueException('anException', 1476109435);
402  },
403  ],
404  ],
405  ],
406  ],
407  ];
408 
409  $languageService = $this->prophesize(LanguageService::class);
410  ‪$GLOBALS['LANG'] = $languageService->reveal();
412  $flashMessage = $this->prophesize(FlashMessage::class);
413  GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
415  $flashMessageService = $this->prophesize(FlashMessageService::class);
416  GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
418  $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
419  $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
420 
421  $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
422 
423  (new ‪TcaRadioItems())->addData($input);
424  }
425 
430  {
431  $input = [
432  'tableName' => 'aTable',
433  'processedTca' => [
434  'columns' => [
435  'aField' => [
436  'config' => [
437  'type' => 'radio',
438  'items' => [
439  0 => [
440  0 => 'aLabel',
441  1 => 'aValue',
442  ],
443  ],
444  ],
445  ],
446  ],
447  ],
448  'pageTsConfig' => [
449  'TCEFORM.' => [
450  'aTable.' => [
451  'aField.' => [
452  'altLabels.' => [
453  0 => 'labelOverride',
454  ],
455  ]
456  ],
457  ],
458  ],
459  ];
460 
462  $languageService = $this->prophesize(LanguageService::class);
463  ‪$GLOBALS['LANG'] = $languageService->reveal();
464  $languageService->sL('aLabel')->willReturnArgument(0);
465 
466  $languageService->sL('labelOverride')->shouldBeCalled()->willReturnArgument(0);
467 
468  $expected = $input;
469  $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'labelOverride';
470 
471  self::assertSame($expected, (new ‪TcaRadioItems())->addData($input));
472  (new ‪TcaRadioItems())->addData($input);
473  }
474 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemValueIsNotSet
‪addDataThrowsExceptionIfItemValueIsNotSet()
Definition: TcaRadioItemsTest.php:165
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataTranslatesItemLabels
‪addDataTranslatesItemLabels()
Definition: TcaRadioItemsTest.php:197
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataCallsItemsProcFunc
‪addDataCallsItemsProcFunc()
Definition: TcaRadioItemsTest.php:234
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemsAreNoArray
‪addDataThrowsExceptionIfItemsAreNoArray()
Definition: TcaRadioItemsTest.php:103
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfRadioItemsNotDefined
‪addDataThrowsExceptionIfRadioItemsNotDefined()
Definition: TcaRadioItemsTest.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataKeepExistingItems
‪addDataKeepExistingItems()
Definition: TcaRadioItemsTest.php:72
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataItemsProcFuncReceivesParameters
‪addDataItemsProcFuncReceivesParameters()
Definition: TcaRadioItemsTest.php:280
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\tearDown
‪tearDown()
Definition: TcaRadioItemsTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataItemsProcFuncEnqueuesFlashMessageOnException
‪addDataItemsProcFuncEnqueuesFlashMessageOnException()
Definition: TcaRadioItemsTest.php:363
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataThrowsExceptionIfItemLabelIsNotSet
‪addDataThrowsExceptionIfItemLabelIsNotSet()
Definition: TcaRadioItemsTest.php:133
‪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\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest\addDataTranslatesItemLabelsFromPageTsConfig
‪addDataTranslatesItemLabelsFromPageTsConfig()
Definition: TcaRadioItemsTest.php:429
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaRadioItemsTest
Definition: TcaRadioItemsTest.php:32
‪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\Form\FormDataProvider\TcaRadioItems
Definition: TcaRadioItems.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18