TYPO3 CMS  TYPO3_8-7
SelectViewHelperTest.php
Go to the documentation of this file.
1 <?php
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 
19 
23 class SelectViewHelperTest extends ViewHelperBaseTestcase
24 {
28  protected $viewHelper;
29 
30  protected function setUp()
31  {
32  parent::setUp();
33  $this->arguments['name'] = '';
34  $this->arguments['sortByOptionLabel'] = false;
35  $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper::class, ['setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration', 'renderChildren']);
36  $this->tagBuilder = $this->createMock(\TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder::class);
37  $this->viewHelper->_set('tag', $this->tagBuilder);
38  }
39 
43  public function selectCorrectlySetsTagName()
44  {
45  $this->tagBuilder->expects($this->once())->method('setTagName')->with('select');
46 
47  $this->arguments['options'] = [];
48  $this->injectDependenciesIntoViewHelper($this->viewHelper);
49  $this->viewHelper->_set('tag', $this->tagBuilder);
50  $this->viewHelper->initialize();
51  $this->viewHelper->render();
52  }
53 
57  public function selectCreatesExpectedOptions()
58  {
59  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
60  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
61  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
62  $this->tagBuilder->expects($this->once())->method('render');
63 
64  $this->arguments['options'] = [
65  'value1' => 'label1',
66  'value2' => 'label2'
67  ];
68  $this->arguments['value'] = 'value2';
69  $this->arguments['name'] = 'myName';
70 
71  $this->injectDependenciesIntoViewHelper($this->viewHelper);
72  $this->viewHelper->_set('tag', $this->tagBuilder);
73  $this->viewHelper->initialize();
74  $this->viewHelper->render();
75  }
76 
81  {
82  $this->tagBuilder->expects($this->once())->method('setContent')->with(
83  '<option value="2"></option>' . chr(10) .
84  '<option value="-1">Bar</option>' . chr(10) .
85  '<option value="">Baz</option>' . chr(10) .
86  '<option value="1">Foo</option>' . chr(10)
87  );
88 
89  $this->arguments['optionValueField'] = 'uid';
90  $this->arguments['optionLabelField'] = 'title';
91  $this->arguments['sortByOptionLabel'] = true;
92  $this->arguments['options'] = [
93  [
94  'uid' => 1,
95  'title' => 'Foo'
96  ],
97  [
98  'uid' => -1,
99  'title' => 'Bar'
100  ],
101  [
102  'title' => 'Baz'
103  ],
104  [
105  'uid' => '2'
106  ],
107  ];
108 
109  $this->injectDependenciesIntoViewHelper($this->viewHelper);
110  $this->viewHelper->_set('tag', $this->tagBuilder);
111  $this->viewHelper->initialize();
112  $this->viewHelper->render();
113  }
114 
119  {
120  $this->tagBuilder->expects($this->once())->method('setContent')->with(
121  '<option value="2"></option>' . chr(10) .
122  '<option value="-1">Bar</option>' . chr(10) .
123  '<option value="">Baz</option>' . chr(10) .
124  '<option value="1">Foo</option>' . chr(10)
125  );
126 
127  $obj1 = new \stdClass();
128  $obj1->uid = 1;
129  $obj1->title = 'Foo';
130 
131  $obj2 = new \stdClass();
132  $obj2->uid = -1;
133  $obj2->title = 'Bar';
134 
135  $obj3 = new \stdClass();
136  $obj3->title = 'Baz';
137 
138  $obj4 = new \stdClass();
139  $obj4->uid = 2;
140 
141  $this->arguments['optionValueField'] = 'uid';
142  $this->arguments['optionLabelField'] = 'title';
143  $this->arguments['sortByOptionLabel'] = true;
144  $this->arguments['options'] = [$obj1, $obj2, $obj3, $obj4];
145 
146  $this->injectDependenciesIntoViewHelper($this->viewHelper);
147  $this->viewHelper->_set('tag', $this->tagBuilder);
148  $this->viewHelper->initialize();
149  $this->viewHelper->render();
150  }
151 
156  {
157  $this->tagBuilder->expects($this->once())->method('setContent')->with(
158  '<option value="2"></option>' . chr(10) .
159  '<option value="-1">Bar</option>' . chr(10) .
160  '<option value="">Baz</option>' . chr(10) .
161  '<option value="1">Foo</option>' . chr(10)
162  );
163 
164  $this->arguments['optionValueField'] = 'uid';
165  $this->arguments['optionLabelField'] = 'title';
166  $this->arguments['sortByOptionLabel'] = true;
167  $this->arguments['options'] = new \ArrayObject([
168  [
169  'uid' => 1,
170  'title' => 'Foo'
171  ],
172  [
173  'uid' => -1,
174  'title' => 'Bar'
175  ],
176  [
177  'title' => 'Baz'
178  ],
179  [
180  'uid' => '2'
181  ],
182  ]);
183 
184  $this->injectDependenciesIntoViewHelper($this->viewHelper);
185  $this->viewHelper->_set('tag', $this->tagBuilder);
186  $this->viewHelper->initialize();
187  $this->viewHelper->render();
188  }
189 
194  {
195  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
196  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
197  $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));
198  $this->tagBuilder->expects($this->once())->method('render');
199 
200  $this->arguments['options'] = [
201  'value3' => 'label3',
202  'value1' => 'label1',
203  'value2' => 'label2'
204  ];
205 
206  $this->arguments['value'] = 'value2';
207  $this->arguments['name'] = 'myName';
208 
209  $this->injectDependenciesIntoViewHelper($this->viewHelper);
210  $this->viewHelper->_set('tag', $this->tagBuilder);
211  $this->viewHelper->initialize();
212  $this->viewHelper->render();
213  }
214 
219  {
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="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
223  $this->tagBuilder->expects($this->once())->method('render');
224 
225  $this->arguments['options'] = [
226  'value3' => 'label3',
227  'value1' => 'label1',
228  'value2' => 'label2'
229  ];
230 
231  $this->arguments['value'] = 'value2';
232  $this->arguments['name'] = 'myName';
233  $this->arguments['sortByOptionLabel'] = true;
234 
235  $this->injectDependenciesIntoViewHelper($this->viewHelper);
236  $this->viewHelper->_set('tag', $this->tagBuilder);
237  $this->viewHelper->initialize();
238  $this->viewHelper->render();
239  }
240 
245  {
246  $locale = 'de_DE.UTF-8';
247  try {
248  $this->setLocale(LC_COLLATE, $locale);
249  $this->setLocale(LC_CTYPE, $locale);
250  $this->setLocale(LC_MONETARY, $locale);
251  $this->setLocale(LC_TIME, $locale);
252  } catch (\PHPUnit\Framework\Exception $e) {
253  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
254  }
255  if (stristr(PHP_OS, 'Darwin') !== false) {
256  $this->markTestSkipped('Test skipped caused by a bug in the C libraries on BSD/OSX');
257  }
258 
259  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
260  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
261  $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));
262  $this->tagBuilder->expects($this->once())->method('render');
263  $this->arguments['options'] = [
264  'value4' => 'Bär',
265  'value2' => 'Bämm',
266  'value5' => 'Burg',
267  'value1' => 'Bamberg',
268  'value3' => 'Bar'
269  ];
270  $this->arguments['value'] = 'value2';
271  $this->arguments['name'] = 'myName';
272  $this->arguments['sortByOptionLabel'] = true;
273  $this->injectDependenciesIntoViewHelper($this->viewHelper);
274  $this->viewHelper->_set('tag', $this->tagBuilder);
275  $this->viewHelper->initialize();
276  $this->viewHelper->render();
277  }
278 
283  {
284  $this->tagBuilder = new TagBuilder();
285 
286  $this->arguments['options'] = [
287  'value1' => 'label1',
288  'value2' => 'label2',
289  'value3' => 'label3'
290  ];
291 
292  $this->arguments['value'] = ['value3', 'value1'];
293  $this->arguments['name'] = 'myName';
294  $this->arguments['multiple'] = true;
295 
296  $this->injectDependenciesIntoViewHelper($this->viewHelper);
297  $this->viewHelper->_set('tag', $this->tagBuilder);
298  $this->viewHelper->initializeArguments();
299  $this->viewHelper->initialize();
300  $result = $this->viewHelper->render();
301  $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>';
302  $this->assertSame($expected, $result);
303  }
304 
309  {
310  $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
311  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(2));
312  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
313 
314  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName[__identity]');
315  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName[__identity]');
316  $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));
317  $this->tagBuilder->expects($this->once())->method('render');
318 
319  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
320  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
321  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
322 
323  $this->arguments['options'] = [
324  $user_is,
325  $user_sk,
326  $user_rl
327  ];
328 
329  $this->arguments['value'] = $user_sk;
330  $this->arguments['optionValueField'] = 'id';
331  $this->arguments['optionLabelField'] = 'firstName';
332  $this->arguments['name'] = 'myName';
333  $this->injectDependenciesIntoViewHelper($this->viewHelper);
334  $this->viewHelper->_set('tag', $this->tagBuilder);
335  $this->viewHelper->initialize();
336  $this->viewHelper->render();
337  }
338 
343  {
344  $this->tagBuilder = new TagBuilder();
345  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
346 
347  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
348  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
349  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
350  $this->arguments['options'] = [
351  $user_is,
352  $user_sk,
353  $user_rl
354  ];
355  $this->arguments['value'] = [$user_rl, $user_is];
356  $this->arguments['optionValueField'] = 'id';
357  $this->arguments['optionLabelField'] = 'lastName';
358  $this->arguments['name'] = 'myName';
359  $this->arguments['multiple'] = true;
360 
361  $this->injectDependenciesIntoViewHelper($this->viewHelper);
362  $this->viewHelper->_set('tag', $this->tagBuilder);
363  $this->viewHelper->initializeArguments();
364  $this->viewHelper->initialize();
365  $actual = $this->viewHelper->render();
366  $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><option value="1" selected="selected">Schlecht</option>' . chr(10) .
367  '<option value="2">Kurfuerst</option>' . chr(10) .
368  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
369  '</select>';
370 
371  $this->assertSame($expected, $actual);
372  }
373 
377  public function multipleSelectOnDomainObjectsCreatesExpectedOptionsWithoutOptionValueField()
378  {
380  $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
381  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnCallback(
382  function ($object) {
383  return $object->getId();
384  }
385  ));
386  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
387 
388  $this->tagBuilder = new TagBuilder();
389  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
390 
391  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
392  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
393  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
394 
395  $this->arguments['options'] = [$user_is, $user_sk, $user_rl];
396  $this->arguments['value'] = [$user_rl, $user_is];
397  $this->arguments['optionLabelField'] = 'lastName';
398  $this->arguments['name'] = 'myName';
399  $this->arguments['multiple'] = true;
400 
401  $this->injectDependenciesIntoViewHelper($this->viewHelper);
402  $this->viewHelper->_set('tag', $this->tagBuilder);
403  $this->viewHelper->initializeArguments();
404  $this->viewHelper->initialize();
405  $actual = $this->viewHelper->render();
406  $expected = '<input type="hidden" name="myName" value="" />' .
407  '<select multiple="multiple" name="myName[]">' .
408  '<option value="1" selected="selected">Schlecht</option>' . chr(10) .
409  '<option value="2">Kurfuerst</option>' . chr(10) .
410  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
411  '</select>';
412  $this->assertSame($expected, $actual);
413  }
414 
419  {
420  $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
421  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
422  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
423 
424  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
425  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
426  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
427  $this->tagBuilder->expects($this->once())->method('render');
428 
429  $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
430 
431  $this->arguments['options'] = [
432  $user
433  ];
434  $this->arguments['name'] = 'myName';
435  $this->injectDependenciesIntoViewHelper($this->viewHelper);
436  $this->viewHelper->_set('tag', $this->tagBuilder);
437  $this->viewHelper->initialize();
438  $this->viewHelper->render();
439  }
440 
445  {
446  $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
447  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
448  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
449 
450  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
451  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
452  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
453  $this->tagBuilder->expects($this->once())->method('render');
454 
455  $user = $this->getMockBuilder(\TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass::class)
456  ->setMethods(['__toString'])
457  ->setConstructorArgs([1, 'Ingmar', 'Schlecht'])
458  ->getMock();
459  $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
460 
461  $this->arguments['options'] = [
462  $user
463  ];
464  $this->arguments['name'] = 'myName';
465  $this->injectDependenciesIntoViewHelper($this->viewHelper);
466  $this->viewHelper->_set('tag', $this->tagBuilder);
467  $this->viewHelper->initialize();
468  $this->viewHelper->render();
469  }
470 
475  {
476  $mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
477  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(null));
478  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
479 
480  $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
481  $this->expectExceptionCode(1247826696);
482 
483  $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
484 
485  $this->arguments['options'] = [
486  $user
487  ];
488  $this->arguments['name'] = 'myName';
489  $this->injectDependenciesIntoViewHelper($this->viewHelper);
490  $this->viewHelper->_set('tag', $this->tagBuilder);
491  $this->viewHelper->initialize();
492  $this->viewHelper->render();
493  }
494 
499  {
500  $this->arguments['options'] = [];
501 
502  $this->injectDependenciesIntoViewHelper($this->viewHelper);
503  $this->viewHelper->_set('tag', $this->tagBuilder);
504  $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
505  $this->viewHelper->render();
506  }
507 
512  {
513  $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));
514 
515  $this->arguments['options'] = [
516  'value1' => 'label1',
517  'value2' => 'label2',
518  'value3' => 'label3'
519  ];
520  $this->arguments['name'] = 'myName';
521  $this->arguments['multiple'] = true;
522  $this->arguments['selectAllByDefault'] = true;
523 
524  $this->injectDependenciesIntoViewHelper($this->viewHelper);
525  $this->viewHelper->_set('tag', $this->tagBuilder);
526  $this->viewHelper->initialize();
527  $this->viewHelper->render();
528  }
529 
534  {
535  $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));
536 
537  $this->arguments['options'] = [
538  'value1' => 'label1',
539  'value2' => 'label2',
540  'value3' => 'label3'
541  ];
542  $this->arguments['value'] = ['value2', 'value1'];
543  $this->arguments['name'] = 'myName';
544  $this->arguments['multiple'] = true;
545  $this->arguments['selectAllByDefault'] = true;
546 
547  $this->injectDependenciesIntoViewHelper($this->viewHelper);
548  $this->viewHelper->_set('tag', $this->tagBuilder);
549  $this->viewHelper->initialize();
550  $this->viewHelper->render();
551  }
552 
557  {
558  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
559  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
560  $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));
561  $this->tagBuilder->expects($this->once())->method('render');
562  $this->arguments['options'] = [
563  'value1' => 'label1',
564  'value2' => 'label2',
565  'value3' => 'label3'
566  ];
567  $this->arguments['name'] = 'myName';
568  $this->arguments['prependOptionLabel'] = 'please choose';
569  $this->injectDependenciesIntoViewHelper($this->viewHelper);
570  $this->viewHelper->_set('tag', $this->tagBuilder);
571  $this->viewHelper->initialize();
572  $this->viewHelper->render();
573  }
574 
579  {
580  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
581  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
582  $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));
583  $this->tagBuilder->expects($this->once())->method('render');
584  $this->arguments['options'] = [
585  'value1' => 'label1',
586  'value2' => 'label2',
587  'value3' => 'label3'
588  ];
589  $this->arguments['name'] = 'myName';
590  $this->arguments['prependOptionLabel'] = 'please choose';
591  $this->arguments['prependOptionValue'] = '-1';
592  $this->injectDependenciesIntoViewHelper($this->viewHelper);
593 
594  $this->viewHelper->initialize();
595  $this->viewHelper->render();
596  }
597 }