[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / ContentObject / FluidTemplateContentObjectTest.php
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Mvc\Request;
19 use TYPO3\CMS\Extbase\Service\TypoScriptService;
20 use TYPO3\CMS\Fluid\View\StandaloneView;
21 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22 use TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject;
23
24 /**
25 * Testcase
26 */
27 class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
28 {
29 /**
30 * @var array A backup of registered singleton instances
31 */
32 protected $singletonInstances = array();
33
34 /**
35 * @var FluidTemplateContentObject|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
36 */
37 protected $subject = null;
38
39 /**
40 * @var ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
41 */
42 protected $contentObjectRenderer = null;
43
44 /**
45 * @var StandaloneView|\PHPUnit_Framework_MockObject_MockObject
46 */
47 protected $standaloneView = null;
48
49 /**
50 * @var Request|\PHPUnit_Framework_MockObject_MockObject
51 */
52 protected $request = null;
53
54 /**
55 * Set up
56 */
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 );
68 /** @var $tsfe \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
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
74 /**
75 * Tear down
76 */
77 protected function tearDown()
78 {
79 GeneralUtility::resetSingletonInstances($this->singletonInstances);
80 parent::tearDown();
81 }
82
83 /**
84 * Add a mock standalone view to subject
85 */
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
97 /**
98 * @test
99 */
100 public function constructSetsContentObjectRenderer()
101 {
102 $this->assertSame($this->contentObjectRenderer, $this->subject->getContentObject());
103 }
104
105 /**
106 * @test
107 */
108 public function renderCallsInitializeStandaloneViewInstance()
109 {
110 $this->addMockViewToSubject();
111 $this->subject
112 ->expects($this->once())
113 ->method('initializeStandaloneViewInstance');
114 $this->subject->render(array());
115 }
116
117 /**
118 * @test
119 */
120 public function renderCallsTemplateServiceGetFileNameForGivenTemplateFile()
121 {
122 $this->addMockViewToSubject();
123 /** @var $templateService \PHPUnit_Framework_MockObject_MockObject */
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
132 /**
133 * @test
134 */
135 public function renderCallsStandardWrapForGivenTemplateFileWithStandardWrap()
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
145 /**
146 * @test
147 */
148 public function renderCallsStandardWrapForGivenTemplateRootPathsWithStandardWrap()
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
175 /**
176 * @test
177 */
178 public function renderSetsTemplateFileInView()
179 {
180 $this->addMockViewToSubject();
181 /** @var $templateService \PHPUnit_Framework_MockObject_MockObject */
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
195 /**
196 * @test
197 */
198 public function renderSetsTemplateFileByTemplateInView()
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
221 /**
222 * @test
223 */
224 public function renderSetsTemplateFileByTemplateNameInView()
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
246 /**
247 * @test
248 */
249 public function renderSetsTemplateFileByTemplateNameStdWrapInView()
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
277 /**
278 * @test
279 */
280 public function renderSetsLayoutRootPathInView()
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
290 /**
291 * @test
292 */
293 public function renderCallsStandardWrapForLayoutRootPath()
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
303 /**
304 * @test
305 */
306 public function layoutRootPathsHasStdWrapSupport()
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
326 /**
327 * @test
328 */
329 public function fallbacksForLayoutRootPathAreSet()
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
339 /**
340 * @test
341 */
342 public function fallbacksForLayoutRootPathAreAppendedToLayoutRootPath()
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
352 /**
353 * @test
354 */
355 public function renderSetsPartialRootPathInView()
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
365 /**
366 * @test
367 */
368 public function partialRootPathsHasStdWrapSupport()
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
388 /**
389 * @test
390 */
391 public function renderCallsStandardWrapForPartialRootPath()
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 /**
402 * @test
403 */
404 public function fallbacksForPartialRootPathAreSet()
405 {
406 $this->addMockViewToSubject();
407 $this->standaloneView
408 ->expects($this->once())
409 ->method('setPartialRootPaths')
410 ->with(array(10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
411 $this->subject->render(array('partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
412 }
413
414 /**
415 * @test
416 */
417 public function fallbacksForPartialRootPathAreAppendedToPartialRootPath()
418 {
419 $this->addMockViewToSubject();
420 $this->standaloneView
421 ->expects($this->once())
422 ->method('setPartialRootPaths')
423 ->with(array(0 => PATH_site . 'main', 10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
424 $this->subject->render(array('partialRootPath' => 'main', 'partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
425 }
426
427 /**
428 * @test
429 */
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(array('format' => 'xml'));
438 }
439
440 /**
441 * @test
442 */
443 public function renderCallsStandardWrapForFormat()
444 {
445 $this->addMockViewToSubject();
446 $this->contentObjectRenderer
447 ->expects($this->once())
448 ->method('stdWrap')
449 ->with('foo', array('bar' => 'baz'));
450 $this->subject->render(array('format' => 'foo', 'format.' => array('bar' => 'baz')));
451 }
452
453 /**
454 * @test
455 */
456 public function renderSetsExtbasePluginNameInRequest()
457 {
458 $this->addMockViewToSubject();
459 $this->request
460 ->expects($this->once())
461 ->method('setPluginName')
462 ->with('foo');
463 $configuration = array(
464 'extbase.' => array(
465 'pluginName' => 'foo',
466 ),
467 );
468 $this->subject->render($configuration);
469 }
470
471 /**
472 * @test
473 */
474 public function renderCallsStandardWrapForExtbasePluginName()
475 {
476 $this->addMockViewToSubject();
477 $this->contentObjectRenderer
478 ->expects($this->once())
479 ->method('stdWrap')
480 ->with('foo', array('bar' => 'baz'));
481 $configuration = array(
482 'extbase.' => array(
483 'pluginName' => 'foo',
484 'pluginName.' => array(
485 'bar' => 'baz',
486 ),
487 ),
488 );
489 $this->subject->render($configuration);
490 }
491
492 /**
493 * @test
494 */
495 public function renderSetsExtbaseControllerExtensionNameInRequest()
496 {
497 $this->addMockViewToSubject();
498 $this->request
499 ->expects($this->once())
500 ->method('setControllerExtensionName')
501 ->with('foo');
502 $configuration = array(
503 'extbase.' => array(
504 'controllerExtensionName' => 'foo',
505 ),
506 );
507 $this->subject->render($configuration);
508 }
509
510 /**
511 * @test
512 */
513 public function renderCallsStandardWrapForExtbaseControllerExtensionName()
514 {
515 $this->addMockViewToSubject();
516 $this->contentObjectRenderer
517 ->expects($this->once())
518 ->method('stdWrap')
519 ->with('foo', array('bar' => 'baz'));
520 $configuration = array(
521 'extbase.' => array(
522 'controllerExtensionName' => 'foo',
523 'controllerExtensionName.' => array(
524 'bar' => 'baz',
525 ),
526 ),
527 );
528 $this->subject->render($configuration);
529 }
530
531 /**
532 * @test
533 */
534 public function renderSetsExtbaseControllerNameInRequest()
535 {
536 $this->addMockViewToSubject();
537 $this->request
538 ->expects($this->once())
539 ->method('setControllerName')
540 ->with('foo');
541 $configuration = array(
542 'extbase.' => array(
543 'controllerName' => 'foo',
544 ),
545 );
546 $this->subject->render($configuration);
547 }
548
549 /**
550 * @test
551 */
552 public function renderCallsStandardWrapForExtbaseControllerName()
553 {
554 $this->addMockViewToSubject();
555 $this->contentObjectRenderer
556 ->expects($this->once())
557 ->method('stdWrap')
558 ->with('foo', array('bar' => 'baz'));
559 $configuration = array(
560 'extbase.' => array(
561 'controllerName' => 'foo',
562 'controllerName.' => array(
563 'bar' => 'baz',
564 ),
565 ),
566 );
567 $this->subject->render($configuration);
568 }
569
570 /**
571 * @test
572 */
573 public function renderSetsExtbaseControllerActionNameInRequest()
574 {
575 $this->addMockViewToSubject();
576 $this->request
577 ->expects($this->once())
578 ->method('setControllerActionName')
579 ->with('foo');
580 $configuration = array(
581 'extbase.' => array(
582 'controllerActionName' => 'foo',
583 ),
584 );
585 $this->subject->render($configuration);
586 }
587
588 /**
589 * @test
590 */
591 public function renderCallsStandardWrapForExtbaseControllerActionName()
592 {
593 $this->addMockViewToSubject();
594 $this->contentObjectRenderer
595 ->expects($this->once())
596 ->method('stdWrap')
597 ->with('foo', array('bar' => 'baz'));
598 $configuration = array(
599 'extbase.' => array(
600 'controllerActionName' => 'foo',
601 'controllerActionName.' => array(
602 'bar' => 'baz',
603 ),
604 ),
605 );
606 $this->subject->render($configuration);
607 }
608
609 /**
610 * @test
611 */
612 public function renderAssignsSettingsArrayToView()
613 {
614 $this->addMockViewToSubject();
615
616 $configuration = array(
617 'settings.' => array(
618 'foo' => 'value',
619 'bar.' => array(
620 'baz' => 'value2',
621 ),
622 ),
623 );
624
625 $expectedSettingsToBeSet = array(
626 'foo' => 'value',
627 'bar' => array(
628 'baz' => 'value2',
629 ),
630 );
631
632 /** @var TypoScriptService|\PHPUnit_Framework_MockObject_MockObject $typoScriptServiceMock */
633 $typoScriptServiceMock = $this->getMock(\TYPO3\CMS\Extbase\Service\TypoScriptService::class);
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
649 /**
650 * @test
651 * @expectedException \InvalidArgumentException
652 */
653 public function renderThrowsExceptionForNotAllowedVariableData()
654 {
655 $this->addMockViewToSubject();
656 $configuration = array(
657 'variables.' => array(
658 'data' => 'foo',
659 'data.' => array(
660 'bar' => 'baz',
661 ),
662 ),
663 );
664 $this->subject->render($configuration);
665 }
666
667 /**
668 * @test
669 * @expectedException \InvalidArgumentException
670 */
671 public function renderThrowsExceptionForNotAllowedVariableCurrent()
672 {
673 $this->addMockViewToSubject();
674 $configuration = array(
675 'variables.' => array(
676 'current' => 'foo',
677 'current.' => array(
678 'bar' => 'baz',
679 ),
680 ),
681 );
682 $this->subject->render($configuration);
683 }
684
685 /**
686 * @test
687 */
688 public function renderCallsCObjGetSingleForAllowedVariable()
689 {
690 $this->addMockViewToSubject();
691 $configuration = array(
692 'variables.' => array(
693 'aVar' => 'TEXT',
694 'aVar.' => array(
695 'value' => 'foo',
696 ),
697 ),
698 );
699 $this->contentObjectRenderer
700 ->expects($this->once())
701 ->method('cObjGetSingle')
702 ->with('TEXT', array('value' => 'foo'));
703 $this->subject->render($configuration);
704 }
705
706 /**
707 * @test
708 */
709 public function renderAssignsRenderedContentObjectVariableToView()
710 {
711 $this->addMockViewToSubject();
712 $configuration = array(
713 'variables.' => array(
714 'aVar' => 'TEXT',
715 'aVar.' => array(
716 'value' => 'foo',
717 ),
718 ),
719 );
720 $this->contentObjectRenderer
721 ->expects($this->once())
722 ->method('cObjGetSingle')
723 ->will($this->returnValue('foo'));
724 $this->standaloneView
725 ->expects($this->once())
726 ->method('assignMultiple')
727 ->with(array('aVar' => 'foo', 'data' => array(), 'current' => null));
728 $this->subject->render($configuration);
729 }
730
731 /**
732 * @test
733 */
734 public function renderAssignsContentObjectRendererDataToView()
735 {
736 $this->addMockViewToSubject();
737 $this->contentObjectRenderer->data = array('foo');
738 $this->standaloneView
739 ->expects($this->once())
740 ->method('assignMultiple')
741 ->with(array('data' => array('foo'), 'current' => null));
742 $this->subject->render(array());
743 }
744
745 /**
746 * @test
747 */
748 public function renderAssignsContentObjectRendererCurrentValueToView()
749 {
750 $this->addMockViewToSubject();
751 $this->contentObjectRenderer->data = array('currentKey' => 'currentValue');
752 $this->contentObjectRenderer->currentValKey = 'currentKey';
753 $this->standaloneView
754 ->expects($this->once())
755 ->method('assignMultiple')
756 ->with(array('data' => array('currentKey' => 'currentValue'), 'current' => 'currentValue'));
757 $this->subject->render(array());
758 }
759
760 /**
761 * @test
762 */
763 public function renderCallsRenderOnStandaloneViewie()
764 {
765 $this->addMockViewToSubject();
766 $this->standaloneView
767 ->expects($this->once())
768 ->method('render');
769 $this->subject->render(array());
770 }
771
772 /**
773 * @test
774 */
775 public function renderCallsStandardWrapOnResultStringIfGivenInConfiguration()
776 {
777 $this->addMockViewToSubject();
778 $configuration = array(
779 'stdWrap.' => array(
780 'foo' => 'bar',
781 ),
782 );
783 $this->standaloneView
784 ->expects($this->any())
785 ->method('render')
786 ->will($this->returnValue('baz'));
787 $this->contentObjectRenderer
788 ->expects($this->once())
789 ->method('stdWrap')
790 ->with('baz', array('foo' => 'bar'));
791 $this->subject->render($configuration);
792 }
793 }