[FEATURE] Complete the Property Mapper whitelist change for fluid
[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 dirname(__FILE__) . '/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 style="display: none">' . '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 style="display: none">' . '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 renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
176 $identityProperties = array(
177 'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
178 'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
179 );
180 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
181 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
182 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren'), array(), '', FALSE);
183 $this->injectDependenciesIntoViewHelper($viewHelper);
184 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
185 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
186 $this->assertEquals($expected, $actual);
187 }
188
189 /**
190 * @test
191 */
192 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
193 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
194 $this->injectDependenciesIntoViewHelper($viewHelper);
195 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
196 $this->request->expects($this->never())->method('getControllerSubextensionName');
197 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
198 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
199 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
200 $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);
201 $this->assertEquals($expectedResult, $hiddenFields);
202 }
203
204 /**
205 * @test
206 */
207 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
208 $prefix = 'somePrefix';
209 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
210 $this->injectDependenciesIntoViewHelper($viewHelper);
211 $viewHelper->setArguments(array('fieldNamePrefix' => $prefix));
212 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $prefix);
213 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
214 $viewHelper->render();
215 }
216
217 /**
218 * @test
219 */
220 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
221 $expectedPrefix = 'tx_someextension_someplugin';
222 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer', 'renderTrustedPropertiesField'), array(), '', FALSE);
223 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
224 $viewHelper->injectExtensionService($this->mockExtensionService);
225 $this->injectDependenciesIntoViewHelper($viewHelper);
226 $viewHelper->setArguments(array('extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin'));
227 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
228 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
229 $viewHelper->render();
230 }
231
232 /**
233 * Data Provider for postProcessUriArgumentsForRequestHashWorks
234 */
235 public function argumentsForPostProcessUriArgumentsForRequestHash() {
236 return array(
237 // simple values
238 array(
239 array(
240 'bla' => 'X',
241 'blubb' => 'Y'
242 ),
243 array(
244 'bla',
245 'blubb'
246 )
247 ),
248 // Arrays
249 array(
250 array(
251 'bla' => array(
252 'test1' => 'X',
253 'test2' => 'Y'
254 ),
255 'blubb' => 'Y'
256 ),
257 array(
258 'bla[test1]',
259 'bla[test2]',
260 'blubb'
261 )
262 )
263 );
264 }
265
266 /**
267 * @test
268 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
269 */
270 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
271 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
272 $results = array();
273 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
274 $this->assertEquals($expectedResults, $results);
275 }
276 }
277
278 ?>