[TASK] Make TYPO3 Core PSR-2 standard compliant
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaRadioItemsTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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;
19 use TYPO3\CMS\Core\Tests\UnitTestCase;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaRadioItems;
21 use TYPO3\CMS\Lang\LanguageService;
22 use TYPO3\CMS\Core\Messaging\FlashMessage;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Test case
29 */
30 class TcaRadioItemsTest extends UnitTestCase
31 {
32 /**
33 * @var TcaRadioItems
34 */
35 protected $subject;
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 protected function setUp()
43 {
44 $this->singletonInstances = GeneralUtility::getSingletonInstances();
45 $this->subject = new TcaRadioItems();
46 }
47
48 protected function tearDown()
49 {
50 GeneralUtility::purgeInstances();
51 GeneralUtility::resetSingletonInstances($this->singletonInstances);
52 parent::tearDown();
53 }
54
55 /**
56 * @test
57 */
58 public function addDataThrowsExceptionIfRadioItemsNotDefined()
59 {
60 $input = [
61 'processedTca' => [
62 'columns' => [
63 'aField' => [
64 'config' => [
65 'type' => 'radio',
66 ],
67 ],
68 ],
69 ],
70 ];
71 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438594829);
72 $this->subject->addData($input);
73 }
74
75 /**
76 * @test
77 */
78 public function addDataKeepExistingItems()
79 {
80 $input = [
81 'processedTca' => [
82 'columns' => [
83 'aField' => [
84 'config' => [
85 'type' => 'radio',
86 'items' => [
87 0 => [
88 'foo',
89 'bar',
90 ],
91 ],
92 ],
93 ],
94 ],
95 ],
96 ];
97 $languageService = $this->prophesize(LanguageService::class);
98 $GLOBALS['LANG'] = $languageService->reveal();
99 $languageService->sL(Argument::cetera())->willReturnArgument(0);
100
101 $expected = $input;
102 $this->assertSame($expected, $this->subject->addData($input));
103 }
104
105 /**
106 * @test
107 */
108 public function addDataThrowsExceptionIfItemsAreNoArray()
109 {
110 $input = [
111 'processedTca' => [
112 'columns' => [
113 'aField' => [
114 'config' => [
115 'type' => 'radio',
116 'items' => [
117 0 => 'aoeu',
118 ],
119 ],
120 ],
121 ],
122 ],
123 ];
124 $this->setExpectedException(\UnexpectedValueException::class, $this->any(), 1438607163);
125 $this->subject->addData($input);
126 }
127
128 /**
129 * @test
130 */
131 public function addDataThrowsExceptionIfItemLabelIsNotSet()
132 {
133 $input = [
134 'processedTca' => [
135 'columns' => [
136 'aField' => [
137 'config' => [
138 'type' => 'radio',
139 'items' => [
140 0 => [
141 'funnyKey' => 'funnyValue',
142 ],
143 ],
144 ],
145 ],
146 ],
147 ],
148 ];
149 $this->setExpectedException(\UnexpectedValueException::class, $this->any(), 1438607164);
150 $this->subject->addData($input);
151 }
152
153 /**
154 * @test
155 */
156 public function addDataThrowsExceptionIfItemValueIsNotSet()
157 {
158 $input = [
159 'processedTca' => [
160 'columns' => [
161 'aField' => [
162 'config' => [
163 'type' => 'radio',
164 'items' => [
165 0 => [
166 0 => 'aLabel',
167 ],
168 ],
169 ],
170 ],
171 ],
172 ],
173 ];
174 $this->setExpectedException(\UnexpectedValueException::class, $this->any(), 1438607165);
175 $this->subject->addData($input);
176 }
177
178 /**
179 * @test
180 */
181 public function addDataTranslatesItemLabels()
182 {
183 $input = [
184 'processedTca' => [
185 'columns' => [
186 'aField' => [
187 'config' => [
188 'type' => 'radio',
189 'items' => [
190 0 => [
191 0 => 'aLabel',
192 1 => 'aValue',
193 ],
194 ],
195 ],
196 ],
197 ],
198 ],
199 ];
200
201 /** @var LanguageService|ObjectProphecy $languageService */
202 $languageService = $this->prophesize(LanguageService::class);
203 $GLOBALS['LANG'] = $languageService->reveal();
204
205 $languageService->sL('aLabel')->shouldBeCalled()->willReturn('translated');
206
207 $expected = $input;
208 $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'translated';
209
210 $this->assertSame($expected, $this->subject->addData($input));
211 $this->subject->addData($input);
212 }
213
214 /**
215 * @test
216 */
217 public function addDataCallsItemsProcFunc()
218 {
219 $input = [
220 'tableName' => 'aTable',
221 'databaseRow' => [],
222 'processedTca' => [
223 'columns' => [
224 'aField' => [
225 'config' => [
226 'type' => 'radio',
227 'items' => [],
228 'itemsProcFunc' => function (array $parameters, $pObj) {
229 $parameters['items'] = [
230 'foo' => 'bar',
231 ];
232 },
233 ],
234 ],
235 ],
236 ],
237 ];
238 $expected = $input;
239 $expected['processedTca']['columns']['aField']['config'] = [
240 'type' => 'radio',
241 'items' => [
242 'foo' => 'bar',
243 ],
244 ];
245 $this->assertSame($expected, $this->subject->addData($input));
246 }
247
248 /**
249 * @test
250 */
251 public function addDataItemsProcFuncReceivesParameters()
252 {
253 $input = [
254 'tableName' => 'aTable',
255 'databaseRow' => [
256 'aField' => 'aValue',
257 ],
258 'pageTsConfigMerged' => [
259 'TCEFORM.' => [
260 'aTable.' => [
261 'aField.' => [
262 'itemsProcFunc.' => [
263 'itemParamKey' => 'itemParamValue',
264 ],
265 ]
266 ],
267 ],
268 ],
269 'processedTca' => [
270 'columns' => [
271 'aField' => [
272 'config' => [
273 'type' => 'radio',
274 'aKey' => 'aValue',
275 'items' => [
276 0 => [
277 'foo',
278 'bar',
279 ],
280 ],
281 'itemsProcFunc' => function (array $parameters, $pObj) {
282 if ($parameters['items'] !== [ 0 => [ 'foo', 'bar'] ]
283 || $parameters['config']['aKey'] !== 'aValue'
284 || $parameters['TSconfig'] !== [ 'itemParamKey' => 'itemParamValue' ]
285 || $parameters['table'] !== 'aTable'
286 || $parameters['row'] !== [ 'aField' => 'aValue' ]
287 || $parameters['field'] !== 'aField'
288 ) {
289 throw new \UnexpectedValueException('broken', 1438604329);
290 }
291 },
292 ],
293 ],
294 ],
295 ],
296 ];
297
298 $languageService = $this->prophesize(LanguageService::class);
299 $GLOBALS['LANG'] = $languageService->reveal();
300 $languageService->sL(Argument::cetera())->willReturnArgument(0);
301 /** @var FlashMessage|ObjectProphecy $flashMessage */
302 $flashMessage = $this->prophesize(FlashMessage::class);
303 GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
304 /** @var FlashMessageService|ObjectProphecy $flashMessageService */
305 $flashMessageService = $this->prophesize(FlashMessageService::class);
306 GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
307 /** @var FlashMessageQueue|ObjectProphecy $flashMessageQueue */
308 $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
309 $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
310
311 // itemsProcFunc must NOT have raised an exception
312 $flashMessageQueue->enqueue($flashMessage)->shouldNotBeCalled();
313
314 $this->subject->addData($input);
315 }
316
317 /**
318 * @test
319 */
320 public function addDataItemsProcFuncEnqueuesFlashMessageOnException()
321 {
322 $input = [
323 'tableName' => 'aTable',
324 'databaseRow' => [
325 'aField' => 'aValue',
326 ],
327 'pageTsConfigMerged' => [
328 'TCEFORM.' => [
329 'aTable.' => [
330 'aField.' => [
331 'itemsProcFunc.' => [
332 'itemParamKey' => 'itemParamValue',
333 ],
334 ]
335 ],
336 ],
337 ],
338 'processedTca' => [
339 'columns' => [
340 'aField' => [
341 'config' => [
342 'type' => 'radio',
343 'aKey' => 'aValue',
344 'items' => [
345 0 => [
346 'foo',
347 'bar',
348 ],
349 ],
350 'itemsProcFunc' => function (array $parameters, $pObj) {
351 throw new \UnexpectedValueException('anException', 1438604329);
352 },
353 ],
354 ],
355 ],
356 ],
357 ];
358
359 $languageService = $this->prophesize(LanguageService::class);
360 $GLOBALS['LANG'] = $languageService->reveal();
361 /** @var FlashMessage|ObjectProphecy $flashMessage */
362 $flashMessage = $this->prophesize(FlashMessage::class);
363 GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
364 /** @var FlashMessageService|ObjectProphecy $flashMessageService */
365 $flashMessageService = $this->prophesize(FlashMessageService::class);
366 GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
367 /** @var FlashMessageQueue|ObjectProphecy $flashMessageQueue */
368 $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
369 $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
370
371 $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
372
373 $this->subject->addData($input);
374 }
375
376 /**
377 * @test
378 */
379 public function addDataTranslatesItemLabelsFromPageTsConfig()
380 {
381 $input = [
382 'tableName' => 'aTable',
383 'processedTca' => [
384 'columns' => [
385 'aField' => [
386 'config' => [
387 'type' => 'radio',
388 'items' => [
389 0 => [
390 0 => 'aLabel',
391 1 => 'aValue',
392 ],
393 ],
394 ],
395 ],
396 ],
397 ],
398 'pageTsConfigMerged' => [
399 'TCEFORM.' => [
400 'aTable.' => [
401 'aField.' => [
402 'altLabels.' => [
403 0 => 'labelOverride',
404 ],
405 ]
406 ],
407 ],
408 ],
409 ];
410
411 /** @var LanguageService|ObjectProphecy $languageService */
412 $languageService = $this->prophesize(LanguageService::class);
413 $GLOBALS['LANG'] = $languageService->reveal();
414 $languageService->sL('aLabel')->willReturnArgument(0);
415
416 $languageService->sL('labelOverride')->shouldBeCalled()->willReturnArgument(0);
417
418 $expected = $input;
419 $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'labelOverride';
420
421 $this->assertSame($expected, $this->subject->addData($input));
422 $this->subject->addData($input);
423 }
424 }