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