[+BUGFIX] Fix unit tests
[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 @require_once('vfsStream/vfsStream.php'); // include vfs stream wrapper
32 class Tx_Fluid_Tests_Unit_View_TemplateViewTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
33
34 /**
35 * @test
36 * @author Sebastian Kurfürst <sebastian@typo3.org>
37 */
38 public function expandGenericPathPatternWorksWithBubblingDisabledAndFormatNotOptional() {
39 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', NULL, 'My', 'html');
40
41 $templateView = $this->getAccessibleMock('Tx_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
45 $expected = array('Resources/Private/Templates/My/@action.html');
46 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
47 $this->assertEquals($expected, $actual);
48 }
49
50
51 /**
52 * @test
53 * @author Sebastian Kurfürst <sebastian@typo3.org>
54 */
55 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatNotOptional() {
56 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
57
58 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
59 $templateView->_set('controllerContext', $mockControllerContext);
60 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
61 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
62
63 $expected = array(
64 'Resources/Private/Templates/MySubPackage/My/@action.html'
65 );
66 $this->assertEquals($expected, $actual);
67 }
68
69 /**
70 * @test
71 * @author Sebastian Kurfürst <sebastian@typo3.org>
72 */
73 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatOptional() {
74 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
75
76 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
77 $templateView->_set('controllerContext', $mockControllerContext);
78 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
79 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, TRUE);
80
81 $expected = array(
82 'Resources/Private/Templates/MySubPackage/My/@action.html',
83 'Resources/Private/Templates/MySubPackage/My/@action'
84 );
85 $this->assertEquals($expected, $actual);
86 }
87
88 /**
89 * @test
90 * @author Sebastian Kurfürst <sebastian@typo3.org>
91 */
92 public function expandGenericPathPatternWorksWithSubpackageAndBubblingEnabledAndFormatOptional() {
93 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
94
95 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
96 $templateView->_set('controllerContext', $mockControllerContext);
97 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
98 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', TRUE, TRUE);
99
100 $expected = array(
101 'Resources/Private/Templates/MySubPackage/My/@action.html',
102 'Resources/Private/Templates/MySubPackage/My/@action',
103 'Resources/Private/Templates/MySubPackage/@action.html',
104 'Resources/Private/Templates/MySubPackage/@action',
105 'Resources/Private/Templates/@action.html',
106 'Resources/Private/Templates/@action',
107 );
108 $this->assertEquals($expected, $actual);
109 }
110
111 /**
112 * Helper to build mock controller context needed to test expandGenericPathPattern.
113 *
114 * @param $packageKey
115 * @param $subPackageKey
116 * @param $controllerClassName
117 * @param $format
118 *
119 * @author Sebastian Kurfürst <sebastian@typo3.org>
120 * @author Robert Lemke <robert@typo3.org>
121 */
122 protected function setupMockControllerContextForPathResolving($packageKey, $subPackageKey, $controllerName, $format) {
123 $controllerObjectName = "F3\\$packageKey\\" . ($subPackageKey != $subPackageKey . '\\' ? : '') . 'Controller\\' . $controllerName . 'Controller';
124
125 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
126 $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
127 $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
128 $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
129 $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
130 $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));
131
132 $mockControllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array('getRequest'), array(), '', FALSE);
133 $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));
134
135 return $mockControllerContext;
136 }
137
138 /**
139 * @test
140 * @author Sebastian Kurfürst <sebastian@typo3.org>
141 */
142 public function getTemplateRootPathReturnsUserSpecifiedTemplatePath() {
143 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
144 $templateView->setTemplateRootPath('/foo/bar');
145 $expected = '/foo/bar';
146 $actual = $templateView->_call('getTemplateRootPath');
147 $this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
148 }
149
150 /**
151 * @test
152 * @author Bastian Waidelich <bastian@typo3.org>
153 */
154 public function getPartialRootPathReturnsUserSpecifiedPartialPath() {
155 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
156 $templateView->setPartialRootPath('/foo/bar');
157 $expected = '/foo/bar';
158 $actual = $templateView->_call('getPartialRootPath');
159 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
160 }
161
162 /**
163 * @test
164 * @author Bastian Waidelich <bastian@typo3.org>
165 */
166 public function getLayoutRootPathReturnsUserSpecifiedPartialPath() {
167 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
168 $templateView->setLayoutRootPath('/foo/bar');
169 $expected = '/foo/bar';
170 $actual = $templateView->_call('getLayoutRootPath');
171 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
172 }
173
174 /**
175 * @test
176 * @author Sebastian Kurfürst <sebastian@typo3.org>
177 */
178 public function pathToPartialIsResolvedCorrectly() {
179 $this->markTestSkipped('Needs to be finished');
180 vfsStreamWrapper::register();
181 mkdir('vfs://MyTemplates');
182 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
183 $mockRootDirectory = vfsStreamDirectory::create('ExamplePackagePath/Resources/Private/Partials');
184 $mockRootDirectory->getChild('Resources/Private/Partials')->addChild('Partials');
185 vfsStreamWrapper::setRoot($mockRootDirectory);
186
187 $this->getAccessibleMock('Tx_Fluid_Core_Parser_TemplateParser', array(''), array(), '', FALSE);
188 }
189
190 /**
191 * @test
192 * @author Robert Lemke <robert@typo3.org>
193 */
194 public function resolveTemplatePathAndFilenameChecksDifferentPathPatternsAndReturnsTheFirstPathWhichExists() {
195 vfsStreamWrapper::register();
196 mkdir('vfs://MyTemplates');
197 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
198
199 $paths = array(
200 'vfs://NonExistantDir/UnknowFile.html',
201 'vfs://MyTemplates/@action.html'
202 );
203
204 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('expandGenericPathPattern'), array(), '', FALSE);
205 $templateView->expects($this->once())->method('expandGenericPathPattern')->with('@templateRoot/@subpackage/@controller/@action.@format', FALSE, FALSE)->will($this->returnValue($paths));
206
207 $templateView->setTemplateRootPath('MyTemplates');
208 $templateView->setPartialRootPath('MyPartials');
209 $templateView->setLayoutRootPath('MyLayouts');
210
211 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource', 'myCoolAction'));
212
213 }
214
215 /**
216 * @test
217 * @author Robert Lemke <robert@typo3.org>
218 */
219 public function resolveTemplatePathAndFilenameReturnsTheExplicitlyConfiguredTemplatePathAndFilename() {
220 vfsStreamWrapper::register();
221 mkdir('vfs://MyTemplates');
222 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
223
224 $templateView = $this->getAccessibleMock('Tx_Fluid_View_TemplateView', array('dummy'), array(), '', FALSE);
225 $templateView->_set('templatePathAndFilename', 'vfs://MyTemplates/MyCoolAction.html');
226
227 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource'));
228 }
229 }
230
231 ?>