TYPO3 CMS  TYPO3_7-6
FormViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /* *
6  * This script is backported from the FLOW3 package "TYPO3.Fluid". *
7  * *
8  * It is free software; you can redistribute it and/or modify it under *
9  * the terms of the GNU General Public License as published by the Free *
10  * Software Foundation, either version 3 of the License, or (at your *
11  * *
12  * *
13  * This script is distributed in the hope that it will be useful, but *
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
15  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
16  * Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with the script. *
20  * If not, see http://www.gnu.org/licenses/gpl.html *
21  * *
22  * The TYPO3 project - inspiring people to share! *
23  * */
24 
29 {
34 
39 
40  protected function setUp()
41  {
42  parent::setUp();
43  $this->mockExtensionService = $this->getMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
44  $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
45  }
46 
51  protected function injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
52  {
53  $viewHelper->_set('configurationManager', $this->mockConfigurationManager);
54  parent::injectDependenciesIntoViewHelper($viewHelper);
55  $hashService = $this->getMock(\TYPO3\CMS\Extbase\Security\Cryptography\HashService::class, ['appendHmac']);
56  $hashService->expects($this->any())->method('appendHmac')->will($this->returnValue(''));
57  $this->mvcPropertyMapperConfigurationService->_set('hashService', $hashService);
58  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
59  $viewHelper->_set('hashService', $hashService);
60  }
61 
66  {
67  $formObject = new \stdClass();
68  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
69  $this->injectDependenciesIntoViewHelper($viewHelper);
70  $viewHelper->setArguments(['object' => $formObject]);
71  $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject', $formObject);
72  $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties', []);
73  $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject');
74  $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties');
75  $viewHelper->render();
76  }
77 
82  {
83  $objectName = 'someObjectName';
84  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
85  $this->injectDependenciesIntoViewHelper($viewHelper);
86  $viewHelper->setArguments(['name' => $objectName]);
87  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
88  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
89  $viewHelper->_set('tag', $this->tagBuilder);
90  $viewHelper->render();
91  }
92 
97  {
98  $objectName = 'someObjectName';
99  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
100  $this->injectDependenciesIntoViewHelper($viewHelper);
101  $viewHelper->setArguments(['name' => 'formName', 'objectName' => $objectName]);
102  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
103  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
104  $viewHelper->_set('tag', $this->tagBuilder);
105  $viewHelper->render();
106  }
107 
112  {
113  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'], [], '', false);
114  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
115  $this->injectDependenciesIntoViewHelper($viewHelper);
116  $viewHelper->render();
117  }
118 
123  {
124  $object = new \stdClass();
125  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'], [], '', false);
126  $this->injectDependenciesIntoViewHelper($viewHelper);
127  $viewHelper->setArguments(['object' => $object]);
128  $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
129  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
130  $viewHelper->render();
131  }
132 
137  {
138  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'], [], '', false);
139  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
140  $this->injectDependenciesIntoViewHelper($viewHelper);
141  $viewHelper->render();
142  }
143 
148  {
149  $viewHelper = $this->getAccessibleMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class), ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'], [], '', false);
150  $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
151  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
152  parent::injectDependenciesIntoViewHelper($viewHelper);
153  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
154  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
155  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
156  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
157  $expectedResult = chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
158  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
159  $viewHelper->render();
160  }
161 
166  {
167  $viewHelper = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class), ['renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderHiddenSecuredReferrerField', 'renderTrustedPropertiesField'], [], '', false);
168  $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
169  $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
170  parent::injectDependenciesIntoViewHelper($viewHelper);
171  $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
172  $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
173  $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
174  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
175  $expectedResult = chr(10) . '<div class="hidden">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
176  $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
177  $viewHelper->setArguments(['hiddenFieldClassName' => 'hidden']);
178  $viewHelper->render();
179  }
180 
185  {
186  $identityProperties = [
187  'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
188  'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
189  ];
190  $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->will($this->returnValue(true));
191  $this->viewHelperVariableContainer->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
192  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren'], [], '', false);
193  $this->injectDependenciesIntoViewHelper($viewHelper);
194  $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
195  $actual = $viewHelper->_call('renderAdditionalIdentityFields');
196  $this->assertEquals($expected, $actual);
197  }
198 
203  {
204  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['dummy'], [], '', false);
205  $this->injectDependenciesIntoViewHelper($viewHelper);
206  $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
207  $this->request->expects($this->never())->method('getControllerSubextensionName');
208  $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
209  $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
210  $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
211  $expectedResult = chr(10) . '<input type="hidden" name="__referrer[@extension]" value="extensionName" />'
212  . chr(10) . '<input type="hidden" name="__referrer[@controller]" value="controllerName" />'
213  . chr(10) . '<input type="hidden" name="__referrer[@action]" value="controllerActionName" />'
214  . chr(10) . '<input type="hidden" name="__referrer[arguments]" value="" />'
215  . chr(10) . '<input type="hidden" name="__referrer[@request]" value="" />' . chr(10);
216  $this->assertEquals($expectedResult, $hiddenFields);
217  }
218 
223  {
224  $prefix = 'somePrefix';
225  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
226  $this->injectDependenciesIntoViewHelper($viewHelper);
227  $viewHelper->setArguments(['fieldNamePrefix' => $prefix]);
228  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $prefix);
229  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
230  $viewHelper->_set('tag', $this->tagBuilder);
231  $viewHelper->render();
232  }
233 
238  {
239  $expectedPrefix = 'tx_someextension_someplugin';
240  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'], [], '', false);
241  $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
242  $viewHelper->_set('extensionService', $this->mockExtensionService);
243  $this->injectDependenciesIntoViewHelper($viewHelper);
244  $viewHelper->setArguments(['extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin']);
245  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $expectedPrefix);
246  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
247  $viewHelper->_set('tag', $this->tagBuilder);
248  $viewHelper->render();
249  }
250 
255  {
256  return [
257  // simple values
258  [
259  [
260  'bla' => 'X',
261  'blubb' => 'Y'
262  ],
263  [
264  'bla',
265  'blubb'
266  ]
267  ],
268  // Arrays
269  [
270  [
271  'bla' => [
272  'test1' => 'X',
273  'test2' => 'Y'
274  ],
275  'blubb' => 'Y'
276  ],
277  [
278  'bla[test1]',
279  'bla[test2]',
280  'blubb'
281  ]
282  ]
283  ];
284  }
285 
290  public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
291  {
292  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, ['dummy'], [], '', false);
293  $results = [];
294  $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
295  $this->assertEquals($expectedResults, $results);
296  }
297 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)