a77daab2e49fc94cf0b6c7acc4f73bfbafef92a4
[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
20 extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase
21 {
22 /**
23 * @test
24 */
25 public function getRespectSubmittedDataValueInitiallyReturnsFalse()
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 */
42 public function setRespectSubmittedDataValueToTrue()
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
57 /**
58 * @test
59 */
60 public function getValueAttributeBuildsValueFromPropertyAndFormObjectIfInObjectAccessorModeAndRespectSubmittedDataValueSetFalse(
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();
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
89 /**
90 * @test
91 */
92 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode()
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
122 /**
123 * @test
124 */
125 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode()
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
153 /**
154 * @test
155 */
156 public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified(
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
184 /**
185 * @test
186 */
187 public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode()
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
213 /**
214 * @test
215 */
216 public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven()
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
237 /**
238 * @test
239 */
240 public function addAdditionalIdentityPropertiesIfNeededDoesNotCreateAnythingIfPropertyIsWithoutDot()
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!!!'))
253 );
254 $formFieldViewHelper->_set('arguments', $arguments);
255 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
256 }
257
258 /**
259 * @test
260 *
261 */
262 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParameters()
263 {
264 $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
265
266 $property = 'value.something';
267 $objectName = 'myObject';
268 $expectedProperty = 'myObject[value]';
269
270 $formFieldViewHelper = $this->getAccessibleMock(
271 AbstractFormFieldViewHelper::class,
272 ['renderHiddenIdentityField', 'isObjectAccessorMode'],
273 [],
274 '',
275 false
276 );
277 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
278 $arguments = ['property' => $property];
279 $formFieldViewHelper->_set('arguments', $arguments);
280 $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
281 $this->returnValue(true)
282 );
283 $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
284 ->willReturn(true);
285 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
286 ->willReturn($mockFormObject);
287 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
288 ->willReturn($objectName);
289 $this->viewHelperVariableContainer->get(
290 FormViewHelper::class,
291 'additionalIdentityProperties'
292 )->willReturn([]);
293 $this->viewHelperVariableContainer->addOrUpdate(
294 FormViewHelper::class,
295 'additionalIdentityProperties',
296 [$expectedProperty => null]
297 )->willReturn();
298
299 $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with(
300 $mockFormObject,
301 $expectedProperty
302 );
303
304 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
305 }
306
307 /**
308 * @test
309 */
310 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParametersWithMoreHierarchyLevels(
311 ) {
312 $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
313 $property = 'value.value.something';
314 $objectName = 'myObject';
315 $expectedProperty1 = 'myObject[value]';
316 $expectedProperty2 = 'myObject[value][value]';
317
318 $formFieldViewHelper = $this->getAccessibleMock(
319 AbstractFormFieldViewHelper::class,
320 ['renderHiddenIdentityField', 'isObjectAccessorMode'],
321 [],
322 '',
323 false
324 );
325 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
326 $arguments = ['property' => $property];
327 $formFieldViewHelper->_set('arguments', $arguments);
328 $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
329 $this->returnValue(true)
330 );
331 $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
332 ->willReturn(true);
333 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
334 ->willReturn($mockFormObject);
335 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObjectName')
336 ->willReturn($objectName);
337 $this->viewHelperVariableContainer->get(
338 FormViewHelper::class,
339 'additionalIdentityProperties'
340 )->willReturn([]);
341 $this->viewHelperVariableContainer->addOrUpdate(
342 FormViewHelper::class,
343 'additionalIdentityProperties',
344 [$expectedProperty1 => null]
345 )->willReturn();
346 $this->viewHelperVariableContainer->addOrUpdate(
347 FormViewHelper::class,
348 'additionalIdentityProperties',
349 [$expectedProperty2 => null]
350 )->willReturn();
351
352 $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with(
353 $mockFormObject,
354 $expectedProperty1
355 );
356 $formFieldViewHelper->expects($this->at(2))->method('renderHiddenIdentityField')->with(
357 $mockFormObject,
358 $expectedProperty2
359 );
360
361 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
362 }
363
364 /**
365 * @test
366 */
367 public function renderHiddenFieldForEmptyValueRendersHiddenFieldIfItHasNotBeenRenderedBefore()
368 {
369 $formViewHelper = $this->getAccessibleMock(
370 AbstractFormFieldViewHelper::class,
371 ['getName'],
372 [],
373 '',
374 false
375 );
376 $this->injectDependenciesIntoViewHelper($formViewHelper);
377 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
378 $this->viewHelperVariableContainer->exists(
379 FormViewHelper::class,
380 'renderedHiddenFields'
381 )->willReturn(true);
382 $this->viewHelperVariableContainer->get(
383 FormViewHelper::class,
384 'renderedHiddenFields'
385 )->willReturn([]);
386 $this->viewHelperVariableContainer->addOrUpdate(
387 FormViewHelper::class,
388 'renderedHiddenFields',
389 ['SomeFieldName']
390 )->willReturn();
391 $expected = '<input type="hidden" name="SomeFieldName" value="" />';
392 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
393 $this->assertEquals($expected, $actual);
394 }
395
396 /**
397 * @test
398 */
399 public function renderHiddenFieldForEmptyValueAddsHiddenFieldNameToVariableContainerIfItHasBeenRendered()
400 {
401 $formViewHelper = $this->getAccessibleMock(
402 AbstractFormFieldViewHelper::class,
403 ['getName'],
404 [],
405 '',
406 false
407 );
408 $this->injectDependenciesIntoViewHelper($formViewHelper);
409 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
410 $this->viewHelperVariableContainer->exists(
411 FormViewHelper::class,
412 'renderedHiddenFields'
413 )->willReturn(true);
414 $this->viewHelperVariableContainer->get(
415 FormViewHelper::class,
416 'renderedHiddenFields'
417 )->willReturn(['OldFieldName']);
418 $this->viewHelperVariableContainer->addOrUpdate(
419 FormViewHelper::class,
420 'renderedHiddenFields',
421 ['OldFieldName', 'NewFieldName']
422 )->willReturn();
423 $formViewHelper->_call('renderHiddenFieldForEmptyValue');
424 }
425
426 /**
427 * @test
428 */
429 public function renderHiddenFieldForEmptyValueDoesNotRenderHiddenFieldIfItHasBeenRenderedBefore()
430 {
431 $formViewHelper = $this->getAccessibleMock(
432 AbstractFormFieldViewHelper::class,
433 ['getName'],
434 [],
435 '',
436 false
437 );
438 $this->injectDependenciesIntoViewHelper($formViewHelper);
439 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
440 $this->viewHelperVariableContainer->exists(
441 FormViewHelper::class,
442 'renderedHiddenFields'
443 )->willReturn(true);
444 $this->viewHelperVariableContainer->get(
445 FormViewHelper::class,
446 'renderedHiddenFields'
447 )->willReturn(['SomeFieldName']);
448 $expected = '';
449 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
450 $this->assertEquals($expected, $actual);
451 }
452
453 /**
454 * @test
455 */
456 public function renderHiddenFieldForEmptyValueRemovesEmptySquareBracketsFromHiddenFieldName()
457 {
458 $formViewHelper = $this->getAccessibleMock(
459 AbstractFormFieldViewHelper::class,
460 ['getName'],
461 [],
462 '',
463 false
464 );
465 $this->injectDependenciesIntoViewHelper($formViewHelper);
466 $formViewHelper->expects($this->any())->method('getName')->will(
467 $this->returnValue('SomeFieldName[WithBrackets][]')
468 );
469 $this->viewHelperVariableContainer->exists(
470 FormViewHelper::class,
471 'renderedHiddenFields'
472 )->willReturn(true);
473 $this->viewHelperVariableContainer->get(
474 FormViewHelper::class,
475 'renderedHiddenFields'
476 )->willReturn([]);
477 $this->viewHelperVariableContainer->addOrUpdate(
478 FormViewHelper::class,
479 'renderedHiddenFields',
480 ['SomeFieldName[WithBrackets]']
481 )->willReturn();
482 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
483 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
484 $this->assertEquals($expected, $actual);
485 }
486
487 /**
488 * @test
489 */
490 public function renderHiddenFieldForEmptyValueDoesNotRemoveNonEmptySquareBracketsFromHiddenFieldName()
491 {
492 $formViewHelper = $this->getAccessibleMock(
493 AbstractFormFieldViewHelper::class,
494 ['getName'],
495 [],
496 '',
497 false
498 );
499 $this->injectDependenciesIntoViewHelper($formViewHelper);
500 $formViewHelper->expects($this->any())->method('getName')->will(
501 $this->returnValue('SomeFieldName[WithBrackets][foo]')
502 );
503 $this->viewHelperVariableContainer->exists(
504 FormViewHelper::class,
505 'renderedHiddenFields'
506 )->willReturn(true);
507 $this->viewHelperVariableContainer->get(
508 FormViewHelper::class,
509 'renderedHiddenFields'
510 )->willReturn([]);
511 $this->viewHelperVariableContainer->addOrUpdate(
512 FormViewHelper::class,
513 'renderedHiddenFields',
514 ['SomeFieldName[WithBrackets][foo]']
515 )->willReturn();
516 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
517 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
518 $this->assertEquals($expected, $actual);
519 }
520
521 /**
522 * @test
523 */
524 public function getPropertyValueReturnsArrayValueByPropertyPath()
525 {
526 $formFieldViewHelper = $this->getAccessibleMock(
527 AbstractFormFieldViewHelper::class,
528 ['renderHiddenIdentityField'],
529 [],
530 '',
531 false
532 );
533
534 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
535 $formFieldViewHelper->_set('arguments', ['property' => 'key1.key2']);
536
537 $this->viewHelperVariableContainer->exists(FormViewHelper::class, 'formObject')
538 ->willReturn(true);
539 $this->viewHelperVariableContainer->get(FormViewHelper::class, 'formObject')
540 ->willReturn(['key1' => ['key2' => 'valueX']]);
541
542 $actual = $formFieldViewHelper->_call('getPropertyValue');
543 $this->assertEquals('valueX', $actual);
544 }
545 }