TYPO3 CMS  TYPO3_7-6
SelectViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
18 {
22  protected $viewHelper;
23 
27  protected $backupLocales = [];
28 
29  protected function setUp()
30  {
31  parent::setUp();
32  // Store all locale categories manipulated in tests for reconstruction in tearDown
33  $this->backupLocales = [
34  'LC_COLLATE' => setlocale(LC_COLLATE, 0),
35  'LC_CTYPE' => setlocale(LC_CTYPE, 0),
36  'LC_MONETARY' => setlocale(LC_MONETARY, 0),
37  'LC_TIME' => setlocale(LC_TIME, 0),
38  ];
39  $this->arguments['name'] = '';
40  $this->arguments['sortByOptionLabel'] = false;
41  $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper::class, ['setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration']);
42  }
43 
44  protected function tearDown()
45  {
46  foreach ($this->backupLocales as $category => $locale) {
47  setlocale(constant($category), $locale);
48  }
49  parent::tearDown();
50  }
51 
55  public function selectCorrectlySetsTagName()
56  {
57  $this->tagBuilder->expects($this->once())->method('setTagName')->with('select');
58 
59  $this->arguments['options'] = [];
60  $this->injectDependenciesIntoViewHelper($this->viewHelper);
61 
62  $this->viewHelper->initialize();
63  $this->viewHelper->render();
64  }
65 
69  public function selectCreatesExpectedOptions()
70  {
71  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
72  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
73  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
74  $this->tagBuilder->expects($this->once())->method('render');
75 
76  $this->arguments['options'] = [
77  'value1' => 'label1',
78  'value2' => 'label2'
79  ];
80  $this->arguments['value'] = 'value2';
81  $this->arguments['name'] = 'myName';
82 
83  $this->injectDependenciesIntoViewHelper($this->viewHelper);
84  $this->viewHelper->initialize();
85  $this->viewHelper->render();
86  }
87 
92  {
93  $this->tagBuilder->expects($this->once())->method('setContent')->with(
94  '<option value="2"></option>' . chr(10) .
95  '<option value="-1">Bar</option>' . chr(10) .
96  '<option value="">Baz</option>' . chr(10) .
97  '<option value="1">Foo</option>' . chr(10)
98  );
99 
100  $this->arguments['optionValueField'] = 'uid';
101  $this->arguments['optionLabelField'] = 'title';
102  $this->arguments['sortByOptionLabel'] = true;
103  $this->arguments['options'] = [
104  [
105  'uid' => 1,
106  'title' => 'Foo'
107  ],
108  [
109  'uid' => -1,
110  'title' => 'Bar'
111  ],
112  [
113  'title' => 'Baz'
114  ],
115  [
116  'uid' => '2'
117  ],
118  ];
119 
120  $this->injectDependenciesIntoViewHelper($this->viewHelper);
121  $this->viewHelper->initialize();
122  $this->viewHelper->render();
123  }
124 
129  {
130  $this->tagBuilder->expects($this->once())->method('setContent')->with(
131  '<option value="2"></option>' . chr(10) .
132  '<option value="-1">Bar</option>' . chr(10) .
133  '<option value="">Baz</option>' . chr(10) .
134  '<option value="1">Foo</option>' . chr(10)
135  );
136 
137  $obj1 = new \StdClass();
138  $obj1->uid = 1;
139  $obj1->title = 'Foo';
140 
141  $obj2 = new \StdClass();
142  $obj2->uid = -1;
143  $obj2->title = 'Bar';
144 
145  $obj3 = new \StdClass();
146  $obj3->title = 'Baz';
147 
148  $obj4 = new \StdClass();
149  $obj4->uid = 2;
150 
151  $this->arguments['optionValueField'] = 'uid';
152  $this->arguments['optionLabelField'] = 'title';
153  $this->arguments['sortByOptionLabel'] = true;
154  $this->arguments['options'] = [$obj1, $obj2, $obj3, $obj4];
155 
156  $this->injectDependenciesIntoViewHelper($this->viewHelper);
157  $this->viewHelper->initialize();
158  $this->viewHelper->render();
159  }
160 
165  {
166  $this->tagBuilder->expects($this->once())->method('setContent')->with(
167  '<option value="2"></option>' . chr(10) .
168  '<option value="-1">Bar</option>' . chr(10) .
169  '<option value="">Baz</option>' . chr(10) .
170  '<option value="1">Foo</option>' . chr(10)
171  );
172 
173  $this->arguments['optionValueField'] = 'uid';
174  $this->arguments['optionLabelField'] = 'title';
175  $this->arguments['sortByOptionLabel'] = true;
176  $this->arguments['options'] = new \ArrayObject([
177  [
178  'uid' => 1,
179  'title' => 'Foo'
180  ],
181  [
182  'uid' => -1,
183  'title' => 'Bar'
184  ],
185  [
186  'title' => 'Baz'
187  ],
188  [
189  'uid' => '2'
190  ],
191  ]);
192 
193  $this->injectDependenciesIntoViewHelper($this->viewHelper);
194  $this->viewHelper->initialize();
195  $this->viewHelper->render();
196  }
197 
202  {
203  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
204  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
205  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
206  $this->tagBuilder->expects($this->once())->method('render');
207 
208  $this->arguments['options'] = [];
209  $this->arguments['value'] = 'value2';
210  $this->arguments['name'] = 'myName';
211  $this->injectDependenciesIntoViewHelper($this->viewHelper);
212 
213  $this->viewHelper->initialize();
214  $this->viewHelper->render();
215  }
216 
221  {
222  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
223  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
224  $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));
225  $this->tagBuilder->expects($this->once())->method('render');
226 
227  $this->arguments['options'] = [
228  'value3' => 'label3',
229  'value1' => 'label1',
230  'value2' => 'label2'
231  ];
232 
233  $this->arguments['value'] = 'value2';
234  $this->arguments['name'] = 'myName';
235 
236  $this->injectDependenciesIntoViewHelper($this->viewHelper);
237 
238  $this->viewHelper->initialize();
239  $this->viewHelper->render();
240  }
241 
246  {
247  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
248  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
249  $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));
250  $this->tagBuilder->expects($this->once())->method('render');
251 
252  $this->arguments['options'] = [
253  'value3' => 'label3',
254  'value1' => 'label1',
255  'value2' => 'label2'
256  ];
257 
258  $this->arguments['value'] = 'value2';
259  $this->arguments['name'] = 'myName';
260  $this->arguments['sortByOptionLabel'] = true;
261 
262  $this->injectDependenciesIntoViewHelper($this->viewHelper);
263  $this->viewHelper->initialize();
264  $this->viewHelper->render();
265  }
266 
271  {
272  $locale = 'de_DE.UTF-8';
273  if (!setlocale(LC_COLLATE, $locale)) {
274  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
275  }
276  if (stristr(PHP_OS, 'Darwin')) {
277  $this->markTestSkipped('Test skipped caused by a bug in the C libraries on BSD/OSX');
278  }
279 
280  setlocale(LC_CTYPE, $locale);
281  setlocale(LC_MONETARY, $locale);
282  setlocale(LC_TIME, $locale);
283  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
284  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
285  $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));
286  $this->tagBuilder->expects($this->once())->method('render');
287  $this->arguments['options'] = [
288  'value4' => 'Bär',
289  'value2' => 'Bämm',
290  'value5' => 'Burg',
291  'value1' => 'Bamberg',
292  'value3' => 'Bar'
293  ];
294  $this->arguments['value'] = 'value2';
295  $this->arguments['name'] = 'myName';
296  $this->arguments['sortByOptionLabel'] = true;
297  $this->injectDependenciesIntoViewHelper($this->viewHelper);
298  $this->viewHelper->initialize();
299  $this->viewHelper->render();
300  }
301 
306  {
307  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
308 
309  $this->arguments['options'] = [
310  'value1' => 'label1',
311  'value2' => 'label2',
312  'value3' => 'label3'
313  ];
314 
315  $this->arguments['value'] = ['value3', 'value1'];
316  $this->arguments['name'] = 'myName';
317  $this->arguments['multiple'] = true;
318 
319  $this->injectDependenciesIntoViewHelper($this->viewHelper);
320 
321  $this->viewHelper->initializeArguments();
322  $this->viewHelper->initialize();
323  $result = $this->viewHelper->render();
324  $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>';
325  $this->assertSame($expected, $result);
326  }
327 
332  {
333  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
334  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(2));
335  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
336 
337  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName[__identity]');
338  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName[__identity]');
339  $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));
340  $this->tagBuilder->expects($this->once())->method('render');
341 
342  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
343  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
344  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
345 
346  $this->arguments['options'] = [
347  $user_is,
348  $user_sk,
349  $user_rl
350  ];
351 
352  $this->arguments['value'] = $user_sk;
353  $this->arguments['optionValueField'] = 'id';
354  $this->arguments['optionLabelField'] = 'firstName';
355  $this->arguments['name'] = 'myName';
356  $this->injectDependenciesIntoViewHelper($this->viewHelper);
357 
358  $this->viewHelper->initialize();
359  $this->viewHelper->render();
360  }
361 
366  {
367  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
368  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
369 
370  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
371  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
372  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
373  $this->arguments['options'] = [
374  $user_is,
375  $user_sk,
376  $user_rl
377  ];
378  $this->arguments['value'] = [$user_rl, $user_is];
379  $this->arguments['optionValueField'] = 'id';
380  $this->arguments['optionLabelField'] = 'lastName';
381  $this->arguments['name'] = 'myName';
382  $this->arguments['multiple'] = true;
383 
384  $this->injectDependenciesIntoViewHelper($this->viewHelper);
385 
386  $this->viewHelper->initializeArguments();
387  $this->viewHelper->initialize();
388  $actual = $this->viewHelper->render();
389  $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><option value="1" selected="selected">Schlecht</option>' . chr(10) .
390  '<option value="2">Kurfuerst</option>' . chr(10) .
391  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
392  '</select>';
393 
394  $this->assertSame($expected, $actual);
395  }
396 
400  public function multipleSelectOnDomainObjectsCreatesExpectedOptionsWithoutOptionValueField()
401  {
403  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
404  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnCallback(
405  function ($object) {
406  return $object->getId();
407  }
408  ));
409  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
410 
411  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
412  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
413 
414  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
415  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
416  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
417 
418  $this->arguments['options'] = [$user_is, $user_sk, $user_rl];
419  $this->arguments['value'] = [$user_rl, $user_is];
420  $this->arguments['optionLabelField'] = 'lastName';
421  $this->arguments['name'] = 'myName';
422  $this->arguments['multiple'] = true;
423 
424  $this->injectDependenciesIntoViewHelper($this->viewHelper);
425 
426  $this->viewHelper->initializeArguments();
427  $this->viewHelper->initialize();
428  $actual = $this->viewHelper->render();
429  $expected = '<input type="hidden" name="myName" value="" />' .
430  '<select multiple="multiple" name="myName[]">' .
431  '<option value="1" selected="selected">Schlecht</option>' . chr(10) .
432  '<option value="2">Kurfuerst</option>' . chr(10) .
433  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
434  '</select>';
435  $this->assertSame($expected, $actual);
436  }
437 
442  {
443  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
444  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
445  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
446 
447  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
448  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
449  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
450  $this->tagBuilder->expects($this->once())->method('render');
451 
452  $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
453 
454  $this->arguments['options'] = [
455  $user
456  ];
457  $this->arguments['name'] = 'myName';
458  $this->injectDependenciesIntoViewHelper($this->viewHelper);
459 
460  $this->viewHelper->initialize();
461  $this->viewHelper->render();
462  }
463 
468  {
469  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
470  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
471  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
472 
473  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
474  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
475  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
476  $this->tagBuilder->expects($this->once())->method('render');
477 
478  $user = $this->getMock(\TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass::class, ['__toString'], [1, 'Ingmar', 'Schlecht']);
479  $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
480 
481  $this->arguments['options'] = [
482  $user
483  ];
484  $this->arguments['name'] = 'myName';
485  $this->injectDependenciesIntoViewHelper($this->viewHelper);
486 
487  $this->viewHelper->initialize();
488  $this->viewHelper->render();
489  }
490 
496  {
497  $mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
498  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(null));
499  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
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 
509  $this->viewHelper->initialize();
510  $this->viewHelper->render();
511  }
512 
517  {
518  $this->arguments['options'] = [];
519 
520  $this->injectDependenciesIntoViewHelper($this->viewHelper);
521 
522  $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
523  $this->viewHelper->render();
524  }
525 
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 
544  $this->viewHelper->initialize();
545  $this->viewHelper->render();
546  }
547 
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 
567  $this->viewHelper->initialize();
568  $this->viewHelper->render();
569  }
570 
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->initialize();
589  $this->viewHelper->render();
590  }
591 
596  {
597  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
598  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
599  $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));
600  $this->tagBuilder->expects($this->once())->method('render');
601  $this->arguments['options'] = [
602  'value1' => 'label1',
603  'value2' => 'label2',
604  'value3' => 'label3'
605  ];
606  $this->arguments['name'] = 'myName';
607  $this->arguments['prependOptionLabel'] = 'please choose';
608  $this->arguments['prependOptionValue'] = '-1';
609  $this->injectDependenciesIntoViewHelper($this->viewHelper);
610  $this->viewHelper->initialize();
611  $this->viewHelper->render();
612  }
613 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)