TYPO3 CMS  TYPO3_6-2
AbstractFormFieldViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
18 
23  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
24  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
25 
26  $className = $this->getUniqueId('Object');
27  $fullClassName = 'TYPO3\\Fluid\\ViewHelpers\\Form\\' . $className;
28  eval('namespace TYPO3\\Fluid\\ViewHelpers\\Form; class ' . $className . ' {
29  public function __clone() {}
30  }');
31  $object = $this->getMock($fullClassName);
32  $object->expects($this->any())->method('FLOW3_Persistence_isNew')->will($this->returnValue(FALSE));
33 
34  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
35  $this->injectDependenciesIntoViewHelper($formViewHelper);
36  $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
37 
38  $arguments = array('name' => 'foo', 'value' => $object, 'property' => NULL);
39  $formViewHelper->_set('arguments', $arguments);
40  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
41 
42  $this->assertSame('foo[__identity]', $formViewHelper->_call('getName'));
43  $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
44  }
45 
50  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
51  $this->injectDependenciesIntoViewHelper($formViewHelper);
52 
53  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
54  $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
55  $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
56  $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
57 
58  $arguments = array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla');
59  $formViewHelper->_set('arguments', $arguments);
60  $expected = 'formPrefix[myObjectName][bla]';
61  $actual = $formViewHelper->_call('getName');
62  $this->assertSame($expected, $actual);
63  }
64 
69  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
70  $this->injectDependenciesIntoViewHelper($formViewHelper);
71 
72  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
73  $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
74  $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
75  $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
76 
77  $arguments = array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb');
78  $formViewHelper->_set('arguments', $arguments);
79  $expected = 'formPrefix[myObjectName][bla][blubb]';
80  $actual = $formViewHelper->_call('getName');
81  $this->assertSame($expected, $actual);
82  }
83 
88  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
89  $this->injectDependenciesIntoViewHelper($formViewHelper);
90 
91  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
92  $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue(NULL));
93  $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
94  $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
95 
96  $arguments = array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla');
97  $formViewHelper->_set('arguments', $arguments);
98  $expected = 'formPrefix[bla]';
99  $actual = $formViewHelper->_call('getName');
100  $this->assertSame($expected, $actual);
101  }
102 
107  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
108  $this->injectDependenciesIntoViewHelper($formViewHelper);
109 
110  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
111  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
112  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
113 
114  $arguments = array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla');
115  $formViewHelper->_set('arguments', $arguments);
116  $expected = 'formPrefix[fieldName]';
117  $actual = $formViewHelper->_call('getName');
118  $this->assertSame($expected, $actual);
119  }
120 
125  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'), array(), '', FALSE);
126  $this->injectDependenciesIntoViewHelper($formViewHelper);
127 
128  $className = $this->getUniqueId('test_');
129  $mockObject = eval('
130  class ' . $className . ' {
131  public function getSomething() {
132  return "MyString";
133  }
134  public function getValue() {
135  return new ' . $className . ';
136  }
137  }
138  return new ' . $className . ';
139  ');
140 
141  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
142  $formViewHelper->expects($this->once())->method('addAdditionalIdentityPropertiesIfNeeded');
143  $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')->will($this->returnValue($mockObject));
144  $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')->will($this->returnValue(TRUE));
145 
146  $arguments = array('name' => NULL, 'value' => NULL, 'property' => 'value.something');
147  $formViewHelper->_set('arguments', $arguments);
148  $expected = 'MyString';
149  $actual = $formViewHelper->_call('getValue');
150  $this->assertSame($expected, $actual);
151  }
152 
157  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
158  $this->injectDependenciesIntoViewHelper($formViewHelper);
159  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
160 
161  $mockArguments = array();
162  $formViewHelper->_set('arguments', $mockArguments);
163 
164  $this->assertNull($formViewHelper->_call('getValue'));
165  }
166 
171  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
172  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
173  $this->injectDependenciesIntoViewHelper($formViewHelper);
174 
175  $mockArguments = array('value' => 'someValue');
176  $formViewHelper->_set('arguments', $mockArguments);
177 
178  $this->assertEquals('someValue', $formViewHelper->_call('getValue'));
179  }
180 
185  $mockObject = $this->getMock('stdClass');
186 
187  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
188  $mockPersistenceManager->expects($this->atLeastOnce())->method('getIdentifierByObject')->with($mockObject)->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
189 
190  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
191  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
192  $this->injectDependenciesIntoViewHelper($formViewHelper);
193  $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
194 
195  $mockArguments = array('value' => $mockObject);
196  $formViewHelper->_set('arguments', $mockArguments);
197 
198  $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
199  }
200 
205  $mockObject = $this->getMock('stdClass');
206 
207  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
208  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
209 
210  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
211  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
212  $this->injectDependenciesIntoViewHelper($formViewHelper);
213  $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
214 
215  $mockArguments = array('value' => $mockObject);
216  $formViewHelper->_set('arguments', $mockArguments);
217 
218  $this->assertSame($mockObject, $formViewHelper->_call('getValue', FALSE));
219  }
220 
225  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
226  $this->injectDependenciesIntoViewHelper($formViewHelper);
227 
228  $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue(TRUE));
229 
230  $formViewHelper->_set('arguments', array('name' => NULL, 'value' => NULL, 'property' => 'bla'));
231  $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
232 
233  $formViewHelper->_set('arguments', array('name' => NULL, 'value' => NULL, 'property' => NULL));
234  $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
235  }
236 
241  $formFieldViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
242  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
243  $arguments = array('property' => 'simple');
244  $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will($this->throwException(new \Exception('Should not be executed!!!')));
245  $formFieldViewHelper->_set('arguments', $arguments);
246  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
247  }
248 
253  $className = $this->getUniqueId('test_');
254  $mockFormObject = eval('
255  class ' . $className . ' {
256  public function getSomething() {
257  return "MyString";
258  }
259  public function getValue() {
260  return new ' . $className . ';
261  }
262  }
263  return new ' . $className . ';
264  ');
265  $property = 'value.something';
266  $objectName = 'myObject';
267  $expectedProperty = 'myObject[value]';
268 
269  $formFieldViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
270  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
271  $arguments = array('property' => $property);
272  $formFieldViewHelper->_set('arguments', $arguments);
273  $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
274  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
275 
276  $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty);
277 
278  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
279  }
280 
285  $className = $this->getUniqueId('test_');
286  $mockFormObject = eval('
287  class ' . $className . ' {
288  public function getSomething() {
289  return "MyString";
290  }
291  public function getValue() {
292  return new ' . $className . ';
293  }
294  }
295  return new ' . $className . ';
296  ');
297  $property = 'value.value.something';
298  $objectName = 'myObject';
299  $expectedProperty1 = 'myObject[value]';
300  $expectedProperty2 = 'myObject[value][value]';
301 
302  $formFieldViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
303  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
304  $arguments = array('property' => $property);
305  $formFieldViewHelper->_set('arguments', $arguments);
306  $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
307  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
308 
309  $formFieldViewHelper->expects($this->at(0))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty1);
310  $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty2);
311 
312  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
313  }
314 
319  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
320  $this->injectDependenciesIntoViewHelper($formViewHelper);
321  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
322  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
323  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
324  $expected = '<input type="hidden" name="SomeFieldName" value="" />';
325  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
326  $this->assertEquals($expected, $actual);
327  }
328 
333  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
334  $this->injectDependenciesIntoViewHelper($formViewHelper);
335  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
336  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
337  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('OldFieldName')));
338  $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', array('OldFieldName', 'NewFieldName'));
339  $formViewHelper->_call('renderHiddenFieldForEmptyValue');
340  }
341 
346  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
347  $this->injectDependenciesIntoViewHelper($formViewHelper);
348  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
349  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
350  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('SomeFieldName')));
351  $this->viewHelperVariableContainer->expects($this->never())->method('addOrUpdate');
352  $expected = '';
353  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
354  $this->assertEquals($expected, $actual);
355  }
356 
361  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
362  $this->injectDependenciesIntoViewHelper($formViewHelper);
363  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][]'));
364  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
365  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
366  $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets]'));
367  $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
368  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
369  $this->assertEquals($expected, $actual);
370  }
371 
376  $formViewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
377  $this->injectDependenciesIntoViewHelper($formViewHelper);
378  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][foo]'));
379  $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
380  $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
381  $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets][foo]'));
382  $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
383  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
384  $this->assertEquals($expected, $actual);
385  }
386 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)