‪TYPO3CMS  10.4
FormViewHelperTest.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 
24 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
26 
30 class ‪FormViewHelperTest extends ViewHelperBaseTestcase
31 {
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $uriBuilderMock = $this->createMock(UriBuilder::class);
36  $uriBuilderMock->expects(self::any())->method('reset')->willReturn($uriBuilderMock);
37  $uriBuilderMock->expects(self::any())->method('setArguments')->willReturn($uriBuilderMock);
38  $uriBuilderMock->expects(self::any())->method('setSection')->willReturn($uriBuilderMock);
39  $uriBuilderMock->expects(self::any())->method('setFormat')->willReturn($uriBuilderMock);
40  $uriBuilderMock->expects(self::any())->method('setCreateAbsoluteUri')->willReturn($uriBuilderMock);
41  $uriBuilderMock->expects(self::any())->method('setAddQueryString')->willReturn($uriBuilderMock);
42  $uriBuilderMock->expects(self::any())->method('setArgumentsToBeExcludedFromQueryString')->willReturn($uriBuilderMock);
43  $uriBuilderMock->expects(self::any())->method('setLinkAccessRestrictedPages')->willReturn($uriBuilderMock);
44  $uriBuilderMock->expects(self::any())->method('setTargetPageUid')->willReturn($uriBuilderMock);
45  $uriBuilderMock->expects(self::any())->method('setTargetPageType')->willReturn($uriBuilderMock);
46  $uriBuilderMock->expects(self::any())->method('setNoCache')->willReturn($uriBuilderMock);
47  $uriBuilderMock->expects(self::any())->method('setAddQueryStringMethod')->willReturn($uriBuilderMock);
48  $this->controllerContext->expects(self::any())->method('getUriBuilder')->willReturn($uriBuilderMock);
49  $this->renderingContext->setControllerContext($this->controllerContext);
50 
51  $this->tagBuilder = $this->createMock(TagBuilder::class);
52  }
53 
58  {
59  $viewHelper = $this->getMockBuilder(FormViewHelper::class)
60  ->setMethods(['registerTagAttribute', 'registerUniversalTagAttributes'])
61  ->getMock();
62  $viewHelper->expects(self::exactly(7))
63  ->method('registerTagAttribute')
64  ->withConsecutive(
65  ['enctype', 'string', self::anything()],
66  ['method', 'string', self::anything()],
67  ['name', 'string', self::anything()],
68  ['onreset', 'string', self::anything()],
69  ['onsubmit', 'string', self::anything()],
70  ['target', 'string', self::anything()],
71  ['novalidate', 'bool', self::anything()]
72  );
73  $viewHelper->expects(self::once())->method('registerUniversalTagAttributes');
74  $viewHelper->initializeArguments();
75  }
76 
81  {
82  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['hasArgument']);
83  $viewHelper->expects(self::once())->method('hasArgument')->with('actionUri')->willReturn(true);
84  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
85  ->setMethods(['addAttribute'])
86  ->getMock();
87  $tagBuilder->expects(self::once())->method('addAttribute')->with('action', 'foobar');
88  $viewHelper->setTagBuilder($tagBuilder);
89  $viewHelper->setArguments(['actionUri' => 'foobar']);
90  $viewHelper->_call('setFormActionUri');
91  }
92 
97  {
98  $formObject = new \stdClass();
99  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
100  $this->injectDependenciesIntoViewHelper($viewHelper);
101  $viewHelper->setArguments(['object' => $formObject]);
102  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObject', $formObject);
103  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'additionalIdentityProperties', []);
104  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObject');
105  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'additionalIdentityProperties');
106  $viewHelper->setTagBuilder($this->tagBuilder);
107  $viewHelper->render();
108  }
109 
114  {
115  $objectName = 'someObjectName';
116  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
117  $this->injectDependenciesIntoViewHelper($viewHelper);
118  $viewHelper->setArguments(['name' => $objectName]);
119  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObjectName', $objectName);
120  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObjectName');
121  $viewHelper->setTagBuilder($this->tagBuilder);
122  $viewHelper->render();
123  }
124 
129  {
130  $objectName = 'someObjectName';
131  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
132  $this->injectDependenciesIntoViewHelper($viewHelper);
133  $viewHelper->setArguments(['name' => 'formName', 'objectName' => $objectName]);
134  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObjectName', $objectName);
135  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObjectName');
136  $viewHelper->setTagBuilder($this->tagBuilder);
137  $viewHelper->render();
138  }
139 
144  {
145  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'], [], '', false);
146  $viewHelper->expects(self::once())->method('renderHiddenReferrerFields');
147  $this->injectDependenciesIntoViewHelper($viewHelper);
148  $viewHelper->setTagBuilder($this->tagBuilder);
149  $viewHelper->render();
150  }
151 
156  {
157  $object = new \stdClass();
158  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenReferrerFields', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'], [], '', false);
159  $this->injectDependenciesIntoViewHelper($viewHelper);
160  $viewHelper->setArguments(['object' => $object]);
161  $viewHelper->expects(self::atLeastOnce())->method('getFormObjectName')->willReturn('MyName');
162  $viewHelper->expects(self::once())->method('renderHiddenIdentityField')->with($object, 'MyName');
163  $viewHelper->setTagBuilder($this->tagBuilder);
164  $viewHelper->render();
165  }
166 
171  {
172  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenReferrerFields', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'], [], '', false);
173  $viewHelper->expects(self::once())->method('renderAdditionalIdentityFields');
174  $this->injectDependenciesIntoViewHelper($viewHelper);
175  $viewHelper->setTagBuilder($this->tagBuilder);
176  $viewHelper->render();
177  }
178 
183  {
184  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'], [], '', false);
185  parent::injectDependenciesIntoViewHelper($viewHelper);
186  $viewHelper->expects(self::once())->method('renderHiddenIdentityField')->willReturn('hiddenIdentityField');
187  $viewHelper->expects(self::once())->method('renderAdditionalIdentityFields')->willReturn('additionalIdentityFields');
188  $viewHelper->expects(self::once())->method('renderHiddenReferrerFields')->willReturn('hiddenReferrerFields');
189  $viewHelper->expects(self::once())->method('renderChildren')->willReturn('formContent');
190  $expectedResult = \chr(10) . '<div>hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . \chr(10) . '</div>' . \chr(10) . 'formContent';
191  $this->tagBuilder->expects(self::once())->method('setContent')->with($expectedResult);
192  $viewHelper->setTagBuilder($this->tagBuilder);
193  $viewHelper->render();
194  }
195 
200  {
201  $viewHelper = $this->getAccessibleMock(
202  FormViewHelper::class,
203  [
204  'renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'
205  ],
206  [],
207  '',
208  false
209  );
210  parent::injectDependenciesIntoViewHelper($viewHelper);
211  $viewHelper->expects(self::once())->method('renderHiddenIdentityField')->willReturn('hiddenIdentityField');
212  $viewHelper->expects(self::once())->method('renderAdditionalIdentityFields')->willReturn('additionalIdentityFields');
213  $viewHelper->expects(self::once())->method('renderHiddenReferrerFields')->willReturn('hiddenReferrerFields');
214  $viewHelper->expects(self::once())->method('renderChildren')->willReturn('formContent');
215  $expectedResult = \chr(10) . '<div class="hidden">hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . \chr(10) . '</div>' . \chr(10) . 'formContent';
216  $this->tagBuilder->expects(self::once())->method('setContent')->with($expectedResult);
217  $viewHelper->setArguments(['hiddenFieldClassName' => 'hidden']);
218  $viewHelper->setTagBuilder($this->tagBuilder);
219  $viewHelper->render();
220  }
221 
226  {
227  $identityProperties = [
228  'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
229  'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
230  ];
231  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'additionalIdentityProperties')->willReturn(true);
232  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'additionalIdentityProperties')->willReturn($identityProperties);
233  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren'], [], '', false);
234  $this->injectDependenciesIntoViewHelper($viewHelper);
235  $expected = \chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . \chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
236  $actual = $viewHelper->_call('renderAdditionalIdentityFields');
237  $viewHelper->setTagBuilder($this->tagBuilder);
238  self::assertEquals($expected, $actual);
239  }
240 
245  {
246  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['dummy'], [], '', false);
247  $this->injectDependenciesIntoViewHelper($viewHelper);
248  $hashService = $this->getMockBuilder(HashService::class)
249  ->setMethods(['appendHmac'])
250  ->getMock();
251  $hashService->expects(self::any())->method('appendHmac')->willReturn('');
252  $viewHelper->injectHashService($hashService);
253  $this->request->getControllerExtensionName()->willReturn('extensionName');
254  $this->request->getControllerName()->willReturn('controllerName');
255  $this->request->getControllerActionName()->willReturn('controllerActionName');
256  $this->request->getArguments()->willReturn([]);
257  $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
258  $expectedResult = \chr(10) . '<input type="hidden" name="__referrer[@extension]" value="extensionName" />'
259  . \chr(10) . '<input type="hidden" name="__referrer[@controller]" value="controllerName" />'
260  . \chr(10) . '<input type="hidden" name="__referrer[@action]" value="controllerActionName" />'
261  . \chr(10) . '<input type="hidden" name="__referrer[arguments]" value="" />'
262  . \chr(10) . '<input type="hidden" name="__referrer[@request]" value="" />' . \chr(10);
263  $viewHelper->setTagBuilder($this->tagBuilder);
264  self::assertEquals($expectedResult, $hiddenFields);
265  }
266 
271  {
272  $prefix = 'somePrefix';
273  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
274  $this->injectDependenciesIntoViewHelper($viewHelper);
275  $viewHelper->setArguments(['fieldNamePrefix' => $prefix]);
276  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'fieldNamePrefix', $prefix);
277  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'fieldNamePrefix');
278  $viewHelper->setTagBuilder($this->tagBuilder);
279  $viewHelper->render();
280  }
281 
286  {
287  $expectedPrefix = 'tx_someextension_someplugin';
288  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
289  $mockExtensionService = $this->createMock(ExtensionService::class);
290  $mockExtensionService->expects(self::once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->willReturn('tx_someextension_someplugin');
291  $viewHelper->injectExtensionService($mockExtensionService);
292  $this->injectDependenciesIntoViewHelper($viewHelper);
293  $viewHelper->setArguments(['extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin']);
294  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'fieldNamePrefix', $expectedPrefix);
295  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'fieldNamePrefix');
296  $viewHelper->setTagBuilder($this->tagBuilder);
297  $viewHelper->render();
298  }
299 
304  {
305  return [
306  // simple values
307  [
308  [
309  'bla' => 'X',
310  'blubb' => 'Y'
311  ],
312  [
313  'bla',
314  'blubb'
315  ]
316  ],
317  // Arrays
318  [
319  [
320  'bla' => [
321  'test1' => 'X',
322  'test2' => 'Y'
323  ],
324  'blubb' => 'Y'
325  ],
326  [
327  'bla[test1]',
328  'bla[test2]',
329  'blubb'
330  ]
331  ]
332  ];
333  }
334 
341  public function ‪postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
342  {
343  $formViewHelper = new ‪FormViewHelper();
344  $results = [];
345  $mock = \Closure::bind(static function (‪FormViewHelper $formViewHelper) use ($arguments, &$results) {
346  return $formViewHelper->‪postProcessUriArgumentsForRequestHash($arguments, $results);
347  }, null, FormViewHelper::class);
348  $mock($formViewHelper);
349  self::assertEquals($expectedResults, $results);
350  }
351 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\formObjectNameArgumentOverrulesNameArgument
‪formObjectNameArgumentOverrulesNameArgument()
Definition: FormViewHelperTest.php:128
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields
‪renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields()
Definition: FormViewHelperTest.php:244
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:39
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\initializeArgumentsRegistersExpectedArguments
‪initializeArgumentsRegistersExpectedArguments()
Definition: FormViewHelperTest.php:57
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\argumentsForPostProcessUriArgumentsForRequestHash
‪argumentsForPostProcessUriArgumentsForRequestHash()
Definition: FormViewHelperTest.php:303
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderHiddenReferrerFields
‪renderCallsRenderHiddenReferrerFields()
Definition: FormViewHelperTest.php:143
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified
‪renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified()
Definition: FormViewHelperTest.php:285
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:61
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\postProcessUriArgumentsForRequestHashWorks
‪postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
Definition: FormViewHelperTest.php:341
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\setFormActionUriRespectsOverriddenArgument
‪setFormActionUriRespectsOverriddenArgument()
Definition: FormViewHelperTest.php:80
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsObjectToViewHelperVariableContainer
‪renderAddsObjectToViewHelperVariableContainer()
Definition: FormViewHelperTest.php:96
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsObjectNameToTemplateVariableContainer
‪renderAddsObjectNameToTemplateVariableContainer()
Definition: FormViewHelperTest.php:113
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderHiddenIdentityField
‪renderCallsRenderHiddenIdentityField()
Definition: FormViewHelperTest.php:155
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest
Definition: FormViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper
‪renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper()
Definition: FormViewHelperTest.php:182
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderAdditionalIdentityFields
‪renderCallsRenderAdditionalIdentityFields()
Definition: FormViewHelperTest.php:170
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper\postProcessUriArgumentsForRequestHash
‪postProcessUriArgumentsForRequestHash($arguments, &$results, $currentPrefix='', $level=0)
Definition: FormViewHelper.php:413
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\setUp
‪setUp()
Definition: FormViewHelperTest.php:32
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem
‪renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem()
Definition: FormViewHelperTest.php:225
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper
‪renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper()
Definition: FormViewHelperTest.php:199
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsSpecifiedPrefixToTemplateVariableContainer
‪renderAddsSpecifiedPrefixToTemplateVariableContainer()
Definition: FormViewHelperTest.php:270