‪TYPO3CMS  9.5
FormViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
25 
29 class ‪FormViewHelperTest extends ViewHelperBaseTestcase
30 {
34  protected ‪$mockExtensionService;
35 
40 
41  protected function ‪setUp()
42  {
43  parent::setUp();
44  $this->mockExtensionService = $this->createMock(ExtensionService::class);
45  $this->mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
46  $this->tagBuilder = $this->createMock(TagBuilder::class);
47  }
48 
53  {
54  $viewHelper = $this->getMockBuilder(FormViewHelper::class)
55  ->setMethods(['registerTagAttribute', 'registerUniversalTagAttributes'])
56  ->getMock();
57  $viewHelper->expects($this->at(0))->method('registerTagAttribute')->with('enctype', 'string', $this->anything());
58  $viewHelper->expects($this->at(1))->method('registerTagAttribute')->with('method', 'string', $this->anything());
59  $viewHelper->expects($this->at(2))->method('registerTagAttribute')->with('name', 'string', $this->anything());
60  $viewHelper->expects($this->at(3))->method('registerTagAttribute')->with('onreset', 'string', $this->anything());
61  $viewHelper->expects($this->at(4))->method('registerTagAttribute')->with('onsubmit', 'string', $this->anything());
62  $viewHelper->expects($this->at(6))->method('registerTagAttribute')->with('novalidate', 'bool', $this->anything());
63  $viewHelper->expects($this->once())->method('registerUniversalTagAttributes');
64  $viewHelper->initializeArguments();
65  }
66 
71  {
72  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['hasArgument']);
73  $viewHelper->expects($this->once())->method('hasArgument')->with('actionUri')->willReturn(true);
74  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
75  ->setMethods(['addAttribute'])
76  ->getMock();
77  $tagBuilder->expects($this->once())->method('addAttribute')->with('action', 'foobar');
78  $viewHelper->_set('tag', $tagBuilder);
79  $viewHelper->setArguments(['actionUri' => 'foobar']);
80  $this->callInaccessibleMethod($viewHelper, 'setFormActionUri');
81  }
82 
86  protected function ‪injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
87  {
88  $viewHelper->_set('configurationManager', $this->mockConfigurationManager);
89  parent::injectDependenciesIntoViewHelper($viewHelper);
90  $hashService = $this->getMockBuilder(HashService::class)
91  ->setMethods(['appendHmac'])
92  ->getMock();
93  $hashService->expects($this->any())->method('appendHmac')->will($this->returnValue(''));
94  $this->mvcPropertyMapperConfigurationService->_set('hashService', $hashService);
95  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
96  $viewHelper->_set('hashService', $hashService);
97  }
98 
103  {
104  $formObject = new \stdClass();
105  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
106  $this->‪injectDependenciesIntoViewHelper($viewHelper);
107  $viewHelper->setArguments(['object' => $formObject]);
108  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObject', $formObject);
109  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'additionalIdentityProperties', []);
110  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObject');
111  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'additionalIdentityProperties');
112  $viewHelper->_set('tag', $this->tagBuilder);
113  $viewHelper->render();
114  }
115 
120  {
121  $objectName = 'someObjectName';
122  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
123  $this->‪injectDependenciesIntoViewHelper($viewHelper);
124  $viewHelper->setArguments(['name' => $objectName]);
125  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObjectName', $objectName);
126  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObjectName');
127  $viewHelper->_set('tag', $this->tagBuilder);
128  $viewHelper->render();
129  }
130 
135  {
136  $objectName = 'someObjectName';
137  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
138  $this->‪injectDependenciesIntoViewHelper($viewHelper);
139  $viewHelper->setArguments(['name' => 'formName', 'objectName' => $objectName]);
140  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'formObjectName', $objectName);
141  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'formObjectName');
142  $viewHelper->_set('tag', $this->tagBuilder);
143  $viewHelper->render();
144  }
145 
150  {
151  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'], [], '', false);
152  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
153  $this->‪injectDependenciesIntoViewHelper($viewHelper);
154  $viewHelper->_set('tag', $this->tagBuilder);
155  $viewHelper->render();
156  }
157 
162  {
163  $object = new \stdClass();
164  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'], [], '', false);
165  $this->‪injectDependenciesIntoViewHelper($viewHelper);
166  $viewHelper->setArguments(['object' => $object]);
167  $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
168  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
169  $viewHelper->_set('tag', $this->tagBuilder);
170  $viewHelper->render();
171  }
172 
177  {
178  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'], [], '', false);
179  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
180  $this->‪injectDependenciesIntoViewHelper($viewHelper);
181  $viewHelper->_set('tag', $this->tagBuilder);
182  $viewHelper->render();
183  }
184 
189  {
190  $viewHelper = $this->getAccessibleMock($this->buildAccessibleProxy(FormViewHelper::class), ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'], [], '', false);
191  $this->mvcPropertyMapperConfigurationService->_set('hashService', new HashService());
192  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
193  parent::injectDependenciesIntoViewHelper($viewHelper);
194  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
195  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
196  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
197  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
198  $expectedResult = \chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . \chr(10) . '</div>' . \chr(10) . 'formContent';
199  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
200  $viewHelper->_set('tag', $this->tagBuilder);
201  $viewHelper->render();
202  }
203 
208  {
209  $viewHelper = $this->getMockBuilder($this->buildAccessibleProxy(FormViewHelper::class))
210  ->setMethods(['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'])
211  ->disableOriginalConstructor()
212  ->getMock();
213  $this->mvcPropertyMapperConfigurationService->_set('hashService', new HashService());
214  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
215  parent::injectDependenciesIntoViewHelper($viewHelper);
216  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
217  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
218  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
219  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
220  $expectedResult = \chr(10) . '<div class="hidden">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . \chr(10) . '</div>' . \chr(10) . 'formContent';
221  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
222  $viewHelper->setArguments(['hiddenFieldClassName' => 'hidden']);
223  $viewHelper->_set('tag', $this->tagBuilder);
224  $viewHelper->render();
225  }
226 
231  {
232  $identityProperties = [
233  'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
234  'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
235  ];
236  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'additionalIdentityProperties')->willReturn(true);
237  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'additionalIdentityProperties')->willReturn($identityProperties);
238  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren'], [], '', false);
239  $this->‪injectDependenciesIntoViewHelper($viewHelper);
240  $expected = \chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . \chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
241  $actual = $viewHelper->_call('renderAdditionalIdentityFields');
242  $viewHelper->_set('tag', $this->tagBuilder);
243  $this->assertEquals($expected, $actual);
244  }
245 
250  {
251  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['dummy'], [], '', false);
252  $this->‪injectDependenciesIntoViewHelper($viewHelper);
253  $this->request->getControllerExtensionName()->willReturn('extensionName');
254  $this->request->getControllerName()->willReturn('controllerName');
255  $this->request->getControllerActionName()->willReturn('controllerActionName');
256  $this->request->getControllerVendorName()->willReturn('controllerVendorName');
257  $this->request->getArguments()->willReturn([]);
258  $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
259  $expectedResult = \chr(10) . '<input type="hidden" name="__referrer[@extension]" value="extensionName" />'
260  . \chr(10) . '<input type="hidden" name="__referrer[@vendor]" value="controllerVendorName" />'
261  . \chr(10) . '<input type="hidden" name="__referrer[@controller]" value="controllerName" />'
262  . \chr(10) . '<input type="hidden" name="__referrer[@action]" value="controllerActionName" />'
263  . \chr(10) . '<input type="hidden" name="__referrer[arguments]" value="" />'
264  . \chr(10) . '<input type="hidden" name="__referrer[@request]" value="" />' . \chr(10);
265  $viewHelper->_set('tag', $this->tagBuilder);
266  $this->assertEquals($expectedResult, $hiddenFields);
267  }
268 
273  {
274  $prefix = 'somePrefix';
275  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
276  $this->‪injectDependenciesIntoViewHelper($viewHelper);
277  $viewHelper->setArguments(['fieldNamePrefix' => $prefix]);
278  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'fieldNamePrefix', $prefix);
279  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'fieldNamePrefix');
280  $viewHelper->_set('tag', $this->tagBuilder);
281  $viewHelper->render();
282  }
283 
288  {
289  $expectedPrefix = 'tx_someextension_someplugin';
290  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
291  $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
292  $viewHelper->_set('extensionService', $this->mockExtensionService);
293  $this->‪injectDependenciesIntoViewHelper($viewHelper);
294  $viewHelper->setArguments(['extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin']);
295  $this->viewHelperVariableContainer->add(FormViewHelper::class, 'fieldNamePrefix', $expectedPrefix);
296  $this->viewHelperVariableContainer->remove(FormViewHelper::class, 'fieldNamePrefix');
297  $viewHelper->_set('tag', $this->tagBuilder);
298  $viewHelper->render();
299  }
300 
305  {
306  return [
307  // simple values
308  [
309  [
310  'bla' => 'X',
311  'blubb' => 'Y'
312  ],
313  [
314  'bla',
315  'blubb'
316  ]
317  ],
318  // Arrays
319  [
320  [
321  'bla' => [
322  'test1' => 'X',
323  'test2' => 'Y'
324  ],
325  'blubb' => 'Y'
326  ],
327  [
328  'bla[test1]',
329  'bla[test2]',
330  'blubb'
331  ]
332  ]
333  ];
334  }
335 
342  public function ‪postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
343  {
344  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['dummy'], [], '', false);
345  $results = [];
346  $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
347  $this->assertEquals($expectedResults, $results);
348  }
349 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\formObjectNameArgumentOverrulesNameArgument
‪formObjectNameArgumentOverrulesNameArgument()
Definition: FormViewHelperTest.php:132
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields
‪renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields()
Definition: FormViewHelperTest.php:247
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\initializeArgumentsRegistersExpectedArguments
‪initializeArgumentsRegistersExpectedArguments()
Definition: FormViewHelperTest.php:50
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:22
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\argumentsForPostProcessUriArgumentsForRequestHash
‪argumentsForPostProcessUriArgumentsForRequestHash()
Definition: FormViewHelperTest.php:302
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderHiddenReferrerFields
‪renderCallsRenderHiddenReferrerFields()
Definition: FormViewHelperTest.php:147
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:24
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified
‪renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified()
Definition: FormViewHelperTest.php:285
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\$mockConfigurationManager
‪ConfigurationManagerInterface $mockConfigurationManager
Definition: FormViewHelperTest.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:52
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\postProcessUriArgumentsForRequestHashWorks
‪postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
Definition: FormViewHelperTest.php:340
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\setFormActionUriRespectsOverriddenArgument
‪setFormActionUriRespectsOverriddenArgument()
Definition: FormViewHelperTest.php:68
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsObjectToViewHelperVariableContainer
‪renderAddsObjectToViewHelperVariableContainer()
Definition: FormViewHelperTest.php:100
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsObjectNameToTemplateVariableContainer
‪renderAddsObjectNameToTemplateVariableContainer()
Definition: FormViewHelperTest.php:117
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderHiddenIdentityField
‪renderCallsRenderHiddenIdentityField()
Definition: FormViewHelperTest.php:159
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest
Definition: FormViewHelperTest.php:30
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper
‪renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper()
Definition: FormViewHelperTest.php:186
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderCallsRenderAdditionalIdentityFields
‪renderCallsRenderAdditionalIdentityFields()
Definition: FormViewHelperTest.php:174
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\$mockExtensionService
‪ExtensionService $mockExtensionService
Definition: FormViewHelperTest.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\setUp
‪setUp()
Definition: FormViewHelperTest.php:39
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem
‪renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem()
Definition: FormViewHelperTest.php:228
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\injectDependenciesIntoViewHelper
‪injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
Definition: FormViewHelperTest.php:84
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers
Definition: BaseViewHelperTest.php:2
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper
‪renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper()
Definition: FormViewHelperTest.php:205
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\FormViewHelperTest\renderAddsSpecifiedPrefixToTemplateVariableContainer
‪renderAddsSpecifiedPrefixToTemplateVariableContainer()
Definition: FormViewHelperTest.php:270