TYPO3  7.6
Unit/ContentObject/FluidTemplateContentObjectTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
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 
23 
27 class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
28 {
32  protected $singletonInstances = array();
33 
37  protected $subject = null;
38 
42  protected $contentObjectRenderer = null;
43 
47  protected $standaloneView = null;
48 
52  protected $request = null;
53 
57  protected function setUp()
58  {
59  $this->singletonInstances = GeneralUtility::getSingletonInstances();
60  $this->contentObjectRenderer = $this->getMock(
61  \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class
62  );
63  $this->subject = $this->getAccessibleMock(
64  \TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject::class,
65  array('initializeStandaloneViewInstance'),
66  array($this->contentObjectRenderer)
67  );
69  $tsfe = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(), array(), '', false);
70  $tsfe->tmpl = $this->getMock(\TYPO3\CMS\Core\TypoScript\TemplateService::class);
71  $GLOBALS['TSFE'] = $tsfe;
72  }
73 
77  protected function tearDown()
78  {
79  GeneralUtility::resetSingletonInstances($this->singletonInstances);
80  parent::tearDown();
81  }
82 
86  protected function addMockViewToSubject()
87  {
88  $this->standaloneView = $this->getMock(\TYPO3\CMS\Fluid\View\StandaloneView::class, array(), array(), '', false);
89  $this->request = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class);
90  $this->standaloneView
91  ->expects($this->any())
92  ->method('getRequest')
93  ->will($this->returnValue($this->request));
94  $this->subject->_set('view', $this->standaloneView);
95  }
96 
101  {
102  $this->assertSame($this->contentObjectRenderer, $this->subject->getContentObject());
103  }
104 
109  {
110  $this->addMockViewToSubject();
111  $this->subject
112  ->expects($this->once())
113  ->method('initializeStandaloneViewInstance');
114  $this->subject->render(array());
115  }
116 
120  public function renderCallsTemplateServiceGetFileNameForGivenTemplateFile()
121  {
122  $this->addMockViewToSubject();
124  $templateService = $GLOBALS['TSFE']->tmpl;
125  $templateService
126  ->expects($this->any())
127  ->method('getFileName')
128  ->with('foo');
129  $this->subject->render(array('file' => 'foo'));
130  }
131 
136  {
137  $this->addMockViewToSubject();
138  $this->contentObjectRenderer
139  ->expects($this->any())
140  ->method('stdWrap')
141  ->with('foo', array('bar' => 'baz'));
142  $this->subject->render(array('file' => 'foo', 'file.' => array('bar' => 'baz')));
143  }
144 
149  {
150  $this->addMockViewToSubject();
151  $this->contentObjectRenderer
152  ->expects($this->at(0))
153  ->method('stdWrap')
154  ->with('dummyPath', array('wrap' => '|5/'));
155  $this->contentObjectRenderer
156  ->expects($this->at(1))
157  ->method('stdWrap')
158  ->with('', array('field' => 'someField'));
159  $this->subject->render(array(
160  'templateName' => 'foobar',
161  'templateRootPaths.' => array(
162  10 => 'dummyPath',
163  '10.' => array(
164  'wrap' => '|5/',
165  ),
166  15 => 'dummyPath6/',
167  '25.' => array(
168  'field' => 'someField',
169  ),
170  )
171  )
172  );
173  }
174 
178  public function renderSetsTemplateFileInView()
179  {
180  $this->addMockViewToSubject();
182  $templateService = $GLOBALS['TSFE']->tmpl;
183  $templateService
184  ->expects($this->any())
185  ->method('getFileName')
186  ->with('foo')
187  ->will($this->returnValue('bar'));
188  $this->standaloneView
189  ->expects($this->any())
190  ->method('setTemplatePathAndFilename')
191  ->with(PATH_site . 'bar');
192  $this->subject->render(array('file' => 'foo'));
193  }
194 
199  {
200  $this->addMockViewToSubject();
201 
202  $this->contentObjectRenderer
203  ->expects($this->any())
204  ->method('cObjGetSingle')
205  ->with('FILE', array('file' => PATH_site . 'foo/bar.html'))
206  ->will($this->returnValue('baz'));
207 
208  $this->standaloneView
209  ->expects($this->any())
210  ->method('setTemplateSource')
211  ->with('baz');
212 
213  $this->subject->render(array(
214  'template' => 'FILE',
215  'template.' => array(
216  'file' => PATH_site . 'foo/bar.html'
217  )
218  ));
219  }
220 
225  {
226  $this->addMockViewToSubject();
227 
228  $this->standaloneView
229  ->expects($this->any())
230  ->method('getFormat')
231  ->will($this->returnValue('html'));
232  $this->standaloneView
233  ->expects($this->once())
234  ->method('setTemplate')
235  ->with('foo');
236 
237  $this->subject->render(array(
238  'templateName' => 'foo',
239  'templateRootPaths.' => array(
240  0 => 'dummyPath1/',
241  1 => 'dummyPath2/')
242  )
243  );
244  }
245 
250  {
251  $this->addMockViewToSubject();
252 
253  $this->contentObjectRenderer
254  ->expects($this->once())
255  ->method('stdWrap')
256  ->with('TEXT', array('value' => 'bar'))
257  ->will($this->returnValue('bar'));
258  $this->standaloneView
259  ->expects($this->any())
260  ->method('getFormat')
261  ->will($this->returnValue('html'));
262  $this->standaloneView
263  ->expects($this->once())
264  ->method('setTemplate')
265  ->with('bar');
266 
267  $this->subject->render(array(
268  'templateName' => 'TEXT',
269  'templateName.' => array('value' => 'bar'),
270  'templateRootPaths.' => array(
271  0 => 'dummyPath1/',
272  1 => 'dummyPath2/')
273  )
274  );
275  }
276 
281  {
282  $this->addMockViewToSubject();
283  $this->standaloneView
284  ->expects($this->once())
285  ->method('setLayoutRootPaths')
286  ->with(array(PATH_site . 'foo/bar.html'));
287  $this->subject->render(array('layoutRootPath' => 'foo/bar.html'));
288  }
289 
294  {
295  $this->addMockViewToSubject();
296  $this->contentObjectRenderer
297  ->expects($this->once())
298  ->method('stdWrap')
299  ->with('foo', array('bar' => 'baz'));
300  $this->subject->render(array('layoutRootPath' => 'foo', 'layoutRootPath.' => array('bar' => 'baz')));
301  }
302 
307  {
308  $this->addMockViewToSubject();
309  $this->contentObjectRenderer
310  ->expects($this->at(0))
311  ->method('stdWrap')
312  ->with('FILE', array('file' => 'foo/bar.html'));
313  $this->subject->render(
314  array(
315  'layoutRootPaths.' => array(
316  10 => 'FILE',
317  '10.' => array(
318  'file' => 'foo/bar.html',
319  ),
320  20 => 'foo/bar2.html',
321  )
322  )
323  );
324  }
325 
330  {
331  $this->addMockViewToSubject();
332  $this->standaloneView
333  ->expects($this->once())
334  ->method('setLayoutRootPaths')
335  ->with(array(10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html'));
336  $this->subject->render(array('layoutRootPaths.' => array(10 => 'foo/bar.html', 20 => 'foo/bar2.html')));
337  }
338 
343  {
344  $this->addMockViewToSubject();
345  $this->standaloneView
346  ->expects($this->once())
347  ->method('setLayoutRootPaths')
348  ->with(array(0 => PATH_site . 'foo/main.html', 10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html'));
349  $this->subject->render(array('layoutRootPath' => 'foo/main.html', 'layoutRootPaths.' => array(10 => 'foo/bar.html', 20 => 'foo/bar2.html')));
350  }
351 
356  {
357  $this->addMockViewToSubject();
358  $this->standaloneView
359  ->expects($this->once())
360  ->method('setPartialRootPaths')
361  ->with(array(PATH_site . 'foo/bar.html'));
362  $this->subject->render(array('partialRootPath' => 'foo/bar.html'));
363  }
364 
369  {
370  $this->addMockViewToSubject();
371  $this->contentObjectRenderer
372  ->expects($this->at(0))
373  ->method('stdWrap')
374  ->with('FILE', array('file' => 'foo/bar.html'));
375  $this->subject->render(
376  array(
377  'partialRootPaths.' => array(
378  10 => 'FILE',
379  '10.' => array(
380  'file' => 'foo/bar.html',
381  ),
382  20 => 'foo/bar2.html',
383  )
384  )
385  );
386  }
387 
392  {
393  $this->addMockViewToSubject();
394  $this->contentObjectRenderer
395  ->expects($this->once())
396  ->method('stdWrap')
397  ->with('foo', array('bar' => 'baz'));
398  $this->subject->render(array('partialRootPath' => 'foo', 'partialRootPath.' => array('bar' => 'baz')));
399  }
400 
401 
406  {
407  $this->addMockViewToSubject();
408  $this->standaloneView
409  ->expects($this->once())
410  ->method('setPartialRootPaths')
411  ->with(array(10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
412  $this->subject->render(array('partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
413  }
414 
419  {
420  $this->addMockViewToSubject();
421  $this->standaloneView
422  ->expects($this->once())
423  ->method('setPartialRootPaths')
424  ->with(array(0 => PATH_site . 'main', 10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
425  $this->subject->render(array('partialRootPath' => 'main', 'partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
426  }
427 
431  public function renderSetsFormatInView()
432  {
433  $this->addMockViewToSubject();
434  $this->standaloneView
435  ->expects($this->once())
436  ->method('setFormat')
437  ->with('xml');
438  $this->subject->render(array('format' => 'xml'));
439  }
440 
445  {
446  $this->addMockViewToSubject();
447  $this->contentObjectRenderer
448  ->expects($this->once())
449  ->method('stdWrap')
450  ->with('foo', array('bar' => 'baz'));
451  $this->subject->render(array('format' => 'foo', 'format.' => array('bar' => 'baz')));
452  }
453 
458  {
459  $this->addMockViewToSubject();
460  $this->request
461  ->expects($this->once())
462  ->method('setPluginName')
463  ->with('foo');
464  $configuration = array(
465  'extbase.' => array(
466  'pluginName' => 'foo',
467  ),
468  );
469  $this->subject->render($configuration);
470  }
471 
476  {
477  $this->addMockViewToSubject();
478  $this->contentObjectRenderer
479  ->expects($this->once())
480  ->method('stdWrap')
481  ->with('foo', array('bar' => 'baz'));
482  $configuration = array(
483  'extbase.' => array(
484  'pluginName' => 'foo',
485  'pluginName.' => array(
486  'bar' => 'baz',
487  ),
488  ),
489  );
490  $this->subject->render($configuration);
491  }
492 
497  {
498  $this->addMockViewToSubject();
499  $this->request
500  ->expects($this->once())
501  ->method('setControllerExtensionName')
502  ->with('foo');
503  $configuration = array(
504  'extbase.' => array(
505  'controllerExtensionName' => 'foo',
506  ),
507  );
508  $this->subject->render($configuration);
509  }
510 
515  {
516  $this->addMockViewToSubject();
517  $this->contentObjectRenderer
518  ->expects($this->once())
519  ->method('stdWrap')
520  ->with('foo', array('bar' => 'baz'));
521  $configuration = array(
522  'extbase.' => array(
523  'controllerExtensionName' => 'foo',
524  'controllerExtensionName.' => array(
525  'bar' => 'baz',
526  ),
527  ),
528  );
529  $this->subject->render($configuration);
530  }
531 
536  {
537  $this->addMockViewToSubject();
538  $this->request
539  ->expects($this->once())
540  ->method('setControllerName')
541  ->with('foo');
542  $configuration = array(
543  'extbase.' => array(
544  'controllerName' => 'foo',
545  ),
546  );
547  $this->subject->render($configuration);
548  }
549 
554  {
555  $this->addMockViewToSubject();
556  $this->contentObjectRenderer
557  ->expects($this->once())
558  ->method('stdWrap')
559  ->with('foo', array('bar' => 'baz'));
560  $configuration = array(
561  'extbase.' => array(
562  'controllerName' => 'foo',
563  'controllerName.' => array(
564  'bar' => 'baz',
565  ),
566  ),
567  );
568  $this->subject->render($configuration);
569  }
570 
575  {
576  $this->addMockViewToSubject();
577  $this->request
578  ->expects($this->once())
579  ->method('setControllerActionName')
580  ->with('foo');
581  $configuration = array(
582  'extbase.' => array(
583  'controllerActionName' => 'foo',
584  ),
585  );
586  $this->subject->render($configuration);
587  }
588 
593  {
594  $this->addMockViewToSubject();
595  $this->contentObjectRenderer
596  ->expects($this->once())
597  ->method('stdWrap')
598  ->with('foo', array('bar' => 'baz'));
599  $configuration = array(
600  'extbase.' => array(
601  'controllerActionName' => 'foo',
602  'controllerActionName.' => array(
603  'bar' => 'baz',
604  ),
605  ),
606  );
607  $this->subject->render($configuration);
608  }
609 
613  public function renderAssignsSettingsArrayToView()
614  {
615  $this->addMockViewToSubject();
616 
617  $configuration = array(
618  'settings.' => array(
619  'foo' => 'value',
620  'bar.' => array(
621  'baz' => 'value2',
622  ),
623  ),
624  );
625 
626  $expectedSettingsToBeSet = array(
627  'foo' => 'value',
628  'bar' => array(
629  'baz' => 'value2',
630  ),
631  );
632 
634  $typoScriptServiceMock = $this->getMock(\TYPO3\CMS\Extbase\Service\TypoScriptService::class);
635  $typoScriptServiceMock
636  ->expects($this->once())
637  ->method('convertTypoScriptArrayToPlainArray')
638  ->with($configuration['settings.'])
639  ->will($this->returnValue($expectedSettingsToBeSet));
640  GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Service\TypoScriptService::class, $typoScriptServiceMock);
641 
642  $this->standaloneView
643  ->expects($this->at(1))
644  ->method('assign')
645  ->with('settings', $expectedSettingsToBeSet);
646 
647  $this->subject->render($configuration);
648  }
649 
655  {
656  $this->addMockViewToSubject();
657  $configuration = array(
658  'variables.' => array(
659  'data' => 'foo',
660  'data.' => array(
661  'bar' => 'baz',
662  ),
663  ),
664  );
665  $this->subject->render($configuration);
666  }
667 
673  {
674  $this->addMockViewToSubject();
675  $configuration = array(
676  'variables.' => array(
677  'current' => 'foo',
678  'current.' => array(
679  'bar' => 'baz',
680  ),
681  ),
682  );
683  $this->subject->render($configuration);
684  }
685 
690  {
691  $this->addMockViewToSubject();
692  $configuration = array(
693  'variables.' => array(
694  'aVar' => 'TEXT',
695  'aVar.' => array(
696  'value' => 'foo',
697  ),
698  ),
699  );
700  $this->contentObjectRenderer
701  ->expects($this->once())
702  ->method('cObjGetSingle')
703  ->with('TEXT', array('value' => 'foo'));
704  $this->subject->render($configuration);
705  }
706 
711  {
712  $this->addMockViewToSubject();
713  $configuration = array(
714  'variables.' => array(
715  'aVar' => 'TEXT',
716  'aVar.' => array(
717  'value' => 'foo',
718  ),
719  ),
720  );
721  $this->contentObjectRenderer
722  ->expects($this->once())
723  ->method('cObjGetSingle')
724  ->will($this->returnValue('foo'));
725  $this->standaloneView
726  ->expects($this->once())
727  ->method('assignMultiple')
728  ->with(array('aVar' => 'foo', 'data' => array(), 'current' => null));
729  $this->subject->render($configuration);
730  }
731 
736  {
737  $this->addMockViewToSubject();
738  $this->contentObjectRenderer->data = array('foo');
739  $this->standaloneView
740  ->expects($this->once())
741  ->method('assignMultiple')
742  ->with(array('data' => array('foo'), 'current' => null));
743  $this->subject->render(array());
744  }
745 
750  {
751  $this->addMockViewToSubject();
752  $this->contentObjectRenderer->data = array('currentKey' => 'currentValue');
753  $this->contentObjectRenderer->currentValKey = 'currentKey';
754  $this->standaloneView
755  ->expects($this->once())
756  ->method('assignMultiple')
757  ->with(array('data' => array('currentKey' => 'currentValue'), 'current' => 'currentValue'));
758  $this->subject->render(array());
759  }
760 
765  {
766  $this->addMockViewToSubject();
767  $this->standaloneView
768  ->expects($this->once())
769  ->method('render');
770  $this->subject->render(array());
771  }
772 
777  {
778  $this->addMockViewToSubject();
779  $configuration = array(
780  'stdWrap.' => array(
781  'foo' => 'bar',
782  ),
783  );
784  $this->standaloneView
785  ->expects($this->any())
786  ->method('render')
787  ->will($this->returnValue('baz'));
788  $this->contentObjectRenderer
789  ->expects($this->once())
790  ->method('stdWrap')
791  ->with('baz', array('foo' => 'bar'));
792  $this->subject->render($configuration);
793  }
794 }