[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / FormViewHelperTest.php
1 <?php
2
3 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
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
25 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
26
27 /**
28 * Test for the Form view helper
29 */
30 class FormViewHelperTest extends ViewHelperBaseTestcase {
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
34 */
35 protected $mockExtensionService;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
39 */
40 protected $mockConfigurationManager;
41
42 protected function setUp() {
43 parent::setUp();
44 $this->mockExtensionService = $this->getMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
45 $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
46 }
47
48 /**
49 * @param \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper
50 * @return void
51 */
52 protected function injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper) {
53 $viewHelper->_set('configurationManager', $this->mockConfigurationManager);
54 parent::injectDependenciesIntoViewHelper($viewHelper);
55 $hashService = $this->getMock(\TYPO3\CMS\Extbase\Security\Cryptography\HashService::class, array('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
62 /**
63 * @test
64 */
65 public function renderAddsObjectToViewHelperVariableContainer() {
66 $formObject = new \stdClass();
67 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
68 $this->injectDependenciesIntoViewHelper($viewHelper);
69 $viewHelper->setArguments(array('object' => $formObject));
70 $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject', $formObject);
71 $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties', array());
72 $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject');
73 $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties');
74 $viewHelper->render();
75 }
76
77 /**
78 * @test
79 */
80 public function renderAddsObjectNameToTemplateVariableContainer() {
81 $objectName = 'someObjectName';
82 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
83 $this->injectDependenciesIntoViewHelper($viewHelper);
84 $viewHelper->setArguments(array('name' => $objectName));
85 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
86 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
87 $viewHelper->render();
88 }
89
90 /**
91 * @test
92 */
93 public function formObjectNameArgumentOverrulesNameArgument() {
94 $objectName = 'someObjectName';
95 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
96 $this->injectDependenciesIntoViewHelper($viewHelper);
97 $viewHelper->setArguments(array('name' => 'formName', 'objectName' => $objectName));
98 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $objectName);
99 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
100 $viewHelper->render();
101 }
102
103 /**
104 * @test
105 */
106 public function renderCallsRenderHiddenReferrerFields() {
107 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
108 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
109 $this->injectDependenciesIntoViewHelper($viewHelper);
110 $viewHelper->render();
111 }
112
113 /**
114 * @test
115 */
116 public function renderCallsRenderHiddenIdentityField() {
117 $object = new \stdClass();
118 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'), array(), '', FALSE);
119 $this->injectDependenciesIntoViewHelper($viewHelper);
120 $viewHelper->setArguments(array('object' => $object));
121 $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
122 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
123 $viewHelper->render();
124 }
125
126 /**
127 * @test
128 */
129 public function renderCallsRenderAdditionalIdentityFields() {
130 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
131 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
132 $this->injectDependenciesIntoViewHelper($viewHelper);
133 $viewHelper->render();
134 }
135
136 /**
137 * @test
138 */
139 public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper() {
140 $viewHelper = $this->getAccessibleMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
141 $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
142 $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
143 parent::injectDependenciesIntoViewHelper($viewHelper);
144 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
145 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
146 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
147 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
148 $expectedResult = chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
149 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
150 $viewHelper->render();
151 }
152
153 /**
154 * @test
155 */
156 public function renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper() {
157 $viewHelper = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
158 $this->mvcPropertyMapperConfigurationService->_set('hashService', new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
159 $viewHelper->_set('mvcPropertyMapperConfigurationService', $this->mvcPropertyMapperConfigurationService);
160 parent::injectDependenciesIntoViewHelper($viewHelper);
161 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
162 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
163 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
164 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
165 $expectedResult = chr(10) . '<div class="hidden">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
166 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
167 $viewHelper->setArguments(array('hiddenFieldClassName' => 'hidden'));
168 $viewHelper->render();
169 }
170
171 /**
172 * @test
173 */
174 public function renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
175 $identityProperties = array(
176 'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
177 'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
178 );
179 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->will($this->returnValue(TRUE));
180 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
181 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren'), array(), '', FALSE);
182 $this->injectDependenciesIntoViewHelper($viewHelper);
183 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
184 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
185 $this->assertEquals($expected, $actual);
186 }
187
188 /**
189 * @test
190 */
191 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
192 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('dummy'), array(), '', FALSE);
193 $this->injectDependenciesIntoViewHelper($viewHelper);
194 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
195 $this->request->expects($this->never())->method('getControllerSubextensionName');
196 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
197 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
198 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
199 $expectedResult = chr(10) . '<input type="hidden" name="__referrer[@extension]" value="extensionName" />' . chr(10) . '<input type="hidden" name="__referrer[@controller]" value="controllerName" />' . chr(10) . '<input type="hidden" name="__referrer[@action]" value="controllerActionName" />' . chr(10) . '<input type="hidden" name="__referrer[arguments]" value="" />' . chr(10);
200 $this->assertEquals($expectedResult, $hiddenFields);
201 }
202
203 /**
204 * @test
205 */
206 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
207 $prefix = 'somePrefix';
208 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
209 $this->injectDependenciesIntoViewHelper($viewHelper);
210 $viewHelper->setArguments(array('fieldNamePrefix' => $prefix));
211 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $prefix);
212 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
213 $viewHelper->render();
214 }
215
216 /**
217 * @test
218 */
219 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
220 $expectedPrefix = 'tx_someextension_someplugin';
221 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
222 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
223 $viewHelper->_set('extensionService', $this->mockExtensionService);
224 $this->injectDependenciesIntoViewHelper($viewHelper);
225 $viewHelper->setArguments(array('extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin'));
226 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $expectedPrefix);
227 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
228 $viewHelper->render();
229 }
230
231 /**
232 * Data Provider for postProcessUriArgumentsForRequestHashWorks
233 */
234 public function argumentsForPostProcessUriArgumentsForRequestHash() {
235 return array(
236 // simple values
237 array(
238 array(
239 'bla' => 'X',
240 'blubb' => 'Y'
241 ),
242 array(
243 'bla',
244 'blubb'
245 )
246 ),
247 // Arrays
248 array(
249 array(
250 'bla' => array(
251 'test1' => 'X',
252 'test2' => 'Y'
253 ),
254 'blubb' => 'Y'
255 ),
256 array(
257 'bla[test1]',
258 'bla[test2]',
259 'blubb'
260 )
261 )
262 );
263 }
264
265 /**
266 * @test
267 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
268 */
269 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
270 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, array('dummy'), array(), '', FALSE);
271 $results = array();
272 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
273 $this->assertEquals($expectedResults, $results);
274 }
275
276 }