55c8ece235d2705797dcc437601c5ba6e5b12ffd
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / View / TemplateViewTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\View;
3
4 /* *
5 * This script is backported from the TYPO3 Flow 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 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 include_once(__DIR__ . '/Fixtures/TransparentSyntaxTreeNode.php');
15 include_once(__DIR__ . '/Fixtures/TemplateViewFixture.php');
16
17 /**
18 * Testcase for the TemplateView
19 */
20 @require_once('vfsStream/vfsStream.php'); // include vfs stream wrapper
21 class TemplateViewTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
22
23 /**
24 * @test
25 */
26 public function expandGenericPathPatternWorksWithOldNamingSchemeOfSubPackage() {
27 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'ViewHelpers_Widget', 'Paginate', 'html');
28 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
29 $templateView->_set('controllerContext', $mockControllerContext);
30 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
31 $expected = array('Resources/Private/Templates/ViewHelpers/Widget/Paginate/@action.html');
32 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
33 $this->assertEquals($expected, $actual);
34 }
35
36 /**
37 * @test
38 */
39 public function expandGenericPathPatternWorksWithNewNamingSchemeOfSubPackage() {
40 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'ViewHelpers\\Widget', 'Paginate', 'html');
41 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
42 $templateView->_set('controllerContext', $mockControllerContext);
43 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
44 $expected = array('Resources/Private/Templates/ViewHelpers/Widget/Paginate/@action.html');
45 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
46 $this->assertEquals($expected, $actual);
47 }
48
49 /**
50 * @test
51 */
52 public function expandGenericPathPatternWorksWithBubblingDisabledAndFormatNotOptional() {
53 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', NULL, 'My', 'html');
54
55 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
56 $templateView->_set('controllerContext', $mockControllerContext);
57 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
58
59 $expected = array('Resources/Private/Templates/My/@action.html');
60 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
61 $this->assertEquals($expected, $actual);
62 }
63
64
65 /**
66 * @test
67 */
68 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatNotOptional() {
69 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
70
71 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
72 $templateView->_set('controllerContext', $mockControllerContext);
73 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
74 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
75
76 $expected = array(
77 'Resources/Private/Templates/MySubPackage/My/@action.html'
78 );
79 $this->assertEquals($expected, $actual);
80 }
81
82 /**
83 * @test
84 */
85 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatOptional() {
86 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
87
88 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
89 $templateView->_set('controllerContext', $mockControllerContext);
90 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
91 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, TRUE);
92
93 $expected = array(
94 'Resources/Private/Templates/MySubPackage/My/@action.html',
95 'Resources/Private/Templates/MySubPackage/My/@action'
96 );
97 $this->assertEquals($expected, $actual);
98 }
99
100 /**
101 * @test
102 */
103 public function expandGenericPathPatternWorksWithSubpackageAndBubblingEnabledAndFormatOptional() {
104 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
105
106 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
107 $templateView->_set('controllerContext', $mockControllerContext);
108 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
109 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', TRUE, TRUE);
110
111 $expected = array(
112 'Resources/Private/Templates/MySubPackage/My/@action.html',
113 'Resources/Private/Templates/MySubPackage/My/@action',
114 'Resources/Private/Templates/MySubPackage/@action.html',
115 'Resources/Private/Templates/MySubPackage/@action',
116 'Resources/Private/Templates/@action.html',
117 'Resources/Private/Templates/@action'
118 );
119 $this->assertEquals($expected, $actual);
120 }
121
122 /**
123 * Helper to build mock controller context needed to test expandGenericPathPattern.
124 *
125 * @param string $packageKey
126 * @param string $subPackageKey
127 * @param string $controllerClassName
128 * @param string $format
129 * @return \PHPUnit_Framework_MockObject_MockObject
130 */
131 protected function setupMockControllerContextForPathResolving($packageKey, $subPackageKey, $controllerName, $format) {
132 $controllerObjectName = "TYPO3\\$packageKey\\" . ($subPackageKey != $subPackageKey . '\\' ? : '') . 'Controller\\' . $controllerName . 'Controller';
133 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
134 $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
135 $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
136 $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
137 $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
138 $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));
139
140 $mockControllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext', array('getRequest'), array(), '', FALSE);
141 $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));
142
143 return $mockControllerContext;
144 }
145
146 /**
147 * @test
148 */
149 public function getTemplateRootPathReturnsUserSpecifiedTemplatePath() {
150 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
151 $templateView->setTemplateRootPath('/foo/bar');
152 $expected = '/foo/bar';
153 $actual = $templateView->_call('getTemplateRootPath');
154 $this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
155 }
156
157 /**
158 * @test
159 */
160 public function getPartialRootPathReturnsUserSpecifiedPartialPath() {
161 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
162 $templateView->setPartialRootPath('/foo/bar');
163 $expected = '/foo/bar';
164 $actual = $templateView->_call('getPartialRootPath');
165 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
166 }
167
168 /**
169 * @test
170 */
171 public function getLayoutRootPathReturnsUserSpecifiedPartialPath() {
172 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
173 $templateView->setLayoutRootPath('/foo/bar');
174 $expected = '/foo/bar';
175 $actual = $templateView->_call('getLayoutRootPath');
176 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
177 }
178
179 /**
180 * @test
181 */
182 public function pathToPartialIsResolvedCorrectly() {
183 $this->markTestSkipped('Needs to be finished');
184 \vfsStreamWrapper::register();
185 mkdir('vfs://MyTemplates');
186 \file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
187 $mockRootDirectory = \vfsStreamDirectory::create('ExamplePackagePath/Resources/Private/Partials');
188 $mockRootDirectory->getChild('Resources/Private/Partials')->addChild('Partials');
189 \vfsStreamWrapper::setRoot($mockRootDirectory);
190
191 $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser', array(''), array(), '', FALSE);
192 }
193
194 /**
195 * @test
196 */
197 public function resolveTemplatePathAndFilenameChecksDifferentPathPatternsAndReturnsTheFirstPathWhichExists() {
198 \vfsStreamWrapper::register();
199 mkdir('vfs://MyTemplates');
200 \file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
201
202 $paths = array(
203 'vfs://NonExistantDir/UnknowFile.html',
204 'vfs://MyTemplates/@action.html'
205 );
206
207 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('expandGenericPathPattern'), array(), '', FALSE);
208 $templateView->expects($this->once())->method('expandGenericPathPattern')->with('@templateRoot/@subpackage/@controller/@action.@format', FALSE, FALSE)->will($this->returnValue($paths));
209
210 $templateView->setTemplateRootPath('MyTemplates');
211 $templateView->setPartialRootPath('MyPartials');
212 $templateView->setLayoutRootPath('MyLayouts');
213
214 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource', 'myCoolAction'));
215
216 }
217
218 /**
219 * @test
220 */
221 public function resolveTemplatePathAndFilenameReturnsTheExplicitlyConfiguredTemplatePathAndFilename() {
222 \vfsStreamWrapper::register();
223 mkdir('vfs://MyTemplates');
224 \file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
225
226 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
227 $templateView->_set('templatePathAndFilename', 'vfs://MyTemplates/MyCoolAction.html');
228
229 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource'));
230 }
231 }
232
233 ?>