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