‪TYPO3CMS  ‪main
FormEditorControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪FormEditorControllerTest extends UnitTestCase
28 {
29  protected bool ‪$resetSingletonInstances = true;
30 
31  public function ‪setUp(): void
32  {
33  parent::setUp();
34  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
35  }
36 
37  #[Test]
39  {
40  $mockTranslationService = $this->getAccessibleMock(TranslationService::class, ['translate'], [], '', false);
41  GeneralUtility::setSingletonInstance(TranslationService::class, $mockTranslationService);
42  $mockTranslationService
43  ->method('translate')
44  ->willReturnArgument(4);
45 
46  $subject = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
47  $subject->_set('prototypeConfiguration', [
48  'formEditor' => [
49  'formElementGroups' => [
50  'input' => [
51  'label' => 'Basic elements',
52  ],
53  'select' => [
54  'label' => 'Select elements',
55  ],
56  ],
57  ],
58  ]);
59 
60  $input = [
61  'Password' => [
62  'group' => 'input',
63  'groupSorting' => 110,
64  'iconIdentifier' => 'form-password',
65  'label' => 'Password label',
66  ],
67  'Text' => [
68  'group' => 'input',
69  'groupSorting' => 100,
70  'iconIdentifier' => 'form-text',
71  'label' => 'Text label',
72  ],
73  'SingleSelect' => [
74  'group' => 'select',
75  'groupSorting' => 100,
76  'iconIdentifier' => 'form-single-select',
77  'label' => 'Single select label',
78  ],
79  ];
80 
81  $expected = [
82  0 => [
83  'key' => 'input',
84  'elements' => [
85  0 => [
86  'key' => 'Text',
87  'cssKey' => 'text',
88  'label' => 'Text label',
89  'sorting' => 100,
90  'iconIdentifier' => 'form-text',
91  ],
92  1 => [
93  'key' => 'Password',
94  'cssKey' => 'password',
95  'label' => 'Password label',
96  'sorting' => 110,
97  'iconIdentifier' => 'form-password',
98  ],
99  ],
100  'label' => 'Basic elements',
101  ],
102  1 => [
103  'key' => 'select',
104  'elements' => [
105  0 => [
106  'key' => 'SingleSelect',
107  'cssKey' => 'singleselect',
108  'label' => 'Single select label',
109  'sorting' => 100,
110  'iconIdentifier' => 'form-single-select',
111  ],
112  ],
113  'label' => 'Select elements',
114  ],
115  ];
116 
117  self::assertSame($expected, $subject->_call('getInsertRenderablesPanelConfiguration', $input));
118  }
119 
120  #[Test]
122  {
123  $mockTranslationService = $this->getAccessibleMock(TranslationService::class, ['translateValuesRecursive'], [], '', false);
124  GeneralUtility::setSingletonInstance(TranslationService::class, $mockTranslationService);
125  $mockTranslationService
126  ->method('translateValuesRecursive')
127  ->willReturnArgument(0);
128 
129  $subject = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
130  $subject->_set('prototypeConfiguration', [
131  'formEditor' => [
132  'someOtherValues' => [
133  'horst' => [
134  'key' => 'value',
135  ],
136  'gertrud' => [
137  'key' => 'value',
138  ],
139  ],
140  'formElementPropertyValidatorsDefinition' => [
141  'NotEmpty' => [
142  'key' => 'value',
143  ],
144  ],
145  ],
146  'formElementsDefinition' => [
147  'Form' => [
148  'formEditor' => [
149  'key' => 'value',
150  ],
151  'someOtherValues' => [
152  'horst' => [
153  'key' => 'value',
154  ],
155  'gertrud' => [
156  'key' => 'value',
157  ],
158  ],
159  ],
160  'Text' => [
161  'formEditor' => [
162  'key' => 'value',
163  ],
164  'someOtherValues' => [
165  'horst' => [
166  'key' => 'value',
167  ],
168  'gertrud' => [
169  'key' => 'value',
170  ],
171  ],
172  ],
173  ],
174  'finishersDefinition' => [
175  'Confirmation' => [
176  'formEditor' => [
177  'key' => 'value',
178  ],
179  'someOtherValues' => [
180  'horst' => [
181  'key' => 'value',
182  ],
183  'gertrud' => [
184  'key' => 'value',
185  ],
186  ],
187  ],
188  'EmailToSender' => [
189  'formEditor' => [
190  'key' => 'value',
191  ],
192  'someOtherValues' => [
193  'horst' => [
194  'key' => 'value',
195  ],
196  'gertrud' => [
197  'key' => 'value',
198  ],
199  ],
200  ],
201  ],
202  'someOtherValues' => [
203  'horst' => [
204  'key' => 'value',
205  ],
206  'gertrud' => [
207  'key' => 'value',
208  ],
209  ],
210  ]);
211 
212  $expected = [
213  'formElements' => [
214  'Form' => [
215  'key' => 'value',
216  ],
217  'Text' => [
218  'key' => 'value',
219  ],
220  ],
221  'finishers' => [
222  'Confirmation' => [
223  'key' => 'value',
224  ],
225  'EmailToSender' => [
226  'key' => 'value',
227  ],
228  ],
229  'formElementPropertyValidators' => [
230  'NotEmpty' => [
231  'key' => 'value',
232  ],
233  ],
234  ];
235 
236  self::assertSame($expected, $subject->_call('getFormEditorDefinitions'));
237  }
238 
239  #[Test]
241  {
242  $this->expectException(RenderingException::class);
243  $this->expectExceptionCode(1480294721);
244 
245  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
246 
247  $mockController->_set('prototypeConfiguration', [
248  'formEditor' => [
249  'formEditorFluidConfiguration' => [
250  'templatePathAndFilename' => '',
251  ],
252  ],
253  ]);
254 
255  $mockController->_call('renderFormEditorTemplates', []);
256  }
257 
258  #[Test]
260  {
261  $this->expectException(RenderingException::class);
262  $this->expectExceptionCode(1480294721);
263 
264  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
265 
266  $mockController->_set('prototypeConfiguration', [
267  'formEditor' => [
268  'formEditorFluidConfiguration' => [
269  'templatePathAndFilename' => '',
270  'layoutRootPaths' => '',
271  ],
272  ],
273  ]);
274 
275  $mockController->_call('renderFormEditorTemplates', []);
276  }
277 
278  #[Test]
280  {
281  $this->expectException(RenderingException::class);
282  $this->expectExceptionCode(1480294722);
283 
284  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
285 
286  $mockController->_set('prototypeConfiguration', [
287  'formEditor' => [
288  'formEditorFluidConfiguration' => [
289  'templatePathAndFilename' => '',
290  'layoutRootPaths' => [],
291  ],
292  ],
293  ]);
294 
295  $mockController->_call('renderFormEditorTemplates', []);
296  }
297 
298  #[Test]
300  {
301  $this->expectException(RenderingException::class);
302  $this->expectExceptionCode(1480294722);
303 
304  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
305 
306  $mockController->_set('prototypeConfiguration', [
307  'formEditor' => [
308  'formEditorFluidConfiguration' => [
309  'templatePathAndFilename' => '',
310  'layoutRootPaths' => [],
311  ],
312  ],
313  ]);
314 
315  $mockController->_call('renderFormEditorTemplates', []);
316  }
317 
318  #[Test]
320  {
321  $this->expectException(RenderingException::class);
322  $this->expectExceptionCode(1485636499);
323 
324  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
325 
326  $mockController->_set('prototypeConfiguration', [
327  'formEditor' => [
328  'formEditorFluidConfiguration' => [],
329  ],
330  ]);
331 
332  $mockController->_call('renderFormEditorTemplates', []);
333  }
334 
335  #[Test]
337  {
338  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
339 
340  $input = [
341  0 => [
342  'bar' => 'baz',
343  ],
344  1 => [
345  'type' => 'SOMEELEMENT',
346  'properties' => [
347  'options' => [
348  5 => '5',
349  4 => '4',
350  3 => '3',
351  2 => '2',
352  1 => '1',
353  ],
354  ],
355  ],
356  2 => [
357  0 => [
358  'type' => 'TEST',
359  'properties' => [
360  'options' => [
361  5 => '5',
362  4 => '4',
363  3 => '3',
364  2 => '2',
365  1 => '1',
366  ],
367  ],
368  ],
369  ],
370  ];
371 
372  $multiValueProperties = [
373  'TEST' => [
374  0 => 'properties.options',
375  ],
376  ];
377 
378  $expected = [
379  0 => [
380  'bar' => 'baz',
381  ],
382  1 => [
383  'type' => 'SOMEELEMENT',
384  'properties' => [
385  'options' => [
386  5 => '5',
387  4 => '4',
388  3 => '3',
389  2 => '2',
390  1 => '1',
391  ],
392  ],
393  ],
394  2 => [
395  0 => [
396  'type' => 'TEST',
397  'properties' => [
398  'options' => [
399  ['_label' => '5', '_value' => 5],
400  ['_label' => '4', '_value' => 4],
401  ['_label' => '3', '_value' => 3],
402  ['_label' => '2', '_value' => 2],
403  ['_label' => '1', '_value' => 1],
404  ],
405  ],
406  ],
407  ],
408  ];
409 
410  self::assertSame($expected, $mockController->_call('transformMultiValuePropertiesForFormEditor', $input, 'type', $multiValueProperties));
411  }
412 
413  #[Test]
415  {
416  $mockController = $this->getAccessibleMock(FormEditorController::class, null, [], '', false);
417 
418  $input = [
419  'heinz' => 1,
420  'klaus' => [],
421  'sabine' => [
422  'heinz' => '2',
423  'klaus' => [],
424  'horst' => [
425  'heinz' => '',
426  'paul' => [[]],
427  ],
428  ],
429  ];
430 
431  $expected = [
432  'heinz' => 1,
433  'sabine' => [
434  'heinz' => '2',
435  'horst' => [
436  'heinz' => '',
437  ],
438  ],
439  ];
440 
441  self::assertSame($expected, $mockController->_call('filterEmptyArrays', $input));
442  }
443 }
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest
Definition: FormEditorControllerTest.php:28
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\transformMultiValuePropertiesForFormEditorConvertMultiValueDataIntoMetaData
‪transformMultiValuePropertiesForFormEditorConvertMultiValueDataIntoMetaData()
Definition: FormEditorControllerTest.php:336
‪TYPO3\CMS\Form\Controller\FormEditorController
Definition: FormEditorController.php:55
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:45
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\renderFormEditorTemplatesThrowsExceptionIfTemplatePathAndFilenameNotSet
‪renderFormEditorTemplatesThrowsExceptionIfTemplatePathAndFilenameNotSet()
Definition: FormEditorControllerTest.php:319
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\getFormEditorDefinitionsReturnReducedConfiguration
‪getFormEditorDefinitionsReturnReducedConfiguration()
Definition: FormEditorControllerTest.php:121
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\getInsertRenderablesPanelConfigurationReturnsGroupedAndSortedConfiguration
‪getInsertRenderablesPanelConfigurationReturnsGroupedAndSortedConfiguration()
Definition: FormEditorControllerTest.php:38
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FormEditorControllerTest.php:29
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\filterEmptyArraysRemovesEmptyArrayKeys
‪filterEmptyArraysRemovesEmptyArrayKeys()
Definition: FormEditorControllerTest.php:414
‪TYPO3\CMS\Form\Tests\Unit\Controller
Definition: FormEditorControllerTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotSet
‪renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotSet()
Definition: FormEditorControllerTest.php:279
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Form\Domain\Exception\RenderingException
Definition: RenderingException.php:29
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotArray
‪renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotArray()
Definition: FormEditorControllerTest.php:259
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotArray
‪renderFormEditorTemplatesThrowsExceptionIfPartialRootPathsNotArray()
Definition: FormEditorControllerTest.php:299
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotSet
‪renderFormEditorTemplatesThrowsExceptionIfLayoutRootPathsNotSet()
Definition: FormEditorControllerTest.php:240
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormEditorControllerTest\setUp
‪setUp()
Definition: FormEditorControllerTest.php:31