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