TYPO3 CMS  TYPO3_8-7
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 
16 
21 {
26  {
27  $formViewHelper = $this->getAccessibleMock(
28  AbstractFormFieldViewHelper::class,
29  ['isObjectAccessorMode'],
30  [],
31  '',
32  false
33  );
34  $this->injectDependenciesIntoViewHelper($formViewHelper);
35 
36  $this->assertFalse($formViewHelper->_call('getRespectSubmittedDataValue'));
37  }
38 
39  /*
40  * @test
41  */
43  {
44  $formViewHelper = $this->getAccessibleMock(
45  AbstractFormFieldViewHelper::class,
46  ['isObjectAccessorMode'],
47  [],
48  '',
49  false
50  );
51  $this->injectDependenciesIntoViewHelper($formViewHelper);
52 
53  $formViewHelper->_set('respectSubmittedDataValue', true);
54  $this->assertTrue($formViewHelper->_call('getRespectSubmittedDataValue'));
55  }
56 
61  ) {
62  $formViewHelper = $this->getAccessibleMock(
63  AbstractFormFieldViewHelper::class,
64  ['isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'],
65  [],
66  '',
67  false
68  );
69  $this->injectDependenciesIntoViewHelper($formViewHelper);
70  $formViewHelper->_set('respectSubmittedDataValue', false);
71 
72  $mockObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters('testing', 1476108385);
73 
74  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
75  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
76  ->willReturn($mockObject);
77  $this->viewHelperVariableContainer->exists(
78  FormViewHelper::class,
79  'formObject'
80  )->willReturn(true);
81 
82  $arguments = ['name' => null, 'value' => null, 'property' => 'value.something'];
83  $formViewHelper->_set('arguments', $arguments);
84  $expected = 'MyString';
85  $actual = $formViewHelper->_call('getValueAttribute');
86  $this->assertSame($expected, $actual);
87  }
88 
93  {
94  $formViewHelper = $this->getAccessibleMock(
95  AbstractFormFieldViewHelper::class,
96  ['isObjectAccessorMode'],
97  [],
98  '',
99  false
100  );
101  $this->injectDependenciesIntoViewHelper($formViewHelper);
102 
103  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
104  $this->viewHelperVariableContainer->get(
105  FormViewHelper::class,
106  'formObjectName'
107  )->willReturn('myObjectName');
108  $this->viewHelperVariableContainer->exists(
109  FormViewHelper::class,
110  'fieldNamePrefix'
111  )->willReturn(true);
112  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
113  ->willReturn('formPrefix');
114 
115  $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
116  $formViewHelper->_set('arguments', $arguments);
117  $expected = 'formPrefix[myObjectName][bla]';
118  $actual = $formViewHelper->_call('getName');
119  $this->assertSame($expected, $actual);
120  }
121 
126  {
127  $formViewHelper = $this->getAccessibleMock(
128  AbstractFormFieldViewHelper::class,
129  ['isObjectAccessorMode'],
130  [],
131  '',
132  false
133  );
134  $this->injectDependenciesIntoViewHelper($formViewHelper);
135 
136  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
137  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
138  ->willReturn('myObjectName');
139  $this->viewHelperVariableContainer->exists(
140  FormViewHelper::class,
141  'fieldNamePrefix'
142  )->willReturn(true);
143  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
144  ->willReturn('formPrefix');
145 
146  $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'];
147  $formViewHelper->_set('arguments', $arguments);
148  $expected = 'formPrefix[myObjectName][bla][blubb]';
149  $actual = $formViewHelper->_call('getName');
150  $this->assertSame($expected, $actual);
151  }
152 
157  ) {
158  $formViewHelper = $this->getAccessibleMock(
159  AbstractFormFieldViewHelper::class,
160  ['isObjectAccessorMode'],
161  [],
162  '',
163  false
164  );
165  $this->injectDependenciesIntoViewHelper($formViewHelper);
166 
167  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
168  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
169  ->willReturn(null);
170  $this->viewHelperVariableContainer->exists(
171  FormViewHelper::class,
172  'fieldNamePrefix'
173  )->willReturn(true);
174  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
175  ->willReturn('formPrefix');
176 
177  $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
178  $formViewHelper->_set('arguments', $arguments);
179  $expected = 'formPrefix[bla]';
180  $actual = $formViewHelper->_call('getName');
181  $this->assertSame($expected, $actual);
182  }
183 
188  {
189  $formViewHelper = $this->getAccessibleMock(
190  AbstractFormFieldViewHelper::class,
191  ['isObjectAccessorMode'],
192  [],
193  '',
194  false
195  );
196  $this->injectDependenciesIntoViewHelper($formViewHelper);
197 
198  $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
199  $this->viewHelperVariableContainer->exists(
200  FormViewHelper::class,
201  'fieldNamePrefix'
202  )->willReturn(true);
203  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
204  ->willReturn('formPrefix');
205 
206  $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
207  $formViewHelper->_set('arguments', $arguments);
208  $expected = 'formPrefix[fieldName]';
209  $actual = $formViewHelper->_call('getName');
210  $this->assertSame($expected, $actual);
211  }
212 
217  {
218  $formViewHelper = $this->getAccessibleMock(
219  AbstractFormFieldViewHelper::class,
220  ['dummy'],
221  [],
222  '',
223  false
224  );
225  $this->injectDependenciesIntoViewHelper($formViewHelper);
226 
227  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObjectName')
228  ->willReturn(true);
229 
230  $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => 'bla']);
231  $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
232 
233  $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => null]);
234  $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
235  }
236 
241  {
242  $formFieldViewHelper = $this->getAccessibleMock(
243  AbstractFormFieldViewHelper::class,
244  ['renderHiddenIdentityField'],
245  [],
246  '',
247  false
248  );
249  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
250  $arguments = ['property' => 'simple'];
251  $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will(
252  $this->throwException(new \Exception('Should not be executed!!!', 1476108413))
253  );
254  $formFieldViewHelper->_set('arguments', $arguments);
255  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
256  }
257 
262  {
263  $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
264 
265  $property = 'value.something';
266  $objectName = 'myObject';
267  $expectedProperty = 'myObject[value]';
268 
269  $formFieldViewHelper = $this->getAccessibleMock(
270  AbstractFormFieldViewHelper::class,
271  ['renderHiddenIdentityField', 'isObjectAccessorMode'],
272  [],
273  '',
274  false
275  );
276  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
277  $arguments = ['property' => $property];
278  $formFieldViewHelper->_set('arguments', $arguments);
279  $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
280  $this->returnValue(true)
281  );
282  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
283  ->willReturn(true);
284  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
285  ->willReturn($mockFormObject);
286  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
287  ->willReturn($objectName);
288  $this->viewHelperVariableContainer->get(
289  FormViewHelper::class,
290  'additionalIdentityProperties'
291  )->willReturn([]);
292  $this->viewHelperVariableContainer->addOrUpdate(
293  FormViewHelper::class,
294  'additionalIdentityProperties',
295  [$expectedProperty => null]
296  )->willReturn();
297 
298  $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with(
299  $mockFormObject,
300  $expectedProperty
301  );
302 
303  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
304  }
305 
310  ) {
311  $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
312  $property = 'value.value.something';
313  $objectName = 'myObject';
314  $expectedProperty1 = 'myObject[value]';
315  $expectedProperty2 = 'myObject[value][value]';
316 
317  $formFieldViewHelper = $this->getAccessibleMock(
318  AbstractFormFieldViewHelper::class,
319  ['renderHiddenIdentityField', 'isObjectAccessorMode'],
320  [],
321  '',
322  false
323  );
324  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
325  $arguments = ['property' => $property];
326  $formFieldViewHelper->_set('arguments', $arguments);
327  $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
328  $this->returnValue(true)
329  );
330  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
331  ->willReturn(true);
332  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
333  ->willReturn($mockFormObject);
334  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
335  ->willReturn($objectName);
336  $this->viewHelperVariableContainer->get(
337  FormViewHelper::class,
338  'additionalIdentityProperties'
339  )->willReturn([]);
340  $this->viewHelperVariableContainer->addOrUpdate(
341  FormViewHelper::class,
342  'additionalIdentityProperties',
343  [$expectedProperty1 => null]
344  )->willReturn();
345  $this->viewHelperVariableContainer->addOrUpdate(
346  FormViewHelper::class,
347  'additionalIdentityProperties',
348  [$expectedProperty2 => null]
349  )->willReturn();
350 
351  $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with(
352  $mockFormObject,
353  $expectedProperty1
354  );
355  $formFieldViewHelper->expects($this->at(2))->method('renderHiddenIdentityField')->with(
356  $mockFormObject,
357  $expectedProperty2
358  );
359 
360  $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
361  }
362 
367  {
368  $formViewHelper = $this->getAccessibleMock(
369  AbstractFormFieldViewHelper::class,
370  ['getName'],
371  [],
372  '',
373  false
374  );
375  $this->injectDependenciesIntoViewHelper($formViewHelper);
376  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
377  $this->viewHelperVariableContainer->exists(
378  FormViewHelper::class,
379  'renderedHiddenFields'
380  )->willReturn(true);
381  $this->viewHelperVariableContainer->get(
382  FormViewHelper::class,
383  'renderedHiddenFields'
384  )->willReturn([]);
385  $this->viewHelperVariableContainer->addOrUpdate(
386  FormViewHelper::class,
387  'renderedHiddenFields',
388  ['SomeFieldName']
389  )->willReturn();
390  $expected = '<input type="hidden" name="SomeFieldName" value="" />';
391  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
392  $this->assertEquals($expected, $actual);
393  }
394 
399  {
400  $formViewHelper = $this->getAccessibleMock(
401  AbstractFormFieldViewHelper::class,
402  ['getName'],
403  [],
404  '',
405  false
406  );
407  $this->injectDependenciesIntoViewHelper($formViewHelper);
408  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
409  $this->viewHelperVariableContainer->exists(
410  FormViewHelper::class,
411  'renderedHiddenFields'
412  )->willReturn(true);
413  $this->viewHelperVariableContainer->get(
414  FormViewHelper::class,
415  'renderedHiddenFields'
416  )->willReturn(['OldFieldName']);
417  $this->viewHelperVariableContainer->addOrUpdate(
418  FormViewHelper::class,
419  'renderedHiddenFields',
420  ['OldFieldName', 'NewFieldName']
421  )->willReturn();
422  $formViewHelper->_call('renderHiddenFieldForEmptyValue');
423  }
424 
429  {
430  $formViewHelper = $this->getAccessibleMock(
431  AbstractFormFieldViewHelper::class,
432  ['getName'],
433  [],
434  '',
435  false
436  );
437  $this->injectDependenciesIntoViewHelper($formViewHelper);
438  $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
439  $this->viewHelperVariableContainer->exists(
440  FormViewHelper::class,
441  'renderedHiddenFields'
442  )->willReturn(true);
443  $this->viewHelperVariableContainer->get(
444  FormViewHelper::class,
445  'renderedHiddenFields'
446  )->willReturn(['SomeFieldName']);
447  $expected = '';
448  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
449  $this->assertEquals($expected, $actual);
450  }
451 
456  {
457  $formViewHelper = $this->getAccessibleMock(
458  AbstractFormFieldViewHelper::class,
459  ['getName'],
460  [],
461  '',
462  false
463  );
464  $this->injectDependenciesIntoViewHelper($formViewHelper);
465  $formViewHelper->expects($this->any())->method('getName')->will(
466  $this->returnValue('SomeFieldName[WithBrackets][]')
467  );
468  $this->viewHelperVariableContainer->exists(
469  FormViewHelper::class,
470  'renderedHiddenFields'
471  )->willReturn(true);
472  $this->viewHelperVariableContainer->get(
473  FormViewHelper::class,
474  'renderedHiddenFields'
475  )->willReturn([]);
476  $this->viewHelperVariableContainer->addOrUpdate(
477  FormViewHelper::class,
478  'renderedHiddenFields',
479  ['SomeFieldName[WithBrackets]']
480  )->willReturn();
481  $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
482  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
483  $this->assertEquals($expected, $actual);
484  }
485 
490  {
491  $formViewHelper = $this->getAccessibleMock(
492  AbstractFormFieldViewHelper::class,
493  ['getName'],
494  [],
495  '',
496  false
497  );
498  $this->injectDependenciesIntoViewHelper($formViewHelper);
499  $formViewHelper->expects($this->any())->method('getName')->will(
500  $this->returnValue('SomeFieldName[WithBrackets][foo]')
501  );
502  $this->viewHelperVariableContainer->exists(
503  FormViewHelper::class,
504  'renderedHiddenFields'
505  )->willReturn(true);
506  $this->viewHelperVariableContainer->get(
507  FormViewHelper::class,
508  'renderedHiddenFields'
509  )->willReturn([]);
510  $this->viewHelperVariableContainer->addOrUpdate(
511  FormViewHelper::class,
512  'renderedHiddenFields',
513  ['SomeFieldName[WithBrackets][foo]']
514  )->willReturn();
515  $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
516  $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
517  $this->assertEquals($expected, $actual);
518  }
519 
524  {
525  $formFieldViewHelper = $this->getAccessibleMock(
526  AbstractFormFieldViewHelper::class,
527  ['renderHiddenIdentityField'],
528  [],
529  '',
530  false
531  );
532 
533  $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
534  $formFieldViewHelper->_set('arguments', ['property' => 'key1.key2']);
535 
536  $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
537  ->willReturn(true);
538  $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
539  ->willReturn(['key1' => ['key2' => 'valueX']]);
540 
541  $actual = $formFieldViewHelper->_call('getPropertyValue');
542  $this->assertEquals('valueX', $actual);
543  }
544 }