22da2a396c0fc82b7d9cee4c6cb306a80572b461
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / View / TemplateViewTest.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 include_once(dirname(__FILE__) . '/Fixtures/TransparentSyntaxTreeNode.php');
24 include_once(dirname(__FILE__) . '/Fixtures/TemplateViewFixture.php');
25
26 /**
27 * Testcase for the TemplateView
28 *
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30 */
31 class Tx_Fluid_Tests_Unit_View_TemplateViewTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
32
33 /**
34 * @test
35 * @author Sebastian Kurfürst <sebastian@typo3.org>
36 */
37 public function expandGenericPathPatternWorksWithBubblingDisabledAndFormatNotOptional() {
38 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', NULL, 'My', 'html');
39
40 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
41 $templateView->_set('controllerContext', $mockControllerContext);
42 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
43
44 $expected = array('Resources/Private/Templates/My/@action.html');
45 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
46 $this->assertEquals($expected, $actual);
47 }
48
49
50 /**
51 * @test
52 * @author Sebastian Kurfürst <sebastian@typo3.org>
53 */
54 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatNotOptional() { $this->markTestIncomplete("Not implemented in v4");
55 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
56
57 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
58 $templateView->_set('controllerContext', $mockControllerContext);
59 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
60 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
61
62 $expected = array(
63 'Resources/Private/Templates/MySubPackage/My/@action.html'
64 );
65 $this->assertEquals($expected, $actual);
66 }
67
68 /**
69 * @test
70 * @author Sebastian Kurfürst <sebastian@typo3.org>
71 */
72 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatOptional() { $this->markTestIncomplete("Not implemented in v4");
73 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
74
75 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
76 $templateView->_set('controllerContext', $mockControllerContext);
77 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
78 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, TRUE);
79
80 $expected = array(
81 'Resources/Private/Templates/MySubPackage/My/@action.html',
82 'Resources/Private/Templates/MySubPackage/My/@action'
83 );
84 $this->assertEquals($expected, $actual);
85 }
86
87 /**
88 * @test
89 * @author Sebastian Kurfürst <sebastian@typo3.org>
90 */
91 public function expandGenericPathPatternWorksWithSubpackageAndBubblingEnabledAndFormatOptional() { $this->markTestIncomplete("Not implemented in v4");
92 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
93
94 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
95 $templateView->_set('controllerContext', $mockControllerContext);
96 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
97 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', TRUE, TRUE);
98
99 $expected = array(
100 'Resources/Private/Templates/MySubPackage/My/@action.html',
101 'Resources/Private/Templates/MySubPackage/My/@action',
102 'Resources/Private/Templates/MySubPackage/@action.html',
103 'Resources/Private/Templates/MySubPackage/@action',
104 'Resources/Private/Templates/@action.html',
105 'Resources/Private/Templates/@action',
106 );
107 $this->assertEquals($expected, $actual);
108 }
109
110 /**
111 * Helper to build mock controller context needed to test expandGenericPathPattern.
112 *
113 * @param $packageKey
114 * @param $subPackageKey
115 * @param $controllerClassName
116 * @param $format
117 *
118 * @author Sebastian Kurfürst <sebastian@typo3.org>
119 * @author Robert Lemke <robert@typo3.org>
120 */
121 protected function setupMockControllerContextForPathResolving($packageKey, $subPackageKey, $controllerName, $format) {
122 $controllerObjectName = 'Tx_' . $packageKey . '_' . ($subPackageKey !== '' ? '_' . $subPackageKey . '_' : '') . 'Controller_' . $controllerName . 'Controller';
123
124 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
125 $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
126 $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
127 $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
128 $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
129 $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));
130
131 $mockControllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array('getRequest'), array(), '', FALSE);
132 $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));
133
134 return $mockControllerContext;
135 }
136
137 /**
138 * @test
139 * @author Sebastian Kurfürst <sebastian@typo3.org>
140 */
141 public function getTemplateRootPathReturnsUserSpecifiedTemplatePath() {
142 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
143 $templateView->setTemplateRootPath('/foo/bar');
144 $expected = '/foo/bar';
145 $actual = $templateView->_call('getTemplateRootPath');
146 $this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
147 }
148
149 /**
150 * @test
151 * @author Bastian Waidelich <bastian@typo3.org>
152 */
153 public function getPartialRootPathReturnsUserSpecifiedPartialPath() {
154 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
155 $templateView->setPartialRootPath('/foo/bar');
156 $expected = '/foo/bar';
157 $actual = $templateView->_call('getPartialRootPath');
158 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
159 }
160
161 /**
162 * @test
163 * @author Bastian Waidelich <bastian@typo3.org>
164 */
165 public function getLayoutRootPathReturnsUserSpecifiedPartialPath() {
166 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
167 $templateView->setLayoutRootPath('/foo/bar');
168 $expected = '/foo/bar';
169 $actual = $templateView->_call('getLayoutRootPath');
170 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
171 }
172
173 /**
174 * @test
175 * @author Sebastian Kurfürst <sebastian@typo3.org>
176 */
177 public function pathToPartialIsResolvedCorrectly() {
178 $this->markTestSkipped('Needs to be finished');
179 vfsStreamWrapper::register();
180 mkdir('vfs://MyTemplates');
181 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
182 $mockRootDirectory = vfsStreamDirectory::create('ExamplePackagePath/Resources/Private/Partials');
183 $mockRootDirectory->getChild('Resources/Private/Partials')->addChild('Partials');
184 vfsStreamWrapper::setRoot($mockRootDirectory);
185
186 $this->getAccessibleMock('Tx_Fluid_Core_Parser_TemplateParser', array(''), array(), '', FALSE);
187 }
188
189 /**
190 * @test
191 * @author Robert Lemke <robert@typo3.org>
192 */
193 public function resolveTemplatePathAndFilenameChecksDifferentPathPatternsAndReturnsTheFirstPathWhichExists() { $this->markTestIncomplete("Not implemented in v4");
194 vfsStreamWrapper::register();
195 mkdir('vfs://MyTemplates');
196 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
197
198 $paths = array(
199 'vfs://NonExistantDir/UnknowFile.html',
200 'vfs://MyTemplates/@action.html'
201 );
202
203 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('expandGenericPathPattern'), array(), '', FALSE);
204 $templateView->expects($this->once())->method('expandGenericPathPattern')->with('@templateRoot/@subpackage/@controller/@action.@format', FALSE, FALSE)->will($this->returnValue($paths));
205
206 $templateView->setTemplateRootPath('MyTemplates');
207 $templateView->setPartialRootPath('MyPartials');
208 $templateView->setLayoutRootPath('MyLayouts');
209
210 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource', 'myCoolAction'));
211
212 }
213
214 /**
215 * @test
216 * @author Robert Lemke <robert@typo3.org>
217 */
218 public function resolveTemplatePathAndFilenameReturnsTheExplicitlyConfiguredTemplatePathAndFilename() { $this->markTestIncomplete("Not implemented in v4");
219 vfsStreamWrapper::register();
220 mkdir('vfs://MyTemplates');
221 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
222
223 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
224 $templateView->_set('templatePathAndFilename', 'vfs://MyTemplates/MyCoolAction.html');
225
226 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource'));
227 }
228 }
229
230 ?>