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