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