836277c20095e5229b9b00df9bd0446c75ec4309
[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 * @author Christian Kuhn <lolli@schwarzbu.ch>
28 */
29 class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
30
31 /**
32 * @var array A backup of registered singleton instances
33 */
34 protected $singletonInstances = array();
35
36 /**
37 * @var FluidTemplateContentObject|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
38 */
39 protected $subject = NULL;
40
41 /**
42 * @var ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
43 */
44 protected $contentObjectRenderer = NULL;
45
46 /**
47 * @var StandaloneView|\PHPUnit_Framework_MockObject_MockObject
48 */
49 protected $standaloneView = NULL;
50
51 /**
52 * @var Request|\PHPUnit_Framework_MockObject_MockObject
53 */
54 protected $request = NULL;
55
56 /**
57 * Set up
58 */
59 protected function setUp() {
60 $this->singletonInstances = GeneralUtility::getSingletonInstances();
61 $this->contentObjectRenderer = $this->getMock(
62 \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class
63 );
64 $this->subject = $this->getAccessibleMock(
65 \TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject::class,
66 array('initializeStandaloneViewInstance'),
67 array($this->contentObjectRenderer)
68 );
69 /** @var $tsfe \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
70 $tsfe = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(), array(), '', FALSE);
71 $tsfe->tmpl = $this->getMock(\TYPO3\CMS\Core\TypoScript\TemplateService::class);
72 $GLOBALS['TSFE'] = $tsfe;
73 }
74
75 /**
76 * Tear down
77 */
78 protected function tearDown() {
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 $this->standaloneView = $this->getMock(\TYPO3\CMS\Fluid\View\StandaloneView::class, array(), array(), '', FALSE);
88 $this->request = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class);
89 $this->standaloneView
90 ->expects($this->any())
91 ->method('getRequest')
92 ->will($this->returnValue($this->request));
93 $this->subject->_set('view', $this->standaloneView);
94 }
95
96 /**
97 * @test
98 */
99 public function constructSetsContentObjectRenderer() {
100 $this->assertSame($this->contentObjectRenderer, $this->subject->getContentObject());
101 }
102
103 /**
104 * @test
105 */
106 public function renderCallsInitializeStandaloneViewInstance() {
107 $this->addMockViewToSubject();
108 $this->subject
109 ->expects($this->once())
110 ->method('initializeStandaloneViewInstance');
111 $this->subject->render(array());
112 }
113
114 /**
115 * @test
116 */
117 public function renderCallsTemplateServiceGetFileNameForGivenTemplateFile() {
118 $this->addMockViewToSubject();
119 /** @var $templateService \PHPUnit_Framework_MockObject_MockObject */
120 $templateService = $GLOBALS['TSFE']->tmpl;
121 $templateService
122 ->expects($this->any())
123 ->method('getFileName')
124 ->with('foo');
125 $this->subject->render(array('file' => 'foo'));
126 }
127
128 /**
129 * @test
130 */
131 public function renderCallsStandardWrapForGivenTemplateFileWithStandardWrap() {
132 $this->addMockViewToSubject();
133 $this->contentObjectRenderer
134 ->expects($this->any())
135 ->method('stdWrap')
136 ->with('foo', array('bar' => 'baz'));
137 $this->subject->render(array('file' => 'foo', 'file.' => array('bar' => 'baz')));
138 }
139
140 /**
141 * @test
142 */
143 public function renderSetsTemplateFileInView() {
144 $this->addMockViewToSubject();
145 /** @var $templateService \PHPUnit_Framework_MockObject_MockObject */
146 $templateService = $GLOBALS['TSFE']->tmpl;
147 $templateService
148 ->expects($this->any())
149 ->method('getFileName')
150 ->with('foo')
151 ->will($this->returnValue('bar'));
152 $this->standaloneView
153 ->expects($this->any())
154 ->method('setTemplatePathAndFilename')
155 ->with(PATH_site . 'bar');
156 $this->subject->render(array('file' => 'foo'));
157 }
158
159 /**
160 * @test
161 */
162 public function renderSetsTemplateFileByTemplateInView() {
163 $this->addMockViewToSubject();
164
165 $this->contentObjectRenderer
166 ->expects($this->any())
167 ->method('cObjGetSingle')
168 ->with('FILE', array('file' => PATH_site . 'foo/bar.html'))
169 ->will($this->returnValue('baz'));
170
171 $this->standaloneView
172 ->expects($this->any())
173 ->method('setTemplateSource')
174 ->with('baz');
175
176 $this->subject->render(array(
177 'template' => 'FILE',
178 'template.' => array(
179 'file' => PATH_site . 'foo/bar.html'
180 )
181 ));
182 }
183
184 /**
185 * @test
186 */
187 public function renderSetsTemplateFileByTemplateNameInView() {
188 $this->addMockViewToSubject();
189
190 $this->standaloneView
191 ->expects($this->any())
192 ->method('getFormat')
193 ->will($this->returnValue('html'));
194 $this->standaloneView
195 ->expects($this->once())
196 ->method('setTemplate')
197 ->with('foo');
198
199 $this->subject->render(array(
200 'templateName' => 'foo',
201 'templateRootPaths.' => array(
202 0 => 'dummyPath1/',
203 1 => 'dummyPath2/')
204 )
205 );
206 }
207
208 /**
209 * @test
210 */
211 public function renderSetsTemplateFileByTemplateNameStdWrapInView() {
212 $this->addMockViewToSubject();
213
214 $this->contentObjectRenderer
215 ->expects($this->once())
216 ->method('stdWrap')
217 ->with('TEXT', array('value' => 'bar'))
218 ->will($this->returnValue('bar'));
219 $this->standaloneView
220 ->expects($this->any())
221 ->method('getFormat')
222 ->will($this->returnValue('html'));
223 $this->standaloneView
224 ->expects($this->once())
225 ->method('setTemplate')
226 ->with('bar');
227
228 $this->subject->render(array(
229 'templateName' => 'TEXT',
230 'templateName.' => array('value' => 'bar'),
231 'templateRootPaths.' => array(
232 0 => 'dummyPath1/',
233 1 => 'dummyPath2/')
234 )
235 );
236 }
237
238 /**
239 * @test
240 */
241 public function renderSetsLayoutRootPathInView() {
242 $this->addMockViewToSubject();
243 $this->standaloneView
244 ->expects($this->once())
245 ->method('setLayoutRootPaths')
246 ->with(array(PATH_site . 'foo/bar.html'));
247 $this->subject->render(array('layoutRootPath' => 'foo/bar.html'));
248 }
249
250 /**
251 * @test
252 */
253 public function renderCallsStandardWrapForLayoutRootPath() {
254 $this->addMockViewToSubject();
255 $this->contentObjectRenderer
256 ->expects($this->once())
257 ->method('stdWrap')
258 ->with('foo', array('bar' => 'baz'));
259 $this->subject->render(array('layoutRootPath' => 'foo', 'layoutRootPath.' => array('bar' => 'baz')));
260 }
261
262 /**
263 * @test
264 */
265 public function fallbacksForLayoutRootPathAreSet() {
266 $this->addMockViewToSubject();
267 $this->standaloneView
268 ->expects($this->once())
269 ->method('setLayoutRootPaths')
270 ->with(array(10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html'));
271 $this->subject->render(array('layoutRootPaths.' => array(10 => 'foo/bar.html', 20 => 'foo/bar2.html')));
272 }
273
274 /**
275 * @test
276 */
277 public function fallbacksForLayoutRootPathAreAppendedToLayoutRootPath() {
278 $this->addMockViewToSubject();
279 $this->standaloneView
280 ->expects($this->once())
281 ->method('setLayoutRootPaths')
282 ->with(array(0 => PATH_site . 'foo/main.html', 10 => PATH_site . 'foo/bar.html', 20 => PATH_site . 'foo/bar2.html'));
283 $this->subject->render(array('layoutRootPath' => 'foo/main.html', 'layoutRootPaths.' => array(10 => 'foo/bar.html', 20 => 'foo/bar2.html')));
284 }
285
286 /**
287 * @test
288 */
289 public function renderSetsPartialRootPathInView() {
290 $this->addMockViewToSubject();
291 $this->standaloneView
292 ->expects($this->once())
293 ->method('setPartialRootPaths')
294 ->with(array(PATH_site . 'foo/bar.html'));
295 $this->subject->render(array('partialRootPath' => 'foo/bar.html'));
296 }
297
298 /**
299 * @test
300 */
301 public function renderCallsStandardWrapForPartialRootPath() {
302 $this->addMockViewToSubject();
303 $this->contentObjectRenderer
304 ->expects($this->once())
305 ->method('stdWrap')
306 ->with('foo', array('bar' => 'baz'));
307 $this->subject->render(array('partialRootPath' => 'foo', 'partialRootPath.' => array('bar' => 'baz')));
308 }
309
310
311 /**
312 * @test
313 */
314 public function fallbacksForPartialRootPathAreSet() {
315 $this->addMockViewToSubject();
316 $this->standaloneView
317 ->expects($this->once())
318 ->method('setPartialRootPaths')
319 ->with(array(10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
320 $this->subject->render(array('partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
321 }
322
323 /**
324 * @test
325 */
326 public function fallbacksForPartialRootPathAreAppendedToPartialRootPath() {
327 $this->addMockViewToSubject();
328 $this->standaloneView
329 ->expects($this->once())
330 ->method('setPartialRootPaths')
331 ->with(array(0 => PATH_site . 'main', 10 => PATH_site . 'foo', 20 => PATH_site . 'bar'));
332 $this->subject->render(array('partialRootPath' => 'main', 'partialRootPaths.' => array(10 => 'foo', 20 => 'bar')));
333 }
334
335 /**
336 * @test
337 */
338 public function renderSetsFormatInView() {
339 $this->addMockViewToSubject();
340 $this->standaloneView
341 ->expects($this->once())
342 ->method('setFormat')
343 ->with('xml');
344 $this->subject->render(array('format' => 'xml'));
345 }
346
347 /**
348 * @test
349 */
350 public function renderCallsStandardWrapForFormat() {
351 $this->addMockViewToSubject();
352 $this->contentObjectRenderer
353 ->expects($this->once())
354 ->method('stdWrap')
355 ->with('foo', array('bar' => 'baz'));
356 $this->subject->render(array('format' => 'foo', 'format.' => array('bar' => 'baz')));
357 }
358
359 /**
360 * @test
361 */
362 public function renderSetsExtbasePluginNameInRequest() {
363 $this->addMockViewToSubject();
364 $this->request
365 ->expects($this->once())
366 ->method('setPluginName')
367 ->with('foo');
368 $configuration = array(
369 'extbase.' => array(
370 'pluginName' => 'foo',
371 ),
372 );
373 $this->subject->render($configuration);
374 }
375
376 /**
377 * @test
378 */
379 public function renderCallsStandardWrapForExtbasePluginName() {
380 $this->addMockViewToSubject();
381 $this->contentObjectRenderer
382 ->expects($this->once())
383 ->method('stdWrap')
384 ->with('foo', array('bar' => 'baz'));
385 $configuration = array(
386 'extbase.' => array(
387 'pluginName' => 'foo',
388 'pluginName.' => array(
389 'bar' => 'baz',
390 ),
391 ),
392 );
393 $this->subject->render($configuration);
394 }
395
396 /**
397 * @test
398 */
399 public function renderSetsExtbaseControllerExtensionNameInRequest() {
400 $this->addMockViewToSubject();
401 $this->request
402 ->expects($this->once())
403 ->method('setControllerExtensionName')
404 ->with('foo');
405 $configuration = array(
406 'extbase.' => array(
407 'controllerExtensionName' => 'foo',
408 ),
409 );
410 $this->subject->render($configuration);
411 }
412
413 /**
414 * @test
415 */
416 public function renderCallsStandardWrapForExtbaseControllerExtensionName() {
417 $this->addMockViewToSubject();
418 $this->contentObjectRenderer
419 ->expects($this->once())
420 ->method('stdWrap')
421 ->with('foo', array('bar' => 'baz'));
422 $configuration = array(
423 'extbase.' => array(
424 'controllerExtensionName' => 'foo',
425 'controllerExtensionName.' => array(
426 'bar' => 'baz',
427 ),
428 ),
429 );
430 $this->subject->render($configuration);
431 }
432
433 /**
434 * @test
435 */
436 public function renderSetsExtbaseControllerNameInRequest() {
437 $this->addMockViewToSubject();
438 $this->request
439 ->expects($this->once())
440 ->method('setControllerName')
441 ->with('foo');
442 $configuration = array(
443 'extbase.' => array(
444 'controllerName' => 'foo',
445 ),
446 );
447 $this->subject->render($configuration);
448 }
449
450 /**
451 * @test
452 */
453 public function renderCallsStandardWrapForExtbaseControllerName() {
454 $this->addMockViewToSubject();
455 $this->contentObjectRenderer
456 ->expects($this->once())
457 ->method('stdWrap')
458 ->with('foo', array('bar' => 'baz'));
459 $configuration = array(
460 'extbase.' => array(
461 'controllerName' => 'foo',
462 'controllerName.' => array(
463 'bar' => 'baz',
464 ),
465 ),
466 );
467 $this->subject->render($configuration);
468 }
469
470 /**
471 * @test
472 */
473 public function renderSetsExtbaseControllerActionNameInRequest() {
474 $this->addMockViewToSubject();
475 $this->request
476 ->expects($this->once())
477 ->method('setControllerActionName')
478 ->with('foo');
479 $configuration = array(
480 'extbase.' => array(
481 'controllerActionName' => 'foo',
482 ),
483 );
484 $this->subject->render($configuration);
485 }
486
487 /**
488 * @test
489 */
490 public function renderCallsStandardWrapForExtbaseControllerActionName() {
491 $this->addMockViewToSubject();
492 $this->contentObjectRenderer
493 ->expects($this->once())
494 ->method('stdWrap')
495 ->with('foo', array('bar' => 'baz'));
496 $configuration = array(
497 'extbase.' => array(
498 'controllerActionName' => 'foo',
499 'controllerActionName.' => array(
500 'bar' => 'baz',
501 ),
502 ),
503 );
504 $this->subject->render($configuration);
505 }
506
507 /**
508 * @test
509 */
510 public function renderAssignsSettingsArrayToView() {
511 $this->addMockViewToSubject();
512
513 $configuration = array(
514 'settings.' => array(
515 'foo' => 'value',
516 'bar.' => array(
517 'baz' => 'value2',
518 ),
519 ),
520 );
521
522 $expectedSettingsToBeSet = array(
523 'foo' => 'value',
524 'bar' => array(
525 'baz' => 'value2',
526 ),
527 );
528
529 /** @var TypoScriptService|\PHPUnit_Framework_MockObject_MockObject $typoScriptServiceMock */
530 $typoScriptServiceMock = $this->getMock(\TYPO3\CMS\Extbase\Service\TypoScriptService::class);
531 $typoScriptServiceMock
532 ->expects($this->once())
533 ->method('convertTypoScriptArrayToPlainArray')
534 ->with($configuration['settings.'])
535 ->will($this->returnValue($expectedSettingsToBeSet));
536 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Service\TypoScriptService::class, $typoScriptServiceMock);
537
538 $this->standaloneView
539 ->expects($this->at(1))
540 ->method('assign')
541 ->with('settings', $expectedSettingsToBeSet);
542
543 $this->subject->render($configuration);
544 }
545
546 /**
547 * @test
548 * @expectedException \InvalidArgumentException
549 */
550 public function renderThrowsExceptionForNotAllowedVariableData() {
551 $this->addMockViewToSubject();
552 $configuration = array(
553 'variables.' => array(
554 'data' => 'foo',
555 'data.' => array(
556 'bar' => 'baz',
557 ),
558 ),
559 );
560 $this->subject->render($configuration);
561 }
562
563 /**
564 * @test
565 * @expectedException \InvalidArgumentException
566 */
567 public function renderThrowsExceptionForNotAllowedVariableCurrent() {
568 $this->addMockViewToSubject();
569 $configuration = array(
570 'variables.' => array(
571 'current' => 'foo',
572 'current.' => array(
573 'bar' => 'baz',
574 ),
575 ),
576 );
577 $this->subject->render($configuration);
578 }
579
580 /**
581 * @test
582 */
583 public function renderCallsCObjGetSingleForAllowedVariable() {
584 $this->addMockViewToSubject();
585 $configuration = array(
586 'variables.' => array(
587 'aVar' => 'TEXT',
588 'aVar.' => array(
589 'value' => 'foo',
590 ),
591 ),
592 );
593 $this->contentObjectRenderer
594 ->expects($this->once())
595 ->method('cObjGetSingle')
596 ->with('TEXT', array('value' => 'foo'));
597 $this->subject->render($configuration);
598 }
599
600 /**
601 * @test
602 */
603 public function renderAssignsRenderedContentObjectVariableToView() {
604 $this->addMockViewToSubject();
605 $configuration = array(
606 'variables.' => array(
607 'aVar' => 'TEXT',
608 'aVar.' => array(
609 'value' => 'foo',
610 ),
611 ),
612 );
613 $this->contentObjectRenderer
614 ->expects($this->once())
615 ->method('cObjGetSingle')
616 ->will($this->returnValue('foo'));
617 $this->standaloneView
618 ->expects($this->at(1))
619 ->method('assign')
620 ->with('aVar', 'foo');
621 $this->subject->render($configuration);
622 }
623
624 /**
625 * @test
626 */
627 public function renderAssignsContentObjectRendererDataToView() {
628 $this->addMockViewToSubject();
629 $this->contentObjectRenderer->data = array('foo');
630 $this->standaloneView
631 ->expects($this->at(1))
632 ->method('assign')
633 ->with('data', array('foo'));
634 $this->subject->render(array());
635 }
636
637 /**
638 * @test
639 */
640 public function renderAssignsContentObjectRendererCurrentValueToView() {
641 $this->addMockViewToSubject();
642 $this->contentObjectRenderer->data = array('currentKey' => 'currentValue');
643 $this->contentObjectRenderer->currentValKey= 'currentKey';
644 $this->standaloneView
645 ->expects($this->at(2))
646 ->method('assign')
647 ->with('current', 'currentValue');
648 $this->subject->render(array());
649 }
650
651 /**
652 * @test
653 */
654 public function renderCallsRenderOnStandaloneViewie() {
655 $this->addMockViewToSubject();
656 $this->standaloneView
657 ->expects($this->once())
658 ->method('render');
659 $this->subject->render(array());
660 }
661
662 /**
663 * @test
664 */
665 public function renderCallsStandardWrapOnResultStringIfGivenInConfiguration() {
666 $this->addMockViewToSubject();
667 $configuration = array(
668 'stdWrap.' => array(
669 'foo' => 'bar',
670 ),
671 );
672 $this->standaloneView
673 ->expects($this->any())
674 ->method('render')
675 ->will($this->returnValue('baz'));
676 $this->contentObjectRenderer
677 ->expects($this->once())
678 ->method('stdWrap')
679 ->with('baz', array('foo' => 'bar'));
680 $this->subject->render($configuration);
681 }
682
683 }