TYPO3 CMS  TYPO3_8-7
FormViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
24 class FormViewHelperTest extends ViewHelperBaseTestcase
25 {
30 
35 
36  protected function setUp()
37  {
38  parent::setUp();
39  $this->mockExtensionService = $this->createMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
40  $this->mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
41  $this->tagBuilder = $this->createMock(\TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder::class);
42  }
43 
48  {
49  $viewHelper = $this->getMockBuilder(FormViewHelper::class)
50  ->setMethods(['registerTagAttribute', 'registerUniversalTagAttributes'])
51  ->getMock();
52  $viewHelper->expects($this->at(0))->method('registerTagAttribute')->with('enctype', 'string', $this->anything());
53  $viewHelper->expects($this->at(1))->method('registerTagAttribute')->with('method', 'string', $this->anything());
54  $viewHelper->expects($this->at(2))->method('registerTagAttribute')->with('name', 'string', $this->anything());
55  $viewHelper->expects($this->at(3))->method('registerTagAttribute')->with('onreset', 'string', $this->anything());
56  $viewHelper->expects($this->at(4))->method('registerTagAttribute')->with('onsubmit', 'string', $this->anything());
57  $viewHelper->expects($this->at(6))->method('registerTagAttribute')->with('novalidate', 'bool', $this->anything());
58  $viewHelper->expects($this->once())->method('registerUniversalTagAttributes');
59  $viewHelper->initializeArguments();
60  }
61 
66  {
67  $viewHelper = $this->getAccessibleMock(FormViewHelper::class, ['hasArgument']);
68  $viewHelper->expects($this->once())->method('hasArgument')->with('actionUri')->willReturn(true);
69  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
70  ->setMethods(['addAttribute'])
71  ->getMock();
72  $tagBuilder->expects($this->once())->method('addAttribute')->with('action', 'foobar');
73  $viewHelper->_set('tag', $tagBuilder);
74  $viewHelper->setArguments(['actionUri' => 'foobar']);
75  $this->callInaccessibleMethod($viewHelper, 'setFormActionUri');
76  }
77 
81  protected function injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
82  {
83  $viewHelper->_set('configurationManager', $this->mockConfigurationManager);
84  parent::injectDependenciesIntoViewHelper($viewHelper);
85  $hashService = $this->getMockBuilder(\TYPO3\CMS\Extbase\Security\Cryptography\HashService::class)
86  ->setMethods(['appendHmac'])
87  ->getMock();
88  $hashService->expects($this->any())->method('appendHmac')->will($this->returnValue(''));
89  $this->mvcPropertyMapperConfigurationService->_set('hashService', $hashService);
90  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
91  $viewHelper->_set('hashService', $hashService);
92  }
93 
98  {
99  $formObject = new \stdClass();
100  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
101  $this->injectDependenciesIntoViewHelper($viewHelper);
102  $viewHelper->setArguments(['object' => $formObject]);
103  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject', $formObject);
104  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties', []);
105  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject');
106  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties');
107  $viewHelper->_set('tag', $this->tagBuilder);
108  $viewHelper->render();
109  }
110 
115  {
116  $objectName = 'someObjectName';
117  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
118  $this->injectDependenciesIntoViewHelper($viewHelper);
119  $viewHelper->setArguments(['name' => $objectName]);
120  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
121  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
122  $viewHelper->_set('tag', $this->tagBuilder);
123  $viewHelper->render();
124  }
125 
130  {
131  $objectName = 'someObjectName';
132  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
133  $this->injectDependenciesIntoViewHelper($viewHelper);
134  $viewHelper->setArguments(['name' => 'formName', 'objectName' => $objectName]);
135  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
136  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
137  $viewHelper->_set('tag', $this->tagBuilder);
138  $viewHelper->render();
139  }
140 
145  {
146  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'], [], '', false);
147  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
148  $this->injectDependenciesIntoViewHelper($viewHelper);
149  $viewHelper->_set('tag', $this->tagBuilder);
150  $viewHelper->render();
151  }
152 
157  {
158  $object = new \stdClass();
159  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'], [], '', false);
160  $this->injectDependenciesIntoViewHelper($viewHelper);
161  $viewHelper->setArguments(['object' => $object]);
162  $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
163  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
164  $viewHelper->_set('tag', $this->tagBuilder);
165  $viewHelper->render();
166  }
167 
172  {
173  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'], [], '', false);
174  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
175  $this->injectDependenciesIntoViewHelper($viewHelper);
176  $viewHelper->_set('tag', $this->tagBuilder);
177  $viewHelper->render();
178  }
179 
184  {
185  $viewHelper = $this->getAccessibleMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class), ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'], [], '', false);
186  $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
187  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
188  parent::injectDependenciesIntoViewHelper($viewHelper);
189  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
190  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
191  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
192  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
193  $expectedResult = chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
194  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
195  $viewHelper->_set('tag', $this->tagBuilder);
196  $viewHelper->render();
197  }
198 
203  {
204  $viewHelper = $this->getMockBuilder($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class))
205  ->setMethods(['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'])
206  ->disableOriginalConstructor()
207  ->getMock();
208  $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
209  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
210  parent::injectDependenciesIntoViewHelper($viewHelper);
211  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
212  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
213  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
214  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
215  $expectedResult = chr(10) . '<div class="hidden">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
216  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
217  $viewHelper->setArguments(['hiddenFieldClassName' => 'hidden']);
218  $viewHelper->_set('tag', $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(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->willReturn(true);
232  $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->willReturn($identityProperties);
233  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\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->_set('tag', $this->tagBuilder);
238  $this->assertEquals($expected, $actual);
239  }
240 
245  {
246  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['dummy'], [], '', false);
247  $this->injectDependenciesIntoViewHelper($viewHelper);
248  $this->request->getControllerExtensionName()->willReturn('extensionName');
249  $this->request->getControllerName()->willReturn('controllerName');
250  $this->request->getControllerActionName()->willReturn('controllerActionName');
251  $this->request->getControllerVendorName()->willReturn('controllerVendorName');
252  $this->request->getArguments()->willReturn([]);
253  $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
254  $expectedResult = chr(10) . '<input type="hidden" name="__referrer[@extension]" value="extensionName" />'
255  . chr(10) . '<input type="hidden" name="__referrer[@vendor]" value="controllerVendorName" />'
256  . chr(10) . '<input type="hidden" name="__referrer[@controller]" value="controllerName" />'
257  . chr(10) . '<input type="hidden" name="__referrer[@action]" value="controllerActionName" />'
258  . chr(10) . '<input type="hidden" name="__referrer[arguments]" value="" />'
259  . chr(10) . '<input type="hidden" name="__referrer[@request]" value="" />' . chr(10);
260  $viewHelper->_set('tag', $this->tagBuilder);
261  $this->assertEquals($expectedResult, $hiddenFields);
262  }
263 
268  {
269  $prefix = 'somePrefix';
270  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
271  $this->injectDependenciesIntoViewHelper($viewHelper);
272  $viewHelper->setArguments(['fieldNamePrefix' => $prefix]);
273  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $prefix);
274  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
275  $viewHelper->_set('tag', $this->tagBuilder);
276  $viewHelper->render();
277  }
278 
283  {
284  $expectedPrefix = 'tx_someextension_someplugin';
285  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
286  $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
287  $viewHelper->_set('extensionService', $this->mockExtensionService);
288  $this->injectDependenciesIntoViewHelper($viewHelper);
289  $viewHelper->setArguments(['extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin']);
290  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $expectedPrefix);
291  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
292  $viewHelper->_set('tag', $this->tagBuilder);
293  $viewHelper->render();
294  }
295 
300  {
301  return [
302  // simple values
303  [
304  [
305  'bla' => 'X',
306  'blubb' => 'Y'
307  ],
308  [
309  'bla',
310  'blubb'
311  ]
312  ],
313  // Arrays
314  [
315  [
316  'bla' => [
317  'test1' => 'X',
318  'test2' => 'Y'
319  ],
320  'blubb' => 'Y'
321  ],
322  [
323  'bla[test1]',
324  'bla[test2]',
325  'blubb'
326  ]
327  ]
328  ];
329  }
330 
335  public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
336  {
337  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['dummy'], [], '', false);
338  $results = [];
339  $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
340  $this->assertEquals($expectedResults, $results);
341  }
342 }
injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)