[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / CheckboxViewHelperTest.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 use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder;
16 use TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper;
17
18 /**
19 * Test for the "Checkbox" Form view helper
20 */
21 class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase {
22
23 /**
24 * @var CheckboxViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
25 */
26 protected $viewHelper;
27
28 protected function setUp() {
29 parent::setUp();
30 $this->viewHelper = $this->getAccessibleMock(CheckboxViewHelper::class, array('setErrorClassAttribute', 'getName', 'getValueAttribute', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
31 $this->arguments['property'] = '';
32 $this->injectDependenciesIntoViewHelper($this->viewHelper);
33 $this->viewHelper->initializeArguments();
34 }
35
36 /**
37 * @test
38 */
39 public function renderCorrectlySetsTagNameAndDefaultAttributes() {
40 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
41 $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
42 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
43 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
44 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
45 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
46
47 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
48 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
49 $this->viewHelper->_set('tag', $mockTagBuilder);
50
51 $this->viewHelper->initialize();
52 $this->viewHelper->render();
53 }
54
55 /**
56 * @test
57 */
58 public function renderSetsCheckedAttributeIfSpecified() {
59 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
60 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
61 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
62 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
63 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
64
65 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
66 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
67 $this->viewHelper->_set('tag', $mockTagBuilder);
68
69 $this->viewHelper->initialize();
70 $this->viewHelper->render(TRUE);
71 }
72
73 /**
74 * @test
75 */
76 public function renderIgnoresValueOfBoundPropertyIfCheckedIsSet() {
77 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
78 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
79 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
80 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
81
82 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
83 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
84 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
85 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
86 $this->viewHelper->_set('tag', $mockTagBuilder);
87
88 $this->viewHelper->initialize();
89 $this->viewHelper->render(TRUE);
90 $this->viewHelper->render(FALSE);
91 }
92
93 /**
94 * @test
95 */
96 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean() {
97 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
98 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
99 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
100 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
101 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
102
103 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
104 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
105 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
106 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
107 $this->viewHelper->_set('tag', $mockTagBuilder);
108
109 $this->viewHelper->initialize();
110 $this->viewHelper->render();
111 }
112
113 /**
114 * @test
115 */
116 public function renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray() {
117 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
118 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
119 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
120 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo[]');
121 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
122
123 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
124 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
125 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
126 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array()));
127 $this->viewHelper->_set('tag', $mockTagBuilder);
128
129 $this->viewHelper->initialize();
130 $this->viewHelper->render();
131 }
132
133 /**
134 * @test
135 */
136 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray() {
137 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
138 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
139 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
140 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
141 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
142
143 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
144 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
145 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
146 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array('foo', 'bar', 'baz')));
147 $this->viewHelper->_set('tag', $mockTagBuilder);
148
149 $this->viewHelper->initialize();
150 $this->viewHelper->render();
151 }
152
153 /**
154 * @test
155 */
156 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArrayObject() {
157 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
158 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
159 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
160 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
161 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
162
163 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
164 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
165 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
166 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \ArrayObject(array('foo', 'bar', 'baz'))));
167 $this->viewHelper->_set('tag', $mockTagBuilder);
168
169 $this->viewHelper->initialize();
170 $this->viewHelper->render();
171 }
172
173 /**
174 * @test
175 */
176 public function renderSetsCheckedAttributeIfBoundPropertyIsNotNull() {
177 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
178 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
179 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
180 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
181 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
182
183 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
184 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
185 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
186 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \stdClass()));
187 $this->viewHelper->_set('tag', $mockTagBuilder);
188
189 $this->viewHelper->initialize();
190 $this->viewHelper->render();
191 }
192
193 /**
194 * @test
195 */
196 public function renderSetsCheckedAttributeForListOfObjects() {
197 $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
198 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
199 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
200 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 2);
201 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
202
203 $object1 = new \stdClass();
204 $object2 = new \stdClass();
205 $object3 = new \stdClass();
206
207 $this->viewHelper->expects($this->any())->method('getName')->willReturn('foo');
208 $this->viewHelper->expects($this->any())->method('getValueAttribute')->willReturn(2);
209 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->willReturn(TRUE);
210 $this->viewHelper->expects($this->any())->method('getPropertyValue')->willReturn(array($object1, $object2, $object3));
211 $this->viewHelper->_set('tag', $mockTagBuilder);
212
213 $mockPersistenceManager = $this->getMock(PersistenceManagerInterface::class);
214 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValueMap(array(
215 array($object1, 1),
216 array($object2, 2),
217 array($object3, 3),
218 )));
219 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
220
221 $this->viewHelper->initialize();
222 $this->viewHelper->render();
223 }
224
225 /**
226 * @test
227 */
228 public function renderCallsSetErrorClassAttribute() {
229 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
230 $this->viewHelper->render();
231 }
232
233 }