Raised Extbase and Fluid to version 1.3.0
[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 style="display: none">' . '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 renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
149 $identityProperties = array(
150 'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
151 'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
152 );
153 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
154 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
155 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren'), array(), '', FALSE);
156 $this->injectDependenciesIntoViewHelper($viewHelper);
157
158 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) .
159 '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
160 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
161 $this->assertEquals($expected, $actual);
162 }
163
164 /**
165 * @test
166 * @author Christopher Hlubek <hlubek@networkteam.com>
167 */
168 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
169 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
170 $this->injectDependenciesIntoViewHelper($viewHelper);
171
172 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
173 $this->request->expects($this->never())->method('getControllerSubextensionName');
174 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
175 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
176
177 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
178 $expectedResult = chr(10) . '<input type="hidden" name="__referrer[extensionName]" value="extensionName" />' . chr(10) .
179 '<input type="hidden" name="__referrer[controllerName]" value="controllerName" />' . chr(10) .
180 '<input type="hidden" name="__referrer[actionName]" value="controllerActionName" />' . chr(10);
181 $this->assertEquals($expectedResult, $hiddenFields);
182 }
183
184 /**
185 * @test
186 * @author Bastian Waidelich <bastian@typo3.org>
187 */
188 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
189 $prefix = 'somePrefix';
190
191 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
192 $this->injectDependenciesIntoViewHelper($viewHelper);
193
194 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('fieldNamePrefix' => $prefix)));
195
196 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $prefix);
197 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
198 $viewHelper->render();
199 }
200
201 /**
202 * @test
203 * @author Bastian Waidelich <bastian@typo3.org>
204 */
205 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
206 $expectedPrefix = 'tx_someextension_someplugin';
207
208 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
209 $this->injectDependenciesIntoViewHelper($viewHelper);
210
211 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin')));
212
213 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
214 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
215 $viewHelper->render();
216 }
217
218 /**
219 * Data Provider for postProcessUriArgumentsForRequestHashWorks
220 */
221 public function argumentsForPostProcessUriArgumentsForRequestHash() {
222 return array(
223 // simple values
224 array(
225 array(
226 'bla' => 'X',
227 'blubb' => 'Y'
228 ),
229 array(
230 'bla',
231 'blubb'
232 )
233 ),
234 // Arrays
235 array(
236 array(
237 'bla' => array(
238 'test1' => 'X',
239 'test2' => 'Y'
240 ),
241 'blubb' => 'Y'
242 ),
243 array(
244 'bla[test1]',
245 'bla[test2]',
246 'blubb'
247 )
248 )
249 );
250 }
251 /**
252 * @test
253 * @author Sebastian Kurfürst <sebastian@typo3.org>
254 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
255 */
256 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
257 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
258 $results = array();
259 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
260 $this->assertEquals($expectedResults, $results);
261 }
262 }
263 ?>