TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
28 {
32  protected $singletonInstances = [];
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->getMockBuilder(
61  \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class
62  )->getMock();
63  $this->subject = $this->getAccessibleMock(
64  \TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject::class,
65  ['initializeStandaloneViewInstance'],
66  [$this->contentObjectRenderer]
67  );
69  $tsfe = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
70  $tsfe->tmpl = $this->getMockBuilder(\TYPO3\CMS\Core\TypoScript\TemplateService::class)->getMock();
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->createMock(\TYPO3\CMS\Fluid\View\StandaloneView::class);
89  $this->request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)->getMock();
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->getContentObjectRenderer());
103  }
104 
109  {
110  $this->addMockViewToSubject();
111  $this->subject
112  ->expects($this->once())
113  ->method('initializeStandaloneViewInstance');
114  $this->subject->render([]);
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(['file' => 'foo']);
130  }
131 
136  {
137  $this->addMockViewToSubject();
138  $this->contentObjectRenderer
139  ->expects($this->any())
140  ->method('stdWrap')
141  ->with('foo', ['bar' => 'baz']);
142  $this->subject->render(['file' => 'foo', 'file.' => ['bar' => 'baz']]);
143  }
144 
149  {
150  $this->addMockViewToSubject();
151  $this->contentObjectRenderer
152  ->expects($this->at(0))
153  ->method('stdWrap')
154  ->with('dummyPath', ['wrap' => '|5/']);
155  $this->contentObjectRenderer
156  ->expects($this->at(1))
157  ->method('stdWrap')
158  ->with('', ['field' => 'someField']);
159  $this->subject->render([
160  'templateName' => 'foobar',
161  'templateRootPaths.' => [
162  10 => 'dummyPath',
163  '10.' => [
164  'wrap' => '|5/',
165  ],
166  15 => 'dummyPath6/',
167  '25.' => [
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(['file' => 'foo']);
193  }
194 
199  {
200  $this->addMockViewToSubject();
201 
202  $this->contentObjectRenderer
203  ->expects($this->any())
204  ->method('cObjGetSingle')
205  ->with('FILE', ['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([
214  'template' => 'FILE',
215  'template.' => [
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([
238  'templateName' => 'foo',
239  'templateRootPaths.' => [
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', ['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([
268  'templateName' => 'TEXT',
269  'templateName.' => ['value' => 'bar'],
270  'templateRootPaths.' => [
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([PATH_site . 'foo/bar.html']);
287  $this->subject->render(['layoutRootPath' => 'foo/bar.html']);
288  }
289 
294  {
295  $this->addMockViewToSubject();
296  $this->contentObjectRenderer
297  ->expects($this->once())
298  ->method('stdWrap')
299  ->with('foo', ['bar' => 'baz']);
300  $this->subject->render(['layoutRootPath' => 'foo', 'layoutRootPath.' => ['bar' => 'baz']]);
301  }
302 
307  {
308  $this->addMockViewToSubject();
309  $this->contentObjectRenderer
310  ->expects($this->at(0))
311  ->method('stdWrap')
312  ->with('FILE', ['file' => 'foo/bar.html']);
313  $this->subject->render(
314  [
315  'layoutRootPaths.' => [
316  10 => 'FILE',
317  '10.' => [
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([10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html']);
336  $this->subject->render(['layoutRootPaths.' => [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([0 => PATH_site . 'foo/main.html', 10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html']);
349  $this->subject->render(['layoutRootPath' => 'foo/main.html', 'layoutRootPaths.' => [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([PATH_site . 'foo/bar.html']);
362  $this->subject->render(['partialRootPath' => 'foo/bar.html']);
363  }
364 
369  {
370  $this->addMockViewToSubject();
371  $this->contentObjectRenderer
372  ->expects($this->at(0))
373  ->method('stdWrap')
374  ->with('FILE', ['file' => 'foo/bar.html']);
375  $this->subject->render(
376  [
377  'partialRootPaths.' => [
378  10 => 'FILE',
379  '10.' => [
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', ['bar' => 'baz']);
398  $this->subject->render(['partialRootPath' => 'foo', 'partialRootPath.' => ['bar' => 'baz']]);
399  }
400 
405  {
406  $this->addMockViewToSubject();
407  $this->standaloneView
408  ->expects($this->once())
409  ->method('setPartialRootPaths')
410  ->with([10 => PATH_site . 'foo', 20 => PATH_site . 'bar']);
411  $this->subject->render(['partialRootPaths.' => [10 => 'foo', 20 => 'bar']]);
412  }
413 
418  {
419  $this->addMockViewToSubject();
420  $this->standaloneView
421  ->expects($this->once())
422  ->method('setPartialRootPaths')
423  ->with([0 => PATH_site . 'main', 10 => PATH_site . 'foo', 20 => PATH_site . 'bar']);
424  $this->subject->render(['partialRootPath' => 'main', 'partialRootPaths.' => [10 => 'foo', 20 => 'bar']]);
425  }
426 
430  public function renderSetsFormatInView()
431  {
432  $this->addMockViewToSubject();
433  $this->standaloneView
434  ->expects($this->once())
435  ->method('setFormat')
436  ->with('xml');
437  $this->subject->render(['format' => 'xml']);
438  }
439 
444  {
445  $this->addMockViewToSubject();
446  $this->contentObjectRenderer
447  ->expects($this->once())
448  ->method('stdWrap')
449  ->with('foo', ['bar' => 'baz']);
450  $this->subject->render(['format' => 'foo', 'format.' => ['bar' => 'baz']]);
451  }
452 
457  {
458  $this->addMockViewToSubject();
459  $this->request
460  ->expects($this->once())
461  ->method('setPluginName')
462  ->with('foo');
463  $configuration = [
464  'extbase.' => [
465  'pluginName' => 'foo',
466  ],
467  ];
468  $this->subject->render($configuration);
469  }
470 
475  {
476  $this->addMockViewToSubject();
477  $this->contentObjectRenderer
478  ->expects($this->once())
479  ->method('stdWrap')
480  ->with('foo', ['bar' => 'baz']);
481  $configuration = [
482  'extbase.' => [
483  'pluginName' => 'foo',
484  'pluginName.' => [
485  'bar' => 'baz',
486  ],
487  ],
488  ];
489  $this->subject->render($configuration);
490  }
491 
496  {
497  $this->addMockViewToSubject();
498  $this->request
499  ->expects($this->once())
500  ->method('setControllerExtensionName')
501  ->with('foo');
502  $configuration = [
503  'extbase.' => [
504  'controllerExtensionName' => 'foo',
505  ],
506  ];
507  $this->subject->render($configuration);
508  }
509 
514  {
515  $this->addMockViewToSubject();
516  $this->contentObjectRenderer
517  ->expects($this->once())
518  ->method('stdWrap')
519  ->with('foo', ['bar' => 'baz']);
520  $configuration = [
521  'extbase.' => [
522  'controllerExtensionName' => 'foo',
523  'controllerExtensionName.' => [
524  'bar' => 'baz',
525  ],
526  ],
527  ];
528  $this->subject->render($configuration);
529  }
530 
535  {
536  $this->addMockViewToSubject();
537  $this->request
538  ->expects($this->once())
539  ->method('setControllerName')
540  ->with('foo');
541  $configuration = [
542  'extbase.' => [
543  'controllerName' => 'foo',
544  ],
545  ];
546  $this->subject->render($configuration);
547  }
548 
553  {
554  $this->addMockViewToSubject();
555  $this->contentObjectRenderer
556  ->expects($this->once())
557  ->method('stdWrap')
558  ->with('foo', ['bar' => 'baz']);
559  $configuration = [
560  'extbase.' => [
561  'controllerName' => 'foo',
562  'controllerName.' => [
563  'bar' => 'baz',
564  ],
565  ],
566  ];
567  $this->subject->render($configuration);
568  }
569 
574  {
575  $this->addMockViewToSubject();
576  $this->request
577  ->expects($this->once())
578  ->method('setControllerActionName')
579  ->with('foo');
580  $configuration = [
581  'extbase.' => [
582  'controllerActionName' => 'foo',
583  ],
584  ];
585  $this->subject->render($configuration);
586  }
587 
592  {
593  $this->addMockViewToSubject();
594  $this->contentObjectRenderer
595  ->expects($this->once())
596  ->method('stdWrap')
597  ->with('foo', ['bar' => 'baz']);
598  $configuration = [
599  'extbase.' => [
600  'controllerActionName' => 'foo',
601  'controllerActionName.' => [
602  'bar' => 'baz',
603  ],
604  ],
605  ];
606  $this->subject->render($configuration);
607  }
608 
612  public function renderAssignsSettingsArrayToView()
613  {
614  $this->addMockViewToSubject();
615 
616  $configuration = [
617  'settings.' => [
618  'foo' => 'value',
619  'bar.' => [
620  'baz' => 'value2',
621  ],
622  ],
623  ];
624 
625  $expectedSettingsToBeSet = [
626  'foo' => 'value',
627  'bar' => [
628  'baz' => 'value2',
629  ],
630  ];
631 
633  $typoScriptServiceMock = $this->getMockBuilder(\TYPO3\CMS\Extbase\Service\TypoScriptService::class)->getMock();
634  $typoScriptServiceMock
635  ->expects($this->once())
636  ->method('convertTypoScriptArrayToPlainArray')
637  ->with($configuration['settings.'])
638  ->will($this->returnValue($expectedSettingsToBeSet));
639  GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Service\TypoScriptService::class, $typoScriptServiceMock);
640 
641  $this->standaloneView
642  ->expects($this->at(1))
643  ->method('assign')
644  ->with('settings', $expectedSettingsToBeSet);
645 
646  $this->subject->render($configuration);
647  }
648 
653  {
654  $this->addMockViewToSubject();
655  $configuration = [
656  'variables.' => [
657  'data' => 'foo',
658  'data.' => [
659  'bar' => 'baz',
660  ],
661  ],
662  ];
663  $this->expectException(\InvalidArgumentException::class);
664  $this->expectExceptionCode(1288095720);
665  $this->subject->render($configuration);
666  }
667 
672  {
673  $this->addMockViewToSubject();
674  $configuration = [
675  'variables.' => [
676  'current' => 'foo',
677  'current.' => [
678  'bar' => 'baz',
679  ],
680  ],
681  ];
682  $this->expectException(\InvalidArgumentException::class);
683  $this->expectExceptionCode(1288095720);
684  $this->subject->render($configuration);
685  }
686 
691  {
692  $this->addMockViewToSubject();
693  $configuration = [
694  'variables.' => [
695  'aVar' => 'TEXT',
696  'aVar.' => [
697  'value' => 'foo',
698  ],
699  ],
700  ];
701  $this->contentObjectRenderer
702  ->expects($this->once())
703  ->method('cObjGetSingle')
704  ->with('TEXT', ['value' => 'foo']);
705  $this->subject->render($configuration);
706  }
707 
712  {
713  $this->addMockViewToSubject();
714  $configuration = [
715  'variables.' => [
716  'aVar' => 'TEXT',
717  'aVar.' => [
718  'value' => 'foo',
719  ],
720  ],
721  ];
722  $this->contentObjectRenderer
723  ->expects($this->once())
724  ->method('cObjGetSingle')
725  ->will($this->returnValue('foo'));
726  $this->standaloneView
727  ->expects($this->once())
728  ->method('assignMultiple')
729  ->with(['aVar' => 'foo', 'data' => [], 'current' => null]);
730  $this->subject->render($configuration);
731  }
732 
737  {
738  $this->addMockViewToSubject();
739  $this->contentObjectRenderer->data = ['foo'];
740  $this->standaloneView
741  ->expects($this->once())
742  ->method('assignMultiple')
743  ->with(['data' => ['foo'], 'current' => null]);
744  $this->subject->render([]);
745  }
746 
751  {
752  $this->addMockViewToSubject();
753  $this->contentObjectRenderer->data = ['currentKey' => 'currentValue'];
754  $this->contentObjectRenderer->currentValKey = 'currentKey';
755  $this->standaloneView
756  ->expects($this->once())
757  ->method('assignMultiple')
758  ->with(['data' => ['currentKey' => 'currentValue'], 'current' => 'currentValue']);
759  $this->subject->render([]);
760  }
761 
766  {
767  $this->addMockViewToSubject();
768  $this->standaloneView
769  ->expects($this->once())
770  ->method('render');
771  $this->subject->render([]);
772  }
773 
778  {
779  $this->addMockViewToSubject();
780  $configuration = [
781  'stdWrap.' => [
782  'foo' => 'bar',
783  ],
784  ];
785  $this->standaloneView
786  ->expects($this->any())
787  ->method('render')
788  ->will($this->returnValue('baz'));
789  $this->contentObjectRenderer
790  ->expects($this->once())
791  ->method('stdWrap')
792  ->with('baz', ['foo' => 'bar']);
793  $this->subject->render($configuration);
794  }
795 }
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getAccessibleMock($originalClassName, $methods=[], array $arguments=[], $mockClassName= '', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)