25d3a3b11fd1a5600f0952fbe1e8b01e9c618fe5
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / AbstractFormFieldViewHelperTest.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 Lesser General Public License as published by the *
8 * Free 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 Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
24
25 /**
26 * Test for the Abstract Form view helper
27 *
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 */
30 class Tx_Fluid_Tests_Unit_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
31
32 /**
33 * @test
34 * @author Robert Lemke <robert@typo3.org>
35 * @author Bastian Waidelich <bastian@typo3.org>
36 */
37 public function ifAnAttributeValueIsAnObjectMaintainedByThePersistenceManagerItIsConvertedToAUID() { $this->markTestIncomplete("Works differently in v4.");
38 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
39 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
40
41 $className = 'Object' . uniqid();
42 $fullClassName = 'Tx_Fluid_ViewHelpers_Form_' . $className;
43 eval('class ' . $className . ' implements \\F3\\FLOW3\\Persistence\\Aspect\\PersistenceMagicInterface {
44 public function FLOW3_Persistence_isClone() { return FALSE; }
45 public function FLOW3_AOP_Proxy_getProperty($name) {}
46 public function FLOW3_AOP_Proxy_getProxyTargetClassName() {}
47 public function __clone() {}
48 }');
49 $object = $this->getMock($fullClassName);
50 $object->expects($this->any())->method('FLOW3_Persistence_isNew')->will($this->returnValue(FALSE));
51
52 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
53 $this->injectDependenciesIntoViewHelper($formViewHelper);
54 $formViewHelper->injectPersistenceManager($mockPersistenceManager);
55
56 // TODO mock arguments
57 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'foo', 'value' => $object, 'property' => NULL));
58 $formViewHelper->_set('arguments', $arguments);
59 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
60
61 $this->assertSame('foo[__identity]', $formViewHelper->_call('getName'));
62 $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
63 }
64
65 /**
66 * @test
67 * @author Sebastian Kurfürst <sebastian@typo3.org>
68 * @author Bastian Waidelich <bastian@typo3.org>
69 */
70 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode() {
71 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
72 $this->injectDependenciesIntoViewHelper($formViewHelper);
73
74 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
75 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
76 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
77 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
78
79 // TODO mock arguments
80 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
81 $formViewHelper->_set('arguments', $arguments);
82 $expected = 'formPrefix[myObjectName][bla]';
83 $actual = $formViewHelper->_call('getName');
84 $this->assertSame($expected, $actual);
85 }
86
87 /**
88 * @test
89 * @author Sebastian Kurfürst <sebastian@typo3.org>
90 * @author Bastian Waidelich <bastian@typo3.org>
91 */
92 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode() {
93 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
94 $this->injectDependenciesIntoViewHelper($formViewHelper);
95
96 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
97 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
98 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
99 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
100
101 // TODO mock arguments
102 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'));
103 $formViewHelper->_set('arguments', $arguments);
104 $expected = 'formPrefix[myObjectName][bla][blubb]';
105 $actual = $formViewHelper->_call('getName');
106 $this->assertSame($expected, $actual);
107 }
108
109 /**
110 * @test
111 * @author Bastian Waidelich <bastian@typo3.org>
112 */
113 public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified() {
114 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
115 $this->injectDependenciesIntoViewHelper($formViewHelper);
116
117 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
118 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(NULL));
119 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
120 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
121
122 // TODO mock arguments
123 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
124 $formViewHelper->_set('arguments', $arguments);
125 $expected = 'formPrefix[bla]';
126 $actual = $formViewHelper->_call('getName');
127 $this->assertSame($expected, $actual);
128 }
129
130 /**
131 * @test
132 * @author Bastian Waidelich <bastian@typo3.org>
133 */
134 public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode() {
135 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
136 $this->injectDependenciesIntoViewHelper($formViewHelper);
137
138 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
139 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
140 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
141
142 // TODO mock arguments
143 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
144 $formViewHelper->_set('arguments', $arguments);
145 $expected = 'formPrefix[fieldName]';
146 $actual = $formViewHelper->_call('getName');
147 $this->assertSame($expected, $actual);
148 }
149
150 /**
151 * @test
152 * @author Sebastian Kurfürst <sebastian@typo3.org>
153 * @author Bastian Waidelich <bastian@typo3.org>
154 */
155 public function getValueBuildsValueFromPropertyAndFormObjectIfInObjectAccessorMode() {
156 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'), array(), '', FALSE);
157 $this->injectDependenciesIntoViewHelper($formViewHelper);
158
159 $className = 'test_' . uniqid();
160 $mockObject = eval('
161 class ' . $className . ' {
162 public function getSomething() {
163 return "MyString";
164 }
165 public function getValue() {
166 return new ' . $className . ';
167 }
168 }
169 return new ' . $className . ';
170 ');
171
172 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
173 $formViewHelper->expects($this->once())->method('addAdditionalIdentityPropertiesIfNeeded');
174 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockObject));
175 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue(TRUE));
176
177 // TODO mock arguments
178 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'value.something'));
179 $formViewHelper->_set('arguments', $arguments);
180 $expected = 'MyString';
181 $actual = $formViewHelper->_call('getValue');
182 $this->assertSame($expected, $actual);
183 }
184
185 /**
186 * @test
187 * @author Bastian Waidelich <bastian@typo3.org>
188 */
189 public function getValueReturnsNullIfNotInObjectAccessorModeAndValueArgumentIsNoSet() {
190 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
191 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
192
193 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
194 $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(FALSE));
195 $formViewHelper->_set('arguments', $mockArguments);
196
197 $this->assertNull($formViewHelper->_call('getValue'));
198 }
199
200 /**
201 * @test
202 * @author Bastian Waidelich <bastian@typo3.org>
203 */
204 public function getValueReturnsValueArgumentIfSpecified() {
205 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
206 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
207
208 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
209 $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(TRUE));
210 $mockArguments->expects($this->any())->method('offsetGet')->with('value')->will($this->returnValue('someValue'));
211 $formViewHelper->_set('arguments', $mockArguments);
212
213 $this->assertEquals('someValue', $formViewHelper->_call('getValue'));
214 }
215
216 /**
217 * @test
218 * @author Sebastian Kurfürst <sebastian@typo3.org>
219 */
220 public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven() {
221 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
222 $this->injectDependenciesIntoViewHelper($formViewHelper);
223
224 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(TRUE));
225
226 $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'bla')));
227 $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
228
229 $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => NULL)));
230 $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
231 }
232
233 /**
234 * @test
235 * @author Christopher Hlubek <hlubek@networkteam.com>
236 */
237 public function getErrorsForPropertyReturnsErrorsFromRequestIfPropertyIsSet() {
238 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
239 $this->injectDependenciesIntoViewHelper($formViewHelper);
240 $formViewHelper->expects($this->once())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
241 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
242 $mockArguments->expects($this->once())->method('offsetGet')->with('property')->will($this->returnValue('bar'));
243 $formViewHelper->_set('arguments', $mockArguments);
244 $this->viewHelperVariableContainer->expects($this->any())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('foo'));
245
246 $mockArgumentError = $this->getMock('Tx_Extbase_MVC_Controller_ArgumentError', array(), array('foo'));
247 $mockArgumentError->expects($this->once())->method('getPropertyName')->will($this->returnValue('foo'));
248 $mockPropertyError = $this->getMock('Tx_Extbase_Validation_PropertyError', array(), array('bar'));
249 $mockPropertyError->expects($this->once())->method('getPropertyName')->will($this->returnValue('bar'));
250 $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
251 $mockPropertyError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockError)));
252 $mockArgumentError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockPropertyError)));
253 $this->request->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockArgumentError)));
254
255 $errors = $formViewHelper->_call('getErrorsForProperty');
256 $this->assertEquals(array($mockError), $errors);
257 }
258
259 /**
260 * @test
261 * @author Bastian Waidelich <bastian@typo3.org>
262 */
263 public function getErrorsForPropertyReturnsEmptyArrayIfPropertyIsNotSet() {
264 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument'), array(), '', FALSE);
265 $this->injectDependenciesIntoViewHelper($formViewHelper);
266 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
267 $mockArguments->expects($this->once())->method('hasArgument')->with('property')->will($this->returnValue(FALSE));
268 $formViewHelper->_set('arguments', $mockArguments);
269
270 $errors = $formViewHelper->_call('getErrorsForProperty');
271 $this->assertEquals(array(), $errors);
272 }
273
274
275 /**
276 * @test
277 * @author Bastian Waidelich <bastian@typo3.org>
278 */
279 public function setErrorClassAttributeDoesNotSetClassAttributeIfNoErrorOccured() {
280 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
281 $this->injectDependenciesIntoViewHelper($formViewHelper);
282 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
283 $mockArguments->expects($this->once())->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
284 $formViewHelper->_set('arguments', $mockArguments);
285
286 $this->tagBuilder->expects($this->never())->method('addAttribute');
287
288 $formViewHelper->_call('setErrorClassAttribute');
289 }
290
291 /**
292 * @test
293 * @author Bastian Waidelich <bastian@typo3.org>
294 */
295 public function setErrorClassAttributeSetsErrorClassIfAnErrorOccured() {
296 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
297 $this->injectDependenciesIntoViewHelper($formViewHelper);
298 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
299 $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
300 $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
301 $formViewHelper->_set('arguments', $mockArguments);
302
303 $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
304 $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
305
306 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'error');
307
308 $formViewHelper->_call('setErrorClassAttribute');
309 }
310
311 /**
312 * @test
313 * @author Bastian Waidelich <bastian@typo3.org>
314 */
315 public function setErrorClassAttributeAppendsErrorClassToExistingClassesIfAnErrorOccured() {
316 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
317 $this->injectDependenciesIntoViewHelper($formViewHelper);
318 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
319 $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
320 $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
321 $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
322 $formViewHelper->_set('arguments', $mockArguments);
323
324 $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
325 $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
326
327 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes error');
328
329 $formViewHelper->_call('setErrorClassAttribute');
330 }
331
332 /**
333 * @test
334 * @author Bastian Waidelich <bastian@typo3.org>
335 */
336 public function setErrorClassAttributeSetsCustomErrorClassIfAnErrorOccured() {
337 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
338 $this->injectDependenciesIntoViewHelper($formViewHelper);
339 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
340 $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
341 $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
342 $mockArguments->expects($this->at(2))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
343 $formViewHelper->_set('arguments', $mockArguments);
344
345 $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
346 $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
347
348 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'custom-error-class');
349
350 $formViewHelper->_call('setErrorClassAttribute');
351 }
352
353 /**
354 * @test
355 * @author Bastian Waidelich <bastian@typo3.org>
356 */
357 public function setErrorClassAttributeAppendsCustomErrorClassIfAnErrorOccured() {
358 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
359 $this->injectDependenciesIntoViewHelper($formViewHelper);
360 $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
361 $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
362 $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
363 $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
364 $mockArguments->expects($this->at(3))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
365 $formViewHelper->_set('arguments', $mockArguments);
366
367 $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
368 $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
369
370 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes custom-error-class');
371
372 $formViewHelper->_call('setErrorClassAttribute');
373 }
374
375 /**
376 * @test
377 * @author Sebastian Kurfürst <sebastian@typo3.org>
378 */
379 public function addAdditionalIdentityPropertiesIfNeededDoesNotCreateAnythingIfPropertyIsWithoutDot() {
380 $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
381 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
382 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => 'simple'));
383 $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will($this->throwException(new Exception('Should not be executed!!!')));
384 $formFieldViewHelper->_set('arguments', $arguments);
385 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
386 }
387
388 /**
389 * @test
390 * @author Sebastian Kurfürst <sebastian@typo3.org>
391 */
392 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParameters() {
393 $className = 'test_' . uniqid();
394 $mockFormObject = eval('
395 class ' . $className . ' {
396 public function getSomething() {
397 return "MyString";
398 }
399 public function getValue() {
400 return new ' . $className . ';
401 }
402 }
403 return new ' . $className . ';
404 ');
405 $property = 'value.something';
406 $objectName = 'myObject';
407 $expectedProperty = 'myObject[value]';
408
409 $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
410 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
411 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
412 $formFieldViewHelper->_set('arguments', $arguments);
413 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
414 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
415
416 $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty);
417
418 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
419 }
420
421 /**
422 * @test
423 * @author Sebastian Kurfürst <sebastian@typo3.org>
424 */
425 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParametersWithMoreHierarchyLevels() {
426 $className = 'test_' . uniqid();
427 $mockFormObject = eval('
428 class ' . $className . ' {
429 public function getSomething() {
430 return "MyString";
431 }
432 public function getValue() {
433 return new ' . $className . ';
434 }
435 }
436 return new ' . $className . ';
437 ');
438 $property = 'value.value.something';
439 $objectName = 'myObject';
440 $expectedProperty1 = 'myObject[value]';
441 $expectedProperty2 = 'myObject[value][value]';
442
443 $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
444 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
445 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
446 $formFieldViewHelper->_set('arguments', $arguments);
447 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
448 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
449
450 $formFieldViewHelper->expects($this->at(0))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty1);
451 $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty2);
452
453 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
454 }
455
456 /**
457 * @test
458 * @author Bastian Waidelich <bastian@typo3.org>
459 */
460 public function renderHiddenFieldForEmptyValueRendersHiddenFieldIfItHasNotBeenRenderedBefore() {
461 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
462 $this->injectDependenciesIntoViewHelper($formViewHelper);
463
464 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
465 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
466 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
467
468 $expected = '<input type="hidden" name="SomeFieldName" value="" />';
469 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
470 $this->assertEquals($expected, $actual);
471 }
472
473 /**
474 * @test
475 * @author Bastian Waidelich <bastian@typo3.org>
476 */
477 public function renderHiddenFieldForEmptyValueAddsHiddenFieldNameToVariableContainerIfItHasBeenRendered() {
478 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
479 $this->injectDependenciesIntoViewHelper($formViewHelper);
480
481 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
482 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
483 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('OldFieldName')));
484 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('OldFieldName', 'NewFieldName'));
485
486 $formViewHelper->_call('renderHiddenFieldForEmptyValue');
487 }
488
489 /**
490 * @test
491 * @author Bastian Waidelich <bastian@typo3.org>
492 */
493 public function renderHiddenFieldForEmptyValueDoesNotRenderHiddenFieldIfItHasBeenRenderedBefore() {
494 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
495 $this->injectDependenciesIntoViewHelper($formViewHelper);
496
497 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
498 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
499 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('SomeFieldName')));
500 $this->viewHelperVariableContainer->expects($this->never())->method('addOrUpdate');
501
502 $expected = '';
503 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
504 $this->assertEquals($expected, $actual);
505 }
506
507 /**
508 * @test
509 * @author Bastian Waidelich <bastian@typo3.org>
510 */
511 public function renderHiddenFieldForEmptyValueRemovesEmptySquareBracketsFromHiddenFieldName() {
512 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
513 $this->injectDependenciesIntoViewHelper($formViewHelper);
514
515 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][]'));
516 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
517 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
518 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets]'));
519
520 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
521 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
522 $this->assertEquals($expected, $actual);
523 }
524
525 /**
526 * @test
527 * @author Bastian Waidelich <bastian@typo3.org>
528 */
529 public function renderHiddenFieldForEmptyValueDoesNotRemoveNonEmptySquareBracketsFromHiddenFieldName() {
530 $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
531 $this->injectDependenciesIntoViewHelper($formViewHelper);
532
533 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][foo]'));
534 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
535 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
536 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets][foo]'));
537
538 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
539 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
540 $this->assertEquals($expected, $actual);
541 }
542 }
543
544 ?>