TYPO3 CMS  TYPO3_8-7
FluidTemplateContentObjectTest.php
Go to the documentation of this file.
1 <?php
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 
25 
29 class FluidTemplateContentObjectTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
30 {
34  protected $singletonInstances = [];
35 
39  protected $subject = null;
40 
44  protected $contentObjectRenderer = null;
45 
49  protected $standaloneView = null;
50 
54  protected $request = null;
55 
59  protected function setUp()
60  {
61  $this->singletonInstances = GeneralUtility::getSingletonInstances();
62  $this->contentObjectRenderer = $this->getMockBuilder(
63  \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class
64  )->getMock();
65  $this->subject = $this->getAccessibleMock(
66  \TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject::class,
67  ['initializeStandaloneViewInstance'],
68  [$this->contentObjectRenderer]
69  );
71  $tsfe = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
72  $tsfe->tmpl = $this->getMockBuilder(\TYPO3\CMS\Core\TypoScript\TemplateService::class)->getMock();
73  $GLOBALS['TSFE'] = $tsfe;
74  }
75 
79  protected function tearDown()
80  {
81  GeneralUtility::resetSingletonInstances($this->singletonInstances);
82  parent::tearDown();
83  }
84 
88  protected function addMockViewToSubject()
89  {
90  $this->standaloneView = $this->createMock(\TYPO3\CMS\Fluid\View\StandaloneView::class);
91  $this->request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)->getMock();
92  $this->standaloneView
93  ->expects($this->any())
94  ->method('getRequest')
95  ->will($this->returnValue($this->request));
96  $this->subject->_set('view', $this->standaloneView);
97  }
98 
103  {
104  $this->assertSame($this->contentObjectRenderer, $this->subject->getContentObjectRenderer());
105  }
106 
111  {
112  $this->addMockViewToSubject();
113  $this->subject
114  ->expects($this->once())
115  ->method('initializeStandaloneViewInstance');
116  $this->subject->render([]);
117  }
118 
122  public function renderCallsTemplateServiceGetFileNameForGivenTemplateFile()
123  {
124  $this->addMockViewToSubject();
126  $templateService = $GLOBALS['TSFE']->tmpl;
127  $templateService
128  ->expects($this->any())
129  ->method('getFileName')
130  ->with('foo');
131  $this->subject->render(['file' => 'foo']);
132  }
133 
138  {
139  $this->addMockViewToSubject();
140  $this->contentObjectRenderer
141  ->expects($this->any())
142  ->method('stdWrap')
143  ->with('foo', ['bar' => 'baz']);
144  $this->subject->render(['file' => 'foo', 'file.' => ['bar' => 'baz']]);
145  }
146 
151  {
152  $this->addMockViewToSubject();
153  $this->contentObjectRenderer
154  ->expects($this->at(0))
155  ->method('stdWrap')
156  ->with('dummyPath', ['wrap' => '|5/']);
157  $this->contentObjectRenderer
158  ->expects($this->at(1))
159  ->method('stdWrap')
160  ->with('', ['field' => 'someField']);
161  $this->subject->render(
162  [
163  'templateName' => 'foobar',
164  'templateRootPaths.' => [
165  10 => 'dummyPath',
166  '10.' => [
167  'wrap' => '|5/',
168  ],
169  15 => 'dummyPath6/',
170  '25.' => [
171  'field' => 'someField',
172  ],
173  ]
174  ]
175  );
176  }
177 
181  public function renderSetsTemplateFileInView()
182  {
183  $this->addMockViewToSubject();
185  $templateService = $GLOBALS['TSFE']->tmpl;
186  $templateService
187  ->expects($this->any())
188  ->method('getFileName')
189  ->with('foo')
190  ->will($this->returnValue('bar'));
191  $this->standaloneView
192  ->expects($this->any())
193  ->method('setTemplatePathAndFilename')
194  ->with(PATH_site . 'bar');
195  $this->subject->render(['file' => 'foo']);
196  }
197 
202  {
203  $this->addMockViewToSubject();
204 
205  $this->contentObjectRenderer
206  ->expects($this->any())
207  ->method('cObjGetSingle')
208  ->with('FILE', ['file' => PATH_site . 'foo/bar.html'])
209  ->will($this->returnValue('baz'));
210 
211  $this->standaloneView
212  ->expects($this->any())
213  ->method('setTemplateSource')
214  ->with('baz');
215 
216  $this->subject->render([
217  'template' => 'FILE',
218  'template.' => [
219  'file' => PATH_site . 'foo/bar.html'
220  ]
221  ]);
222  }
223 
228  {
229  $this->addMockViewToSubject();
230 
231  $this->standaloneView
232  ->expects($this->any())
233  ->method('getFormat')
234  ->will($this->returnValue('html'));
235  $this->standaloneView
236  ->expects($this->once())
237  ->method('setTemplate')
238  ->with('foo');
239 
240  $this->subject->render(
241  [
242  'templateName' => 'foo',
243  'templateRootPaths.' => [
244  0 => 'dummyPath1/',
245  1 => 'dummyPath2/']
246  ]
247  );
248  }
249 
254  {
255  $this->addMockViewToSubject();
256 
257  $this->contentObjectRenderer
258  ->expects($this->once())
259  ->method('stdWrap')
260  ->with('TEXT', ['value' => 'bar'])
261  ->will($this->returnValue('bar'));
262  $this->standaloneView
263  ->expects($this->any())
264  ->method('getFormat')
265  ->will($this->returnValue('html'));
266  $this->standaloneView
267  ->expects($this->once())
268  ->method('setTemplate')
269  ->with('bar');
270 
271  $this->subject->render(
272  [
273  'templateName' => 'TEXT',
274  'templateName.' => ['value' => 'bar'],
275  'templateRootPaths.' => [
276  0 => 'dummyPath1/',
277  1 => 'dummyPath2/']
278  ]
279  );
280  }
281 
286  {
287  $this->addMockViewToSubject();
288  $this->standaloneView
289  ->expects($this->once())
290  ->method('setLayoutRootPaths')
291  ->with([PATH_site . 'foo/bar.html']);
292  $this->subject->render(['layoutRootPath' => 'foo/bar.html']);
293  }
294 
299  {
300  $this->addMockViewToSubject();
301  $this->contentObjectRenderer
302  ->expects($this->once())
303  ->method('stdWrap')
304  ->with('foo', ['bar' => 'baz']);
305  $this->subject->render(['layoutRootPath' => 'foo', 'layoutRootPath.' => ['bar' => 'baz']]);
306  }
307 
312  {
313  $this->addMockViewToSubject();
314  $this->contentObjectRenderer
315  ->expects($this->at(0))
316  ->method('stdWrap')
317  ->with('FILE', ['file' => 'foo/bar.html']);
318  $this->subject->render(
319  [
320  'layoutRootPaths.' => [
321  10 => 'FILE',
322  '10.' => [
323  'file' => 'foo/bar.html',
324  ],
325  20 => 'foo/bar2.html',
326  ]
327  ]
328  );
329  }
330 
335  {
336  $this->addMockViewToSubject();
337  $this->standaloneView
338  ->expects($this->once())
339  ->method('setLayoutRootPaths')
340  ->with([10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html']);
341  $this->subject->render(['layoutRootPaths.' => [10 => 'foo/bar.html', 20 => 'foo/bar2.html']]);
342  }
343 
348  {
349  $this->addMockViewToSubject();
350  $this->standaloneView
351  ->expects($this->once())
352  ->method('setLayoutRootPaths')
353  ->with([0 => PATH_site . 'foo/main.html', 10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html']);
354  $this->subject->render(['layoutRootPath' => 'foo/main.html', 'layoutRootPaths.' => [10 => 'foo/bar.html', 20 => 'foo/bar2.html']]);
355  }
356 
361  {
362  $this->addMockViewToSubject();
363  $this->standaloneView
364  ->expects($this->once())
365  ->method('setPartialRootPaths')
366  ->with([PATH_site . 'foo/bar.html']);
367  $this->subject->render(['partialRootPath' => 'foo/bar.html']);
368  }
369 
374  {
375  $this->addMockViewToSubject();
376  $this->contentObjectRenderer
377  ->expects($this->at(0))
378  ->method('stdWrap')
379  ->with('FILE', ['file' => 'foo/bar.html']);
380  $this->subject->render(
381  [
382  'partialRootPaths.' => [
383  10 => 'FILE',
384  '10.' => [
385  'file' => 'foo/bar.html',
386  ],
387  20 => 'foo/bar2.html',
388  ]
389  ]
390  );
391  }
392 
397  {
398  $this->addMockViewToSubject();
399  $this->contentObjectRenderer
400  ->expects($this->once())
401  ->method('stdWrap')
402  ->with('foo', ['bar' => 'baz']);
403  $this->subject->render(['partialRootPath' => 'foo', 'partialRootPath.' => ['bar' => 'baz']]);
404  }
405 
410  {
411  $this->addMockViewToSubject();
412  $this->standaloneView
413  ->expects($this->once())
414  ->method('setPartialRootPaths')
415  ->with([10 => PATH_site . 'foo', 20 => PATH_site . 'bar']);
416  $this->subject->render(['partialRootPaths.' => [10 => 'foo', 20 => 'bar']]);
417  }
418 
423  {
424  $this->addMockViewToSubject();
425  $this->standaloneView
426  ->expects($this->once())
427  ->method('setPartialRootPaths')
428  ->with([0 => PATH_site . 'main', 10 => PATH_site . 'foo', 20 => PATH_site . 'bar']);
429  $this->subject->render(['partialRootPath' => 'main', 'partialRootPaths.' => [10 => 'foo', 20 => 'bar']]);
430  }
431 
435  public function renderSetsFormatInView()
436  {
437  $this->addMockViewToSubject();
438  $this->standaloneView
439  ->expects($this->once())
440  ->method('setFormat')
441  ->with('xml');
442  $this->subject->render(['format' => 'xml']);
443  }
444 
449  {
450  $this->addMockViewToSubject();
451  $this->contentObjectRenderer
452  ->expects($this->once())
453  ->method('stdWrap')
454  ->with('foo', ['bar' => 'baz']);
455  $this->subject->render(['format' => 'foo', 'format.' => ['bar' => 'baz']]);
456  }
457 
462  {
463  $this->addMockViewToSubject();
464  $this->request
465  ->expects($this->once())
466  ->method('setPluginName')
467  ->with('foo');
468  $configuration = [
469  'extbase.' => [
470  'pluginName' => 'foo',
471  ],
472  ];
473  $this->subject->render($configuration);
474  }
475 
480  {
481  $this->addMockViewToSubject();
482  $this->contentObjectRenderer
483  ->expects($this->once())
484  ->method('stdWrap')
485  ->with('foo', ['bar' => 'baz']);
486  $configuration = [
487  'extbase.' => [
488  'pluginName' => 'foo',
489  'pluginName.' => [
490  'bar' => 'baz',
491  ],
492  ],
493  ];
494  $this->subject->render($configuration);
495  }
496 
501  {
502  $this->addMockViewToSubject();
503  $this->request
504  ->expects($this->once())
505  ->method('setControllerExtensionName')
506  ->with('foo');
507  $configuration = [
508  'extbase.' => [
509  'controllerExtensionName' => 'foo',
510  ],
511  ];
512  $this->subject->render($configuration);
513  }
514 
519  {
520  $this->addMockViewToSubject();
521  $this->contentObjectRenderer
522  ->expects($this->once())
523  ->method('stdWrap')
524  ->with('foo', ['bar' => 'baz']);
525  $configuration = [
526  'extbase.' => [
527  'controllerExtensionName' => 'foo',
528  'controllerExtensionName.' => [
529  'bar' => 'baz',
530  ],
531  ],
532  ];
533  $this->subject->render($configuration);
534  }
535 
540  {
541  $this->addMockViewToSubject();
542  $this->request
543  ->expects($this->once())
544  ->method('setControllerName')
545  ->with('foo');
546  $configuration = [
547  'extbase.' => [
548  'controllerName' => 'foo',
549  ],
550  ];
551  $this->subject->render($configuration);
552  }
553 
558  {
559  $this->addMockViewToSubject();
560  $this->contentObjectRenderer
561  ->expects($this->once())
562  ->method('stdWrap')
563  ->with('foo', ['bar' => 'baz']);
564  $configuration = [
565  'extbase.' => [
566  'controllerName' => 'foo',
567  'controllerName.' => [
568  'bar' => 'baz',
569  ],
570  ],
571  ];
572  $this->subject->render($configuration);
573  }
574 
579  {
580  $this->addMockViewToSubject();
581  $this->request
582  ->expects($this->once())
583  ->method('setControllerActionName')
584  ->with('foo');
585  $configuration = [
586  'extbase.' => [
587  'controllerActionName' => 'foo',
588  ],
589  ];
590  $this->subject->render($configuration);
591  }
592 
597  {
598  $this->addMockViewToSubject();
599  $this->contentObjectRenderer
600  ->expects($this->once())
601  ->method('stdWrap')
602  ->with('foo', ['bar' => 'baz']);
603  $configuration = [
604  'extbase.' => [
605  'controllerActionName' => 'foo',
606  'controllerActionName.' => [
607  'bar' => 'baz',
608  ],
609  ],
610  ];
611  $this->subject->render($configuration);
612  }
613 
617  public function renderAssignsSettingsArrayToView()
618  {
619  $this->addMockViewToSubject();
620 
621  $configuration = [
622  'settings.' => [
623  'foo' => 'value',
624  'bar.' => [
625  'baz' => 'value2',
626  ],
627  ],
628  ];
629 
630  $expectedSettingsToBeSet = [
631  'foo' => 'value',
632  'bar' => [
633  'baz' => 'value2',
634  ],
635  ];
636 
638  $typoScriptServiceMock = $this->getMockBuilder(TypoScriptService::class)->getMock();
639  $typoScriptServiceMock
640  ->expects($this->once())
641  ->method('convertTypoScriptArrayToPlainArray')
642  ->with($configuration['settings.'])
643  ->will($this->returnValue($expectedSettingsToBeSet));
644  GeneralUtility::addInstance(TypoScriptService::class, $typoScriptServiceMock);
645 
646  $this->standaloneView
647  ->expects($this->at(1))
648  ->method('assign')
649  ->with('settings', $expectedSettingsToBeSet);
650 
651  $this->subject->render($configuration);
652  }
653 
658  {
659  $this->addMockViewToSubject();
660  $configuration = [
661  'variables.' => [
662  'data' => 'foo',
663  'data.' => [
664  'bar' => 'baz',
665  ],
666  ],
667  ];
668  $this->expectException(\InvalidArgumentException::class);
669  $this->expectExceptionCode(1288095720);
670  $this->subject->render($configuration);
671  }
672 
677  {
678  $this->addMockViewToSubject();
679  $configuration = [
680  'variables.' => [
681  'current' => 'foo',
682  'current.' => [
683  'bar' => 'baz',
684  ],
685  ],
686  ];
687  $this->expectException(\InvalidArgumentException::class);
688  $this->expectExceptionCode(1288095720);
689  $this->subject->render($configuration);
690  }
691 
696  {
697  $this->addMockViewToSubject();
698  $configuration = [
699  'variables.' => [
700  'aVar' => 'TEXT',
701  'aVar.' => [
702  'value' => 'foo',
703  ],
704  ],
705  ];
706  $this->contentObjectRenderer
707  ->expects($this->once())
708  ->method('cObjGetSingle')
709  ->with('TEXT', ['value' => 'foo']);
710  $this->subject->render($configuration);
711  }
712 
717  {
718  $this->addMockViewToSubject();
719  $configuration = [
720  'variables.' => [
721  'aVar' => 'TEXT',
722  'aVar.' => [
723  'value' => 'foo',
724  ],
725  ],
726  ];
727  $this->contentObjectRenderer
728  ->expects($this->once())
729  ->method('cObjGetSingle')
730  ->will($this->returnValue('foo'));
731  $this->standaloneView
732  ->expects($this->once())
733  ->method('assignMultiple')
734  ->with(['aVar' => 'foo', 'data' => [], 'current' => null]);
735  $this->subject->render($configuration);
736  }
737 
742  {
743  $this->addMockViewToSubject();
744  $this->contentObjectRenderer->data = ['foo'];
745  $this->standaloneView
746  ->expects($this->once())
747  ->method('assignMultiple')
748  ->with(['data' => ['foo'], 'current' => null]);
749  $this->subject->render([]);
750  }
751 
756  {
757  $this->addMockViewToSubject();
758  $this->contentObjectRenderer->data = ['currentKey' => 'currentValue'];
759  $this->contentObjectRenderer->currentValKey = 'currentKey';
760  $this->standaloneView
761  ->expects($this->once())
762  ->method('assignMultiple')
763  ->with(['data' => ['currentKey' => 'currentValue'], 'current' => 'currentValue']);
764  $this->subject->render([]);
765  }
766 
771  {
772  $this->addMockViewToSubject();
773  $this->standaloneView
774  ->expects($this->once())
775  ->method('render');
776  $this->subject->render([]);
777  }
778 
783  {
784  $this->addMockViewToSubject();
785  $configuration = [
786  'stdWrap.' => [
787  'foo' => 'bar',
788  ],
789  ];
790  $this->standaloneView
791  ->expects($this->any())
792  ->method('render')
793  ->will($this->returnValue('baz'));
794  $this->contentObjectRenderer
795  ->expects($this->once())
796  ->method('stdWrap')
797  ->with('baz', ['foo' => 'bar']);
798  $this->subject->render($configuration);
799  }
800 
808  public function renderFluidTemplateAssetsIntoPageRendererRendersAndAttachesAssets($viewMock, $expectedHeader, $expectedFooter)
809  {
810  $pageRendererMock = $this->getMockBuilder(PageRenderer::class)->setMethods(['addHeaderData', 'addFooterData'])->getMock();
811  if (!empty(trim($expectedHeader))) {
812  $pageRendererMock->expects($this->once())->method('addHeaderData')->with($expectedHeader);
813  } else {
814  $pageRendererMock->expects($this->never())->method('addHeaderData');
815  }
816  if (!empty(trim($expectedFooter))) {
817  $pageRendererMock->expects($this->once())->method('addFooterData')->with($expectedFooter);
818  } else {
819  $pageRendererMock->expects($this->never())->method('addFooterData');
820  }
821  $subject = $this->getMockBuilder(FluidTemplateContentObject::class)->setMethods(['getPageRenderer'])->disableOriginalConstructor()->getMock();
822  $subject->expects($this->once())->method('getPageRenderer')->willReturn($pageRendererMock);
823  $viewProperty = new \ReflectionProperty($subject, 'view');
824  $viewProperty->setAccessible(true);
825  $viewProperty->setValue($subject, $viewMock);
826 
827  $method = new \ReflectionMethod($subject, 'renderFluidTemplateAssetsIntoPageRenderer');
828  $method->setAccessible(true);
829  $method->invoke($subject);
830  }
831 
835  public function headerAssetDataProvider()
836  {
837  $viewWithHeaderData = $this->getMockBuilder(TemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
838  $viewWithHeaderData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn('custom-header-data');
839  $viewWithHeaderData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn(null);
840  $viewWithFooterData = $this->getMockBuilder(TemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
841  $viewWithFooterData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn(null);
842  $viewWithFooterData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn('custom-footer-data');
843  $viewWithBothData = $this->getMockBuilder(TemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
844  $viewWithBothData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn('custom-header-data');
845  $viewWithBothData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn('custom-footer-data');
846  return [
847  [$viewWithHeaderData, 'custom-header-data', null],
848  [$viewWithFooterData, null, 'custom-footer-data'],
849  [$viewWithBothData, 'custom-header-data', 'custom-footer-data']
850  ];
851  }
852 }
static addInstance($className, $instance)
static resetSingletonInstances(array $newSingletonInstances)
renderFluidTemplateAssetsIntoPageRendererRendersAndAttachesAssets($viewMock, $expectedHeader, $expectedFooter)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']