3b208dafcbfa8f961ecd77c547ed0e79971589dc
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / CheckboxViewHelperTest.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
24
25 /**
26 * Test for the "Checkbox" Form view helper
27 *
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 */
30 class Tx_Fluid_Tests_Unit_ViewHelpers_Form_CheckboxViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
31
32 /**
33 * var Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper
34 */
35 protected $viewHelper;
36
37 public function setUp() {
38 parent::setUp();
39 $this->viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper', array('setErrorClassAttribute', 'getName', 'getValue', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
40 $this->injectDependenciesIntoViewHelper($this->viewHelper);
41 $this->viewHelper->initializeArguments();
42 }
43
44 /**
45 * @test
46 * @author Bastian Waidelich <bastian@typo3.org>
47 */
48 public function renderCorrectlySetsTagNameAndDefaultAttributes() {
49 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
50 $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
51 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
52 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
53 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
54 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
55
56 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
57 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
58 $this->viewHelper->_set('tag', $mockTagBuilder);
59
60 $this->viewHelper->initialize();
61 $this->viewHelper->render();
62 }
63
64 /**
65 * @test
66 * @author Bastian Waidelich <bastian@typo3.org>
67 */
68 public function renderSetsCheckedAttributeIfSpecified() {
69 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
70 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
71 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
72 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
73 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
74
75 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
76 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
77 $this->viewHelper->_set('tag', $mockTagBuilder);
78
79 $this->viewHelper->initialize();
80 $this->viewHelper->render(TRUE);
81 }
82
83 /**
84 * @test
85 * @author Bastian Waidelich <bastian@typo3.org>
86 */
87 public function renderIgnoresBoundPropertyIfCheckedIsSet() {
88 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
89 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
90 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
91 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
92
93 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
94 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
95 $this->viewHelper->expects($this->never())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
96 $this->viewHelper->expects($this->never())->method('getPropertyValue')->will($this->returnValue(TRUE));
97 $this->viewHelper->_set('tag', $mockTagBuilder);
98
99 $this->viewHelper->initialize();
100 $this->viewHelper->render(TRUE);
101 $this->viewHelper->render(FALSE);
102 }
103
104 /**
105 * @test
106 * @author Bastian Waidelich <bastian@typo3.org>
107 */
108 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean() {
109 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
110 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
111 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
112 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
113 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
114
115 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
116 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
117 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
118 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
119 $this->viewHelper->_set('tag', $mockTagBuilder);
120
121 $this->viewHelper->initialize();
122 $this->viewHelper->render();
123 }
124
125 /**
126 * @test
127 * @author Bastian Waidelich <bastian@typo3.org>
128 */
129 public function renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray() {
130 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
131 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
132 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
133 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo[]');
134 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
135
136 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
137 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
138 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
139 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array()));
140 $this->viewHelper->_set('tag', $mockTagBuilder);
141
142 $this->viewHelper->initialize();
143 $this->viewHelper->render();
144 }
145
146 /**
147 * @test
148 * @author Bastian Waidelich <bastian@typo3.org>
149 */
150 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray() {
151 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
152 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
153 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
154 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
155 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
156
157 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
158 $this->viewHelper->expects($this->any())->method('getValue')->will($this->returnValue('bar'));
159 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
160 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array('foo', 'bar', 'baz')));
161 $this->viewHelper->_set('tag', $mockTagBuilder);
162
163 $this->viewHelper->initialize();
164 $this->viewHelper->render();
165 }
166
167 /**
168 * @test
169 * @expectedException Tx_Fluid_Core_ViewHelper_Exception
170 * @author Bastian Waidelich <bastian@typo3.org>
171 */
172 public function bindingObjectsToACheckboxThatAreNotOfTypeBooleanOrArrayThrowsException() {
173 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
174
175 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
176 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new stdClass()));
177 $this->viewHelper->_set('tag', $mockTagBuilder);
178
179 $this->viewHelper->initialize();
180 $this->viewHelper->render();
181 }
182
183 /**
184 * @test
185 * @author Bastian Waidelich <bastian@typo3.org>
186 */
187 public function renderCallsSetErrorClassAttribute() {
188 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
189 $this->viewHelper->render();
190 }
191 }
192
193 ?>