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