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