17 use PHPUnit\Framework\Exception;
21 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
34 protected function setUp()
37 $this->arguments[
'name'] =
'';
38 $this->arguments[
'sortByOptionLabel'] =
false;
39 $this->viewHelper = $this->getAccessibleMock(SelectViewHelper::class, [
'setErrorClassAttribute',
'registerFieldNameForFormTokenGeneration',
'renderChildren']);
40 $this->tagBuilder = $this->createMock(TagBuilder::class);
41 $this->viewHelper->setTagBuilder($this->tagBuilder);
49 $this->tagBuilder->expects($this->atLeastOnce())->method(
'setTagName')->with(
'select');
51 $this->arguments[
'options'] = [];
52 $this->injectDependenciesIntoViewHelper($this->viewHelper);
53 $this->viewHelper->setTagBuilder($this->tagBuilder);
54 $this->viewHelper->initializeArgumentsAndRender();
62 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
63 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
64 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
'<option value="value1">label1</option>' . \chr(10) .
'<option value="value2" selected="selected">label2</option>' . \chr(10));
65 $this->tagBuilder->expects($this->once())->method(
'render');
67 $this->arguments[
'options'] = [
71 $this->arguments[
'value'] =
'value2';
72 $this->arguments[
'name'] =
'myName';
74 $this->injectDependenciesIntoViewHelper($this->viewHelper);
75 $this->viewHelper->setTagBuilder($this->tagBuilder);
76 $this->viewHelper->initializeArgumentsAndRender();
84 $this->tagBuilder->expects($this->exactly(2))->method(
'addAttribute')->withConsecutive(
85 [
'required',
'required'],
88 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
89 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
'<option value="value1">label1</option>' . \chr(10) .
'<option value="value2" selected="selected">label2</option>' . \chr(10));
90 $this->tagBuilder->expects($this->once())->method(
'render');
92 $this->arguments[
'options'] = [
96 $this->arguments[
'value'] =
'value2';
97 $this->arguments[
'name'] =
'myName';
98 $this->arguments[
'required'] =
true;
100 $this->injectDependenciesIntoViewHelper($this->viewHelper);
101 $this->viewHelper->setTagBuilder($this->tagBuilder);
102 $this->viewHelper->initializeArgumentsAndRender();
110 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
111 '<option value="2"></option>' . \chr(10) .
112 '<option value="-1">Bar</option>' . \chr(10) .
113 '<option value="">Baz</option>' . \chr(10) .
114 '<option value="1">Foo</option>' . \chr(10)
117 $this->arguments[
'optionValueField'] =
'uid';
118 $this->arguments[
'optionLabelField'] =
'title';
119 $this->arguments[
'sortByOptionLabel'] =
true;
120 $this->arguments[
'options'] = [
137 $this->injectDependenciesIntoViewHelper($this->viewHelper);
138 $this->viewHelper->setTagBuilder($this->tagBuilder);
139 $this->viewHelper->initializeArgumentsAndRender();
147 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
148 '<option value="2"></option>' . \chr(10) .
149 '<option value="-1">Bar</option>' . \chr(10) .
150 '<option value="">Baz</option>' . \chr(10) .
151 '<option value="1">Foo</option>' . \chr(10)
154 $obj1 = new \stdClass();
156 $obj1->title =
'Foo';
158 $obj2 = new \stdClass();
160 $obj2->title =
'Bar';
162 $obj3 = new \stdClass();
163 $obj3->title =
'Baz';
165 $obj4 = new \stdClass();
168 $this->arguments[
'optionValueField'] =
'uid';
169 $this->arguments[
'optionLabelField'] =
'title';
170 $this->arguments[
'sortByOptionLabel'] =
true;
171 $this->arguments[
'options'] = [$obj1, $obj2, $obj3, $obj4];
173 $this->injectDependenciesIntoViewHelper($this->viewHelper);
174 $this->viewHelper->setTagBuilder($this->tagBuilder);
175 $this->viewHelper->initializeArgumentsAndRender();
183 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
184 '<option value="2"></option>' . \chr(10) .
185 '<option value="-1">Bar</option>' . \chr(10) .
186 '<option value="">Baz</option>' . \chr(10) .
187 '<option value="1">Foo</option>' . \chr(10)
190 $this->arguments[
'optionValueField'] =
'uid';
191 $this->arguments[
'optionLabelField'] =
'title';
192 $this->arguments[
'sortByOptionLabel'] =
true;
193 $this->arguments[
'options'] = new \ArrayObject([
210 $this->injectDependenciesIntoViewHelper($this->viewHelper);
211 $this->viewHelper->setTagBuilder($this->tagBuilder);
212 $this->viewHelper->initializeArgumentsAndRender();
220 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
221 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
222 $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));
223 $this->tagBuilder->expects($this->once())->method(
'render');
225 $this->arguments[
'options'] = [
226 'value3' =>
'label3',
227 'value1' =>
'label1',
231 $this->arguments[
'value'] =
'value2';
232 $this->arguments[
'name'] =
'myName';
234 $this->injectDependenciesIntoViewHelper($this->viewHelper);
235 $this->viewHelper->setTagBuilder($this->tagBuilder);
236 $this->viewHelper->initializeArgumentsAndRender();
244 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
245 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
246 $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));
247 $this->tagBuilder->expects($this->once())->method(
'render');
249 $this->arguments[
'options'] = [
250 'value3' =>
'label3',
251 'value1' =>
'label1',
255 $this->arguments[
'value'] =
'value2';
256 $this->arguments[
'name'] =
'myName';
257 $this->arguments[
'sortByOptionLabel'] =
true;
259 $this->injectDependenciesIntoViewHelper($this->viewHelper);
260 $this->viewHelper->setTagBuilder($this->tagBuilder);
261 $this->viewHelper->initializeArgumentsAndRender();
270 $locale =
'de_DE.UTF-8';
272 $this->setLocale(LC_COLLATE, $locale);
273 $this->setLocale(LC_CTYPE, $locale);
274 $this->setLocale(LC_MONETARY, $locale);
275 $this->setLocale(LC_TIME, $locale);
276 }
catch (Exception $e) {
277 $this->markTestSkipped(
'Locale ' . $locale .
' is not available.');
280 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
281 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
282 $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));
283 $this->tagBuilder->expects($this->once())->method(
'render');
284 $this->arguments[
'options'] = [
288 'value1' =>
'Bamberg',
291 $this->arguments[
'value'] =
'value2';
292 $this->arguments[
'name'] =
'myName';
293 $this->arguments[
'sortByOptionLabel'] =
true;
294 $this->injectDependenciesIntoViewHelper($this->viewHelper);
295 $this->viewHelper->setTagBuilder($this->tagBuilder);
296 $this->viewHelper->initializeArgumentsAndRender();
304 $this->tagBuilder =
new TagBuilder();
305 $this->viewHelper->expects(self::exactly(3))->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName[]');
307 $this->arguments[
'options'] = [
308 'value1' =>
'label1',
309 'value2' =>
'label2',
313 $this->arguments[
'value'] = [
'value3',
'value1'];
314 $this->arguments[
'name'] =
'myName';
315 $this->arguments[
'multiple'] =
true;
317 $this->injectDependenciesIntoViewHelper($this->viewHelper);
318 $this->viewHelper->setTagBuilder($this->tagBuilder);
319 $result = $this->viewHelper->initializeArgumentsAndRender();
320 $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>';
321 $this->assertSame($expected, $result);
329 $this->tagBuilder =
new TagBuilder();
330 $this->viewHelper->expects(self::once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName[]');
332 $this->arguments[
'options'] = [];
333 $this->arguments[
'value'] = [
'value3',
'value1'];
334 $this->arguments[
'name'] =
'myName';
335 $this->arguments[
'multiple'] =
true;
337 $this->injectDependenciesIntoViewHelper($this->viewHelper);
338 $this->viewHelper->setTagBuilder($this->tagBuilder);
339 $result = $this->viewHelper->initializeArgumentsAndRender();
340 $expected =
'<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"></select>';
341 self::assertSame($expected, $result);
349 $mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
350 $mockPersistenceManager->expects($this->any())->method(
'getIdentifierByObject')->will($this->returnValue(2));
351 $this->viewHelper->_set(
'persistenceManager', $mockPersistenceManager);
353 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName[__identity]');
354 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName[__identity]');
355 $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));
356 $this->tagBuilder->expects($this->once())->method(
'render');
358 $user_is =
new UserDomainClass(1,
'Ingmar',
'Schlecht');
359 $user_sk =
new UserDomainClass(2,
'Sebastian',
'Kurfuerst');
360 $user_rl =
new UserDomainClass(3,
'Robert',
'Lemke');
362 $this->arguments[
'options'] = [
368 $this->arguments[
'value'] = $user_sk;
369 $this->arguments[
'optionValueField'] =
'id';
370 $this->arguments[
'optionLabelField'] =
'firstName';
371 $this->arguments[
'name'] =
'myName';
372 $this->injectDependenciesIntoViewHelper($this->viewHelper);
373 $this->viewHelper->setTagBuilder($this->tagBuilder);
374 $this->viewHelper->initializeArgumentsAndRender();
382 $this->tagBuilder =
new TagBuilder();
383 $this->viewHelper->expects($this->exactly(3))->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName[]');
385 $user_is =
new UserDomainClass(1,
'Ingmar',
'Schlecht');
386 $user_sk =
new UserDomainClass(2,
'Sebastian',
'Kurfuerst');
387 $user_rl =
new UserDomainClass(3,
'Robert',
'Lemke');
388 $this->arguments[
'options'] = [
393 $this->arguments[
'value'] = [$user_rl, $user_is];
394 $this->arguments[
'optionValueField'] =
'id';
395 $this->arguments[
'optionLabelField'] =
'lastName';
396 $this->arguments[
'name'] =
'myName';
397 $this->arguments[
'multiple'] =
true;
399 $this->injectDependenciesIntoViewHelper($this->viewHelper);
400 $this->viewHelper->setTagBuilder($this->tagBuilder);
401 $actual = $this->viewHelper->initializeArgumentsAndRender();
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) .
407 $this->assertSame($expected, $actual);
416 $mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
417 $mockPersistenceManager->expects($this->any())->method(
'getIdentifierByObject')->will($this->returnCallback(
419 return $object->getId();
422 $this->viewHelper->_set(
'persistenceManager', $mockPersistenceManager);
424 $this->tagBuilder =
new TagBuilder();
425 $this->viewHelper->expects($this->exactly(3))->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName[]');
427 $user_is =
new UserDomainClass(1,
'Ingmar',
'Schlecht');
428 $user_sk =
new UserDomainClass(2,
'Sebastian',
'Kurfuerst');
429 $user_rl =
new UserDomainClass(3,
'Robert',
'Lemke');
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;
437 $this->injectDependenciesIntoViewHelper($this->viewHelper);
438 $this->viewHelper->setTagBuilder($this->tagBuilder);
439 $actual = $this->viewHelper->initializeArgumentsAndRender();
440 $expected =
'<input type="hidden" name="myName" value="" />' .
441 '<select multiple="multiple" name="myName[]">' .
442 '<option value="1" selected="selected">Schlecht</option>' . \chr(10) .
443 '<option value="2">Kurfuerst</option>' . \chr(10) .
444 '<option value="3" selected="selected">Lemke</option>' . \chr(10) .
446 $this->assertSame($expected, $actual);
454 $mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
455 $mockPersistenceManager->expects($this->any())->method(
'getIdentifierByObject')->will($this->returnValue(
'fakeUID'));
456 $this->viewHelper->_set(
'persistenceManager', $mockPersistenceManager);
458 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
459 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
460 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
'<option value="fakeUID">fakeUID</option>' . \chr(10));
461 $this->tagBuilder->expects($this->once())->method(
'render');
463 $user =
new UserDomainClass(1,
'Ingmar',
'Schlecht');
465 $this->arguments[
'options'] = [
468 $this->arguments[
'name'] =
'myName';
469 $this->injectDependenciesIntoViewHelper($this->viewHelper);
470 $this->viewHelper->setTagBuilder($this->tagBuilder);
471 $this->viewHelper->initializeArgumentsAndRender();
479 $mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
480 $mockPersistenceManager->expects($this->any())->method(
'getIdentifierByObject')->will($this->returnValue(
'fakeUID'));
481 $this->viewHelper->_set(
'persistenceManager', $mockPersistenceManager);
483 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
484 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
485 $this->tagBuilder->expects($this->once())->method(
'setContent')->with(
'<option value="fakeUID">toStringResult</option>' . \chr(10));
486 $this->tagBuilder->expects($this->once())->method(
'render');
488 $user = $this->getMockBuilder(UserDomainClass::class)
489 ->setMethods([
'__toString'])
490 ->setConstructorArgs([1,
'Ingmar',
'Schlecht'])
492 $user->expects($this->atLeastOnce())->method(
'__toString')->will($this->returnValue(
'toStringResult'));
494 $this->arguments[
'options'] = [
497 $this->arguments[
'name'] =
'myName';
498 $this->injectDependenciesIntoViewHelper($this->viewHelper);
499 $this->viewHelper->setTagBuilder($this->tagBuilder);
500 $this->viewHelper->initializeArgumentsAndRender();
508 $mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
509 $mockPersistenceManager->expects($this->any())->method(
'getIdentifierByObject')->will($this->returnValue(
null));
510 $this->viewHelper->_set(
'persistenceManager', $mockPersistenceManager);
512 $this->expectException(\
TYPO3Fluid\Fluid\Core\ViewHelper\Exception::class);
513 $this->expectExceptionCode(1247826696);
515 $user =
new UserDomainClass(1,
'Ingmar',
'Schlecht');
517 $this->arguments[
'options'] = [
520 $this->arguments[
'name'] =
'myName';
521 $this->injectDependenciesIntoViewHelper($this->viewHelper);
522 $this->viewHelper->setTagBuilder($this->tagBuilder);
523 $this->viewHelper->initializeArgumentsAndRender();
531 $this->arguments[
'options'] = [];
533 $this->injectDependenciesIntoViewHelper($this->viewHelper);
534 $this->viewHelper->setTagBuilder($this->tagBuilder);
535 $this->viewHelper->expects($this->once())->method(
'setErrorClassAttribute');
536 $this->viewHelper->initializeArgumentsAndRender();
544 $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));
546 $this->arguments[
'options'] = [
547 'value1' =>
'label1',
548 'value2' =>
'label2',
551 $this->arguments[
'name'] =
'myName';
552 $this->arguments[
'multiple'] =
true;
553 $this->arguments[
'selectAllByDefault'] =
true;
555 $this->injectDependenciesIntoViewHelper($this->viewHelper);
556 $this->viewHelper->setTagBuilder($this->tagBuilder);
557 $this->viewHelper->initializeArgumentsAndRender();
565 $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));
567 $this->arguments[
'options'] = [
568 'value1' =>
'label1',
569 'value2' =>
'label2',
572 $this->arguments[
'value'] = [
'value2',
'value1'];
573 $this->arguments[
'name'] =
'myName';
574 $this->arguments[
'multiple'] =
true;
575 $this->arguments[
'selectAllByDefault'] =
true;
577 $this->injectDependenciesIntoViewHelper($this->viewHelper);
578 $this->viewHelper->setTagBuilder($this->tagBuilder);
579 $this->viewHelper->initializeArgumentsAndRender();
587 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
588 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
589 $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));
590 $this->tagBuilder->expects($this->once())->method(
'render');
591 $this->arguments[
'options'] = [
592 'value1' =>
'label1',
593 'value2' =>
'label2',
596 $this->arguments[
'name'] =
'myName';
597 $this->arguments[
'prependOptionLabel'] =
'please choose';
598 $this->injectDependenciesIntoViewHelper($this->viewHelper);
599 $this->viewHelper->setTagBuilder($this->tagBuilder);
600 $this->viewHelper->initializeArgumentsAndRender();
608 $this->tagBuilder->expects($this->once())->method(
'addAttribute')->with(
'name',
'myName');
609 $this->viewHelper->expects($this->once())->method(
'registerFieldNameForFormTokenGeneration')->with(
'myName');
610 $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));
611 $this->tagBuilder->expects($this->once())->method(
'render');
612 $this->arguments[
'options'] = [
613 'value1' =>
'label1',
614 'value2' =>
'label2',
617 $this->arguments[
'name'] =
'myName';
618 $this->arguments[
'prependOptionLabel'] =
'please choose';
619 $this->arguments[
'prependOptionValue'] =
'-1';
620 $this->injectDependenciesIntoViewHelper($this->viewHelper);
621 $this->viewHelper->setTagBuilder($this->tagBuilder);
622 $this->viewHelper->initializeArgumentsAndRender();