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