[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / AbstractFormFieldViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form;
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 use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
14 use TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper;
15
16 /**
17 * Test for the Abstract Form view helper
18 */
19 class AbstractFormFieldViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase
20 {
21 /**
22 * @test
23 */
24 public function getRespectSubmittedDataValueInitiallyReturnsFalse()
25 {
26 $formViewHelper = $this->getAccessibleMock(
27 AbstractFormFieldViewHelper::class,
28 ['isObjectAccessorMode'],
29 [],
30 '',
31 false
32 );
33 $this->injectDependenciesIntoViewHelper($formViewHelper);
34
35 $this->assertFalse($formViewHelper->_call('getRespectSubmittedDataValue'));
36 }
37
38 /*
39 * @test
40 */
41 public function setRespectSubmittedDataValueToTrue()
42 {
43 $formViewHelper = $this->getAccessibleMock(
44 AbstractFormFieldViewHelper::class,
45 ['isObjectAccessorMode'],
46 [],
47 '',
48 false
49 );
50 $this->injectDependenciesIntoViewHelper($formViewHelper);
51
52 $formViewHelper->_set('respectSubmittedDataValue', true);
53 $this->assertTrue($formViewHelper->_call('getRespectSubmittedDataValue'));
54 }
55
56 /**
57 * @test
58 */
59 public function getValueAttributeBuildsValueFromPropertyAndFormObjectIfInObjectAccessorModeAndRespectSubmittedDataValueSetFalse(
60 ) {
61 $formViewHelper = $this->getAccessibleMock(
62 AbstractFormFieldViewHelper::class,
63 ['isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'],
64 [],
65 '',
66 false
67 );
68 $this->injectDependenciesIntoViewHelper($formViewHelper);
69 $formViewHelper->_set('respectSubmittedDataValue', false);
70
71 $mockObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
72
73 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
74 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
75 ->willReturn($mockObject);
76 $this->viewHelperVariableContainer->exists(
77 FormViewHelper::class,
78 'formObject'
79 )->willReturn(true);
80
81 $arguments = ['name' => null, 'value' => null, 'property' => 'value.something'];
82 $formViewHelper->_set('arguments', $arguments);
83 $expected = 'MyString';
84 $actual = $formViewHelper->_call('getValueAttribute');
85 $this->assertSame($expected, $actual);
86 }
87
88 /**
89 * @test
90 */
91 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode()
92 {
93 $formViewHelper = $this->getAccessibleMock(
94 AbstractFormFieldViewHelper::class,
95 ['isObjectAccessorMode'],
96 [],
97 '',
98 false
99 );
100 $this->injectDependenciesIntoViewHelper($formViewHelper);
101
102 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
103 $this->viewHelperVariableContainer->get(
104 FormViewHelper::class,
105 'formObjectName'
106 )->willReturn('myObjectName');
107 $this->viewHelperVariableContainer->exists(
108 FormViewHelper::class,
109 'fieldNamePrefix'
110 )->willReturn(true);
111 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
112 ->willReturn('formPrefix');
113
114 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
115 $formViewHelper->_set('arguments', $arguments);
116 $expected = 'formPrefix[myObjectName][bla]';
117 $actual = $formViewHelper->_call('getName');
118 $this->assertSame($expected, $actual);
119 }
120
121 /**
122 * @test
123 */
124 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode()
125 {
126 $formViewHelper = $this->getAccessibleMock(
127 AbstractFormFieldViewHelper::class,
128 ['isObjectAccessorMode'],
129 [],
130 '',
131 false
132 );
133 $this->injectDependenciesIntoViewHelper($formViewHelper);
134
135 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
136 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
137 ->willReturn('myObjectName');
138 $this->viewHelperVariableContainer->exists(
139 FormViewHelper::class,
140 'fieldNamePrefix'
141 )->willReturn(true);
142 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
143 ->willReturn('formPrefix');
144
145 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'];
146 $formViewHelper->_set('arguments', $arguments);
147 $expected = 'formPrefix[myObjectName][bla][blubb]';
148 $actual = $formViewHelper->_call('getName');
149 $this->assertSame($expected, $actual);
150 }
151
152 /**
153 * @test
154 */
155 public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified(
156 ) {
157 $formViewHelper = $this->getAccessibleMock(
158 AbstractFormFieldViewHelper::class,
159 ['isObjectAccessorMode'],
160 [],
161 '',
162 false
163 );
164 $this->injectDependenciesIntoViewHelper($formViewHelper);
165
166 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
167 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
168 ->willReturn(null);
169 $this->viewHelperVariableContainer->exists(
170 FormViewHelper::class,
171 'fieldNamePrefix'
172 )->willReturn(true);
173 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
174 ->willReturn('formPrefix');
175
176 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
177 $formViewHelper->_set('arguments', $arguments);
178 $expected = 'formPrefix[bla]';
179 $actual = $formViewHelper->_call('getName');
180 $this->assertSame($expected, $actual);
181 }
182
183 /**
184 * @test
185 */
186 public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode()
187 {
188 $formViewHelper = $this->getAccessibleMock(
189 AbstractFormFieldViewHelper::class,
190 ['isObjectAccessorMode'],
191 [],
192 '',
193 false
194 );
195 $this->injectDependenciesIntoViewHelper($formViewHelper);
196
197 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
198 $this->viewHelperVariableContainer->exists(
199 FormViewHelper::class,
200 'fieldNamePrefix'
201 )->willReturn(true);
202 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'fieldNamePrefix')
203 ->willReturn('formPrefix');
204
205 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
206 $formViewHelper->_set('arguments', $arguments);
207 $expected = 'formPrefix[fieldName]';
208 $actual = $formViewHelper->_call('getName');
209 $this->assertSame($expected, $actual);
210 }
211
212 /**
213 * @test
214 */
215 public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven()
216 {
217 $formViewHelper = $this->getAccessibleMock(
218 AbstractFormFieldViewHelper::class,
219 ['dummy'],
220 [],
221 '',
222 false
223 );
224 $this->injectDependenciesIntoViewHelper($formViewHelper);
225
226 $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObjectName')
227 ->willReturn(true);
228
229 $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => 'bla']);
230 $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
231
232 $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => null]);
233 $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
234 }
235
236 /**
237 * @test
238 */
239 public function addAdditionalIdentityPropertiesIfNeededDoesNotCreateAnythingIfPropertyIsWithoutDot()
240 {
241 $formFieldViewHelper = $this->getAccessibleMock(
242 AbstractFormFieldViewHelper::class,
243 ['renderHiddenIdentityField'],
244 [],
245 '',
246 false
247 );
248 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
249 $arguments = ['property' => 'simple'];
250 $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will(
251 $this->throwException(new \Exception('Should not be executed!!!'))
252 );
253 $formFieldViewHelper->_set('arguments', $arguments);
254 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
255 }
256
257 /**
258 * @test
259 *
260 */
261 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParameters()
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
306 /**
307 * @test
308 */
309 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParametersWithMoreHierarchyLevels(
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
363 /**
364 * @test
365 */
366 public function renderHiddenFieldForEmptyValueRendersHiddenFieldIfItHasNotBeenRenderedBefore()
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
395 /**
396 * @test
397 */
398 public function renderHiddenFieldForEmptyValueAddsHiddenFieldNameToVariableContainerIfItHasBeenRendered()
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
425 /**
426 * @test
427 */
428 public function renderHiddenFieldForEmptyValueDoesNotRenderHiddenFieldIfItHasBeenRenderedBefore()
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
452 /**
453 * @test
454 */
455 public function renderHiddenFieldForEmptyValueRemovesEmptySquareBracketsFromHiddenFieldName()
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
486 /**
487 * @test
488 */
489 public function renderHiddenFieldForEmptyValueDoesNotRemoveNonEmptySquareBracketsFromHiddenFieldName()
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
520 /**
521 * @test
522 */
523 public function getPropertyValueReturnsArrayValueByPropertyPath()
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 }