[TASK] Cleanup TCA select field configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / NodeFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form;
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
17 use Prophecy\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Backend\Form\AbstractNode;
19 use TYPO3\CMS\Backend\Form\Element;
20 use TYPO3\CMS\Core\Tests\UnitTestCase;
21 use TYPO3\CMS\Backend\Form\NodeFactory;
22 use TYPO3\CMS\Backend\Form\NodeInterface;
23 use TYPO3\CMS\Backend\Form\NodeResolverInterface;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Test case
28 */
29 class NodeFactoryTest extends UnitTestCase {
30
31 /**
32 * @test
33 * @expectedException \TYPO3\CMS\Backend\Form\Exception
34 * @expectedExceptionCode 1432207533
35 */
36 public function constructThrowsExceptionIfOverrideMissesNodeNameKey() {
37 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
38 1433089391 => array(
39 'class' => 'foo',
40 'priority' => 23,
41 ),
42 );
43 new NodeFactory();
44 }
45
46 /**
47 * @test
48 * @expectedException \TYPO3\CMS\Backend\Form\Exception
49 * @expectedExceptionCode 1432207533
50 */
51 public function constructThrowsExceptionIfOverrideMissesPriorityKey() {
52 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
53 1433089393 => array(
54 'nodeName' => 'foo',
55 'class' => 'bar',
56 ),
57 );
58 new NodeFactory();
59 }
60
61 /**
62 * @test
63 * @expectedException \TYPO3\CMS\Backend\Form\Exception
64 * @expectedExceptionCode 1432207533
65 */
66 public function constructThrowsExceptionIfOverrideMissesClassKey() {
67 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
68 1433089392 => array(
69 'nodeName' => 'foo',
70 'priority' => 23,
71 ),
72 );
73 new NodeFactory();
74 }
75
76 /**
77 * @test
78 * @expectedException \TYPO3\CMS\Backend\Form\Exception
79 * @expectedExceptionCode 1432223531
80 */
81 public function constructThrowsExceptionIfOverridePriorityIsLowerThanZero() {
82 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
83 1433089394 => array(
84 'nodeName' => 'foo',
85 'class' => 'bar',
86 'priority' => -23,
87 ),
88 );
89 new NodeFactory();
90
91 }
92 /**
93 * @test
94 * @expectedException \TYPO3\CMS\Backend\Form\Exception
95 * @expectedExceptionCode 1432223531
96 */
97 public function constructThrowsExceptionIfOverridePriorityIsHigherThanHundred() {
98 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
99 1433089395 => array(
100 'nodeName' => 'foo',
101 'class' => 'bar',
102 'priority' => 142,
103 ),
104 );
105 new NodeFactory();
106 }
107
108 /**
109 * @test
110 * @expectedException \TYPO3\CMS\Backend\Form\Exception
111 * @expectedExceptionCode 1432223893
112 */
113 public function constructorThrowsExceptionIfOverrideTwoNodesWithSamePriorityAndSameNodeNameAreRegistered() {
114 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
115 1433089396 => array(
116 'nodeName' => 'foo',
117 'priority' => 20,
118 'class' => 'fooClass',
119 ),
120 1433089397 => array(
121 'nodeName' => 'foo',
122 'priority' => 20,
123 'class' => 'barClass',
124 ),
125 );
126 new NodeFactory();
127 }
128
129 /**
130 * @test
131 * @expectedException \TYPO3\CMS\Backend\Form\Exception
132 * @expectedExceptionCode 1433155522
133 */
134 public function constructThrowsExceptionIfResolverMissesNodeNameKey() {
135 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
136 1433154905 => array(
137 'class' => 'foo',
138 'priority' => 23,
139 ),
140 );
141 new NodeFactory();
142 }
143
144 /**
145 * @test
146 * @expectedException \TYPO3\CMS\Backend\Form\Exception
147 * @expectedExceptionCode 1433155522
148 */
149 public function constructThrowsExceptionIfResolverMissesPriorityKey() {
150 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
151 1433154905 => array(
152 'nodeName' => 'foo',
153 'class' => 'bar',
154 ),
155 );
156 new NodeFactory();
157 }
158
159 /**
160 * @test
161 * @expectedException \TYPO3\CMS\Backend\Form\Exception
162 * @expectedExceptionCode 1433155522
163 */
164 public function constructThrowsExceptionIfResolverMissesClassKey() {
165 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
166 1433154906 => array(
167 'nodeName' => 'foo',
168 'priority' => 23,
169 ),
170 );
171 new NodeFactory();
172 }
173
174 /**
175 * @test
176 * @expectedException \TYPO3\CMS\Backend\Form\Exception
177 * @expectedExceptionCode 1433155563
178 */
179 public function constructThrowsExceptionIfResolverPriorityIsLowerThanZero() {
180 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
181 1433154907 => array(
182 'nodeName' => 'foo',
183 'class' => 'bar',
184 'priority' => -23,
185 ),
186 );
187 new NodeFactory();
188
189 }
190 /**
191 * @test
192 * @expectedException \TYPO3\CMS\Backend\Form\Exception
193 * @expectedExceptionCode 1433155563
194 */
195 public function constructThrowsExceptionIfResolverPriorityIsHigherThanHundred() {
196 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
197 1433154908 => array(
198 'nodeName' => 'foo',
199 'class' => 'bar',
200 'priority' => 142,
201 ),
202 );
203 new NodeFactory();
204 }
205
206 /**
207 * @test
208 * @expectedException \TYPO3\CMS\Backend\Form\Exception
209 * @expectedExceptionCode 1433155705
210 */
211 public function constructorThrowsExceptionIfResolverTwoNodesWithSamePriorityAndSameNodeNameAreRegistered() {
212 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
213 1433154909 => array(
214 'nodeName' => 'foo',
215 'priority' => 20,
216 'class' => 'fooClass',
217 ),
218 1433154910 => array(
219 'nodeName' => 'foo',
220 'priority' => 20,
221 'class' => 'barClass',
222 ),
223 );
224 new NodeFactory();
225 }
226
227 /**
228 * @test
229 */
230 public function constructorThrowsNoExceptionIfResolverWithSamePriorityButDifferentNodeNameAreRegistered() {
231 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
232 1433154909 => array(
233 'nodeName' => 'foo',
234 'priority' => 20,
235 'class' => 'fooClass',
236 ),
237 1433154910 => array(
238 'nodeName' => 'bar',
239 'priority' => 20,
240 'class' => 'barClass',
241 ),
242 );
243 new NodeFactory();
244 }
245
246 /**
247 * @test
248 * @expectedException \TYPO3\CMS\Backend\Form\Exception
249 * @expectedExceptionCode 1431452406
250 */
251 public function createThrowsExceptionIfRenderTypeIsNotGiven() {
252 $subject = new NodeFactory();
253 $subject->create(array());
254 }
255
256 /**
257 * @test
258 * @expectedException \TYPO3\CMS\Backend\Form\Exception
259 * @expectedExceptionCode 1431872546
260 */
261 public function createThrowsExceptionIfNodeDoesNotImplementNodeInterface() {
262 $mockNode = new \stdClass();
263 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
264 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'), array(), '', FALSE);
265 $mockSubject->expects($this->once())->method('instantiate')->will($this->returnValue($mockNode));
266 $mockSubject->create(array('renderType' => 'foo'));
267 }
268
269 /**
270 * @test
271 */
272 public function createReturnsInstanceOfUnknownElementIfTypeIsNotRegistered() {
273 $subject = new NodeFactory();
274 $this->assertInstanceOf(Element\UnknownElement::class, $subject->create(array('renderType' => 'foo')));
275 }
276
277 /**
278 * @test
279 */
280 public function createReturnsInstanceOfSelectTreeElementIfNeeded() {
281 $data = array(
282 'type' => 'select',
283 'renderType' => 'selectTree',
284 );
285 $subject = new NodeFactory();
286 $this->assertInstanceOf(Element\SelectTreeElement::class, $subject->create($data));
287 }
288
289 /**
290 * @test
291 */
292 public function createReturnsInstanceOfSelectSingleElementIfNeeded() {
293 $data = array(
294 'type' => 'select',
295 'renderType' => 'selectSingle',
296 'parameterArray' => array(
297 'fieldConf' => array(
298 'config' => array(
299 'maxitems' => 1,
300 ),
301 ),
302 ),
303 );
304 $subject = new NodeFactory();
305 $this->assertInstanceOf(Element\SelectSingleElement::class, $subject->create($data));
306 }
307
308 /**
309 * @test
310 */
311 public function createInstantiatesNewRegisteredElement() {
312 $data = array('renderType' => 'foo');
313 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
314 array(
315 'nodeName' => 'foo',
316 'priority' => 20,
317 'class' => \stdClass::class,
318 ),
319 );
320 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
321 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
322 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
323 $mockSubject->expects($this->once())->method('instantiate')->with('stdClass')->will($this->returnValue($mockNode));
324 $mockSubject->create($data);
325 }
326
327 /**
328 * @test
329 */
330 public function createInstantiatesElementRegisteredWithHigherPriorityWithOneGivenOrder() {
331 $data = array('renderType' => 'foo');
332 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
333 1433089467 => array(
334 'nodeName' => 'foo',
335 'priority' => 20,
336 'class' => 'foo1Class',
337 ),
338 1433089468 => array(
339 'nodeName' => 'foo',
340 'priority' => 30,
341 'class' => 'foo2Class',
342 ),
343 );
344 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
345 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
346 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
347 $mockSubject->expects($this->once())->method('instantiate')->with('foo2Class')->will($this->returnValue($mockNode));
348 $mockSubject->create($data);
349 }
350
351 /**
352 * @test
353 */
354 public function createInstantiatesElementRegisteredWithHigherPriorityWithOtherGivenOrder() {
355 $data = array('renderType' => 'foo');
356 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
357 1433089469 => array(
358 'nodeName' => 'foo',
359 'priority' => 30,
360 'class' => 'foo2Class',
361 ),
362 1433089470 => array(
363 'nodeName' => 'foo',
364 'priority' => 20,
365 'class' => 'foo1Class',
366 ),
367 );
368 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
369 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
370 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
371 $mockSubject->expects($this->once())->method('instantiate')->with('foo2Class')->will($this->returnValue($mockNode));
372 $mockSubject->create($data);
373 }
374
375 /**
376 * @test
377 * @expectedException \TYPO3\CMS\Backend\Form\Exception
378 * @expectedExceptionCode 1433157422
379 */
380 public function createThrowsExceptionIfResolverDoesNotImplementNodeResolverInterface() {
381 $data = array('renderType' => 'foo');
382 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
383 1433156887 => array(
384 'nodeName' => 'foo',
385 'priority' => 10,
386 'class' => 'fooClass',
387 ),
388 );
389 $mockResolver = $this->getMock(\stdClass::class);
390
391 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
392 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
393 $mockSubject->expects($this->at(0))->method('instantiate')->will($this->returnValue($mockResolver));
394 $mockSubject->create($data);
395 }
396
397 /**
398 * @test
399 */
400 public function createInstantiatesResolverWithHighestPriorityFirstWithOneGivenOrder() {
401 $data = array('renderType' => 'foo');
402 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
403 array(
404 'nodeName' => 'foo',
405 'priority' => 20,
406 'class' => \stdClass::class,
407 ),
408 );
409 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
410
411 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
412 1433156887 => array(
413 'nodeName' => 'foo',
414 'priority' => 10,
415 'class' => 'foo1Class',
416 ),
417 1433156888 => array(
418 'nodeName' => 'foo',
419 'priority' => 30,
420 'class' => 'foo2Class',
421 ),
422 );
423 $mockResolver1 = $this->getMock(NodeResolverInterface::class);
424 $mockResolver2 = $this->getMock(NodeResolverInterface::class);
425
426 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
427 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
428 $mockSubject->expects($this->at(0))->method('instantiate')->with('foo2Class')->will($this->returnValue($mockResolver2));
429 $mockSubject->expects($this->at(1))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
430 $mockSubject->expects($this->at(2))->method('instantiate')->will($this->returnValue($mockNode));
431 $mockSubject->create($data);
432 }
433
434 /**
435 * @test
436 */
437 public function createInstantiatesResolverWithHighestPriorityFirstWithOtherGivenOrder() {
438 $data = array('renderType' => 'foo');
439 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
440 array(
441 'nodeName' => 'foo',
442 'priority' => 20,
443 'class' => \stdClass::class,
444 ),
445 );
446 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
447
448 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
449 1433156887 => array(
450 'nodeName' => 'foo',
451 'priority' => 30,
452 'class' => 'foo1Class',
453 ),
454 1433156888 => array(
455 'nodeName' => 'foo',
456 'priority' => 10,
457 'class' => 'foo2Class',
458 ),
459 );
460 $mockResolver1 = $this->getMock(NodeResolverInterface::class);
461 $mockResolver2 = $this->getMock(NodeResolverInterface::class);
462
463 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
464 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
465 $mockSubject->expects($this->at(0))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
466 $mockSubject->expects($this->at(1))->method('instantiate')->with('foo2Class')->will($this->returnValue($mockResolver2));
467 $mockSubject->expects($this->at(2))->method('instantiate')->will($this->returnValue($mockNode));
468 $mockSubject->create($data);
469 }
470
471 /**
472 * @test
473 */
474 public function createInstantiatesNodeClassReturnedByResolver() {
475 $data = array('renderType' => 'foo');
476 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
477 array(
478 'nodeName' => 'foo',
479 'priority' => 20,
480 'class' => \stdClass::class,
481 ),
482 );
483 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
484
485 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
486 1433156887 => array(
487 'nodeName' => 'foo',
488 'priority' => 30,
489 'class' => 'foo1Class',
490 ),
491 );
492 $mockResolver1 = $this->getMock(NodeResolverInterface::class);
493 $mockResolver1->expects($this->once())->method('resolve')->will($this->returnValue('fooNodeClass'));
494
495 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
496 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
497 $mockSubject->expects($this->at(0))->method('instantiate')->will($this->returnValue($mockResolver1));
498 $mockSubject->expects($this->at(1))->method('instantiate')->with('fooNodeClass')->will($this->returnValue($mockNode));
499 $mockSubject->create($data);
500 }
501
502 /**
503 * @test
504 */
505 public function createDoesNotCallSecondResolverWithLowerPriorityIfFirstResolverReturnedClassName() {
506 $data = array('renderType' => 'foo');
507 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
508 array(
509 'nodeName' => 'foo',
510 'priority' => 20,
511 'class' => \stdClass::class,
512 ),
513 );
514 $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', FALSE);
515
516 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
517 1433156887 => array(
518 'nodeName' => 'foo',
519 'priority' => 30,
520 'class' => 'foo1Class',
521 ),
522 1433156888 => array(
523 'nodeName' => 'foo',
524 'priority' => 10,
525 'class' => 'foo2Class',
526 ),
527 );
528 $mockResolver1 = $this->getMock(NodeResolverInterface::class);
529 $mockResolver1->expects($this->once())->method('resolve')->will($this->returnValue('fooNodeClass'));
530
531 /** @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
532 $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
533 $mockSubject->expects($this->at(0))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
534 $mockSubject->expects($this->at(1))->method('instantiate')->with('fooNodeClass')->will($this->returnValue($mockNode));
535 $mockSubject->create($data);
536 }
537
538 }