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