Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / AbstractFormViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form;
3
4 /* *
5 * This script is backported from the FLOW3 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 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * Test for the Abstract Form view helper
26 */
27 class AbstractFormViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
28
29 /**
30 * @test
31 */
32 public function renderHiddenIdentityFieldReturnsAHiddenInputFieldContainingTheObjectsUID() {
33 $className = $this->getUniqueId('Object');
34 $fullClassName = '\\TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\' . $className;
35 eval('namespace TYPO3\\CMS\\Fluid\\ViewHelpers\\Form; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {
36 }');
37 $object = $this->getAccessibleMock($fullClassName, array('dummy'));
38 $object->_set('uid', 123);
39 $expectedResult = chr(10) . '<input type="hidden" name="prefix[theName][__identity]" value="123" />' . chr(10);
40 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('prefixFieldName', 'registerFieldNameForFormTokenGeneration'), array(), '', FALSE);
41 $viewHelper->expects($this->any())->method('prefixFieldName')->with('theName')->will($this->returnValue('prefix[theName]'));
42 $actualResult = $viewHelper->_call('renderHiddenIdentityField', $object, 'theName');
43 $this->assertSame($expectedResult, $actualResult);
44 }
45
46 /**
47 * @test
48 */
49 public function renderHiddenIdentityFieldReturnsAHiddenInputFieldIfObjectIsNewButAClone() {
50 $className = $this->getUniqueId('Object');
51 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity {
52 }');
53 $object = $this->getAccessibleMock($className, array('dummy'));
54 $object->_set('uid', 123);
55 $object = clone $object;
56 $expectedResult = chr(10) . '<input type="hidden" name="prefix[theName][__identity]" value="123" />' . chr(10);
57 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', array('prefixFieldName', 'registerFieldNameForFormTokenGeneration'), array(), '', FALSE);
58 $viewHelper->expects($this->any())->method('prefixFieldName')->with('theName')->will($this->returnValue('prefix[theName]'));
59 $actualResult = $viewHelper->_call('renderHiddenIdentityField', $object, 'theName');
60 $this->assertSame($expectedResult, $actualResult);
61 }
62
63 /**
64 * @test
65 */
66 public function prefixFieldNameReturnsEmptyStringIfGivenFieldNameIsNULL() {
67 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormViewHelper', array('dummy'), array(), '', FALSE);
68 $this->injectDependenciesIntoViewHelper($viewHelper);
69 $this->assertSame('', $viewHelper->_call('prefixFieldName', NULL));
70 }
71
72 /**
73 * @test
74 */
75 public function prefixFieldNameReturnsEmptyStringIfGivenFieldNameIsEmpty() {
76 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormViewHelper', array('dummy'), array(), '', FALSE);
77 $this->injectDependenciesIntoViewHelper($viewHelper);
78 $this->assertSame('', $viewHelper->_call('prefixFieldName', ''));
79 }
80
81 /**
82 * @test
83 */
84 public function prefixFieldNameReturnsGivenFieldNameIfFieldNamePrefixIsEmpty() {
85 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormViewHelper', array('dummy'), array(), '', FALSE);
86 $this->injectDependenciesIntoViewHelper($viewHelper);
87 $this->viewHelperVariableContainer->expects($this->any())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
88 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(''));
89 $this->assertSame('someFieldName', $viewHelper->_call('prefixFieldName', 'someFieldName'));
90 }
91
92 /**
93 * @test
94 */
95 public function prefixFieldNamePrefixesGivenFieldNameWithFieldNamePrefix() {
96 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormViewHelper', array('dummy'), array(), '', FALSE);
97 $this->injectDependenciesIntoViewHelper($viewHelper);
98 $this->viewHelperVariableContainer->expects($this->any())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
99 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('somePrefix'));
100 $this->assertSame('somePrefix[someFieldName]', $viewHelper->_call('prefixFieldName', 'someFieldName'));
101 }
102
103 /**
104 * @test
105 */
106 public function prefixFieldNamePreservesSquareBracketsOfFieldName() {
107 $viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\AbstractFormViewHelper', array('dummy'), array(), '', FALSE);
108 $this->injectDependenciesIntoViewHelper($viewHelper);
109 $this->viewHelperVariableContainer->expects($this->any())->method('exists')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
110 $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('somePrefix[foo]'));
111 $this->assertSame('somePrefix[foo][someFieldName][bar]', $viewHelper->_call('prefixFieldName', 'someFieldName[bar]'));
112 }
113 }