[BUGFIX] Enable CheckboxViewHelper binding to ArrayObject and Null
[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 require_once(__DIR__ . '/FormFieldViewHelperBaseTestcase.php');
15
16 /**
17 * Test for the "Checkbox" Form view helper
18 */
19 class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase {
20
21 /**
22 * @var \TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper
23 */
24 protected $viewHelper;
25
26 public function setUp() {
27 parent::setUp();
28 $this->viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\CheckboxViewHelper', array('setErrorClassAttribute', 'getName', 'getValue', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
29 $this->arguments['property'] = '';
30 $this->injectDependenciesIntoViewHelper($this->viewHelper);
31 $this->viewHelper->initializeArguments();
32 }
33
34 /**
35 * @test
36 */
37 public function renderCorrectlySetsTagNameAndDefaultAttributes() {
38 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
39 $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
40 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
41 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
42 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
43 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
44
45 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
46 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
47 $this->viewHelper->injectTagBuilder($mockTagBuilder);
48
49 $this->viewHelper->initialize();
50 $this->viewHelper->render();
51 }
52
53 /**
54 * @test
55 */
56 public function renderSetsCheckedAttributeIfSpecified() {
57 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
58 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
59 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
60 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
61 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
62
63 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
64 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
65 $this->viewHelper->injectTagBuilder($mockTagBuilder);
66
67 $this->viewHelper->initialize();
68 $this->viewHelper->render(TRUE);
69 }
70
71 /**
72 * @test
73 */
74 public function renderIgnoresValueOfBoundPropertyIfCheckedIsSet() {
75 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
76 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
77 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
78 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
79
80 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
81 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
82 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
83 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
84 $this->viewHelper->injectTagBuilder($mockTagBuilder);
85
86 $this->viewHelper->initialize();
87 $this->viewHelper->render(TRUE);
88 $this->viewHelper->render(FALSE);
89 }
90
91 /**
92 * @test
93 */
94 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean() {
95 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
96 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
97 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
98 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
99 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
100
101 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
102 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
103 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
104 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
105 $this->viewHelper->injectTagBuilder($mockTagBuilder);
106
107 $this->viewHelper->initialize();
108 $this->viewHelper->render();
109 }
110
111 /**
112 * @test
113 */
114 public function renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray() {
115 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
116 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
117 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
118 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo[]');
119 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
120
121 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
122 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
123 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
124 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array()));
125 $this->viewHelper->injectTagBuilder($mockTagBuilder);
126
127 $this->viewHelper->initialize();
128 $this->viewHelper->render();
129 }
130
131 /**
132 * @test
133 */
134 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray() {
135 $mockTagBuilder = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TagBuilder', array('setTagName', 'addAttribute'));
136 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
137 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
138 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
139 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
140
141 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
142 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
143 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
144 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array('foo', 'bar', 'baz')));
145 $this->viewHelper->injectTagBuilder($mockTagBuilder);
146
147 $this->viewHelper->initialize();
148 $this->viewHelper->render();
149 }
150
151 /**
152 * @test
153 */
154 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArrayObject() {
155 $mockTagBuilder = $this->getMock('TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder', array('setTagName', 'addAttribute'));
156 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
157 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
158 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
159 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
160
161 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
162 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
163 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
164 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \ArrayObject(array('foo', 'bar', 'baz'))));
165 $this->viewHelper->injectTagBuilder($mockTagBuilder);
166
167 $this->viewHelper->initialize();
168 $this->viewHelper->render();
169 }
170
171 /**
172 * @test
173 */
174 public function renderSetsCheckedAttributeIfBoundPropertyIsNotNull() {
175 $mockTagBuilder = $this->getMock('TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder', array('setTagName', 'addAttribute'));
176 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
177 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
178 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
179 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
180
181 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
182 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
183 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
184 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \stdClass()));
185 $this->viewHelper->injectTagBuilder($mockTagBuilder);
186
187 $this->viewHelper->initialize();
188 $this->viewHelper->render();
189 }
190
191 /**
192 * @test
193 */
194 public function renderCallsSetErrorClassAttribute() {
195 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
196 $this->viewHelper->render();
197 }
198 }
199
200 ?>