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