TYPO3  7.6
NodeFactoryTest.php
Go to the documentation of this file.
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\Core\Tests\UnitTestCase;
22 
26 class NodeFactoryTest extends UnitTestCase
27 {
34  {
35  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
36  1433089391 => array(
37  'class' => 'foo',
38  'priority' => 23,
39  ),
40  );
41  new NodeFactory();
42  }
43 
50  {
51  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
52  1433089393 => array(
53  'nodeName' => 'foo',
54  'class' => 'bar',
55  ),
56  );
57  new NodeFactory();
58  }
59 
66  {
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 
82  {
83  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
84  1433089394 => array(
85  'nodeName' => 'foo',
86  'class' => 'bar',
87  'priority' => -23,
88  ),
89  );
90  new NodeFactory();
91  }
98  {
99  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
100  1433089395 => array(
101  'nodeName' => 'foo',
102  'class' => 'bar',
103  'priority' => 142,
104  ),
105  );
106  new NodeFactory();
107  }
108 
115  {
116  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
117  1433089396 => array(
118  'nodeName' => 'foo',
119  'priority' => 20,
120  'class' => 'fooClass',
121  ),
122  1433089397 => array(
123  'nodeName' => 'foo',
124  'priority' => 20,
125  'class' => 'barClass',
126  ),
127  );
128  new NodeFactory();
129  }
130 
137  {
138  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
139  1433154905 => array(
140  'class' => 'foo',
141  'priority' => 23,
142  ),
143  );
144  new NodeFactory();
145  }
146 
153  {
154  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
155  1433154905 => array(
156  'nodeName' => 'foo',
157  'class' => 'bar',
158  ),
159  );
160  new NodeFactory();
161  }
162 
169  {
170  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
171  1433154906 => array(
172  'nodeName' => 'foo',
173  'priority' => 23,
174  ),
175  );
176  new NodeFactory();
177  }
178 
185  {
186  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
187  1433154907 => array(
188  'nodeName' => 'foo',
189  'class' => 'bar',
190  'priority' => -23,
191  ),
192  );
193  new NodeFactory();
194  }
201  {
202  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
203  1433154908 => array(
204  'nodeName' => 'foo',
205  'class' => 'bar',
206  'priority' => 142,
207  ),
208  );
209  new NodeFactory();
210  }
211 
218  {
219  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
220  1433154909 => array(
221  'nodeName' => 'foo',
222  'priority' => 20,
223  'class' => 'fooClass',
224  ),
225  1433154910 => array(
226  'nodeName' => 'foo',
227  'priority' => 20,
228  'class' => 'barClass',
229  ),
230  );
231  new NodeFactory();
232  }
233 
238  {
239  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
240  1433154909 => array(
241  'nodeName' => 'foo',
242  'priority' => 20,
243  'class' => 'fooClass',
244  ),
245  1433154910 => array(
246  'nodeName' => 'bar',
247  'priority' => 20,
248  'class' => 'barClass',
249  ),
250  );
251  new NodeFactory();
252  }
253 
260  {
261  $subject = new NodeFactory();
262  $subject->create(array());
263  }
264 
270  public function createThrowsExceptionIfNodeDoesNotImplementNodeInterface()
271  {
272  $mockNode = new \stdClass();
274  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'), array(), '', false);
275  $mockSubject->expects($this->once())->method('instantiate')->will($this->returnValue($mockNode));
276  $mockSubject->create(array('renderType' => 'foo'));
277  }
278 
283  {
284  $subject = new NodeFactory();
285  $this->assertInstanceOf(Element\UnknownElement::class, $subject->create(array('renderType' => 'foo')));
286  }
287 
292  {
293  $data = array(
294  'type' => 'select',
295  'renderType' => 'selectTree',
296  );
297  $subject = new NodeFactory();
298  $this->assertInstanceOf(Element\SelectTreeElement::class, $subject->create($data));
299  }
300 
305  {
306  $data = array(
307  'type' => 'select',
308  'renderType' => 'selectSingle',
309  'parameterArray' => array(
310  'fieldConf' => array(
311  'config' => array(
312  'maxitems' => 1,
313  ),
314  ),
315  ),
316  );
317  $subject = new NodeFactory();
318  $this->assertInstanceOf(Element\SelectSingleElement::class, $subject->create($data));
319  }
320 
324  public function createInstantiatesNewRegisteredElement()
325  {
326  $data = array('renderType' => 'foo');
327  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
328  array(
329  'nodeName' => 'foo',
330  'priority' => 20,
331  'class' => \stdClass::class,
332  ),
333  );
334  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
336  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
337  $mockSubject->expects($this->once())->method('instantiate')->with('stdClass')->will($this->returnValue($mockNode));
338  $mockSubject->create($data);
339  }
340 
344  public function createInstantiatesElementRegisteredWithHigherPriorityWithOneGivenOrder()
345  {
346  $data = array('renderType' => 'foo');
347  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
348  1433089467 => array(
349  'nodeName' => 'foo',
350  'priority' => 20,
351  'class' => 'foo1Class',
352  ),
353  1433089468 => array(
354  'nodeName' => 'foo',
355  'priority' => 30,
356  'class' => 'foo2Class',
357  ),
358  );
359  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
361  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
362  $mockSubject->expects($this->once())->method('instantiate')->with('foo2Class')->will($this->returnValue($mockNode));
363  $mockSubject->create($data);
364  }
365 
369  public function createInstantiatesElementRegisteredWithHigherPriorityWithOtherGivenOrder()
370  {
371  $data = array('renderType' => 'foo');
372  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
373  1433089469 => array(
374  'nodeName' => 'foo',
375  'priority' => 30,
376  'class' => 'foo2Class',
377  ),
378  1433089470 => array(
379  'nodeName' => 'foo',
380  'priority' => 20,
381  'class' => 'foo1Class',
382  ),
383  );
384  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
386  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
387  $mockSubject->expects($this->once())->method('instantiate')->with('foo2Class')->will($this->returnValue($mockNode));
388  $mockSubject->create($data);
389  }
390 
396  public function createThrowsExceptionIfResolverDoesNotImplementNodeResolverInterface()
397  {
398  $data = array('renderType' => 'foo');
399  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
400  1433156887 => array(
401  'nodeName' => 'foo',
402  'priority' => 10,
403  'class' => 'fooClass',
404  ),
405  );
406  $mockResolver = $this->getMock(\stdClass::class);
407 
409  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
410  $mockSubject->expects($this->at(0))->method('instantiate')->will($this->returnValue($mockResolver));
411  $mockSubject->create($data);
412  }
413 
417  public function createInstantiatesResolverWithHighestPriorityFirstWithOneGivenOrder()
418  {
419  $data = array('renderType' => 'foo');
420  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
421  array(
422  'nodeName' => 'foo',
423  'priority' => 20,
424  'class' => \stdClass::class,
425  ),
426  );
427  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
428 
429  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
430  1433156887 => array(
431  'nodeName' => 'foo',
432  'priority' => 10,
433  'class' => 'foo1Class',
434  ),
435  1433156888 => array(
436  'nodeName' => 'foo',
437  'priority' => 30,
438  'class' => 'foo2Class',
439  ),
440  );
441  $mockResolver1 = $this->getMock(NodeResolverInterface::class);
442  $mockResolver2 = $this->getMock(NodeResolverInterface::class);
443 
445  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
446  $mockSubject->expects($this->at(0))->method('instantiate')->with('foo2Class')->will($this->returnValue($mockResolver2));
447  $mockSubject->expects($this->at(1))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
448  $mockSubject->expects($this->at(2))->method('instantiate')->will($this->returnValue($mockNode));
449  $mockSubject->create($data);
450  }
451 
455  public function createInstantiatesResolverWithHighestPriorityFirstWithOtherGivenOrder()
456  {
457  $data = array('renderType' => 'foo');
458  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
459  array(
460  'nodeName' => 'foo',
461  'priority' => 20,
462  'class' => \stdClass::class,
463  ),
464  );
465  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
466 
467  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
468  1433156887 => array(
469  'nodeName' => 'foo',
470  'priority' => 30,
471  'class' => 'foo1Class',
472  ),
473  1433156888 => array(
474  'nodeName' => 'foo',
475  'priority' => 10,
476  'class' => 'foo2Class',
477  ),
478  );
479  $mockResolver1 = $this->getMock(NodeResolverInterface::class);
480  $mockResolver2 = $this->getMock(NodeResolverInterface::class);
481 
483  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
484  $mockSubject->expects($this->at(0))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
485  $mockSubject->expects($this->at(1))->method('instantiate')->with('foo2Class')->will($this->returnValue($mockResolver2));
486  $mockSubject->expects($this->at(2))->method('instantiate')->will($this->returnValue($mockNode));
487  $mockSubject->create($data);
488  }
489 
493  public function createInstantiatesNodeClassReturnedByResolver()
494  {
495  $data = array('renderType' => 'foo');
496  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
497  array(
498  'nodeName' => 'foo',
499  'priority' => 20,
500  'class' => \stdClass::class,
501  ),
502  );
503  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
504 
505  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
506  1433156887 => array(
507  'nodeName' => 'foo',
508  'priority' => 30,
509  'class' => 'foo1Class',
510  ),
511  );
512  $mockResolver1 = $this->getMock(NodeResolverInterface::class);
513  $mockResolver1->expects($this->once())->method('resolve')->will($this->returnValue('fooNodeClass'));
514 
516  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
517  $mockSubject->expects($this->at(0))->method('instantiate')->will($this->returnValue($mockResolver1));
518  $mockSubject->expects($this->at(1))->method('instantiate')->with('fooNodeClass')->will($this->returnValue($mockNode));
519  $mockSubject->create($data);
520  }
521 
525  public function createDoesNotCallSecondResolverWithLowerPriorityIfFirstResolverReturnedClassName()
526  {
527  $data = array('renderType' => 'foo');
528  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array(
529  array(
530  'nodeName' => 'foo',
531  'priority' => 20,
532  'class' => \stdClass::class,
533  ),
534  );
535  $mockNode = $this->getMock(NodeInterface::class, array(), array(), '', false);
536 
537  $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array(
538  1433156887 => array(
539  'nodeName' => 'foo',
540  'priority' => 30,
541  'class' => 'foo1Class',
542  ),
543  1433156888 => array(
544  'nodeName' => 'foo',
545  'priority' => 10,
546  'class' => 'foo2Class',
547  ),
548  );
549  $mockResolver1 = $this->getMock(NodeResolverInterface::class);
550  $mockResolver1->expects($this->once())->method('resolve')->will($this->returnValue('fooNodeClass'));
551 
553  $mockSubject = $this->getMock(NodeFactory::class, array('instantiate'));
554  $mockSubject->expects($this->at(0))->method('instantiate')->with('foo1Class')->will($this->returnValue($mockResolver1));
555  $mockSubject->expects($this->at(1))->method('instantiate')->with('fooNodeClass')->will($this->returnValue($mockNode));
556  $mockSubject->create($data);
557  }
558 }