‪TYPO3CMS  11.5
FormFrontendControllerTest.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 Prophecy\Argument;
29 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪FormFrontendControllerTest extends UnitTestCase
36 {
37  use \Prophecy\PhpUnit\ProphecyTrait;
38  public function ‪setUp(): void
39  {
40  parent::setUp();
41  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
42  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
43  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
44  $cacheManagerProphecy->getCache('runtime')->willReturn($cacheFrontendProphecy->reveal());
45  $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
46  $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
47  }
48 
49  public function ‪tearDown(): void
50  {
51  GeneralUtility::purgeInstances();
52  parent::tearDown();
53  }
54 
59  {
60  $mockController = $this->getAccessibleMock(FormFrontendController::class, [
61  'dummy',
62  ], [], '', false);
63 
64  $configurationServiceProphecy = $this->prophesize(ConfigurationService::class);
65  GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationServiceProphecy->reveal());
66 
67  $sheetIdentifier = md5(
68  implode('', [
69  '1:/foo',
70  'standard',
71  'ext-form-identifier',
72  'EmailToReceiver',
73  ])
74  );
75 
76  $flexFormTools = new ‪FlexFormTools();
77  $contentObject = new ContentObjectRenderer();
78  $contentObject->data = [
79  'pi_flexform' => $flexFormTools->flexArray2Xml([
80  'data' => [
81  $sheetIdentifier => [
82  'lDEF' => [
83  'settings.finishers.EmailToReceiver.subject' => [
84  'vDEF' => 'Message Subject overridden',
85  ],
86  'settings.finishers.EmailToReceiver.recipients' => [
87  'el' => [
88  'abc' => [
89  '_arrayContainer' => [
90  'el' => [
91  'email' => [
92  'vDEF' => 'your.company@example.com overridden',
93  ],
94  'name' => [
95  'vDEF' => '',
96  ],
97  ],
98  ],
99  ],
100  ],
101  ],
102  'settings.finishers.EmailToReceiver.format' => [
103  'vDEF' => 'html overridden',
104  ],
105  ],
106  ],
107  ],
108  ]),
109  ];
110 
111  $frontendConfigurationManager = $this->createMock(FrontendConfigurationManager::class);
112  $frontendConfigurationManager
113  ->method('getContentObject')
114  ->willReturn($contentObject);
115 
116  $mockController->_set('configurationManager', $frontendConfigurationManager);
117 
118  $configurationServiceProphecy->getPrototypeConfiguration(Argument::cetera())->willReturn([
119  'finishersDefinition' => [
120  'EmailToReceiver' => [
121  'FormEngine' => [
122  'elements' => [
123  'subject' => [],
124  'recipients' => [],
125  'format' => [],
126  ],
127  ],
128  ],
129  ],
130  ]);
131 
132  $mockController->_set('settings', [
133  'overrideFinishers' => 0,
134  'finishers' => [
135  'EmailToReceiver' => [
136  'subject' => 'Message Subject overridden',
137  'recipients' => [
138  'your.company@example.com overridden' => '',
139  ],
140  'format' => 'html overridden',
141  ],
142  ],
143  ]);
144 
145  $input = [
146  'identifier' => 'ext-form-identifier',
147  'persistenceIdentifier' => '1:/foo',
148  'prototypeName' => 'standard',
149  'finishers' => [
150  0 => [
151  'identifier' => 'EmailToReceiver',
152  'options' => [
153  'subject' => 'Message Subject',
154  'recipients' => [
155  'your.company@example.com' => '',
156  ],
157  'format' => 'html',
158  ],
159  ],
160  ],
161  ];
162 
163  $expected = [
164  'identifier' => 'ext-form-identifier',
165  'persistenceIdentifier' => '1:/foo',
166  'prototypeName' => 'standard',
167  'finishers' => [
168  0 => [
169  'identifier' => 'EmailToReceiver',
170  'options' => [
171  'subject' => 'Message Subject',
172  'recipients' => [
173  'your.company@example.com' => '',
174  ],
175  'format' => 'html',
176  ],
177  ],
178  ],
179  ];
180 
181  self::assertSame($expected, $mockController->_call('overrideByFlexFormSettings', $input));
182  }
183 
188  {
189  $mockController = $this->getAccessibleMock(FormFrontendController::class, [
190  'dummy',
191  ], [], '', false);
192 
193  $configurationServiceProphecy = $this->prophesize(ConfigurationService::class);
194  GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationServiceProphecy->reveal());
195 
196  $sheetIdentifier = md5(
197  implode('', [
198  '1:/foo',
199  'standard',
200  'ext-form-identifier',
201  'EmailToReceiver',
202  ])
203  );
204 
205  $flexFormTools = new FlexFormTools();
206  $contentObject = new ContentObjectRenderer();
207  $contentObject->data = [
208  'pi_flexform' => $flexFormTools->flexArray2Xml([
209  'data' => [
210  $sheetIdentifier => [
211  'lDEF' => [
212  'settings.finishers.EmailToReceiver.subject' => [
213  'vDEF' => 'Message Subject overridden',
214  ],
215  'settings.finishers.EmailToReceiver.recipients' => [
216  'el' => [
217  'abc' => [
218  '_arrayContainer' => [
219  'el' => [
220  'email' => [
221  'vDEF' => 'your.company@example.com overridden',
222  ],
223  'name' => [
224  'vDEF' => '',
225  ],
226  ],
227  ],
228  ],
229  ],
230  ],
231  'settings.finishers.EmailToReceiver.format' => [
232  'vDEF' => 'html overridden',
233  ],
234  ],
235  ],
236  ],
237  ]),
238  ];
239 
240  $frontendConfigurationManager = $this->createMock(FrontendConfigurationManager::class);
241  $frontendConfigurationManager
242  ->method('getContentObject')
243  ->willReturn($contentObject);
244 
245  $mockController->_set('configurationManager', $frontendConfigurationManager);
246 
247  $configurationServiceProphecy->getPrototypeConfiguration(Argument::cetera())->willReturn([
248  'finishersDefinition' => [
249  'EmailToReceiver' => [
250  'FormEngine' => [
251  'elements' => [
252  'subject' => [
253  'config' => [
254  'type' => 'input',
255  ],
256  ],
257  'recipients' => [
258  'type' => 'array',
259  'section' => true,
260  'sectionItemKey' => 'email',
261  'sectionItemValue' => 'name',
262  ],
263  'format' => [
264  'config' => [
265  'type' => 'input',
266  ],
267  ],
268  ],
269  ],
270  ],
271  ],
272  ]);
273 
274  $mockController->_set('settings', [
275  'overrideFinishers' => 1,
276  'finishers' => [
277  'EmailToReceiver' => [
278  'subject' => 'Message Subject overridden',
279  'recipients' => [
280  'abcxyz' => [
281  'email' => 'your.company@example.com overridden',
282  'name' => '',
283  ],
284  ],
285  'format' => 'html overridden',
286  ],
287  ],
288  ]);
289 
290  $input = [
291  'persistenceIdentifier' => '1:/foo',
292  'identifier' => 'ext-form-identifier',
293  'prototypeName' => 'standard',
294  'finishers' => [
295  0 => [
296  'identifier' => 'EmailToReceiver',
297  'options' => [
298  'subject' => 'Message Subject',
299  'recipients' => [
300  'your.company@example.com' => '',
301  ],
302  'format' => 'html',
303  ],
304  ],
305  ],
306  ];
307 
308  $expected = [
309  'persistenceIdentifier' => '1:/foo',
310  'identifier' => 'ext-form-identifier',
311  'prototypeName' => 'standard',
312  'finishers' => [
313  0 => [
314  'identifier' => 'EmailToReceiver',
315  'options' => [
316  'subject' => 'Message Subject overridden',
317  'recipients' => [
318  'your.company@example.com overridden' => '',
319  ],
320  'format' => 'html overridden',
321  ],
322  ],
323  ],
324  ];
325 
326  self::assertSame($expected, $mockController->_call('overrideByFlexFormSettings', $input));
327  }
328 
333  {
334  $mockController = $this->getAccessibleMock(FormFrontendController::class, [
335  'dummy',
336  ], [], '', false);
337 
338  $configurationServiceProphecy = $this->prophesize(ConfigurationService::class);
339  GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationServiceProphecy->reveal());
340 
341  $sheetIdentifier = md5(
342  implode('', [
343  '1:/foo',
344  'standard',
345  'ext-form-identifier',
346  'EmailToReceiver',
347  ])
348  );
349 
350  $flexFormTools = new FlexFormTools();
351  $contentObject = new ContentObjectRenderer();
352  $contentObject->data = [
353  'pi_flexform' => $flexFormTools->flexArray2Xml([
354  'data' => [
355  $sheetIdentifier => [
356  'lDEF' => [
357  'settings.finishers.EmailToReceiver.subject' => [
358  'vDEF' => 'Message Subject overridden',
359  ],
360  'settings.finishers.EmailToReceiver.recipients' => [
361  'el' => [
362  'abc' => [
363  '_arrayContainer' => [
364  'el' => [
365  'email' => [
366  'vDEF' => 'your.company@example.com overridden',
367  ],
368  'name' => [
369  'vDEF' => '',
370  ],
371  ],
372  ],
373  ],
374  ],
375  ],
376  'settings.finishers.EmailToReceiver.format' => [
377  'vDEF' => 'html overridden',
378  ],
379  ],
380  ],
381  ],
382  ]),
383  ];
384 
385  $frontendConfigurationManager = $this->createMock(FrontendConfigurationManager::class);
386  $frontendConfigurationManager
387  ->method('getContentObject')
388  ->willReturn($contentObject);
389 
390  $mockController->_set('configurationManager', $frontendConfigurationManager);
391 
392  $configurationServiceProphecy->getPrototypeConfiguration(Argument::cetera())->willReturn([
393  'finishersDefinition' => [
394  'EmailToReceiver' => [
395  'FormEngine' => [
396  'elements' => [
397  'subject' => [
398  'config' => [
399  'type' => 'input',
400  ],
401  ],
402  'recipients' => [
403  'type' => 'array',
404  'section' => true,
405  'sectionItemKey' => 'email',
406  'sectionItemValue' => 'name',
407  ],
408  ],
409  ],
410  ],
411  ],
412  ]);
413 
414  $mockController->_set('settings', [
415  'overrideFinishers' => 1,
416  'finishers' => [
417  'EmailToReceiver' => [
418  'subject' => 'Message Subject overridden',
419  'recipients' => [
420  'abcxyz' => [
421  'email' => 'your.company@example.com overridden',
422  'name' => '',
423  ],
424  ],
425  'format' => 'html overridden',
426  ],
427  ],
428  ]);
429 
430  $input = [
431  'persistenceIdentifier' => '1:/foo',
432  'identifier' => 'ext-form-identifier',
433  'prototypeName' => 'standard',
434  'finishers' => [
435  0 => [
436  'identifier' => 'EmailToReceiver',
437  'options' => [
438  'subject' => 'Message Subject',
439  'recipients' => [
440  'your.company@example.com' => '',
441  ],
442  'format' => 'html',
443  ],
444  ],
445  ],
446  ];
447 
448  $expected = [
449  'persistenceIdentifier' => '1:/foo',
450  'identifier' => 'ext-form-identifier',
451  'prototypeName' => 'standard',
452  'finishers' => [
453  0 => [
454  'identifier' => 'EmailToReceiver',
455  'options' => [
456  'subject' => 'Message Subject overridden',
457  'recipients' => [
458  'your.company@example.com overridden' => '',
459  ],
460  'format' => 'html',
461  ],
462  ],
463  ],
464  ];
465 
466  self::assertSame($expected, $mockController->_call('overrideByFlexFormSettings', $input));
467  }
468 
473  {
474  $mockController = $this->getAccessibleMock(FormFrontendController::class, ['dummy'], [], '', false);
475  $mockController->_set('settings', [
476  'formDefinitionOverrides' => [
477  ],
478  ]);
479 
480  $input = [
481  'identifier' => 'ext-form-identifier',
482  'prototypeName' => 'standard',
483  'label' => 'Label',
484  'renderables' => [
485  0 => [
486  'identifier' => 'page-1',
487  'type' => 'Page',
488  'label' => 'Label',
489  'renderables' => [
490  0 => [
491  'identifier' => 'text-1',
492  'type' => 'Text',
493  'label' => 'Label',
494  ],
495  ],
496  ],
497  ],
498  ];
499 
500  $expected = [
501  'identifier' => 'ext-form-identifier',
502  'prototypeName' => 'standard',
503  'label' => 'Label',
504  'renderables' => [
505  0 => [
506  'identifier' => 'page-1',
507  'type' => 'Page',
508  'label' => 'Label',
509  'renderables' => [
510  0 => [
511  'identifier' => 'text-1',
512  'type' => 'Text',
513  'label' => 'Label',
514  ],
515  ],
516  ],
517  ],
518  ];
519 
520  self::assertSame($expected, $mockController->_call('overrideByTypoScriptSettings', $input));
521  }
522 
527  {
528  $mockController = $this->getAccessibleMock(FormFrontendController::class, [
529  'dummy',
530  ], [], '', false);
531 
532  $typoScriptServiceProphecy = $this->prophesize(TypoScriptService::class);
533  GeneralUtility::addInstance(TypoScriptService::class, $typoScriptServiceProphecy->reveal());
534  $typoScriptServiceProphecy
535  ->resolvePossibleTypoScriptConfiguration(Argument::cetera())
536  ->willReturnArgument(0);
537 
538  $mockController->_set('settings', [
539  'formDefinitionOverrides' => [
540  'ext-form-identifier' => [
541  'label' => 'Label override',
542  'renderables' => [
543  0 => [
544  'renderables' => [
545  0 => [
546  'label' => 'Label override',
547  ],
548  ],
549  ],
550  ],
551  ],
552  ],
553  ]);
554 
555  $input = [
556  'identifier' => 'ext-form-identifier',
557  'prototypeName' => 'standard',
558  'label' => 'Label',
559  'renderables' => [
560  0 => [
561  'identifier' => 'page-1',
562  'type' => 'Page',
563  'label' => 'Label',
564  'renderables' => [
565  0 => [
566  'identifier' => 'text-1',
567  'type' => 'Text',
568  'label' => 'Label',
569  ],
570  ],
571  ],
572  ],
573  ];
574 
575  $expected = [
576  'identifier' => 'ext-form-identifier',
577  'prototypeName' => 'standard',
578  'label' => 'Label override',
579  'renderables' => [
580  0 => [
581  'identifier' => 'page-1',
582  'type' => 'Page',
583  'label' => 'Label',
584  'renderables' => [
585  0 => [
586  'identifier' => 'text-1',
587  'type' => 'Text',
588  'label' => 'Label override',
589  ],
590  ],
591  ],
592  ],
593  ];
594 
595  self::assertSame($expected, $mockController->_call('overrideByTypoScriptSettings', $input));
596  }
597 
602  {
603  $formDefinitionYaml = [
604  'identifier' => 'ext-form-identifier',
605  'prototypeName' => 'standard',
606  'label' => [
607  'value' => 'Label override',
608  '_typoScriptNodeValue' => 'TEXT',
609  ],
610  'renderables' => [
611  0 => [
612  'identifier' => 'page-1',
613  'type' => 'Page',
614  'label' => 'Label',
615  'renderables' => [
616  0 => [
617  'identifier' => 'text-1',
618  'type' => 'Text',
619  'label' => 'Label',
620  ],
621  ],
622  ],
623  ],
624  ];
625 
626  $formTypoScript = [
627  'formDefinitionOverrides' => [
628  'ext-form-identifier' => [
629  'renderables' => [
630  0 => [
631  'renderables' => [
632  0 => [
633  'label' => [
634  'value' => 'Label override',
635  '_typoScriptNodeValue' => 'TEXT',
636  ],
637  ],
638  ],
639  ],
640  ],
641  ],
642  ],
643  ];
644 
645  $evaluatedFormTypoScript = [
646  'renderables' => [
647  0 => [
648  'renderables' => [
649  0 => [
650  'label' => 'Label override',
651  ],
652  ],
653  ],
654  ],
655  ];
656 
657  $expected = [
658  'identifier' => 'ext-form-identifier',
659  'prototypeName' => 'standard',
660  'label' => [
661  'value' => 'Label override',
662  '_typoScriptNodeValue' => 'TEXT',
663  ],
664  'renderables' => [
665  0 => [
666  'identifier' => 'page-1',
667  'type' => 'Page',
668  'label' => 'Label',
669  'renderables' => [
670  0 => [
671  'identifier' => 'text-1',
672  'type' => 'Text',
673  'label' => 'Label override',
674  ],
675  ],
676  ],
677  ],
678  ];
679 
680  $controllerMock = $this->getAccessibleMock(FormFrontendController::class, [
681  'dummy',
682  ], [], '', false);
683 
684  $typoScriptServiceProphecy = $this->prophesize(TypoScriptService::class);
685  $typoScriptServiceProphecy->resolvePossibleTypoScriptConfiguration($formTypoScript['formDefinitionOverrides']['ext-form-identifier'])->willReturn($evaluatedFormTypoScript);
686  GeneralUtility::addInstance(TypoScriptService::class, $typoScriptServiceProphecy->reveal());
687 
688  $controllerMock->_set('settings', $formTypoScript);
689 
690  self::assertSame($expected, $controllerMock->_call('overrideByTypoScriptSettings', $formDefinitionYaml));
691  }
692 }
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\setUp
‪setUp()
Definition: FormFrontendControllerTest.php:37
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByFlexFormSettingsReturnsNotOverriddenConfigurationKeyIfFlexformOverridesAreNotRepresentedInFormEngineConfiguration
‪overrideByFlexFormSettingsReturnsNotOverriddenConfigurationKeyIfFlexformOverridesAreNotRepresentedInFormEngineConfiguration()
Definition: FormFrontendControllerTest.php:331
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByTypoScriptSettingsReturnsOverriddenConfigurationIfTypoScriptOverridesExists
‪overrideByTypoScriptSettingsReturnsOverriddenConfigurationIfTypoScriptOverridesExists()
Definition: FormFrontendControllerTest.php:525
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByTypoScriptSettingsDoesNotEvaluateTypoScriptLookalikeInstructionsFromYamlSettings
‪overrideByTypoScriptSettingsDoesNotEvaluateTypoScriptLookalikeInstructionsFromYamlSettings()
Definition: FormFrontendControllerTest.php:600
‪TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager
Definition: FrontendConfigurationManager.php:33
‪TYPO3\CMS\Form\Controller\FormFrontendController
Definition: FormFrontendController.php:42
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByTypoScriptSettingsReturnsNotOverriddenConfigurationIfNoTypoScriptOverridesExists
‪overrideByTypoScriptSettingsReturnsNotOverriddenConfigurationIfNoTypoScriptOverridesExists()
Definition: FormFrontendControllerTest.php:471
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\tearDown
‪tearDown()
Definition: FormFrontendControllerTest.php:48
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByFlexFormSettingsReturnsOverriddenConfigurationIfFlexformOverridesEnabled
‪overrideByFlexFormSettingsReturnsOverriddenConfigurationIfFlexformOverridesEnabled()
Definition: FormFrontendControllerTest.php:186
‪TYPO3\CMS\Form\Tests\Unit\Controller
Definition: FormEditorControllerTest.php:18
‪TYPO3\CMS\Form\Mvc\Configuration\TypoScriptService
Definition: TypoScriptService.php:29
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:50
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest\overrideByFlexFormSettingsReturnsNoOverriddenConfigurationIfFlexformOverridesDisabled
‪overrideByFlexFormSettingsReturnsNoOverriddenConfigurationIfFlexformOverridesDisabled()
Definition: FormFrontendControllerTest.php:57
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormFrontendControllerTest
Definition: FormFrontendControllerTest.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50