f66eace87e98ed523b4db3ee3d535c463eb388a7
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / FormViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
3
4 /* *
5 * This script is backported from the FLOW3 package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free *
9 * Software Foundation, either version 3 of the License, or (at your *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
15 * Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with the script. *
19 * If not, see http://www.gnu.org/licenses/gpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 require_once __DIR__ . '/ViewHelperBaseTestcase.php';
24
25 /**
26 * Test for the Form view helper
27 */
28 class FormViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
32 */
33 protected $mockExtensionService;
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
37 */
38 protected $mockConfigurationManager;
39
40 public function setUp() {
41 parent::setUp();
42 $this->mockExtensionService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\ExtensionService');
43 $this->mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
44 }
45
46 /**
47 * @param \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper
48 * @return void
49 */
50 protected function injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper) {
51 $viewHelper->injectConfigurationManager($this->mockConfigurationManager);
52 parent::injectDependenciesIntoViewHelper($viewHelper);
53 $this->mvcPropertyMapperConfigurationService->injectHashService(new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
54 $viewHelper->injectMvcPropertyMapperConfigurationService($this->mvcPropertyMapperConfigurationService);
55 }
56
57 /**
58 * @test
59 */
60 public function renderAddsObjectToViewHelperVariableContainer() {
61 $formObject = new \stdClass();
62 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
63 $this->injectDependenciesIntoViewHelper($viewHelper);
64 $viewHelper->setArguments(array('object' => $formObject));
65 $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject', $formObject);
66 $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties', array());
67 $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
68 $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties');
69 $viewHelper->render();
70 }
71
72 /**
73 * @test
74 */
75 public function renderAddsObjectNameToTemplateVariableContainer() {
76 $objectName = 'someObjectName';
77 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
78 $this->injectDependenciesIntoViewHelper($viewHelper);
79 $viewHelper->setArguments(array('name' => $objectName));
80 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName', $objectName);
81 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
82 $viewHelper->render();
83 }
84
85 /**
86 * @test
87 */
88 public function formObjectNameArgumentOverrulesNameArgument() {
89 $objectName = 'someObjectName';
90 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
91 $this->injectDependenciesIntoViewHelper($viewHelper);
92 $viewHelper->setArguments(array('name' => 'formName', 'objectName' => $objectName));
93 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName', $objectName);
94 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
95 $viewHelper->render();
96 }
97
98 /**
99 * @test
100 */
101 public function renderCallsRenderHiddenReferrerFields() {
102 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
103 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
104 $this->injectDependenciesIntoViewHelper($viewHelper);
105 $viewHelper->render();
106 }
107
108 /**
109 * @test
110 */
111 public function renderCallsRenderHiddenIdentityField() {
112 $object = new \stdClass();
113 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenIdentityField', 'getFormObjectName', 'renderTrustedPropertiesField'), array(), '', FALSE);
114 $this->injectDependenciesIntoViewHelper($viewHelper);
115 $viewHelper->setArguments(array('object' => $object));
116 $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
117 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
118 $viewHelper->render();
119 }
120
121 /**
122 * @test
123 */
124 public function renderCallsRenderAdditionalIdentityFields() {
125 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderAdditionalIdentityFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
126 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
127 $this->injectDependenciesIntoViewHelper($viewHelper);
128 $viewHelper->render();
129 }
130
131 /**
132 * @test
133 */
134 public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithOldPropertyMapper() {
135 $viewHelper = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField'), array(), '', FALSE);
136 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('isFeatureEnabled'));
137 $configurationManager->expects($this->once())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(FALSE));
138 $viewHelper->injectConfigurationManager($configurationManager);
139 $this->mvcPropertyMapperConfigurationService->injectHashService(new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
140 $viewHelper->injectMvcPropertyMapperConfigurationService($this->mvcPropertyMapperConfigurationService);
141 parent::injectDependenciesIntoViewHelper($viewHelper);
142 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
143 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
144 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
145 $viewHelper->expects($this->once())->method('renderRequestHashField')->will($this->returnValue('requestHashField'));
146 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
147 $expectedResult = chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFieldsrequestHashField' . chr(10) . '</div>' . chr(10) . 'formContent';
148 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
149 $viewHelper->render();
150 }
151
152 /**
153 * @test
154 */
155 public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibilityWithRewrittenPropertyMapper() {
156 $viewHelper = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
157 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('isFeatureEnabled'));
158 $configurationManager->expects($this->once())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
159 $viewHelper->injectConfigurationManager($configurationManager);
160 $this->mvcPropertyMapperConfigurationService->injectHashService(new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
161 $viewHelper->injectMvcPropertyMapperConfigurationService($this->mvcPropertyMapperConfigurationService);
162 parent::injectDependenciesIntoViewHelper($viewHelper);
163 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
164 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
165 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
166 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
167 $expectedResult = chr(10) . '<div>' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
168 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
169 $viewHelper->render();
170 }
171
172 /**
173 * @test
174 */
175 public function renderWrapsHiddenFieldsWithDivAndAnAdditionalClassForXhtmlCompatibilityWithRewrittenPropertyMapper() {
176 $viewHelper = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderTrustedPropertiesField'), array(), '', FALSE);
177 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('isFeatureEnabled'));
178 $configurationManager->expects($this->once())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
179 $viewHelper->injectConfigurationManager($configurationManager);
180 $this->mvcPropertyMapperConfigurationService->injectHashService(new \TYPO3\CMS\Extbase\Security\Cryptography\HashService());
181 $viewHelper->injectMvcPropertyMapperConfigurationService($this->mvcPropertyMapperConfigurationService);
182 parent::injectDependenciesIntoViewHelper($viewHelper);
183 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
184 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
185 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
186 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
187 $expectedResult = chr(10) . '<div class="hidden">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFields' . chr(10) . '</div>' . chr(10) . 'formContent';
188 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
189 $viewHelper->setArguments(array('hiddenFieldClassName' => 'hidden'));
190 $viewHelper->render();
191 }
192
193 /**
194 * @test
195 */
196 public function renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
197 $identityProperties = array(
198 'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
199 'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
200 );
201 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
202 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
203 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren'), array(), '', FALSE);
204 $this->injectDependenciesIntoViewHelper($viewHelper);
205 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
206 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
207 $this->assertEquals($expected, $actual);
208 }
209
210 /**
211 * @test
212 */
213 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
214 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
215 $this->injectDependenciesIntoViewHelper($viewHelper);
216 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
217 $this->request->expects($this->never())->method('getControllerSubextensionName');
218 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
219 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
220 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
221 $expectedResult = chr(10) . '<input type="hidden" name="__referrer[extensionName]" value="extensionName" />' . chr(10) . '<input type="hidden" name="__referrer[controllerName]" value="controllerName" />' . chr(10) . '<input type="hidden" name="__referrer[actionName]" value="controllerActionName" />' . chr(10);
222 $this->assertEquals($expectedResult, $hiddenFields);
223 }
224
225 /**
226 * @test
227 */
228 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
229 $prefix = 'somePrefix';
230 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
231 $this->injectDependenciesIntoViewHelper($viewHelper);
232 $viewHelper->setArguments(array('fieldNamePrefix' => $prefix));
233 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $prefix);
234 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
235 $viewHelper->render();
236 }
237
238 /**
239 * @test
240 */
241 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
242 $expectedPrefix = 'tx_someextension_someplugin';
243 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
244 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
245 $viewHelper->injectExtensionService($this->mockExtensionService);
246 $this->injectDependenciesIntoViewHelper($viewHelper);
247 $viewHelper->setArguments(array('extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin'));
248 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
249 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
250 $viewHelper->render();
251 }
252
253 /**
254 * Data Provider for postProcessUriArgumentsForRequestHashWorks
255 */
256 public function argumentsForPostProcessUriArgumentsForRequestHash() {
257 return array(
258 // simple values
259 array(
260 array(
261 'bla' => 'X',
262 'blubb' => 'Y'
263 ),
264 array(
265 'bla',
266 'blubb'
267 )
268 ),
269 // Arrays
270 array(
271 array(
272 'bla' => array(
273 'test1' => 'X',
274 'test2' => 'Y'
275 ),
276 'blubb' => 'Y'
277 ),
278 array(
279 'bla[test1]',
280 'bla[test2]',
281 'blubb'
282 )
283 )
284 );
285 }
286
287 /**
288 * @test
289 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
290 */
291 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
292 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
293 $results = array();
294 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
295 $this->assertEquals($expectedResults, $results);
296 }
297 }
298
299 ?>