[BUGFIX] EXT:form - fluid 2.2.0 compatibility
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Controller / FormEditorControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Controller;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Object\ObjectManager;
19 use TYPO3\CMS\Form\Controller\FormEditorController;
20 use TYPO3\CMS\Form\Domain\Exception\RenderingException;
21 use TYPO3\CMS\Form\Service\TranslationService;
22
23 /**
24 * Test case
25 */
26 class FormEditorControllerTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
27 {
28
29 /**
30 * @var array A backup of registered singleton instances
31 */
32 protected $singletonInstances = [];
33
34 /**
35 * Set up
36 */
37 public function setUp()
38 {
39 $this->singletonInstances = GeneralUtility::getSingletonInstances();
40 }
41
42 /**
43 * Tear down
44 */
45 public function tearDown()
46 {
47 GeneralUtility::resetSingletonInstances($this->singletonInstances);
48 parent::tearDown();
49 }
50
51 /**
52 * @test
53 */
54 public function getInsertRenderablesPanelConfigurationReturnsGroupedAndSortedConfiguration()
55 {
56 $mockController = $this->getAccessibleMock(FormEditorController::class, [
57 'dummy'
58 ], [], '', false);
59
60 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
61 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
62
63 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
64 'translate'
65 ], [], '', false);
66
67 $mockTranslationService
68 ->expects($this->any())
69 ->method('translate')
70 ->willReturnArgument(4);
71
72 $objectMangerProphecy
73 ->get(TranslationService::class)
74 ->willReturn($mockTranslationService);
75
76 $mockController->_set('prototypeConfiguration', [
77 'formEditor' => [
78 'formElementGroups' => [
79 'input' => [
80 'label' => 'Basic elements',
81 ],
82 'select' => [
83 'label' => 'Select elements',
84 ],
85 ],
86 ],
87 ]);
88
89 $input = [
90 'Password' => [
91 'group' => 'input',
92 'groupSorting' => 110,
93 'iconIdentifier' => 't3-form-icon-password',
94 'label' => 'Password label',
95 ],
96 'Text' => [
97 'group' => 'input',
98 'groupSorting' => 100,
99 'iconIdentifier' => 't3-form-icon-text',
100 'label' => 'Text label',
101 ],
102 'SingleSelect' => [
103 'group' => 'select',
104 'groupSorting' => 100,
105 'iconIdentifier' => 't3-form-icon-single-select',
106 'label' => 'Single select label',
107 ],
108 ];
109
110 $expected = [
111 0 => [
112 'key' => 'input',
113 'elements' => [
114 0 => [
115 'key' => 'Text',
116 'cssKey' => 'text',
117 'label' => 'Text label',
118 'sorting' => 100,
119 'iconIdentifier' => 't3-form-icon-text',
120 ],
121 1 => [
122 'key' => 'Password',
123 'cssKey' => 'password',
124 'label' => 'Password label',
125 'sorting' => 110,
126 'iconIdentifier' => 't3-form-icon-password',
127 ],
128 ],
129 'label' => 'Basic elements',
130 ],
131 1 => [
132 'key' => 'select',
133 'elements' => [
134 0 => [
135 'key' => 'SingleSelect',
136 'cssKey' => 'singleselect',
137 'label' => 'Single select label',
138 'sorting' => 100,
139 'iconIdentifier' => 't3-form-icon-single-select',
140 ],
141 ],
142 'label' => 'Select elements',
143 ],
144 ];
145
146 $this->assertSame($expected, $mockController->_call('getInsertRenderablesPanelConfiguration', $input));
147 }
148
149 /**
150 * @test
151 */
152 public function getFormEditorDefinitionsReturnReducedConfiguration()
153 {
154 $mockController = $this->getAccessibleMock(FormEditorController::class, [
155 'dummy'
156 ], [], '', false);
157
158 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
159 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
160
161 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
162 'translateValuesRecursive'
163 ], [], '', false);
164
165 $mockTranslationService
166 ->expects($this->any())
167 ->method('translateValuesRecursive')
168 ->willReturnArgument(0);
169
170 $objectMangerProphecy
171 ->get(TranslationService::class)
172 ->willReturn($mockTranslationService);
173
174 $mockController->_set('prototypeConfiguration', [
175 'formEditor' => [
176 'someOtherValues' => [
177 'horst' => [
178 'key' => 'value',
179 ],
180 'gertrud' => [
181 'key' => 'value',
182 ],
183 ],
184 'formElementPropertyValidatorsDefinition' => [
185 'NotEmpty' => [
186 'key' => 'value',
187 ],
188 ],
189 ],
190 'formElementsDefinition' => [
191 'Form' => [
192 'formEditor' => [
193 'key' => 'value',
194 ],
195 'someOtherValues' => [
196 'horst' => [
197 'key' => 'value',
198 ],
199 'gertrud' => [
200 'key' => 'value',
201 ],
202 ],
203 ],
204 'Text' => [
205 'formEditor' => [
206 'key' => 'value',
207 ],
208 'someOtherValues' => [
209 'horst' => [
210 'key' => 'value',
211 ],
212 'gertrud' => [
213 'key' => 'value',
214 ],
215 ],
216 ],
217 ],
218 'finishersDefinition' => [
219 'Confirmation' => [
220 'formEditor' => [
221 'key' => 'value',
222 ],
223 'someOtherValues' => [
224 'horst' => [
225 'key' => 'value',
226 ],
227 'gertrud' => [
228 'key' => 'value',
229 ],
230 ],
231 ],
232 'EmailToSender' => [
233 'formEditor' => [
234 'key' => 'value',
235 ],
236 'someOtherValues' => [
237 'horst' => [
238 'key' => 'value',
239 ],
240 'gertrud' => [
241 'key' => 'value',
242 ],
243 ],
244 ],
245 ],
246 'someOtherValues' => [
247 'horst' => [
248 'key' => 'value',
249 ],
250 'gertrud' => [
251 'key' => 'value',
252 ],
253 ],
254 ]);
255
256 $expected = [
257 'formElements' => [
258 'Form' => [
259 'key' => 'value',
260 ],
261 'Text' => [
262 'key' => 'value',
263 ],
264 ],
265 'finishers' => [
266 'Confirmation' => [
267 'key' => 'value',
268 ],
269 'EmailToSender' => [
270 'key' => 'value',
271 ],
272 ],
273 'formElementPropertyValidators' => [
274 'NotEmpty' => [
275 'key' => 'value',
276 ],
277 ],
278 ];
279
280 $this->assertSame($expected, $mockController->_call('getFormEditorDefinitions'));
281 }
282
283 /**
284 * @test
285 */
286 public function convertJsonArrayToAssociativeArrayReturnTransformedArray()
287 {
288 $mockController = $this->getAccessibleMock(FormEditorController::class, [
289 'dummy'
290 ], [], '', false);
291
292 $input = [
293 'francine' => 'stan',
294 'properties' => [
295 'options' => [
296 0 => [
297 '_label' => 'label',
298 '_value' => 'value',
299 ],
300 ],
301 ],
302 ];
303
304 $expected = [
305 'francine' => 'stan',
306 'properties' => [
307 'options' => [
308 'value' => 'label',
309 ],
310 ],
311 ];
312
313 $this->assertSame($expected, $mockController->_call('convertJsonArrayToAssociativeArray', $input));
314 }
315
316 /**
317 * @test
318 */
319 public function renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotSet()
320 {
321 $this->expectException(RenderingException::class);
322 $this->expectExceptionCode(1480294721);
323
324 $mockController = $this->getAccessibleMock(FormEditorController::class, [
325 'dummy'
326 ], [], '', false);
327
328 $input = [
329 'templateRootPaths' => [],
330 ];
331 $mockController->_call('renderFormEditorTemplates', $input, []);
332 }
333
334 /**
335 * @test
336 */
337 public function renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotArray()
338 {
339 $this->expectException(RenderingException::class);
340 $this->expectExceptionCode(1480294721);
341
342 $mockController = $this->getAccessibleMock(FormEditorController::class, [
343 'dummy'
344 ], [], '', false);
345
346 $input = [
347 'templateRootPaths' => [],
348 'layoutRootPaths' => '',
349 ];
350 $mockController->_call('renderFormEditorTemplates', $input, []);
351 }
352
353 /**
354 * @test
355 */
356 public function renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotSet()
357 {
358 $this->expectException(RenderingException::class);
359 $this->expectExceptionCode(1480294722);
360
361 $mockController = $this->getAccessibleMock(FormEditorController::class, [
362 'dummy'
363 ], [], '', false);
364
365 $input = [
366 'templateRootPaths' => [],
367 'layoutRootPaths' => [],
368 ];
369 $mockController->_call('renderFormEditorTemplates', $input, []);
370 }
371
372 /**
373 * @test
374 */
375 public function renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotArray()
376 {
377 $this->expectException(RenderingException::class);
378 $this->expectExceptionCode(1480294722);
379
380 $mockController = $this->getAccessibleMock(FormEditorController::class, [
381 'dummy'
382 ], [], '', false);
383
384 $input = [
385 'templateRootPaths' => [],
386 'layoutRootPaths' => [],
387 ];
388 $mockController->_call('renderFormEditorTemplates', $input, []);
389 }
390 }