a4a26d942ee58f22d9209424bae38ba020933e1c
[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
14 /**
15 * Test for the Abstract Form view helper
16 */
17 class AbstractFormFieldViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase
18 {
19 /**
20 * @test
21 */
22 public function getRespectSubmittedDataValueInitiallyReturnsFalse()
23 {
24 $formViewHelper = $this->getAccessibleMock(
25 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
26 false
27 );
28 $this->injectDependenciesIntoViewHelper($formViewHelper);
29
30 $this->assertFalse($formViewHelper->_call('getRespectSubmittedDataValue'));
31 }
32
33 /**
34 * @test
35 */
36 public function setRespectSubmittedDataValueToTrue()
37 {
38 $formViewHelper = $this->getAccessibleMock(
39 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
40 false
41 );
42 $this->injectDependenciesIntoViewHelper($formViewHelper);
43
44 $formViewHelper->_set('respectSubmittedDataValue', true);
45 $this->assertTrue($formViewHelper->_call('getRespectSubmittedDataValue'));
46 }
47
48 /**
49 * @test
50 */
51 public function getValueAttributeBuildsValueFromPropertyAndFormObjectIfInObjectAccessorModeAndRespectSubmittedDataValueSetFalse()
52 {
53 $formViewHelper = $this->getAccessibleMock(
54 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class,
55 ['isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'], [], '', false
56 );
57 $this->injectDependenciesIntoViewHelper($formViewHelper);
58 $formViewHelper->_set('respectSubmittedDataValue', false);
59
60 $mockObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
61
62 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
63 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with(
64 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
65 )->will($this->returnValue($mockObject));
66 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with(
67 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
68 )->will($this->returnValue(true));
69
70 $arguments = ['name' => null, 'value' => null, 'property' => 'value.something'];
71 $formViewHelper->_set('arguments', $arguments);
72 $expected = 'MyString';
73 $actual = $formViewHelper->_call('getValueAttribute');
74 $this->assertSame($expected, $actual);
75 }
76
77 /**
78 * @test
79 */
80 public function ifAnAttributeValueIsAnObjectMaintainedByThePersistenceManagerItIsConvertedToAUID()
81 {
82 $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
83 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will(
84 $this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66')
85 );
86
87 $object = $this->getMock(\TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\EmptyClass::class);
88 $object->expects($this->any())->method('FLOW3_Persistence_isNew')->will($this->returnValue(false));
89
90 $formViewHelper = $this->getAccessibleMock(
91 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['dummy'], [], '', false
92 );
93 $this->injectDependenciesIntoViewHelper($formViewHelper);
94 $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
95
96 $arguments = ['name' => 'foo', 'value' => $object, 'property' => null];
97 $formViewHelper->_set('arguments', $arguments);
98 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
99
100 $this->assertSame('foo[__identity]', $formViewHelper->_call('getName'));
101 $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
102 }
103
104 /**
105 * @test
106 */
107 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode()
108 {
109 $formViewHelper = $this->getAccessibleMock(
110 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
111 false
112 );
113 $this->injectDependenciesIntoViewHelper($formViewHelper);
114
115 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
116 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with(
117 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
118 )->will($this->returnValue('myObjectName'));
119 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with(
120 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
121 )->will($this->returnValue(true));
122 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with(
123 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
124 )->will($this->returnValue('formPrefix'));
125
126 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
127 $formViewHelper->_set('arguments', $arguments);
128 $expected = 'formPrefix[myObjectName][bla]';
129 $actual = $formViewHelper->_call('getName');
130 $this->assertSame($expected, $actual);
131 }
132
133 /**
134 * @test
135 */
136 public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode()
137 {
138 $formViewHelper = $this->getAccessibleMock(
139 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
140 false
141 );
142 $this->injectDependenciesIntoViewHelper($formViewHelper);
143
144 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
145 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with(
146 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
147 )->will($this->returnValue('myObjectName'));
148 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with(
149 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
150 )->will($this->returnValue(true));
151 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with(
152 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
153 )->will($this->returnValue('formPrefix'));
154
155 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'];
156 $formViewHelper->_set('arguments', $arguments);
157 $expected = 'formPrefix[myObjectName][bla][blubb]';
158 $actual = $formViewHelper->_call('getName');
159 $this->assertSame($expected, $actual);
160 }
161
162 /**
163 * @test
164 */
165 public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified(
166 ) {
167 $formViewHelper = $this->getAccessibleMock(
168 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
169 false
170 );
171 $this->injectDependenciesIntoViewHelper($formViewHelper);
172
173 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
174 $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with(
175 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
176 )->will($this->returnValue(null));
177 $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with(
178 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
179 )->will($this->returnValue(true));
180 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with(
181 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
182 )->will($this->returnValue('formPrefix'));
183
184 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
185 $formViewHelper->_set('arguments', $arguments);
186 $expected = 'formPrefix[bla]';
187 $actual = $formViewHelper->_call('getName');
188 $this->assertSame($expected, $actual);
189 }
190
191 /**
192 * @test
193 */
194 public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode()
195 {
196 $formViewHelper = $this->getAccessibleMock(
197 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
198 false
199 );
200 $this->injectDependenciesIntoViewHelper($formViewHelper);
201
202 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
203 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
204 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
205 )->will($this->returnValue(true));
206 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
207 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix'
208 )->will($this->returnValue('formPrefix'));
209
210 $arguments = ['name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'];
211 $formViewHelper->_set('arguments', $arguments);
212 $expected = 'formPrefix[fieldName]';
213 $actual = $formViewHelper->_call('getName');
214 $this->assertSame($expected, $actual);
215 }
216
217 /**
218 * @test
219 */
220 public function getValueBuildsValueFromPropertyAndFormObjectIfInObjectAccessorMode()
221 {
222 $formViewHelper = $this->getAccessibleMock(
223 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class,
224 ['isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'], [], '', false
225 );
226 $this->injectDependenciesIntoViewHelper($formViewHelper);
227
228 $mockObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
229
230 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
231 $formViewHelper->expects($this->once())->method('addAdditionalIdentityPropertiesIfNeeded');
232 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with(
233 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
234 )->will($this->returnValue($mockObject));
235 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with(
236 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
237 )->will($this->returnValue(true));
238
239 $arguments = ['name' => null, 'value' => null, 'property' => 'value.something'];
240 $formViewHelper->_set('arguments', $arguments);
241 $expected = 'MyString';
242 $actual = $formViewHelper->_call('getValue');
243 $this->assertSame($expected, $actual);
244 }
245
246 /**
247 * @test
248 */
249 public function getValueReturnsNullIfNotInObjectAccessorModeAndValueArgumentIsNoSet()
250 {
251 $formViewHelper = $this->getAccessibleMock(
252 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
253 false
254 );
255 $this->injectDependenciesIntoViewHelper($formViewHelper);
256 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
257
258 $mockArguments = [];
259 $formViewHelper->_set('arguments', $mockArguments);
260
261 $this->assertNull($formViewHelper->_call('getValue'));
262 }
263
264 /**
265 * @test
266 */
267 public function getValueReturnsValueArgumentIfSpecified()
268 {
269 $formViewHelper = $this->getAccessibleMock(
270 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
271 false
272 );
273 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
274 $this->injectDependenciesIntoViewHelper($formViewHelper);
275
276 $mockArguments = ['value' => 'someValue'];
277 $formViewHelper->_set('arguments', $mockArguments);
278
279 $this->assertEquals('someValue', $formViewHelper->_call('getValue'));
280 }
281
282 /**
283 * @test
284 */
285 public function getValueConvertsObjectsToIdentifiersByDefault()
286 {
287 $mockObject = $this->getMock('stdClass');
288
289 $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
290 $mockPersistenceManager->expects($this->atLeastOnce())->method('getIdentifierByObject')->with(
291 $mockObject
292 )->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
293
294 $formViewHelper = $this->getAccessibleMock(
295 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
296 false
297 );
298 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
299 $this->injectDependenciesIntoViewHelper($formViewHelper);
300 $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
301
302 $mockArguments = ['value' => $mockObject];
303 $formViewHelper->_set('arguments', $mockArguments);
304
305 $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
306 }
307
308 /**
309 * @test
310 */
311 public function getValueDoesNotConvertObjectsIfConvertObjectsIsFalse()
312 {
313 $mockObject = $this->getMock('stdClass');
314
315 $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
316 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will(
317 $this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66')
318 );
319
320 $formViewHelper = $this->getAccessibleMock(
321 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['isObjectAccessorMode'], [], '',
322 false
323 );
324 $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(false));
325 $this->injectDependenciesIntoViewHelper($formViewHelper);
326 $formViewHelper->_set('persistenceManager', $mockPersistenceManager);
327
328 $mockArguments = ['value' => $mockObject];
329 $formViewHelper->_set('arguments', $mockArguments);
330
331 $this->assertSame($mockObject, $formViewHelper->_call('getValue', false));
332 }
333
334 /**
335 * @test
336 */
337 public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven()
338 {
339 $formViewHelper = $this->getAccessibleMock(
340 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['dummy'], [], '', false
341 );
342 $this->injectDependenciesIntoViewHelper($formViewHelper);
343
344 $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with(
345 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
346 )->will($this->returnValue(true));
347
348 $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => 'bla']);
349 $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
350
351 $formViewHelper->_set('arguments', ['name' => null, 'value' => null, 'property' => null]);
352 $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
353 }
354
355 /**
356 * @test
357 */
358 public function addAdditionalIdentityPropertiesIfNeededDoesNotCreateAnythingIfPropertyIsWithoutDot()
359 {
360 $formFieldViewHelper = $this->getAccessibleMock(
361 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['renderHiddenIdentityField'], [], '',
362 false
363 );
364 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
365 $arguments = ['property' => 'simple'];
366 $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will(
367 $this->throwException(new \Exception('Should not be executed!!!'))
368 );
369 $formFieldViewHelper->_set('arguments', $arguments);
370 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
371 }
372
373 /**
374 * @test
375 */
376 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParameters()
377 {
378 $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
379 $property = 'value.something';
380 $objectName = 'myObject';
381 $expectedProperty = 'myObject[value]';
382
383 $formFieldViewHelper = $this->getAccessibleMock(
384 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class,
385 ['renderHiddenIdentityField', 'isObjectAccessorMode'], [], '', false
386 );
387 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
388 $arguments = ['property' => $property];
389 $formFieldViewHelper->_set('arguments', $arguments);
390 $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
391 $this->returnValue(true)
392 );
393 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
394 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
395 )->will($this->returnValue(true));
396 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
397 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
398 )->will($this->returnValue($mockFormObject));
399 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with(
400 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
401 )->will($this->returnValue($objectName));
402
403 $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with(
404 $mockFormObject, $expectedProperty
405 );
406
407 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
408 }
409
410 /**
411 * @test
412 */
413 public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParametersWithMoreHierarchyLevels(
414 ) {
415 $mockFormObject = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\ClassWithTwoGetters();
416 $property = 'value.value.something';
417 $objectName = 'myObject';
418 $expectedProperty1 = 'myObject[value]';
419 $expectedProperty2 = 'myObject[value][value]';
420
421 $formFieldViewHelper = $this->getAccessibleMock(
422 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class,
423 ['renderHiddenIdentityField', 'isObjectAccessorMode'], [], '', false
424 );
425 $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
426 $arguments = ['property' => $property];
427 $formFieldViewHelper->_set('arguments', $arguments);
428 $formFieldViewHelper->expects($this->atLeastOnce())->method('isObjectAccessorMode')->will(
429 $this->returnValue(true)
430 );
431 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
432 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
433 )->will($this->returnValue(true));
434 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
435 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject'
436 )->will($this->returnValue($mockFormObject));
437 $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with(
438 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName'
439 )->will($this->returnValue($objectName));
440
441 $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with(
442 $mockFormObject, $expectedProperty1
443 );
444 $formFieldViewHelper->expects($this->at(2))->method('renderHiddenIdentityField')->with(
445 $mockFormObject, $expectedProperty2
446 );
447
448 $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
449 }
450
451 /**
452 * @test
453 */
454 public function renderHiddenFieldForEmptyValueRendersHiddenFieldIfItHasNotBeenRenderedBefore()
455 {
456 $formViewHelper = $this->getAccessibleMock(
457 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['getName'], [], '', false
458 );
459 $this->injectDependenciesIntoViewHelper($formViewHelper);
460 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
461 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
462 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
463 )->will($this->returnValue(true));
464 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
465 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
466 )->will($this->returnValue([]));
467 $expected = '<input type="hidden" name="SomeFieldName" value="" />';
468 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
469 $this->assertEquals($expected, $actual);
470 }
471
472 /**
473 * @test
474 */
475 public function renderHiddenFieldForEmptyValueAddsHiddenFieldNameToVariableContainerIfItHasBeenRendered()
476 {
477 $formViewHelper = $this->getAccessibleMock(
478 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['getName'], [], '', false
479 );
480 $this->injectDependenciesIntoViewHelper($formViewHelper);
481 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
482 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
483 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
484 )->will($this->returnValue(true));
485 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
486 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
487 )->will($this->returnValue(['OldFieldName']));
488 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with(
489 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields', ['OldFieldName', 'NewFieldName']
490 );
491 $formViewHelper->_call('renderHiddenFieldForEmptyValue');
492 }
493
494 /**
495 * @test
496 */
497 public function renderHiddenFieldForEmptyValueDoesNotRenderHiddenFieldIfItHasBeenRenderedBefore()
498 {
499 $formViewHelper = $this->getAccessibleMock(
500 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['getName'], [], '', false
501 );
502 $this->injectDependenciesIntoViewHelper($formViewHelper);
503 $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
504 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
505 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
506 )->will($this->returnValue(true));
507 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
508 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
509 )->will($this->returnValue(['SomeFieldName']));
510 $this->viewHelperVariableContainer->expects($this->never())->method('addOrUpdate');
511 $expected = '';
512 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
513 $this->assertEquals($expected, $actual);
514 }
515
516 /**
517 * @test
518 */
519 public function renderHiddenFieldForEmptyValueRemovesEmptySquareBracketsFromHiddenFieldName()
520 {
521 $formViewHelper = $this->getAccessibleMock(
522 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['getName'], [], '', false
523 );
524 $this->injectDependenciesIntoViewHelper($formViewHelper);
525 $formViewHelper->expects($this->any())->method('getName')->will(
526 $this->returnValue('SomeFieldName[WithBrackets][]')
527 );
528 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
529 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
530 )->will($this->returnValue(true));
531 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
532 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
533 )->will($this->returnValue([]));
534 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with(
535 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields', ['SomeFieldName[WithBrackets]']
536 );
537 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
538 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
539 $this->assertEquals($expected, $actual);
540 }
541
542 /**
543 * @test
544 */
545 public function renderHiddenFieldForEmptyValueDoesNotRemoveNonEmptySquareBracketsFromHiddenFieldName()
546 {
547 $formViewHelper = $this->getAccessibleMock(
548 \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::class, ['getName'], [], '', false
549 );
550 $this->injectDependenciesIntoViewHelper($formViewHelper);
551 $formViewHelper->expects($this->any())->method('getName')->will(
552 $this->returnValue('SomeFieldName[WithBrackets][foo]')
553 );
554 $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with(
555 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
556 )->will($this->returnValue(true));
557 $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with(
558 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields'
559 )->will($this->returnValue([]));
560 $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with(
561 \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'renderedHiddenFields',
562 ['SomeFieldName[WithBrackets][foo]']
563 );
564 $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
565 $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
566 $this->assertEquals($expected, $actual);
567 }
568 }