[~FEATURE] Fluid (ViewHelpers): Adjusted AbstractFormViewHelper to be able to namespa...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / ViewHelpers / FormViewHelperTest_testcase.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 require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
32 class Tx_Fluid_ViewHelpers_FormViewHelperTest_testcase extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
33
34 /**
35 * @test
36 * @author Bastian Waidelich <bastian@typo3.org>
37 */
38 public function renderHiddenIdentityFieldReturnsAHiddenInputFieldContainingTheObjectsUID() {
39 $object = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractDomainObject'), array('_isNew'));
40 $object->_set('uid', '123');
41 $object->expects($this->once())->method('_isNew')->will($this->returnValue(FALSE));
42
43 $expectedResult = chr(10) . '<input type="hidden" name="prefix[theName][uid]" value="123" />' . chr(10);
44
45 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('prefixFieldName'), array(), '', FALSE);
46 $this->injectDependenciesIntoViewHelper($viewHelper);
47 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'theName', 'object' => $object)));
48 $viewHelper->expects($this->any())->method('prefixFieldName')->with('theName')->will($this->returnValue('prefix[theName]'));
49
50 $actualResult = $viewHelper->_call('renderHiddenIdentityField');
51 $this->assertSame($expectedResult, $actualResult);
52 }
53
54 /**
55 * @test
56 * @author Bastian Waidelich <bastian@typo3.org>
57 */
58 public function renderHiddenIdentityFieldReturnsAHiddenInputFieldIfObjectIsNewButAClone() {
59 $object = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractDomainObject'), array('_isNew', '_isClone'));
60 $object->_set('uid', '123');
61 $object->expects($this->once())->method('_isNew')->will($this->returnValue(TRUE));
62 $object->expects($this->once())->method('_isClone')->will($this->returnValue(TRUE));
63
64 $expectedResult = chr(10) . '<input type="hidden" name="prefix[theName][uid]" value="123" />' . chr(10);
65
66 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('prefixFieldName'), array(), '', FALSE);
67 $this->injectDependenciesIntoViewHelper($viewHelper);
68 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'theName', 'object' => $object)));
69 $viewHelper->expects($this->any())->method('prefixFieldName')->with('theName')->will($this->returnValue('prefix[theName]'));
70
71 $actualResult = $viewHelper->_call('renderHiddenIdentityField');
72 $this->assertSame($expectedResult, $actualResult);
73 }
74
75 /**
76 * @test
77 * @author Bastian Waidelich <bastian@typo3.org>
78 */
79 public function renderHiddenIdentityFieldReturnsACommentIfTheObjectIsWithoutIdentity() {
80 $object = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractDomainObject'), array('_isNew'));
81 $object->expects($this->once())->method('_isNew')->will($this->returnValue(FALSE));
82
83 $expectedResult = chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
84
85 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('prefixFieldName'), array(), '', FALSE);
86 $this->injectDependenciesIntoViewHelper($viewHelper);
87 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'theName', 'object' => $object)));
88
89 $actualResult = $viewHelper->_call('renderHiddenIdentityField');
90 $this->assertSame($expectedResult, $actualResult);
91 }
92
93 /**
94 * @test
95 * @author Sebastian Kurfürst <sebastian@typo3.org>
96 */
97 public function renderAddsObjectToTemplateVariableContainer() {
98 $formObject = new stdClass();
99
100 $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'addFormNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer'));
101 $this->injectDependenciesIntoViewHelper($viewHelper);
102
103 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $formObject)));
104 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject', $formObject);
105 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
106 $viewHelper->render();
107 }
108
109 /**
110 * @test
111 * @author Sebastian Kurfürst <sebastian@typo3.org>
112 */
113 public function renderAddsFormNameToTemplateVariableContainer() {
114 $formName = 'someFormName';
115
116 $viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer'));
117 $this->injectDependenciesIntoViewHelper($viewHelper);
118
119 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => $formName)));
120
121 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $formName);
122 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
123 $viewHelper->render();
124 }
125
126 /**
127 * @test
128 * @author Christopher Hlubek <hlubek@networkteam.com>
129 */
130 public function renderCallsRenderHiddenReferrerFields() {
131 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenReferrerFields'));
132 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
133 $this->injectDependenciesIntoViewHelper($viewHelper);
134
135 $viewHelper->render();
136 }
137
138 /**
139 * @test
140 * @author Christopher Hlubek <hlubek@networkteam.com>
141 * @author Bastian Waidelich <bastian@typo3.org>
142 */
143 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
144 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('dummy'), array(), '', FALSE);
145 $this->injectDependenciesIntoViewHelper($viewHelper);
146
147 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
148 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
149 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
150
151 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
152 $expectedResult = chr(10) . '<input type="hidden" name="__referrer[extensionName]" value="extensionName" />' . chr(10) .
153 '<input type="hidden" name="__referrer[controllerName]" value="controllerName" />' . chr(10) .
154 '<input type="hidden" name="__referrer[actionName]" value="controllerActionName" />' . chr(10);
155 $this->assertEquals($expectedResult, $hiddenFields);
156 }
157
158 /**
159 * @test
160 * @author Bastian Waidelich <bastian@typo3.org>
161 */
162 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
163 $prefix = 'somePrefix';
164
165 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields'), array(), '', FALSE);
166 $this->injectDependenciesIntoViewHelper($viewHelper);
167
168 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('fieldNamePrefix' => $prefix)));
169
170 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $prefix);
171 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
172 $viewHelper->render();
173 }
174
175 /**
176 * @test
177 * @author Bastian Waidelich <bastian@typo3.org>
178 */
179 public function renderAddsExtensionPrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
180 $expectedPrefix = 'tx_someextension_someplugin';
181
182 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields'), array(), '', FALSE);
183 $this->injectDependenciesIntoViewHelper($viewHelper);
184
185 $viewHelper->setArguments(
186 new Tx_Fluid_Core_ViewHelper_Arguments(
187 array(
188 'extensionName' => 'someExtension',
189 'pluginName' => 'somePlugin',
190 )
191 )
192 );
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 * @test
201 * @author Bastian Waidelich <bastian@typo3.org>
202 */
203 public function renderUsesCurrentExtensionNameForExtensionPrefixIfNoPrefixAndNoExtensionNameIsSpecified() {
204 $expectedPrefix = 'tx_currentextensionname_someplugin';
205
206 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('currentExtensionName'));
207
208 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields'), array(), '', FALSE);
209 $this->injectDependenciesIntoViewHelper($viewHelper);
210
211 $viewHelper->setArguments(
212 new Tx_Fluid_Core_ViewHelper_Arguments(
213 array(
214 'pluginName' => 'somePlugin',
215 )
216 )
217 );
218
219 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
220 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
221 $viewHelper->render();
222 }
223
224 /**
225 * @test
226 * @author Bastian Waidelich <bastian@typo3.org>
227 */
228 public function renderUsesCurrentPluginNameForExtensionPrefixIfNoPrefixAndNoPluginNameIsSpecified() {
229 $expectedPrefix = 'tx_someextension_currentpluginname';
230
231 $this->request->expects($this->atLeastOnce())->method('getPluginName')->will($this->returnValue('currentPluginName'));
232
233 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields'), array(), '', FALSE);
234 $this->injectDependenciesIntoViewHelper($viewHelper);
235
236 $viewHelper->setArguments(
237 new Tx_Fluid_Core_ViewHelper_Arguments(
238 array(
239 'extensionName' => 'someExtension',
240 )
241 )
242 );
243
244 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
245 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
246 $viewHelper->render();
247 }
248
249 /**
250 * @test
251 * @author Bastian Waidelich <bastian@typo3.org>
252 */
253 public function renderUsesCurrentExtensionNameAndCurrentPluginNameForExtensionPrefixIfNoPrefixAndNoExtensionNameAndNoPluginNameIsSpecified() {
254 $expectedPrefix = 'tx_currentextensionname_currentpluginname';
255
256 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('currentExtensionName'));
257 $this->request->expects($this->atLeastOnce())->method('getPluginName')->will($this->returnValue('currentPluginName'));
258
259 $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields'), array(), '', FALSE);
260 $this->injectDependenciesIntoViewHelper($viewHelper);
261
262 $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array()));
263
264 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
265 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
266 $viewHelper->render();
267 }
268 }
269 ?>