4793f638732b4d44325d26e3c32fdc0382e82343
[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->_set('tag', $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->_set('tag', $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->_set('tag', $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->_set('tag', $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->_set('tag', $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->_set('tag', $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() {
205 $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
206 $mockPersistenceBackend->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
207
208 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
209 $mockPersistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
210 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
211
212 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
213 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
214 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
215 $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));
216 $mockTagBuilder->expects($this->once())->method('render');
217 $this->viewHelper->_set('tag', $mockTagBuilder);
218
219 $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
220 $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
221 $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
222
223 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
224 'options' => array(
225 $user_is,
226 $user_sk,
227 $user_rl
228 ),
229 'value' => $user_sk,
230 'optionValueField' => 'id',
231 'optionLabelField' => 'firstName',
232 'name' => 'myName'
233 ));
234 $this->viewHelper->setArguments($arguments);
235
236 $this->viewHelper->initialize();
237 $this->viewHelper->render();
238 }
239
240 /**
241 * @test
242 * @author Bastian Waidelich <bastian@typo3.org>
243 */
244 public function multipleSelectOnDomainObjectsCreatesExpectedOptions() {
245 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
246 $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('multiple', 'multiple');
247 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'myName[]');
248 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
249 $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));
250 $mockTagBuilder->expects($this->once())->method('render');
251 $this->viewHelper->_set('tag', $mockTagBuilder);
252
253 $user_is = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
254 $user_sk = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
255 $user_rl = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(3, 'Robert', 'Lemke');
256
257 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
258 'options' => array(
259 $user_is,
260 $user_sk,
261 $user_rl
262 ),
263 'value' => array($user_rl, $user_is),
264 'optionValueField' => 'id',
265 'optionLabelField' => 'lastName',
266 'name' => 'myName',
267 'multiple' => 'multiple'
268 ));
269 $this->viewHelper->setArguments($arguments);
270
271 $this->viewHelper->initialize();
272 $this->viewHelper->render();
273 }
274
275 /**
276 * @test
277 * @author Karsten Dambekalns <karsten@typo3.org>
278 */
279 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesUuidForValueAndLabel() { $this->markTestIncomplete("This does not work right now due to a renaming in FLOW3.");
280 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
281 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
282 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
283
284 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
285 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
286 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
287 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
288 $mockTagBuilder->expects($this->once())->method('render');
289 $this->viewHelper->_set('tag', $mockTagBuilder);
290
291 $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
292
293 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
294 'options' => array(
295 $user
296 ),
297 'name' => 'myName'
298 ));
299 $this->viewHelper->setArguments($arguments);
300
301 $this->viewHelper->initialize();
302 $this->viewHelper->render();
303 }
304
305 /**
306 * @test
307 * @author Karsten Dambekalns <karsten@typo3.org>
308 */
309 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesToStringForLabelIfAvailable() { $this->markTestIncomplete("This does not work right now due to a renaming in FLOW3.");
310 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
311 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
312 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
313
314 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
315 $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
316 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
317 $mockTagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
318 $mockTagBuilder->expects($this->once())->method('render');
319 $this->viewHelper->_set('tag', $mockTagBuilder);
320
321 $user = $this->getMock('Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass', array('__toString'), array(1, 'Ingmar', 'Schlecht'));
322 $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
323
324 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
325 'options' => array(
326 $user
327 ),
328 'name' => 'myName'
329 ));
330 $this->viewHelper->setArguments($arguments);
331
332 $this->viewHelper->initialize();
333 $this->viewHelper->render();
334 }
335
336 /**
337 * @test
338 * @author Karsten Dambekalns <karsten@typo3.org>
339 * @expectedException Tx_Fluid_Core_ViewHelper_Exception
340 */
341 public function selectOnDomainObjectsThrowsExceptionIfNoValueCanBeFound() {
342 $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
343 $mockPersistenceBackend->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
344
345 $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
346 $mockPersistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
347 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
348
349 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
350 $this->viewHelper->_set('tag', $mockTagBuilder);
351
352 $user = new Tx_Fluid_ViewHelpers_Fixtures_UserDomainClass(1, 'Ingmar', 'Schlecht');
353
354 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
355 'options' => array(
356 $user
357 ),
358 'name' => 'myName'
359 ));
360 $this->viewHelper->setArguments($arguments);
361
362 $this->viewHelper->initialize();
363 $this->viewHelper->render();
364 }
365
366 /**
367 * @test
368 * @author Bastian Waidelich <bastian@typo3.org>
369 */
370 public function renderCallsSetErrorClassAttribute() {
371 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
372 'options' => array()
373 ));
374 $this->viewHelper->setArguments($arguments);
375
376 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
377 $this->viewHelper->render();
378 }
379
380 /**
381 * @test
382 * @author Bastian Waidelich <bastian@typo3.org>
383 */
384 public function allOptionsAreSelectedIfSelectAllIsTrue() {
385 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
386 $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));
387 $this->viewHelper->_set('tag', $mockTagBuilder);
388
389 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
390 'options' => array(
391 'value1' => 'label1',
392 'value2' => 'label2',
393 'value3' => 'label3'
394 ),
395 'name' => 'myName',
396 'multiple' => 'multiple',
397 'selectAllByDefault' => TRUE
398 ));
399 $this->viewHelper->setArguments($arguments);
400
401 $this->viewHelper->initialize();
402 $this->viewHelper->render();
403 }
404
405 /**
406 * @test
407 * @author Bastian Waidelich <bastian@typo3.org>
408 */
409 public function selectAllHasNoEffectIfValueIsSet() {
410 $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('addAttribute', 'setContent', 'render'), array(), '', FALSE);
411 $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));
412 $this->viewHelper->_set('tag', $mockTagBuilder);
413
414 $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array(
415 'options' => array(
416 'value1' => 'label1',
417 'value2' => 'label2',
418 'value3' => 'label3'
419 ),
420 'value' => array('value2', 'value1'),
421 'name' => 'myName',
422 'multiple' => 'multiple',
423 'selectAllByDefault' => TRUE
424 ));
425 $this->viewHelper->setArguments($arguments);
426
427 $this->viewHelper->initialize();
428 $this->viewHelper->render();
429 }
430 }
431
432 ?>