[FEATURE] ViewHelpers f:form.select.option and f:form.select.optgroup
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / SelectViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
17 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
18
19 /**
20 * Test for the "Select" Form view helper
21 */
22 class SelectViewHelperTest extends ViewHelperBaseTestcase
23 {
24 /**
25 * @var \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
26 */
27 protected $viewHelper;
28
29 protected function setUp()
30 {
31 parent::setUp();
32 $this->arguments['name'] = '';
33 $this->arguments['sortByOptionLabel'] = false;
34 $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper::class, ['setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration', 'renderChildren']);
35 $this->tagBuilder = $this->createMock(\TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder::class);
36 $this->viewHelper->_set('tag', $this->tagBuilder);
37 }
38
39 /**
40 * @test
41 */
42 public function selectCorrectlySetsTagName()
43 {
44 $this->tagBuilder->expects($this->once())->method('setTagName')->with('select');
45
46 $this->arguments['options'] = [];
47 $this->injectDependenciesIntoViewHelper($this->viewHelper);
48 $this->viewHelper->_set('tag', $this->tagBuilder);
49 $this->viewHelper->initialize();
50 $this->viewHelper->render();
51 }
52
53 /**
54 * @test
55 */
56 public function selectCreatesExpectedOptions()
57 {
58 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
59 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
60 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
61 $this->tagBuilder->expects($this->once())->method('render');
62
63 $this->arguments['options'] = [
64 'value1' => 'label1',
65 'value2' => 'label2'
66 ];
67 $this->arguments['value'] = 'value2';
68 $this->arguments['name'] = 'myName';
69
70 $this->injectDependenciesIntoViewHelper($this->viewHelper);
71 $this->viewHelper->_set('tag', $this->tagBuilder);
72 $this->viewHelper->initialize();
73 $this->viewHelper->render();
74 }
75
76 /**
77 * @test
78 */
79 public function selectCreatesExpectedOptionsWithArraysAndOptionValueFieldAndOptionLabelFieldSet()
80 {
81 $this->tagBuilder->expects($this->once())->method('setContent')->with(
82 '<option value="2"></option>' . chr(10) .
83 '<option value="-1">Bar</option>' . chr(10) .
84 '<option value="">Baz</option>' . chr(10) .
85 '<option value="1">Foo</option>' . chr(10)
86 );
87
88 $this->arguments['optionValueField'] = 'uid';
89 $this->arguments['optionLabelField'] = 'title';
90 $this->arguments['sortByOptionLabel'] = true;
91 $this->arguments['options'] = [
92 [
93 'uid' => 1,
94 'title' => 'Foo'
95 ],
96 [
97 'uid' => -1,
98 'title' => 'Bar'
99 ],
100 [
101 'title' => 'Baz'
102 ],
103 [
104 'uid' => '2'
105 ],
106 ];
107
108 $this->injectDependenciesIntoViewHelper($this->viewHelper);
109 $this->viewHelper->_set('tag', $this->tagBuilder);
110 $this->viewHelper->initialize();
111 $this->viewHelper->render();
112 }
113
114 /**
115 * @test
116 */
117 public function selectCreatesExpectedOptionsWithStdClassesAndOptionValueFieldAndOptionLabelFieldSet()
118 {
119 $this->tagBuilder->expects($this->once())->method('setContent')->with(
120 '<option value="2"></option>' . chr(10) .
121 '<option value="-1">Bar</option>' . chr(10) .
122 '<option value="">Baz</option>' . chr(10) .
123 '<option value="1">Foo</option>' . chr(10)
124 );
125
126 $obj1 = new \stdClass();
127 $obj1->uid = 1;
128 $obj1->title = 'Foo';
129
130 $obj2 = new \stdClass();
131 $obj2->uid = -1;
132 $obj2->title = 'Bar';
133
134 $obj3 = new \stdClass();
135 $obj3->title = 'Baz';
136
137 $obj4 = new \stdClass();
138 $obj4->uid = 2;
139
140 $this->arguments['optionValueField'] = 'uid';
141 $this->arguments['optionLabelField'] = 'title';
142 $this->arguments['sortByOptionLabel'] = true;
143 $this->arguments['options'] = [$obj1, $obj2, $obj3, $obj4];
144
145 $this->injectDependenciesIntoViewHelper($this->viewHelper);
146 $this->viewHelper->_set('tag', $this->tagBuilder);
147 $this->viewHelper->initialize();
148 $this->viewHelper->render();
149 }
150
151 /**
152 * @test
153 */
154 public function selectCreatesExpectedOptionsWithArrayObjectsAndOptionValueFieldAndOptionLabelFieldSet()
155 {
156 $this->tagBuilder->expects($this->once())->method('setContent')->with(
157 '<option value="2"></option>' . chr(10) .
158 '<option value="-1">Bar</option>' . chr(10) .
159 '<option value="">Baz</option>' . chr(10) .
160 '<option value="1">Foo</option>' . chr(10)
161 );
162
163 $this->arguments['optionValueField'] = 'uid';
164 $this->arguments['optionLabelField'] = 'title';
165 $this->arguments['sortByOptionLabel'] = true;
166 $this->arguments['options'] = new \ArrayObject([
167 [
168 'uid' => 1,
169 'title' => 'Foo'
170 ],
171 [
172 'uid' => -1,
173 'title' => 'Bar'
174 ],
175 [
176 'title' => 'Baz'
177 ],
178 [
179 'uid' => '2'
180 ],
181 ]);
182
183 $this->injectDependenciesIntoViewHelper($this->viewHelper);
184 $this->viewHelper->_set('tag', $this->tagBuilder);
185 $this->viewHelper->initialize();
186 $this->viewHelper->render();
187 }
188
189 /**
190 * @test
191 */
192 public function anEmptyOptionTagIsRenderedIfOptionsArrayIsEmptyToAssureXhtmlCompatibility()
193 {
194 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
195 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
196 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
197 $this->tagBuilder->expects($this->once())->method('render');
198
199 $this->arguments['options'] = [];
200 $this->arguments['value'] = 'value2';
201 $this->arguments['name'] = 'myName';
202 $this->injectDependenciesIntoViewHelper($this->viewHelper);
203 $this->viewHelper->_set('tag', $this->tagBuilder);
204 $this->viewHelper->initialize();
205 $this->viewHelper->render();
206 }
207
208 /**
209 * @test
210 */
211 public function OrderOfOptionsIsNotAlteredByDefault()
212 {
213 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
214 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
215 $this->tagBuilder->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));
216 $this->tagBuilder->expects($this->once())->method('render');
217
218 $this->arguments['options'] = [
219 'value3' => 'label3',
220 'value1' => 'label1',
221 'value2' => 'label2'
222 ];
223
224 $this->arguments['value'] = 'value2';
225 $this->arguments['name'] = 'myName';
226
227 $this->injectDependenciesIntoViewHelper($this->viewHelper);
228 $this->viewHelper->_set('tag', $this->tagBuilder);
229 $this->viewHelper->initialize();
230 $this->viewHelper->render();
231 }
232
233 /**
234 * @test
235 */
236 public function optionsAreSortedByLabelIfSortByOptionLabelIsSet()
237 {
238 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
239 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
240 $this->tagBuilder->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));
241 $this->tagBuilder->expects($this->once())->method('render');
242
243 $this->arguments['options'] = [
244 'value3' => 'label3',
245 'value1' => 'label1',
246 'value2' => 'label2'
247 ];
248
249 $this->arguments['value'] = 'value2';
250 $this->arguments['name'] = 'myName';
251 $this->arguments['sortByOptionLabel'] = true;
252
253 $this->injectDependenciesIntoViewHelper($this->viewHelper);
254 $this->viewHelper->_set('tag', $this->tagBuilder);
255 $this->viewHelper->initialize();
256 $this->viewHelper->render();
257 }
258
259 /**
260 * @test
261 */
262 public function optionsAreSortedByLabelIfSortByOptionLabelIsSetAndLocaleEqualsUtf8()
263 {
264 $locale = 'de_DE.UTF-8';
265 try {
266 $this->setLocale(LC_COLLATE, $locale);
267 $this->setLocale(LC_CTYPE, $locale);
268 $this->setLocale(LC_MONETARY, $locale);
269 $this->setLocale(LC_TIME, $locale);
270 } catch (\PHPUnit_Framework_Exception $e) {
271 $this->markTestSkipped('Locale ' . $locale . ' is not available.');
272 }
273 if (stristr(PHP_OS, 'Darwin') !== false) {
274 $this->markTestSkipped('Test skipped caused by a bug in the C libraries on BSD/OSX');
275 }
276
277 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
278 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
279 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">Bamberg</option>' . chr(10) . '<option value="value2" selected="selected">Bämm</option>' . chr(10) . '<option value="value3">Bar</option>' . chr(10) . '<option value="value4">Bär</option>' . chr(10) . '<option value="value5">Burg</option>' . chr(10));
280 $this->tagBuilder->expects($this->once())->method('render');
281 $this->arguments['options'] = [
282 'value4' => 'Bär',
283 'value2' => 'Bämm',
284 'value5' => 'Burg',
285 'value1' => 'Bamberg',
286 'value3' => 'Bar'
287 ];
288 $this->arguments['value'] = 'value2';
289 $this->arguments['name'] = 'myName';
290 $this->arguments['sortByOptionLabel'] = true;
291 $this->injectDependenciesIntoViewHelper($this->viewHelper);
292 $this->viewHelper->_set('tag', $this->tagBuilder);
293 $this->viewHelper->initialize();
294 $this->viewHelper->render();
295 }
296
297 /**
298 * @test
299 */
300 public function multipleSelectCreatesExpectedOptions()
301 {
302 $this->tagBuilder = new TagBuilder();
303
304 $this->arguments['options'] = [
305 'value1' => 'label1',
306 'value2' => 'label2',
307 'value3' => 'label3'
308 ];
309
310 $this->arguments['value'] = ['value3', 'value1'];
311 $this->arguments['name'] = 'myName';
312 $this->arguments['multiple'] = true;
313
314 $this->injectDependenciesIntoViewHelper($this->viewHelper);
315 $this->viewHelper->_set('tag', $this->tagBuilder);
316 $this->viewHelper->initializeArguments();
317 $this->viewHelper->initialize();
318 $result = $this->viewHelper->render();
319 $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><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) . '</select>';
320 $this->assertSame($expected, $result);
321 }
322
323 /**
324 * @test
325 */
326 public function selectOnDomainObjectsCreatesExpectedOptions()
327 {
328 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
329 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(2));
330 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
331
332 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName[__identity]');
333 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName[__identity]');
334 $this->tagBuilder->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));
335 $this->tagBuilder->expects($this->once())->method('render');
336
337 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
338 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
339 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
340
341 $this->arguments['options'] = [
342 $user_is,
343 $user_sk,
344 $user_rl
345 ];
346
347 $this->arguments['value'] = $user_sk;
348 $this->arguments['optionValueField'] = 'id';
349 $this->arguments['optionLabelField'] = 'firstName';
350 $this->arguments['name'] = 'myName';
351 $this->injectDependenciesIntoViewHelper($this->viewHelper);
352 $this->viewHelper->_set('tag', $this->tagBuilder);
353 $this->viewHelper->initialize();
354 $this->viewHelper->render();
355 }
356
357 /**
358 * @test
359 */
360 public function multipleSelectOnDomainObjectsCreatesExpectedOptions()
361 {
362 $this->tagBuilder = new TagBuilder();
363 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
364
365 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
366 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
367 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
368 $this->arguments['options'] = [
369 $user_is,
370 $user_sk,
371 $user_rl
372 ];
373 $this->arguments['value'] = [$user_rl, $user_is];
374 $this->arguments['optionValueField'] = 'id';
375 $this->arguments['optionLabelField'] = 'lastName';
376 $this->arguments['name'] = 'myName';
377 $this->arguments['multiple'] = true;
378
379 $this->injectDependenciesIntoViewHelper($this->viewHelper);
380 $this->viewHelper->_set('tag', $this->tagBuilder);
381 $this->viewHelper->initializeArguments();
382 $this->viewHelper->initialize();
383 $actual = $this->viewHelper->render();
384 $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><option value="1" selected="selected">Schlecht</option>' . chr(10) .
385 '<option value="2">Kurfuerst</option>' . chr(10) .
386 '<option value="3" selected="selected">Lemke</option>' . chr(10) .
387 '</select>';
388
389 $this->assertSame($expected, $actual);
390 }
391
392 /**
393 * @test
394 */
395 public function multipleSelectOnDomainObjectsCreatesExpectedOptionsWithoutOptionValueField()
396 {
397 /** @var $mockPersistenceManager \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface */
398 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
399 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnCallback(
400 function ($object) {
401 return $object->getId();
402 }
403 ));
404 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
405
406 $this->tagBuilder = new TagBuilder();
407 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
408
409 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
410 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
411 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
412
413 $this->arguments['options'] = [$user_is, $user_sk, $user_rl];
414 $this->arguments['value'] = [$user_rl, $user_is];
415 $this->arguments['optionLabelField'] = 'lastName';
416 $this->arguments['name'] = 'myName';
417 $this->arguments['multiple'] = true;
418
419 $this->injectDependenciesIntoViewHelper($this->viewHelper);
420 $this->viewHelper->_set('tag', $this->tagBuilder);
421 $this->viewHelper->initializeArguments();
422 $this->viewHelper->initialize();
423 $actual = $this->viewHelper->render();
424 $expected = '<input type="hidden" name="myName" value="" />' .
425 '<select multiple="multiple" name="myName[]">' .
426 '<option value="1" selected="selected">Schlecht</option>' . chr(10) .
427 '<option value="2">Kurfuerst</option>' . chr(10) .
428 '<option value="3" selected="selected">Lemke</option>' . chr(10) .
429 '</select>';
430 $this->assertSame($expected, $actual);
431 }
432
433 /**
434 * @test
435 */
436 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesUuidForValueAndLabel()
437 {
438 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
439 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
440 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
441
442 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
443 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
444 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
445 $this->tagBuilder->expects($this->once())->method('render');
446
447 $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
448
449 $this->arguments['options'] = [
450 $user
451 ];
452 $this->arguments['name'] = 'myName';
453 $this->injectDependenciesIntoViewHelper($this->viewHelper);
454 $this->viewHelper->_set('tag', $this->tagBuilder);
455 $this->viewHelper->initialize();
456 $this->viewHelper->render();
457 }
458
459 /**
460 * @test
461 */
462 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesToStringForLabelIfAvailable()
463 {
464 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
465 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
466 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
467
468 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
469 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
470 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
471 $this->tagBuilder->expects($this->once())->method('render');
472
473 $user = $this->getMockBuilder(\TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass::class)
474 ->setMethods(['__toString'])
475 ->setConstructorArgs([1, 'Ingmar', 'Schlecht'])
476 ->getMock();
477 $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
478
479 $this->arguments['options'] = [
480 $user
481 ];
482 $this->arguments['name'] = 'myName';
483 $this->injectDependenciesIntoViewHelper($this->viewHelper);
484 $this->viewHelper->_set('tag', $this->tagBuilder);
485 $this->viewHelper->initialize();
486 $this->viewHelper->render();
487 }
488
489 /**
490 * @test
491 */
492 public function selectOnDomainObjectsThrowsExceptionIfNoValueCanBeFound()
493 {
494 $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
495 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(null));
496 $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
497
498 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
499 $this->expectExceptionCode(1247826696);
500
501 $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
502
503 $this->arguments['options'] = [
504 $user
505 ];
506 $this->arguments['name'] = 'myName';
507 $this->injectDependenciesIntoViewHelper($this->viewHelper);
508 $this->viewHelper->_set('tag', $this->tagBuilder);
509 $this->viewHelper->initialize();
510 $this->viewHelper->render();
511 }
512
513 /**
514 * @test
515 */
516 public function renderCallsSetErrorClassAttribute()
517 {
518 $this->arguments['options'] = [];
519
520 $this->injectDependenciesIntoViewHelper($this->viewHelper);
521 $this->viewHelper->_set('tag', $this->tagBuilder);
522 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
523 $this->viewHelper->render();
524 }
525
526 /**
527 * @test
528 */
529 public function allOptionsAreSelectedIfSelectAllIsTrue()
530 {
531 $this->tagBuilder->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));
532
533 $this->arguments['options'] = [
534 'value1' => 'label1',
535 'value2' => 'label2',
536 'value3' => 'label3'
537 ];
538 $this->arguments['name'] = 'myName';
539 $this->arguments['multiple'] = true;
540 $this->arguments['selectAllByDefault'] = true;
541
542 $this->injectDependenciesIntoViewHelper($this->viewHelper);
543 $this->viewHelper->_set('tag', $this->tagBuilder);
544 $this->viewHelper->initialize();
545 $this->viewHelper->render();
546 }
547
548 /**
549 * @test
550 */
551 public function selectAllHasNoEffectIfValueIsSet()
552 {
553 $this->tagBuilder->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));
554
555 $this->arguments['options'] = [
556 'value1' => 'label1',
557 'value2' => 'label2',
558 'value3' => 'label3'
559 ];
560 $this->arguments['value'] = ['value2', 'value1'];
561 $this->arguments['name'] = 'myName';
562 $this->arguments['multiple'] = true;
563 $this->arguments['selectAllByDefault'] = true;
564
565 $this->injectDependenciesIntoViewHelper($this->viewHelper);
566 $this->viewHelper->_set('tag', $this->tagBuilder);
567 $this->viewHelper->initialize();
568 $this->viewHelper->render();
569 }
570
571 /**
572 * @test
573 */
574 public function optionsContainPrependedItemWithEmptyValueIfPrependOptionLabelIsSet()
575 {
576 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
577 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
578 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="">please choose</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
579 $this->tagBuilder->expects($this->once())->method('render');
580 $this->arguments['options'] = [
581 'value1' => 'label1',
582 'value2' => 'label2',
583 'value3' => 'label3'
584 ];
585 $this->arguments['name'] = 'myName';
586 $this->arguments['prependOptionLabel'] = 'please choose';
587 $this->injectDependenciesIntoViewHelper($this->viewHelper);
588 $this->viewHelper->_set('tag', $this->tagBuilder);
589 $this->viewHelper->initialize();
590 $this->viewHelper->render();
591 }
592
593 /**
594 * @test
595 */
596 public function optionsContainPrependedItemWithCorrectValueIfPrependOptionLabelAndPrependOptionValueAreSet()
597 {
598 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
599 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
600 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="-1">please choose</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
601 $this->tagBuilder->expects($this->once())->method('render');
602 $this->arguments['options'] = [
603 'value1' => 'label1',
604 'value2' => 'label2',
605 'value3' => 'label3'
606 ];
607 $this->arguments['name'] = 'myName';
608 $this->arguments['prependOptionLabel'] = 'please choose';
609 $this->arguments['prependOptionValue'] = '-1';
610 $this->injectDependenciesIntoViewHelper($this->viewHelper);
611
612 $this->viewHelper->initialize();
613 $this->viewHelper->render();
614 }
615 }