0e1d4e643c934f496dab3bcb3e5aa5603def0f3e
[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 }
54
55 /**
56 * @test
57 */
58 public function renderAddsObjectToViewHelperVariableContainer() {
59 $formObject = new \stdClass();
60 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
61 $this->injectDependenciesIntoViewHelper($viewHelper);
62 $viewHelper->setArguments(array('object' => $formObject));
63 $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject', $formObject);
64 $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties', array());
65 $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
66 $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties');
67 $viewHelper->render();
68 }
69
70 /**
71 * @test
72 */
73 public function renderAddsObjectNameToTemplateVariableContainer() {
74 $objectName = 'someObjectName';
75 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
76 $this->injectDependenciesIntoViewHelper($viewHelper);
77 $viewHelper->setArguments(array('name' => $objectName));
78 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName', $objectName);
79 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
80 $viewHelper->render();
81 }
82
83 /**
84 * @test
85 */
86 public function formObjectNameArgumentOverrulesNameArgument() {
87 $objectName = 'someObjectName';
88 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
89 $this->injectDependenciesIntoViewHelper($viewHelper);
90 $viewHelper->setArguments(array('name' => 'formName', 'objectName' => $objectName));
91 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName', $objectName);
92 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
93 $viewHelper->render();
94 }
95
96 /**
97 * @test
98 */
99 public function renderCallsRenderHiddenReferrerFields() {
100 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields'), array(), '', FALSE);
101 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
102 $this->injectDependenciesIntoViewHelper($viewHelper);
103 $viewHelper->render();
104 }
105
106 /**
107 * @test
108 */
109 public function renderCallsRenderHiddenIdentityField() {
110 $object = new \stdClass();
111 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenIdentityField', 'getFormObjectName'), array(), '', FALSE);
112 $this->injectDependenciesIntoViewHelper($viewHelper);
113 $viewHelper->setArguments(array('object' => $object));
114 $viewHelper->expects($this->atLeastOnce())->method('getFormObjectName')->will($this->returnValue('MyName'));
115 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
116 $viewHelper->render();
117 }
118
119 /**
120 * @test
121 */
122 public function renderCallsRenderAdditionalIdentityFields() {
123 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderAdditionalIdentityFields'), array(), '', FALSE);
124 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
125 $this->injectDependenciesIntoViewHelper($viewHelper);
126 $viewHelper->render();
127 }
128
129 /**
130 * @test
131 */
132 public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibility() {
133 $viewHelper = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField'), array(), '', FALSE);
134 $this->injectDependenciesIntoViewHelper($viewHelper);
135 $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
136 $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
137 $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
138 $viewHelper->expects($this->once())->method('renderRequestHashField')->will($this->returnValue('requestHashField'));
139 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
140 $expectedResult = chr(10) . '<div style="display: none">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFieldsrequestHashField' . chr(10) . '</div>' . chr(10) . 'formContent';
141 $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
142 $viewHelper->render();
143 }
144
145 /**
146 * @test
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('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
154 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
155 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren'), array(), '', FALSE);
156 $this->injectDependenciesIntoViewHelper($viewHelper);
157 $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) . '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
158 $actual = $viewHelper->_call('renderAdditionalIdentityFields');
159 $this->assertEquals($expected, $actual);
160 }
161
162 /**
163 * @test
164 */
165 public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
166 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
167 $this->injectDependenciesIntoViewHelper($viewHelper);
168 $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
169 $this->request->expects($this->never())->method('getControllerSubextensionName');
170 $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
171 $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
172 $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
173 $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);
174 $this->assertEquals($expectedResult, $hiddenFields);
175 }
176
177 /**
178 * @test
179 */
180 public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
181 $prefix = 'somePrefix';
182 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
183 $this->injectDependenciesIntoViewHelper($viewHelper);
184 $viewHelper->setArguments(array('fieldNamePrefix' => $prefix));
185 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $prefix);
186 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
187 $viewHelper->render();
188 }
189
190 /**
191 * @test
192 */
193 public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
194 $expectedPrefix = 'tx_someextension_someplugin';
195 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
196 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->with('SomeExtension', 'SomePlugin')->will($this->returnValue('tx_someextension_someplugin'));
197 $viewHelper->injectExtensionService($this->mockExtensionService);
198 $this->injectDependenciesIntoViewHelper($viewHelper);
199 $viewHelper->setArguments(array('extensionName' => 'SomeExtension', 'pluginName' => 'SomePlugin'));
200 $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
201 $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
202 $viewHelper->render();
203 }
204
205 /**
206 * Data Provider for postProcessUriArgumentsForRequestHashWorks
207 */
208 public function argumentsForPostProcessUriArgumentsForRequestHash() {
209 return array(
210 // simple values
211 array(
212 array(
213 'bla' => 'X',
214 'blubb' => 'Y'
215 ),
216 array(
217 'bla',
218 'blubb'
219 )
220 ),
221 // Arrays
222 array(
223 array(
224 'bla' => array(
225 'test1' => 'X',
226 'test2' => 'Y'
227 ),
228 'blubb' => 'Y'
229 ),
230 array(
231 'bla[test1]',
232 'bla[test2]',
233 'blubb'
234 )
235 )
236 );
237 }
238
239 /**
240 * @test
241 * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
242 */
243 public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
244 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('dummy'), array(), '', FALSE);
245 $results = array();
246 $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
247 $this->assertEquals($expectedResults, $results);
248 }
249 }
250
251 ?>