TYPO3 CMS  TYPO3_6-2
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 = array();
28 
29  public function setUp() {
30  parent::setUp();
31  // Store all locale categories manipulated in tests for reconstruction in tearDown
32  $this->backupLocales = array(
33  'LC_COLLATE' => setlocale(LC_COLLATE, 0),
34  'LC_CTYPE' => setlocale(LC_CTYPE, 0),
35  'LC_MONETARY' => setlocale(LC_MONETARY, 0),
36  'LC_TIME' => setlocale(LC_TIME, 0),
37  );
38  $this->arguments['name'] = '';
39  $this->arguments['sortByOptionLabel'] = FALSE;
40  $this->viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\SelectViewHelper', array('setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration'));
41  }
42 
43  public function tearDown() {
44  foreach ($this->backupLocales as $category => $locale) {
45  setlocale(constant($category), $locale);
46  }
47  parent::tearDown();
48  }
49 
53  public function selectCorrectlySetsTagName() {
54  $this->tagBuilder->expects($this->once())->method('setTagName')->with('select');
55 
56  $this->arguments['options'] = array();
57  $this->injectDependenciesIntoViewHelper($this->viewHelper);
58 
59  $this->viewHelper->initialize();
60  $this->viewHelper->render();
61  }
62 
66  public function selectCreatesExpectedOptions() {
67  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
68  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
69  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="value1">label1</option>' . chr(10) . '<option value="value2" selected="selected">label2</option>' . chr(10));
70  $this->tagBuilder->expects($this->once())->method('render');
71 
72  $this->arguments['options'] = array(
73  'value1' => 'label1',
74  'value2' => 'label2'
75  );
76  $this->arguments['value'] = 'value2';
77  $this->arguments['name'] = 'myName';
78 
79  $this->injectDependenciesIntoViewHelper($this->viewHelper);
80  $this->viewHelper->initialize();
81  $this->viewHelper->render();
82  }
83 
88  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
89  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
90  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
91  $this->tagBuilder->expects($this->once())->method('render');
92 
93  $this->arguments['options'] = array();
94  $this->arguments['value'] = 'value2';
95  $this->arguments['name'] = 'myName';
96  $this->injectDependenciesIntoViewHelper($this->viewHelper);
97 
98  $this->viewHelper->initialize();
99  $this->viewHelper->render();
100  }
101 
106  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
107  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
108  $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));
109  $this->tagBuilder->expects($this->once())->method('render');
110 
111  $this->arguments['options'] = array(
112  'value3' => 'label3',
113  'value1' => 'label1',
114  'value2' => 'label2'
115  );
116 
117  $this->arguments['value'] = 'value2';
118  $this->arguments['name'] = 'myName';
119 
120  $this->injectDependenciesIntoViewHelper($this->viewHelper);
121 
122  $this->viewHelper->initialize();
123  $this->viewHelper->render();
124  }
125 
130  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
131  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
132  $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));
133  $this->tagBuilder->expects($this->once())->method('render');
134 
135  $this->arguments['options'] = array(
136  'value3' => 'label3',
137  'value1' => 'label1',
138  'value2' => 'label2'
139  );
140 
141  $this->arguments['value'] = 'value2';
142  $this->arguments['name'] = 'myName';
143  $this->arguments['sortByOptionLabel'] = TRUE;
144 
145  $this->injectDependenciesIntoViewHelper($this->viewHelper);
146  $this->viewHelper->initialize();
147  $this->viewHelper->render();
148  }
149 
154  $locale = 'de_DE.UTF-8';
155  if (!setlocale(LC_COLLATE, $locale)) {
156  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
157  }
158  if (stristr(PHP_OS, 'Darwin')) {
159  $this->markTestSkipped('Test skipped caused by a bug in the C libraries on BSD/OSX');
160  }
161 
162  setlocale(LC_CTYPE, $locale);
163  setlocale(LC_MONETARY, $locale);
164  setlocale(LC_TIME, $locale);
165  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
166  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
167  $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));
168  $this->tagBuilder->expects($this->once())->method('render');
169  $this->arguments['options'] = array(
170  'value4' => 'Bär',
171  'value2' => 'Bämm',
172  'value5' => 'Burg',
173  'value1' => 'Bamberg',
174  'value3' => 'Bar'
175  );
176  $this->arguments['value'] = 'value2';
177  $this->arguments['name'] = 'myName';
178  $this->arguments['sortByOptionLabel'] = TRUE;
179  $this->injectDependenciesIntoViewHelper($this->viewHelper);
180  $this->viewHelper->initialize();
181  $this->viewHelper->render();
182  }
183 
188  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
189 
190  $this->arguments['options'] = array(
191  'value1' => 'label1',
192  'value2' => 'label2',
193  'value3' => 'label3'
194  );
195 
196  $this->arguments['value'] = array('value3', 'value1');
197  $this->arguments['name'] = 'myName';
198  $this->arguments['multiple'] = 'multiple';
199 
200  $this->injectDependenciesIntoViewHelper($this->viewHelper);
201 
202  $this->viewHelper->initializeArguments();
203  $this->viewHelper->initialize();
204  $result = $this->viewHelper->render();
205  $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>';
206  $this->assertSame($expected, $result);
207  }
208 
213  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
214  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
215  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
216 
217  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
218  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
219  $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));
220  $this->tagBuilder->expects($this->once())->method('render');
221 
222  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
223  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
224  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
225 
226  $this->arguments['options'] = array(
227  $user_is,
228  $user_sk,
229  $user_rl
230  );
231 
232  $this->arguments['value'] = $user_sk;
233  $this->arguments['optionValueField'] = 'id';
234  $this->arguments['optionLabelField'] = 'firstName';
235  $this->arguments['name'] = 'myName';
236  $this->injectDependenciesIntoViewHelper($this->viewHelper);
237 
238  $this->viewHelper->initialize();
239  $this->viewHelper->render();
240  }
241 
246  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
247  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
248 
249  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
250  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
251  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
252  $this->arguments['options'] = array(
253  $user_is,
254  $user_sk,
255  $user_rl
256  );
257  $this->arguments['value'] = array($user_rl, $user_is);
258  $this->arguments['optionValueField'] = 'id';
259  $this->arguments['optionLabelField'] = 'lastName';
260  $this->arguments['name'] = 'myName';
261  $this->arguments['multiple'] = 'multiple';
262 
263  $this->injectDependenciesIntoViewHelper($this->viewHelper);
264 
265  $this->viewHelper->initializeArguments();
266  $this->viewHelper->initialize();
267  $actual = $this->viewHelper->render();
268  $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><option value="1" selected="selected">Schlecht</option>' . chr(10) .
269  '<option value="2">Kurfuerst</option>' . chr(10) .
270  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
271  '</select>';
272 
273  $this->assertSame($expected, $actual);
274  }
275 
279  public function multipleSelectOnDomainObjectsCreatesExpectedOptionsWithoutOptionValueField() {
281  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
282  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnCallback(
283  function ($object) {
284  return $object->getId();
285  }
286  ));
287  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
288 
289  $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
290  $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
291 
292  $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
293  $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(2, 'Sebastian', 'Kurfuerst');
294  $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(3, 'Robert', 'Lemke');
295 
296  $this->arguments['options'] = array($user_is,$user_sk,$user_rl);
297  $this->arguments['value'] = array($user_rl, $user_is);
298  $this->arguments['optionLabelField'] = 'lastName';
299  $this->arguments['name'] = 'myName';
300  $this->arguments['multiple'] = 'multiple';
301 
302  $this->injectDependenciesIntoViewHelper($this->viewHelper);
303 
304  $this->viewHelper->initializeArguments();
305  $this->viewHelper->initialize();
306  $actual = $this->viewHelper->render();
307  $expected = '<input type="hidden" name="myName" value="" />' .
308  '<select multiple="multiple" name="myName[]">' .
309  '<option value="1" selected="selected">Schlecht</option>' . chr(10) .
310  '<option value="2">Kurfuerst</option>' . chr(10) .
311  '<option value="3" selected="selected">Lemke</option>' . chr(10) .
312  '</select>';
313  $this->assertSame($expected, $actual);
314  }
315 
320  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
321  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
322  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
323 
324  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
325  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
326  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">fakeUID</option>' . chr(10));
327  $this->tagBuilder->expects($this->once())->method('render');
328 
329  $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
330 
331  $this->arguments['options'] = array(
332  $user
333  );
334  $this->arguments['name'] = 'myName';
335  $this->injectDependenciesIntoViewHelper($this->viewHelper);
336 
337  $this->viewHelper->initialize();
338  $this->viewHelper->render();
339  }
340 
345  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
346  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
347  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
348 
349  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
350  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
351  $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
352  $this->tagBuilder->expects($this->once())->method('render');
353 
354  $user = $this->getMock('TYPO3\\CMS\\Fluid\\Tests\\Unit\\ViewHelpers\\Form\\Fixtures\\UserDomainClass', array('__toString'), array(1, 'Ingmar', 'Schlecht'));
355  $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
356 
357  $this->arguments['options'] = array(
358  $user
359  );
360  $this->arguments['name'] = 'myName';
361  $this->injectDependenciesIntoViewHelper($this->viewHelper);
362 
363  $this->viewHelper->initialize();
364  $this->viewHelper->render();
365  }
366 
372  $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
373  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
374  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
375 
376  $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\UserDomainClass(1, 'Ingmar', 'Schlecht');
377 
378  $this->arguments['options'] = array(
379  $user
380  );
381  $this->arguments['name'] = 'myName';
382  $this->injectDependenciesIntoViewHelper($this->viewHelper);
383 
384  $this->viewHelper->initialize();
385  $this->viewHelper->render();
386  }
387 
392  $this->arguments['options'] = array();
393 
394  $this->injectDependenciesIntoViewHelper($this->viewHelper);
395 
396  $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
397  $this->viewHelper->render();
398  }
399 
404  $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));
405 
406  $this->arguments['options'] = array(
407  'value1' => 'label1',
408  'value2' => 'label2',
409  'value3' => 'label3'
410  );
411  $this->arguments['name'] = 'myName';
412  $this->arguments['multiple'] = 'multiple';
413  $this->arguments['selectAllByDefault'] = TRUE;
414 
415  $this->injectDependenciesIntoViewHelper($this->viewHelper);
416 
417  $this->viewHelper->initialize();
418  $this->viewHelper->render();
419  }
420 
425  $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));
426 
427  $this->arguments['options'] = array(
428  'value1' => 'label1',
429  'value2' => 'label2',
430  'value3' => 'label3'
431  );
432  $this->arguments['value'] = array('value2', 'value1');
433  $this->arguments['name'] = 'myName';
434  $this->arguments['multiple'] = 'multiple';
435  $this->arguments['selectAllByDefault'] = TRUE;
436 
437  $this->injectDependenciesIntoViewHelper($this->viewHelper);
438 
439  $this->viewHelper->initialize();
440  $this->viewHelper->render();
441  }
442 
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="">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));
450  $this->tagBuilder->expects($this->once())->method('render');
451  $this->arguments['options'] = array(
452  'value1' => 'label1',
453  'value2' => 'label2',
454  'value3' => 'label3'
455  );
456  $this->arguments['name'] = 'myName';
457  $this->arguments['prependOptionLabel'] = 'please choose';
458  $this->injectDependenciesIntoViewHelper($this->viewHelper);
459  $this->viewHelper->initialize();
460  $this->viewHelper->render();
461  }
462 
467  $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
468  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
469  $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));
470  $this->tagBuilder->expects($this->once())->method('render');
471  $this->arguments['options'] = array(
472  'value1' => 'label1',
473  'value2' => 'label2',
474  'value3' => 'label3'
475  );
476  $this->arguments['name'] = 'myName';
477  $this->arguments['prependOptionLabel'] = 'please choose';
478  $this->arguments['prependOptionValue'] = '-1';
479  $this->injectDependenciesIntoViewHelper($this->viewHelper);
480  $this->viewHelper->initialize();
481  $this->viewHelper->render();
482  }
483 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)