[+BUGFIX] Fix unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / SelectViewHelperTest.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__) . '/Fixtures/EmptySyntaxTreeNode.php');
24 require_once(dirname(__FILE__) . '/Fixtures/Fixture_UserDomainClass.php');
25 require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
26
27 /**
28 * Test for the "Select" Form view helper
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class Tx_Fluid_Tests_Unit_ViewHelpers_Form_SelectViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
33
34 /**
35 * var Tx_Fluid_ViewHelpers_Form_SelectViewHelper
36 */
37 protected $viewHelper;
38
39 public function setUp() {
40 parent::setUp();
41 $this->viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_SelectViewHelper', array('setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration'));
42 $this->injectDependenciesIntoViewHelper($this->viewHelper);
43 $this->viewHelper->initializeArguments();
44 }
45
46 /**
47 * @test
48 * @author Bastian Waidelich <bastian@typo3.org>
49 */
50 public function selectCorrectlySetsTagName() {
51 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName'), array(), '', FALSE);
52 $mockTagBuilder->expects($this->once())->method('setTagName')->with('select');
53 $this->viewHelper->injectTagBuilder($mockTagBuilder);
54
55 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
56 'options' => array()
57 ));
58 $this->viewHelper->setArguments($arguments);
59
60 $this->viewHelper->initialize();
61 $this->viewHelper->render();
62 }
63
64 /**
65 * @test
66 * @author Sebastian Kurfürst <sebastian@typo3.org>
67 * @author Bastian Waidelich <bastian@typo3.org>
68 */
69 public function selectCreatesExpectedOptions() {
70 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
71 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
72 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
73 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
74 $mockTagBuilder->expects($this->once())->method('render');
75 $this->viewHelper->injectTagBuilder($mockTagBuilder);
76
77 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
78 'options' => array(
79 'value1' => 'label1',
80 'value2' => 'label2'
81 ),
82 'value' => 'value2',
83 'name' => 'myName'
84 ));
85 $this->viewHelper->setArguments($arguments);
86
87 $this->viewHelper->initialize();
88 $this->viewHelper->render();
89 }
90
91 /**
92 * @test
93 * @author Bastian Waidelich <bastian@typo3.org>
94 */
95 public function anEmptyOptionTagIsRenderedIfOptionsArrayIsEmptyToAssureXhtmlCompatibility() {
96 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
97 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
98 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
99 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
100 $mockTagBuilder->expects($this->once())->method('render');
101 $this->viewHelper->injectTagBuilder($mockTagBuilder);
102
103 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
104 'options' => array(),
105 'value' => 'value2',
106 'name' => 'myName'
107 ));
108 $this->viewHelper->setArguments($arguments);
109
110 $this->viewHelper->initialize();
111 $this->viewHelper->render();
112 }
113
114 /**
115 * @test
116 * @author Bastian Waidelich <bastian@typo3.org>
117 */
118 public function OrderOfOptionsIsNotAlteredByDefault() {
119 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
120 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
121 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
122 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value3">label3</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
123 $mockTagBuilder->expects($this->once())->method('render');
124 $this->viewHelper->injectTagBuilder($mockTagBuilder);
125
126 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
127 'options' => array(
128 'value3' => 'label3',
129 'value1' => 'label1',
130 'value2' => 'label2'
131 ),
132 'value' => 'value2',
133 'name' => 'myName'
134 ));
135 $this->viewHelper->setArguments($arguments);
136
137 $this->viewHelper->initialize();
138 $this->viewHelper->render();
139 }
140
141 /**
142 * @test
143 * @author Bastian Waidelich <bastian@typo3.org>
144 */
145 public function optionsAreSortedByLabelIfSortByOptionLabelIsSet() {
146 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
147 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
148 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
149 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
150 $mockTagBuilder->expects($this->once())->method('render');
151 $this->viewHelper->injectTagBuilder($mockTagBuilder);
152
153 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
154 'options' => array(
155 'value3' => 'label3',
156 'value1' => 'label1',
157 'value2' => 'label2'
158 ),
159 'value' => 'value2',
160 'name' => 'myName',
161 'sortByOptionLabel' => TRUE
162 ));
163 $this->viewHelper->setArguments($arguments);
164
165 $this->viewHelper->initialize();
166 $this->viewHelper->render();
167 }
168
169 /**
170 * @test
171 * @author Bastian Waidelich <bastian@typo3.org>
172 */
173 public function multipleSelectCreatesExpectedOptions() {
174 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
175 $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('multiple', 'multiple');
176 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'myName[]');
177 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
178 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3" selected="selected">label3</option>' . chr(10));
179 $mockTagBuilder->expects($this->once())->method('render');
180 $this->viewHelper->injectTagBuilder($mockTagBuilder);
181
182 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
183 'options' => array(
184 'value1' => 'label1',
185 'value2' => 'label2',
186 'value3' => 'label3'
187 ),
188 'value' => array('value3', 'value1'),
189 'name' => 'myName',
190 'multiple' => 'multiple',
191 ));
192 $this->viewHelper->setArguments($arguments);
193
194 $this->viewHelper->initialize();
195 $this->viewHelper->render();
196 }
197
198 /**
199 * @test
200 * @author Sebastian Kurfürst <sebastian@typo3.org>
201 * @author Bastian Waidelich <bastian@typo3.org>
202 * @author Robert Lemke <robert@typo3.org>
203 */
204 public function selectOnDomainObjectsCreatesExpectedOptions() { $this->markTestIncomplete("TODO - fix test in backporter");
205 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
206 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
207 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
208
209 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
210 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
211 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
212 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="1">Ingmar</option>' . chr(10) . '<option value="2" selected="selected">Sebastian</option>' . chr(10) . '<option value="3">Robert</option>' . chr(10));
213 $mockTagBuilder->expects($this->once())->method('render');
214 $this->viewHelper->injectTagBuilder($mockTagBuilder);
215
216 $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
217 $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
218 $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
219
220 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
221 'options' => array(
222 $user_is,
223 $user_sk,
224 $user_rl
225 ),
226 'value' => $user_sk,
227 'optionValueField' => 'id',
228 'optionLabelField' => 'firstName',
229 'name' => 'myName'
230 ));
231 $this->viewHelper->setArguments($arguments);
232
233 $this->viewHelper->initialize();
234 $this->viewHelper->render();
235 }
236
237 /**
238 * @test
239 * @author Bastian Waidelich <bastian@typo3.org>
240 */
241 public function multipleSelectOnDomainObjectsCreatesExpectedOptions() {
242 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
243 $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('multiple', 'multiple');
244 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'myName[]');
245 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
246 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="1" selected="selected">Schlecht</option>' . chr(10) . '<option value="2">Kurfuerst</option>' . chr(10) . '<option value="3" selected="selected">Lemke</option>' . chr(10));
247 $mockTagBuilder->expects($this->once())->method('render');
248 $this->viewHelper->injectTagBuilder($mockTagBuilder);
249
250 $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
251 $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
252 $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
253
254 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
255 'options' => array(
256 $user_is,
257 $user_sk,
258 $user_rl
259 ),
260 'value' => array($user_rl, $user_is),
261 'optionValueField' => 'id',
262 'optionLabelField' => 'lastName',
263 'name' => 'myName',
264 'multiple' => 'multiple'
265 ));
266 $this->viewHelper->setArguments($arguments);
267
268 $this->viewHelper->initialize();
269 $this->viewHelper->render();
270 }
271
272 /**
273 * @test
274 * @author Karsten Dambekalns <karsten@typo3.org>
275 */
276 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesUuidForValueAndLabel() { $this->markTestIncomplete("TODO - fix test in backporter");
277 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
278 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
279 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
280
281 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
282 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
283 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
284 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
285 $mockTagBuilder->expects($this->once())->method('render');
286 $this->viewHelper->injectTagBuilder($mockTagBuilder);
287
288 $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
289
290 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
291 'options' => array(
292 $user
293 ),
294 'name' => 'myName'
295 ));
296 $this->viewHelper->setArguments($arguments);
297
298 $this->viewHelper->initialize();
299 $this->viewHelper->render();
300 }
301
302 /**
303 * @test
304 * @author Karsten Dambekalns <karsten@typo3.org>
305 */
306 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesToStringForLabelIfAvailable() { $this->markTestIncomplete("TODO - fix test in backporter");
307 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
308 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
309 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
310
311 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
312 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
313 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
314 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
315 $mockTagBuilder->expects($this->once())->method('render');
316 $this->viewHelper->injectTagBuilder($mockTagBuilder);
317
318 $user = $this->getMock('Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass', array('__toString'), array(1, 'Ingmar', 'Schlecht'));
319 $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
320
321 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
322 'options' => array(
323 $user
324 ),
325 'name' => 'myName'
326 ));
327 $this->viewHelper->setArguments($arguments);
328
329 $this->viewHelper->initialize();
330 $this->viewHelper->render();
331 }
332
333 /**
334 * @test
335 * @author Karsten Dambekalns <karsten@typo3.org>
336 * @expectedException Tx_Fluid_Core_ViewHelper_Exception
337 */
338 public function selectOnDomainObjectsThrowsExceptionIfNoValueCanBeFound() { $this->markTestIncomplete("TODO - fix test in backporter");
339 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
340 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
341 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
342
343 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
344 $this->viewHelper->injectTagBuilder($mockTagBuilder);
345
346 $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
347
348 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
349 'options' => array(
350 $user
351 ),
352 'name' => 'myName'
353 ));
354 $this->viewHelper->setArguments($arguments);
355
356 $this->viewHelper->initialize();
357 $this->viewHelper->render();
358 }
359
360 /**
361 * @test
362 * @author Bastian Waidelich <bastian@typo3.org>
363 */
364 public function renderCallsSetErrorClassAttribute() {
365 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
366 'options' => array()
367 ));
368 $this->viewHelper->setArguments($arguments);
369
370 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
371 $this->viewHelper->render();
372 }
373
374 /**
375 * @test
376 * @author Bastian Waidelich <bastian@typo3.org>
377 */
378 public function allOptionsAreSelectedIfSelectAllIsTrue() {
379 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
380 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3" selected="selected">label3</option>' . chr(10));
381 $this->viewHelper->injectTagBuilder($mockTagBuilder);
382
383 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
384 'options' => array(
385 'value1' => 'label1',
386 'value2' => 'label2',
387 'value3' => 'label3'
388 ),
389 'name' => 'myName',
390 'multiple' => 'multiple',
391 'selectAllByDefault' => TRUE
392 ));
393 $this->viewHelper->setArguments($arguments);
394
395 $this->viewHelper->initialize();
396 $this->viewHelper->render();
397 }
398
399 /**
400 * @test
401 * @author Bastian Waidelich <bastian@typo3.org>
402 */
403 public function selectAllHasNoEffectIfValueIsSet() {
404 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
405 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="value1" selected="selected">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
406 $this->viewHelper->injectTagBuilder($mockTagBuilder);
407
408 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
409 'options' => array(
410 'value1' => 'label1',
411 'value2' => 'label2',
412 'value3' => 'label3'
413 ),
414 'value' => array('value2', 'value1'),
415 'name' => 'myName',
416 'multiple' => 'multiple',
417 'selectAllByDefault' => TRUE
418 ));
419 $this->viewHelper->setArguments($arguments);
420
421 $this->viewHelper->initialize();
422 $this->viewHelper->render();
423 }
424 }
425
426 ?>