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