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