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