[+TASK] Fluid (Tests): Backported FormViewHelperTest from Fluid package
[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 * @version $Id:$
30 */
31 class Tx_Fluid_ViewHelpers_FormViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
32 /**
33 * @test
34 * @author Sebastian Kurfürst <sebastian@typo3.org>
35 */
36 public function renderAddsObjectToViewHelperVariableContainer() {
37 $formObject = new stdClass();
38
39 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
40 $this->injectDependenciesIntoViewHelper($viewHelper);
41
42 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $formObject)));
43 $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject', $formObject);
44 $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties', array());
45 $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
46 $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties');
47 $viewHelper->render();
48 }
49
50 /**
51 * @test
52 * @author Sebastian Kurfürst <sebastian@typo3.org>
53 */
54 public function renderAddsFormNameToTemplateVariableContainer() {
55 $formName = 'someFormName';
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' => $formName)));
61
62 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $formName);
63 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
64 $viewHelper->render();
65 }
66
67 /**
68 * @test
69 * @author Christopher Hlubek <hlubek@networkteam.com>
70 */
71 public function renderCallsRenderHiddenReferrerFields() {
72 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields'), array(), '', FALSE);
73 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
74 $this->injectDependenciesIntoViewHelper($viewHelper);
75
76 $viewHelper->render();
77 }
78
79 /**
80 * @test
81 * @author Sebastian Kurfürst <sebastian@typo3.org>
82 */
83 public function renderCallsRenderHiddenIdentityField() {
84 $object = new stdClass();
85 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenIdentityField'), array(), '', FALSE);
86 $this->injectDependenciesIntoViewHelper($viewHelper);
87 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $object, 'name' => 'MyName')));
88 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
89
90 $viewHelper->render();
91 }
92
93 /**
94 * @test
95 * @author Sebastian Kurfürst <sebastian@typo3.org>
96 */
97 public function renderCallsRenderAdditionalIdentityFields() {
98 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderAdditionalIdentityFields'), array(), '', FALSE);
99 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
100 $this->injectDependenciesIntoViewHelper($viewHelper);
101
102 $viewHelper->render();
103 }
104
105 /**
106 * @test
107 * @author Bastian Waidelich <bastian@typo3.org>
108 */
109 public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibility() {
110 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField'), array(), '', FALSE);
111 $this->injectDependenciesIntoViewHelper($viewHelper);
112 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
113 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
114 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
115 $viewHelper->expects($this->once())->method('renderRequestHashField')->will($this->returnValue('requestHashField'));
116 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
117
118 $expectedResult = chr(10) . '<div style="display: none">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFieldsrequestHashField' . chr(10) . '</div>' . chr(10) . 'formContent';
119 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
120
121 $viewHelper->render();
122 }
123
124
125 /**
126 * @test
127 * @author Sebastian Kurfürst <sebastian@typo3.org>
128 */
129 public function renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
130 $identityProperties = array(
131 'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
132 'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
133 );
134 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
135 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
136 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren'), array(), '', FALSE);
137 $this->injectDependenciesIntoViewHelper($viewHelper);
138
139 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) .
140 '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
141 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
142 $this->assertEquals($expected, $actual);
143 }
144
145 /**
146 * @test
147 * @author Christopher Hlubek <hlubek@networkteam.com>
148 */
149 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
150 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
151 $this->injectDependenciesIntoViewHelper($viewHelper);
152
153 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
154 $this->request->expects($this->never())->method('getControllerSubextensionName');
155 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
156 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
157
158 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
159 $expectedResult = chr(10) . '<input type="hidden" name="__referrer[extensionName]" value="extensionName" />' . chr(10) .
160 '<input type="hidden" name="__referrer[controllerName]" value="controllerName" />' . chr(10) .
161 '<input type="hidden" name="__referrer[actionName]" value="controllerActionName" />' . chr(10);
162 $this->assertEquals($expectedResult, $hiddenFields);
163 }
164
165 /**
166 * @test
167 * @author Bastian Waidelich <bastian@typo3.org>
168 */
169 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
170 $prefix = 'somePrefix';
171
172 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
173 $this->injectDependenciesIntoViewHelper($viewHelper);
174
175 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('fieldNamePrefix' => $prefix)));
176
177 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $prefix);
178 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
179 $viewHelper->render();
180 }
181
182 /**
183 * @test
184 * @author Bastian Waidelich <bastian@typo3.org>
185 */
186 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
187 $expectedPrefix = 'tx_someextension_someplugin';
188 $this->request->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtension'));
189 $this->request->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePlugin'));
190
191 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
192 $this->injectDependenciesIntoViewHelper($viewHelper);
193
194 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
195 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
196 $viewHelper->render();
197 }
198
199 /**
200 * Data Provider for postProcessUriArgumentsForRequestHashWorks
201 */
202 public function argumentsForPostProcessUriArgumentsForRequestHash() {
203 return array(
204 // simple values
205 array(
206 array(
207 'bla' => 'X',
208 'blubb' => 'Y'
209 ),
210 array(
211 'bla',
212 'blubb'
213 )
214 ),
215 // Arrays
216 array(
217 array(
218 'bla' => array(
219 'test1' => 'X',
220 'test2' => 'Y'
221 ),
222 'blubb' => 'Y'
223 ),
224 array(
225 'bla[test1]',
226 'bla[test2]',
227 'blubb'
228 )
229 )
230 );
231 }
232 /**
233 * @test
234 * @author Sebastian Kurfürst <sebastian@typo3.org>
235 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
236 */
237 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
238 $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
239 $results = array();
240 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
241 $this->assertEquals($expectedResults, $results);
242 }
243 }
244 ?>