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