53074310a462d34cd84cc7dcde053da95d86fd3b
[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\Core\Tests\UnitTestCase;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems;
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 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 = array();
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->setExpectedException(\UnexpectedValueException::class, $this->any(), 1440499337);
105 $this->subject->addData($input);
106 }
107
108 /**
109 * @test
110 */
111 public function addDataThrowsExceptionIfItemLabelIsNotSet()
112 {
113 $input = [
114 'processedTca' => [
115 'columns' => [
116 'aField' => [
117 'config' => [
118 'type' => 'check',
119 'items' => [
120 0 => [
121 'funnyKey' => 'funnyValue',
122 ],
123 ],
124 ],
125 ],
126 ],
127 ],
128 ];
129 $this->setExpectedException(\UnexpectedValueException::class, $this->any(), 1440499338);
130 $this->subject->addData($input);
131 }
132
133 /**
134 * @test
135 */
136 public function addDataTranslatesItemLabels()
137 {
138 $input = [
139 'processedTca' => [
140 'columns' => [
141 'aField' => [
142 'config' => [
143 'type' => 'check',
144 'items' => [
145 0 => [
146 0 => 'aLabel',
147 1 => 'aValue',
148 ],
149 ],
150 ],
151 ],
152 ],
153 ],
154 ];
155
156 /** @var LanguageService|ObjectProphecy $languageService */
157 $languageService = $this->prophesize(LanguageService::class);
158 $GLOBALS['LANG'] = $languageService->reveal();
159
160 $languageService->sL('aLabel')->shouldBeCalled()->willReturn('translated');
161
162 $expected = $input;
163 $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'translated';
164
165 $this->assertSame($expected, $this->subject->addData($input));
166 $this->subject->addData($input);
167 }
168
169 /**
170 * @test
171 */
172 public function addDataCallsItemsProcFunc()
173 {
174 $input = [
175 'tableName' => 'aTable',
176 'databaseRow' => [],
177 'processedTca' => [
178 'columns' => [
179 'aField' => [
180 'config' => [
181 'type' => 'check',
182 'items' => [],
183 'itemsProcFunc' => function (array $parameters, $pObj) {
184 $parameters['items'] = [
185 'foo' => 'bar',
186 ];
187 },
188 ],
189 ],
190 ],
191 ],
192 ];
193 $expected = $input;
194 $expected['processedTca']['columns']['aField']['config'] = [
195 'type' => 'check',
196 'items' => [
197 'foo' => 'bar',
198 ],
199 ];
200 $this->assertSame($expected, $this->subject->addData($input));
201 }
202
203 /**
204 * @test
205 */
206 public function addDataItemsProcFuncReceivesParameters()
207 {
208 $input = [
209 'tableName' => 'aTable',
210 'databaseRow' => [
211 'aField' => 'aValue',
212 ],
213 'pageTsConfig' => [
214 'TCEFORM.' => [
215 'aTable.' => [
216 'aField.' => [
217 'itemsProcFunc.' => [
218 'itemParamKey' => 'itemParamValue',
219 ],
220 ]
221 ],
222 ],
223 ],
224 'processedTca' => [
225 'columns' => [
226 'aField' => [
227 'config' => [
228 'type' => 'check',
229 'aKey' => 'aValue',
230 'items' => [
231 0 => [
232 'foo',
233 'bar',
234 ],
235 ],
236 'itemsProcFunc' => function (array $parameters, $pObj) {
237 if ($parameters['items'] !== [ 0 => [ 'foo', 'bar'] ]
238 || $parameters['config']['aKey'] !== 'aValue'
239 || $parameters['TSconfig'] !== [ 'itemParamKey' => 'itemParamValue' ]
240 || $parameters['table'] !== 'aTable'
241 || $parameters['row'] !== [ 'aField' => 'aValue' ]
242 || $parameters['field'] !== 'aField'
243 ) {
244 throw new \UnexpectedValueException('broken', 1438604329);
245 }
246 },
247 ],
248 ],
249 ],
250 ],
251 ];
252
253 $languageService = $this->prophesize(LanguageService::class);
254 $GLOBALS['LANG'] = $languageService->reveal();
255 $languageService->sL(Argument::cetera())->willReturnArgument(0);
256 /** @var FlashMessage|ObjectProphecy $flashMessage */
257 $flashMessage = $this->prophesize(FlashMessage::class);
258 GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
259 /** @var FlashMessageService|ObjectProphecy $flashMessageService */
260 $flashMessageService = $this->prophesize(FlashMessageService::class);
261 GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
262 /** @var FlashMessageQueue|ObjectProphecy $flashMessageQueue */
263 $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
264 $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
265
266 // itemsProcFunc must NOT have raised an exception
267 $flashMessageQueue->enqueue($flashMessage)->shouldNotBeCalled();
268
269 $this->subject->addData($input);
270 }
271
272 /**
273 * @test
274 */
275 public function addDataItemsProcFuncEnqueuesFlashMessageOnException()
276 {
277 $input = [
278 'tableName' => 'aTable',
279 'databaseRow' => [
280 'aField' => 'aValue',
281 ],
282 'pageTsConfig' => [
283 'TCEFORM.' => [
284 'aTable.' => [
285 'aField.' => [
286 'itemsProcFunc.' => [
287 'itemParamKey' => 'itemParamValue',
288 ],
289 ]
290 ],
291 ],
292 ],
293 'processedTca' => [
294 'columns' => [
295 'aField' => [
296 'config' => [
297 'type' => 'check',
298 'aKey' => 'aValue',
299 'items' => [
300 0 => [
301 'foo',
302 'bar',
303 ],
304 ],
305 'itemsProcFunc' => function (array $parameters, $pObj) {
306 throw new \UnexpectedValueException('anException', 1438604329);
307 },
308 ],
309 ],
310 ],
311 ],
312 ];
313
314 $languageService = $this->prophesize(LanguageService::class);
315 $GLOBALS['LANG'] = $languageService->reveal();
316 /** @var FlashMessage|ObjectProphecy $flashMessage */
317 $flashMessage = $this->prophesize(FlashMessage::class);
318 GeneralUtility::addInstance(FlashMessage::class, $flashMessage->reveal());
319 /** @var FlashMessageService|ObjectProphecy $flashMessageService */
320 $flashMessageService = $this->prophesize(FlashMessageService::class);
321 GeneralUtility::setSingletonInstance(FlashMessageService::class, $flashMessageService->reveal());
322 /** @var FlashMessageQueue|ObjectProphecy $flashMessageQueue */
323 $flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
324 $flashMessageService->getMessageQueueByIdentifier(Argument::cetera())->willReturn($flashMessageQueue->reveal());
325
326 $flashMessageQueue->enqueue($flashMessage)->shouldBeCalled();
327
328 $this->subject->addData($input);
329 }
330
331 /**
332 * @test
333 */
334 public function addDataTranslatesItemLabelsFromPageTsConfig()
335 {
336 $input = [
337 'tableName' => 'aTable',
338 'processedTca' => [
339 'columns' => [
340 'aField' => [
341 'config' => [
342 'type' => 'check',
343 'items' => [
344 0 => [
345 0 => 'aLabel',
346 1 => 'aValue',
347 ],
348 ],
349 ],
350 ],
351 ],
352 ],
353 'pageTsConfig' => [
354 'TCEFORM.' => [
355 'aTable.' => [
356 'aField.' => [
357 'altLabels.' => [
358 0 => 'labelOverride',
359 ],
360 ]
361 ],
362 ],
363 ],
364 ];
365
366 /** @var LanguageService|ObjectProphecy $languageService */
367 $languageService = $this->prophesize(LanguageService::class);
368 $GLOBALS['LANG'] = $languageService->reveal();
369 $languageService->sL('aLabel')->willReturnArgument(0);
370
371 $languageService->sL('labelOverride')->shouldBeCalled()->willReturnArgument(0);
372
373 $expected = $input;
374 $expected['processedTca']['columns']['aField']['config']['items'][0][0] = 'labelOverride';
375
376 $this->assertSame($expected, $this->subject->addData($input));
377 $this->subject->addData($input);
378 }
379 }