147bcfb12fb9c32f8893e81cdeb1eb6c6e635501
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / View / StandaloneViewTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\View;
3
4 /* *
5 * This script is backported from the FLOW3 package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Testcase for the StandaloneView
25 */
26 class StandaloneViewTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
27
28 /**
29 * @var array A backup of registered singleton instances
30 */
31 protected $singletonInstances = array();
32
33 /**
34 * @var \TYPO3\CMS\Fluid\View\StandaloneView
35 */
36 protected $view;
37
38 /**
39 * @var \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface
40 */
41 protected $mockRenderingContext;
42
43 /**
44 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperVariableContainer
45 */
46 protected $mockViewHelperVariableContainer;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
50 */
51 protected $mockControllerContext;
52
53 /**
54 * @var \TYPO3\CMS\Fluid\Core\Parser\TemplateParser
55 */
56 protected $mockTemplateParser;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
60 */
61 protected $mockObjectManager;
62
63 /**
64 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
65 */
66 protected $mockRequest;
67
68 /**
69 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
70 */
71 protected $mockUriBuilder;
72
73 /**
74 * @var \TYPO3\CMS\Fluid\Core\Parser\ParsedTemplateInterface
75 */
76 protected $mockParsedTemplate;
77
78 /**
79 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
80 */
81 protected $mockConfigurationManager;
82
83 /**
84 * @var \TYPO3\CMS\Extbase\Mvc\Controller\FlashMessageContainer
85 */
86 protected $mockFlashMessageContainer;
87
88 /**
89 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
90 */
91 protected $mockContentObject;
92
93 /**
94 * @var \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler
95 */
96 protected $mockTemplateCompiler;
97
98 /**
99 * Sets up this test case
100 *
101 * @return void
102 */
103 public function setUp() {
104 $this->view = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\StandaloneView', array('dummy'), array(), '', FALSE);
105 $this->mockTemplateParser = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser');
106 $this->mockParsedTemplate = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\ParsedTemplateInterface');
107 $this->mockTemplateParser->expects($this->any())->method('parse')->will($this->returnValue($this->mockParsedTemplate));
108 $this->mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
109 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
110 $this->mockObjectManager->expects($this->any())->method('get')->will($this->returnCallback(array($this, 'objectManagerCallback')));
111 $this->mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
112 $this->mockUriBuilder = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
113 $this->mockFlashMessageContainer = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\FlashMessageContainer');
114 $this->mockContentObject = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
115 $this->mockControllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext');
116 $this->mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->mockRequest));
117 $this->mockViewHelperVariableContainer = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\ViewHelperVariableContainer');
118 $this->mockRenderingContext = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContext');
119 $this->mockRenderingContext->expects($this->any())->method('getControllerContext')->will($this->returnValue($this->mockControllerContext));
120 $this->mockRenderingContext->expects($this->any())->method('getViewHelperVariableContainer')->will($this->returnValue($this->mockViewHelperVariableContainer));
121 $this->view->injectTemplateParser($this->mockTemplateParser);
122 $this->view->injectObjectManager($this->mockObjectManager);
123 $this->view->setRenderingContext($this->mockRenderingContext);
124 $this->mockTemplateCompiler = $this->getMock('TYPO3\\CMS\\Fluid\\Core\\Compiler\\TemplateCompiler');
125 $this->view->_set('templateCompiler', $this->mockTemplateCompiler);
126 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
127 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager', $this->mockObjectManager);
128 \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer', $this->mockContentObject);
129 }
130
131 /**
132 * @return void
133 */
134 public function tearDown() {
135 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
136 }
137
138 /**
139 * @param string $className
140 * @return object
141 */
142 public function objectManagerCallback($className) {
143 switch ($className) {
144 case 'TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface':
145 return $this->mockConfigurationManager;
146 case 'TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser':
147 return $this->mockTemplateParser;
148 case 'TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContext':
149 return $this->mockRenderingContext;
150 case 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request':
151 return $this->mockRequest;
152 case 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder':
153 return $this->mockUriBuilder;
154 case 'TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext':
155 return $this->mockControllerContext;
156 case 'TYPO3\\CMS\\Extbase\\Mvc\\Controller\\FlashMessageContainer':
157 return $this->mockFlashMessageContainer;
158 case 'TYPO3\\CMS\\Fluid\\Core\\Compiler\\TemplateCompiler':
159 return $this->mockTemplateCompiler;
160 }
161 }
162
163 /**
164 * @test
165 */
166 public function constructorSetsSpecifiedContentObject() {
167 $mockContentObject = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
168 // FIXME should be compared with identicalTo() - but that does not seem to work
169 $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->equalTo($this->mockContentObject));
170 new \TYPO3\CMS\Fluid\View\StandaloneView($mockContentObject);
171 }
172
173 /**
174 * @test
175 */
176 public function constructorCreatesContentObjectIfItIsNotSpecified() {
177 // FIXME should be compared with identicalTo() - but that does not seem to work
178 $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->equalTo($this->mockContentObject));
179 new \TYPO3\CMS\Fluid\View\StandaloneView();
180 }
181
182 /**
183 * @test
184 */
185 public function constructorSetsRequestUri() {
186 $expectedRequestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
187 $this->mockRequest->expects($this->once())->method('setRequestURI')->with($expectedRequestUri);
188 new \TYPO3\CMS\Fluid\View\StandaloneView();
189 }
190
191 /**
192 * @test
193 */
194 public function constructorSetsBaseUri() {
195 $expectedBaseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
196 $this->mockRequest->expects($this->once())->method('setBaseURI')->with($expectedBaseUri);
197 new \TYPO3\CMS\Fluid\View\StandaloneView();
198 }
199
200 /**
201 * @test
202 */
203 public function constructorInjectsRequestToUriBuilder() {
204 $this->mockUriBuilder->expects($this->once())->method('setRequest')->with($this->mockRequest);
205 new \TYPO3\CMS\Fluid\View\StandaloneView();
206 }
207
208 /**
209 * @test
210 */
211 public function constructorInjectsRequestToControllerContext() {
212 $this->mockControllerContext->expects($this->once())->method('setRequest')->with($this->mockRequest);
213 new \TYPO3\CMS\Fluid\View\StandaloneView();
214 }
215
216 /**
217 * @test
218 */
219 public function constructorInjectsUriBuilderToControllerContext() {
220 $this->mockControllerContext->expects($this->once())->method('setUriBuilder')->with($this->mockUriBuilder);
221 new \TYPO3\CMS\Fluid\View\StandaloneView();
222 }
223
224 /**
225 * @test
226 */
227 public function constructorInjectsFlashMessageContainerToControllerContext() {
228 $this->mockControllerContext->expects($this->once())->method('setFlashMessageContainer')->with($this->mockFlashMessageContainer);
229 new \TYPO3\CMS\Fluid\View\StandaloneView();
230 }
231
232 /**
233 * @test
234 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
235 */
236 public function renderThrowsExceptionIfTemplateIsNotSpecified() {
237 $this->view->render();
238 }
239
240 /**
241 * @test
242 */
243 public function renderPassesSpecifiedTemplateSourceToTemplateParser() {
244 $this->view->setTemplateSource('The Template Source');
245 $this->mockTemplateParser->expects($this->once())->method('parse')->with('The Template Source');
246 $this->view->render();
247 }
248
249 /**
250 * @test
251 */
252 public function renderLoadsSpecifiedTemplateFileAndPassesSourceToTemplateParser() {
253 $templatePathAndFilename = dirname(__FILE__) . '/Fixtures/StandaloneViewFixture.html';
254 $expectedResult = file_get_contents($templatePathAndFilename);
255 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
256 $this->mockTemplateParser->expects($this->once())->method('parse')->with($expectedResult);
257 $this->view->render();
258 }
259
260 /**
261 * @test
262 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
263 */
264 public function renderThrowsExceptionIfSpecifiedTemplateFileDoesNotExist() {
265 $this->view->setTemplatePathAndFilename('NonExistingTemplatePath');
266 @$this->view->render();
267 }
268
269 /**
270 * @test
271 */
272 public function setFormatSetsRequestFormat() {
273 $this->mockRequest->expects($this->once())->method('setFormat')->with('xml');
274 $this->view->setFormat('xml');
275 }
276
277 /**
278 * @test
279 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
280 */
281 public function getLayoutRootPathThrowsExceptionIfLayoutRootPathAndTemplatePathAreNotSpecified() {
282 $this->view->getLayoutRootPath();
283 }
284
285 /**
286 * @test
287 */
288 public function getLayoutRootPathReturnsSpecifiedLayoutRootPathByDefault() {
289 $templatePathAndFilename = 'some/template/RootPath/SomeTemplate.html';
290 $layoutRootPath = 'some/layout/RootPath';
291 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
292 $this->view->setLayoutRootPath($layoutRootPath);
293 $actualResult = $this->view->getLayoutRootPath();
294 $this->assertEquals($layoutRootPath, $actualResult);
295 }
296
297 /**
298 * @test
299 */
300 public function getLayoutRootPathReturnsDefaultPathIfNoLayoutRootPathIsSpecified() {
301 $templatePathAndFilename = 'some/template/RootPath/SomeTemplate.html';
302 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
303 $expectedResult = 'some/template/RootPath/Layouts';
304 $actualResult = $this->view->getLayoutRootPath();
305 $this->assertEquals($expectedResult, $actualResult);
306 }
307
308 /**
309 * @test
310 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
311 */
312 public function getLayoutSourceThrowsExceptionIfLayoutRootPathDoesNotExist() {
313 $this->view->setLayoutRootPath('some/non/existing/Path');
314 $this->view->_call('getLayoutSource');
315 }
316
317 /**
318 * @test
319 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
320 */
321 public function getLayoutSourceThrowsExceptionIfLayoutFileDoesNotExist() {
322 $layoutRootPath = dirname(__FILE__) . '/Fixtures';
323 $this->view->setLayoutRootPath($layoutRootPath);
324 $this->view->_call('getLayoutSource', 'NonExistingLayout');
325 }
326
327 /**
328 * @test
329 */
330 public function getLayoutSourceReturnsContentOfLayoutFileForTheDefaultFormat() {
331 $layoutRootPath = dirname(__FILE__) . '/Fixtures';
332 $this->view->setLayoutRootPath($layoutRootPath);
333 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('html'));
334 $expectedResult = file_get_contents($layoutRootPath . '/LayoutFixture.html');
335 $actualResult = $this->view->_call('getLayoutSource', 'LayoutFixture');
336 $this->assertEquals($expectedResult, $actualResult);
337 }
338
339 /**
340 * @test
341 */
342 public function getLayoutSourceReturnsContentOfLayoutFileForTheSpecifiedFormat() {
343 $layoutRootPath = dirname(__FILE__) . '/Fixtures';
344 $this->view->setLayoutRootPath($layoutRootPath);
345 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('xml'));
346 $expectedResult = file_get_contents($layoutRootPath . '/LayoutFixture.xml');
347 $actualResult = $this->view->_call('getLayoutSource', 'LayoutFixture');
348 $this->assertEquals($expectedResult, $actualResult);
349 }
350
351 /**
352 * @test
353 */
354 public function getLayoutSourceReturnsContentOfDefaultLayoutFileIfNoLayoutExistsForTheSpecifiedFormat() {
355 $layoutRootPath = dirname(__FILE__) . '/Fixtures';
356 $this->view->setLayoutRootPath($layoutRootPath);
357 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('foo'));
358 $expectedResult = file_get_contents($layoutRootPath . '/LayoutFixture');
359 $actualResult = $this->view->_call('getLayoutSource', 'LayoutFixture');
360 $this->assertEquals($expectedResult, $actualResult);
361 }
362
363 /**
364 * @test
365 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
366 */
367 public function getPartialRootPathThrowsExceptionIfPartialRootPathAndTemplatePathAreNotSpecified() {
368 $this->view->getPartialRootPath();
369 }
370
371 /**
372 * @test
373 */
374 public function getPartialRootPathReturnsSpecifiedPartialRootPathByDefault() {
375 $templatePathAndFilename = 'some/template/RootPath/SomeTemplate.html';
376 $partialRootPath = 'some/partial/RootPath';
377 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
378 $this->view->setPartialRootPath($partialRootPath);
379 $actualResult = $this->view->getPartialRootPath();
380 $this->assertEquals($partialRootPath, $actualResult);
381 }
382
383 /**
384 * @test
385 */
386 public function getPartialRootPathReturnsDefaultPathIfNoPartialRootPathIsSpecified() {
387 $templatePathAndFilename = 'some/template/RootPath/SomeTemplate.html';
388 $this->view->setTemplatePathAndFilename($templatePathAndFilename);
389 $expectedResult = 'some/template/RootPath/Partials';
390 $actualResult = $this->view->getPartialRootPath();
391 $this->assertEquals($expectedResult, $actualResult);
392 }
393
394 /**
395 * @test
396 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
397 */
398 public function getPartialSourceThrowsExceptionIfPartialRootPathDoesNotExist() {
399 $this->view->setPartialRootPath('some/non/existing/Path');
400 $this->view->_call('getPartialSource', 'SomePartial');
401 }
402
403 /**
404 * @test
405 * @expectedException \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
406 */
407 public function getPartialSourceThrowsExceptionIfPartialFileDoesNotExist() {
408 $partialRootPath = dirname(__FILE__) . '/Fixtures';
409 $this->view->setPartialRootPath($partialRootPath);
410 $this->view->_call('getPartialSource', 'NonExistingPartial');
411 }
412
413 /**
414 * @test
415 */
416 public function getPartialSourceReturnsContentOfPartialFileForTheDefaultFormat() {
417 $partialRootPath = dirname(__FILE__) . '/Fixtures';
418 $this->view->setPartialRootPath($partialRootPath);
419 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('html'));
420 $expectedResult = file_get_contents($partialRootPath . '/LayoutFixture.html');
421 $actualResult = $this->view->_call('getPartialSource', 'LayoutFixture');
422 $this->assertEquals($expectedResult, $actualResult);
423 }
424
425 /**
426 * @test
427 */
428 public function getPartialSourceReturnsContentOfPartialFileForTheSpecifiedFormat() {
429 $partialRootPath = dirname(__FILE__) . '/Fixtures';
430 $this->view->setPartialRootPath($partialRootPath);
431 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('xml'));
432 $expectedResult = file_get_contents($partialRootPath . '/LayoutFixture.xml');
433 $actualResult = $this->view->_call('getPartialSource', 'LayoutFixture');
434 $this->assertEquals($expectedResult, $actualResult);
435 }
436
437 /**
438 * @test
439 */
440 public function getPartialSourceReturnsContentOfDefaultPartialFileIfNoPartialExistsForTheSpecifiedFormat() {
441 $partialRootPath = dirname(__FILE__) . '/Fixtures';
442 $this->view->setPartialRootPath($partialRootPath);
443 $this->mockRequest->expects($this->once())->method('getFormat')->will($this->returnValue('foo'));
444 $expectedResult = file_get_contents($partialRootPath . '/LayoutFixture');
445 $actualResult = $this->view->_call('getPartialSource', 'LayoutFixture');
446 $this->assertEquals($expectedResult, $actualResult);
447 }
448 }
449
450 ?>