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