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