[TASK] Clean up Unit tests in fluid
[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 script is backported from the FLOW3 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 require_once dirname(__FILE__) . '/Fixtures/EmptySyntaxTreeNode.php';
14 require_once dirname(__FILE__) . '/Fixtures/Fixture_UserDomainClass.php';
15 require_once dirname(__FILE__) . '/FormFieldViewHelperBaseTestcase.php';
16
17 /**
18 * Test for the "Select" Form view helper
19 */
20 class SelectViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase {
21
22 /**
23 * @var \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
24 */
25 protected $viewHelper;
26
27 /**
28 * @var array Backup of current locale, it is manipulated in tests
29 */
30 protected $backupLocales = array();
31
32 public function setUp() {
33 parent::setUp();
34 // Store all locale categories manipulated in tests for reconstruction in tearDown
35 $this->backupLocales = array(
36 'LC_COLLATE' => setlocale(LC_COLLATE, 0),
37 'LC_CTYPE' => setlocale(LC_CTYPE, 0),
38 'LC_MONETARY' => setlocale(LC_MONETARY, 0),
39 'LC_TIME' => setlocale(LC_TIME, 0),
40 );
41 $this->arguments['name'] = '';
42 $this->arguments['sortByOptionLabel'] = FALSE;
43 $this->viewHelper = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\SelectViewHelper', array('setErrorClassAttribute', 'registerFieldNameForFormTokenGeneration'));
44 }
45
46 public function tearDown() {
47 foreach ($this->backupLocales as $category => $locale) {
48 setlocale(constant($category), $locale);
49 }
50 }
51
52 /**
53 * @test
54 */
55 public function selectCorrectlySetsTagName() {
56 $this->tagBuilder->expects($this->once())->method('setTagName')->with('select');
57 $this->arguments['options'] = array();
58 $this->injectDependenciesIntoViewHelper($this->viewHelper);
59 $this->viewHelper->initialize();
60 $this->viewHelper->render();
61 }
62
63 /**
64 * @test
65 */
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 $this->arguments['options'] = array(
72 'value1' => 'label1',
73 'value2' => 'label2'
74 );
75 $this->arguments['value'] = 'value2';
76 $this->arguments['name'] = 'myName';
77 $this->injectDependenciesIntoViewHelper($this->viewHelper);
78 $this->viewHelper->initialize();
79 $this->viewHelper->render();
80 }
81
82 /**
83 * @test
84 */
85 public function anEmptyOptionTagIsRenderedIfOptionsArrayIsEmptyToAssureXhtmlCompatibility() {
86 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
87 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
88 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value=""></option>' . chr(10));
89 $this->tagBuilder->expects($this->once())->method('render');
90 $this->arguments['options'] = array();
91 $this->arguments['value'] = 'value2';
92 $this->arguments['name'] = 'myName';
93 $this->injectDependenciesIntoViewHelper($this->viewHelper);
94 $this->viewHelper->initialize();
95 $this->viewHelper->render();
96 }
97
98 /**
99 * @test
100 */
101 public function OrderOfOptionsIsNotAlteredByDefault() {
102 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
103 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
104 $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));
105 $this->tagBuilder->expects($this->once())->method('render');
106 $this->arguments['options'] = array(
107 'value3' => 'label3',
108 'value1' => 'label1',
109 'value2' => 'label2'
110 );
111 $this->arguments['value'] = 'value2';
112 $this->arguments['name'] = 'myName';
113 $this->injectDependenciesIntoViewHelper($this->viewHelper);
114 $this->viewHelper->initialize();
115 $this->viewHelper->render();
116 }
117
118 /**
119 * @test
120 */
121 public function optionsAreSortedByLabelIfSortByOptionLabelIsSet() {
122 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
123 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
124 $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));
125 $this->tagBuilder->expects($this->once())->method('render');
126 $this->arguments['options'] = array(
127 'value3' => 'label3',
128 'value1' => 'label1',
129 'value2' => 'label2'
130 );
131 $this->arguments['value'] = 'value2';
132 $this->arguments['name'] = 'myName';
133 $this->arguments['sortByOptionLabel'] = TRUE;
134 $this->injectDependenciesIntoViewHelper($this->viewHelper);
135 $this->viewHelper->initialize();
136 $this->viewHelper->render();
137 }
138
139 /**
140 * @test
141 */
142 public function optionsAreSortedByLabelIfSortByOptionLabelIsSetAndLocaleEqualsUtf8() {
143 $locale = 'de_DE.UTF-8';
144 if (!setlocale(LC_COLLATE, $locale)) {
145 $this->markTestSkipped('Locale ' . $locale . ' is not available.');
146 }
147 if (stristr(PHP_OS, 'Darwin')) {
148 $this->markTestSkipped('Test skipped caused by a bug in the C libraries on BSD/OSX');
149 }
150
151 setlocale(LC_CTYPE, $locale);
152 setlocale(LC_MONETARY, $locale);
153 setlocale(LC_TIME, $locale);
154 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
155 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
156 $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));
157 $this->tagBuilder->expects($this->once())->method('render');
158 $this->arguments['options'] = array(
159 'value4' => 'Bär',
160 'value2' => 'Bämm',
161 'value5' => 'Burg',
162 'value1' => 'Bamberg',
163 'value3' => 'Bar'
164 );
165 $this->arguments['value'] = 'value2';
166 $this->arguments['name'] = 'myName';
167 $this->arguments['sortByOptionLabel'] = TRUE;
168 $this->injectDependenciesIntoViewHelper($this->viewHelper);
169 $this->viewHelper->initialize();
170 $this->viewHelper->render();
171 }
172
173 /**
174 * @test
175 */
176 public function multipleSelectCreatesExpectedOptions() {
177 $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
178 $this->arguments['options'] = array(
179 'value1' => 'label1',
180 'value2' => 'label2',
181 'value3' => 'label3'
182 );
183 $this->arguments['value'] = array('value3', 'value1');
184 $this->arguments['name'] = 'myName';
185 $this->arguments['multiple'] = 'multiple';
186 $this->injectDependenciesIntoViewHelper($this->viewHelper);
187 $this->viewHelper->initializeArguments();
188 $this->viewHelper->initialize();
189 $result = $this->viewHelper->render();
190 $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>';
191 $this->assertSame($expected, $result);
192 }
193
194 /**
195 * @test
196 */
197 public function selectOnDomainObjectsCreatesExpectedOptions() {
198 $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
199 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
200 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
201
202 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
203 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
204 $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));
205 $this->tagBuilder->expects($this->once())->method('render');
206
207 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(1, 'Ingmar', 'Schlecht');
208 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
209 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(3, 'Robert', 'Lemke');
210
211 $this->arguments['options'] = array(
212 $user_is,
213 $user_sk,
214 $user_rl
215 );
216
217 $this->arguments['value'] = $user_sk;
218 $this->arguments['optionValueField'] = 'id';
219 $this->arguments['optionLabelField'] = 'firstName';
220 $this->arguments['name'] = 'myName';
221 $this->injectDependenciesIntoViewHelper($this->viewHelper);
222
223 $this->viewHelper->initialize();
224 $this->viewHelper->render();
225 }
226
227 /**
228 * @test
229 */
230 public function multipleSelectOnDomainObjectsCreatesExpectedOptions() {
231 $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
232 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
233 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(1, 'Ingmar', 'Schlecht');
234 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
235 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(3, 'Robert', 'Lemke');
236 $this->arguments['options'] = array(
237 $user_is,
238 $user_sk,
239 $user_rl
240 );
241 $this->arguments['value'] = array($user_rl, $user_is);
242 $this->arguments['optionValueField'] = 'id';
243 $this->arguments['optionLabelField'] = 'lastName';
244 $this->arguments['name'] = 'myName';
245 $this->arguments['multiple'] = 'multiple';
246 $this->injectDependenciesIntoViewHelper($this->viewHelper);
247 $this->viewHelper->initializeArguments();
248 $this->viewHelper->initialize();
249 $actual = $this->viewHelper->render();
250 $expected = '<input type="hidden" name="myName" value="" /><select multiple="multiple" name="myName[]"><option value="1" selected="selected">Schlecht</option>' . chr(10) . '<option value="2">Kurfuerst</option>' . chr(10) . '<option value="3" selected="selected">Lemke</option>' . chr(10) . '</select>';
251 $this->assertSame($expected, $actual);
252 }
253
254 /**
255 * @test
256 * @author Johannes Künsebeck <jk@hdnet.de>
257 */
258 public function multipleSelectOnDomainObjectsCreatesExpectedOptionsWithoutOptionValueField() {
259 /** @var $mockPersistenceManager \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface */
260 $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
261 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnCallback(function ($object) {
262 return $object->getId();
263 }));
264 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
265 $this->tagBuilder = new \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder();
266 $this->viewHelper->expects($this->exactly(3))->method('registerFieldNameForFormTokenGeneration')->with('myName[]');
267 $user_is = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(1, 'Ingmar', 'Schlecht');
268 $user_sk = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(2, 'Sebastian', 'Kurfuerst');
269 $user_rl = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(3, 'Robert', 'Lemke');
270 $this->arguments['options'] = array($user_is, $user_sk, $user_rl);
271 $this->arguments['value'] = array($user_rl, $user_is);
272 $this->arguments['optionLabelField'] = 'lastName';
273 $this->arguments['name'] = 'myName';
274 $this->arguments['multiple'] = 'multiple';
275 $this->injectDependenciesIntoViewHelper($this->viewHelper);
276 $this->viewHelper->initializeArguments();
277 $this->viewHelper->initialize();
278 $actual = $this->viewHelper->render();
279 $expected = '<input type="hidden" name="myName" value="" />' . '<select multiple="multiple" name="myName[]">' . '<option value="1" selected="selected">Schlecht</option>' . chr(10) . '<option value="2">Kurfuerst</option>' . chr(10) . '<option value="3" selected="selected">Lemke</option>' . chr(10) . '</select>';
280 $this->assertSame($expected, $actual);
281 }
282
283 /**
284 * @test
285 */
286 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesUuidForValueAndLabel() {
287 $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
288 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUUID'));
289 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
290
291 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
292 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
293 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUUID">fakeUUID</option>' . chr(10));
294 $this->tagBuilder->expects($this->once())->method('render');
295
296 $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(1, 'Ingmar', 'Schlecht');
297
298 $this->arguments['options'] = array(
299 $user
300 );
301
302 $this->arguments['name'] = 'myName';
303 $this->injectDependenciesIntoViewHelper($this->viewHelper);
304
305 $this->viewHelper->initialize();
306 $this->viewHelper->render();
307 }
308
309 /**
310 * @test
311 */
312 public function selectWithoutFurtherConfigurationOnDomainObjectsUsesToStringForLabelIfAvailable() {
313 $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
314 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('fakeUID'));
315 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
316
317 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
318 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
319 $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="fakeUID">toStringResult</option>' . chr(10));
320 $this->tagBuilder->expects($this->once())->method('render');
321
322 $user = $this->getMock('TYPO3\\CMS\\Fluid\\Tests\\Unit\\ViewHelpers\\Form\\Fixtures\\Fixture_UserDomainClass', array('__toString'), array(1, 'Ingmar', 'Schlecht'));
323 $user->expects($this->atLeastOnce())->method('__toString')->will($this->returnValue('toStringResult'));
324
325 $this->arguments['options'] = array(
326 $user
327 );
328 $this->arguments['name'] = 'myName';
329 $this->injectDependenciesIntoViewHelper($this->viewHelper);
330
331 $this->viewHelper->initialize();
332 $this->viewHelper->render();
333 }
334
335 /**
336 * @test
337 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
338 */
339 public function selectOnDomainObjectsThrowsExceptionIfNoValueCanBeFound() {
340 $mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
341 $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue(NULL));
342 $this->viewHelper->injectPersistenceManager($mockPersistenceManager);
343
344 $user = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\Fixture_UserDomainClass(1, 'Ingmar', 'Schlecht');
345
346 $this->arguments['options'] = array(
347 $user
348 );
349 $this->arguments['name'] = 'myName';
350 $this->injectDependenciesIntoViewHelper($this->viewHelper);
351
352 $this->viewHelper->initialize();
353 $this->viewHelper->render();
354 }
355
356 /**
357 * @test
358 */
359 public function renderCallsSetErrorClassAttribute() {
360 $this->arguments['options'] = array();
361 $this->injectDependenciesIntoViewHelper($this->viewHelper);
362 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
363 $this->viewHelper->render();
364 }
365
366 /**
367 * @test
368 */
369 public function allOptionsAreSelectedIfSelectAllIsTrue() {
370 $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));
371 $this->arguments['options'] = array(
372 'value1' => 'label1',
373 'value2' => 'label2',
374 'value3' => 'label3'
375 );
376 $this->arguments['name'] = 'myName';
377 $this->arguments['multiple'] = 'multiple';
378 $this->arguments['selectAllByDefault'] = TRUE;
379 $this->injectDependenciesIntoViewHelper($this->viewHelper);
380 $this->viewHelper->initialize();
381 $this->viewHelper->render();
382 }
383
384 /**
385 * @test
386 */
387 public function selectAllHasNoEffectIfValueIsSet() {
388 $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));
389 $this->arguments['options'] = array(
390 'value1' => 'label1',
391 'value2' => 'label2',
392 'value3' => 'label3'
393 );
394 $this->arguments['value'] = array('value2', 'value1');
395 $this->arguments['name'] = 'myName';
396 $this->arguments['multiple'] = 'multiple';
397 $this->arguments['selectAllByDefault'] = TRUE;
398 $this->injectDependenciesIntoViewHelper($this->viewHelper);
399 $this->viewHelper->initialize();
400 $this->viewHelper->render();
401 }
402
403 /**
404 * @test
405 */
406 public function optionsContainPrependedItemWithEmptyValueIfPrependOptionLabelIsSet() {
407 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
408 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
409 $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));
410 $this->tagBuilder->expects($this->once())->method('render');
411 $this->arguments['options'] = array(
412 'value1' => 'label1',
413 'value2' => 'label2',
414 'value3' => 'label3'
415 );
416 $this->arguments['name'] = 'myName';
417 $this->arguments['prependOptionLabel'] = 'please choose';
418 $this->injectDependenciesIntoViewHelper($this->viewHelper);
419 $this->viewHelper->initialize();
420 $this->viewHelper->render();
421 }
422
423 /**
424 * @test
425 */
426 public function optionsContainPrependedItemWithCorrectValueIfPrependOptionLabelAndPrependOptionValueAreSet() {
427 $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
428 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
429 $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));
430 $this->tagBuilder->expects($this->once())->method('render');
431 $this->arguments['options'] = array(
432 'value1' => 'label1',
433 'value2' => 'label2',
434 'value3' => 'label3'
435 );
436 $this->arguments['name'] = 'myName';
437 $this->arguments['prependOptionLabel'] = 'please choose';
438 $this->arguments['prependOptionValue'] = '-1';
439 $this->injectDependenciesIntoViewHelper($this->viewHelper);
440 $this->viewHelper->initialize();
441 $this->viewHelper->render();
442 }
443 }
444
445 ?>