[BUGFIX] Make old style Widgets work again
[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 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 * The TYPO3 project - inspiring people to share! *
12 * */
13 include_once dirname(__FILE__) . '/Fixtures/TransparentSyntaxTreeNode.php';
14 include_once dirname(__FILE__) . '/Fixtures/TemplateViewFixture.php';
15 /**
16 * Testcase for the TemplateView
17 */
18 @require_once 'vfsStream/vfsStream.php';
19
20 // 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 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
55 $templateView->_set('controllerContext', $mockControllerContext);
56 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
57 $expected = array('Resources/Private/Templates/My/@action.html');
58 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
59 $this->assertEquals($expected, $actual);
60 }
61
62 /**
63 * @test
64 */
65 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatNotOptional() {
66 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
67 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
68 $templateView->_set('controllerContext', $mockControllerContext);
69 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
70 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, FALSE);
71 $expected = array(
72 'Resources/Private/Templates/MySubPackage/My/@action.html'
73 );
74 $this->assertEquals($expected, $actual);
75 }
76
77 /**
78 * @test
79 */
80 public function expandGenericPathPatternWorksWithSubpackageAndBubblingDisabledAndFormatOptional() {
81 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
82 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
83 $templateView->_set('controllerContext', $mockControllerContext);
84 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
85 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', FALSE, TRUE);
86 $expected = array(
87 'Resources/Private/Templates/MySubPackage/My/@action.html',
88 'Resources/Private/Templates/MySubPackage/My/@action'
89 );
90 $this->assertEquals($expected, $actual);
91 }
92
93 /**
94 * @test
95 */
96 public function expandGenericPathPatternWorksWithSubpackageAndBubblingEnabledAndFormatOptional() {
97 $mockControllerContext = $this->setupMockControllerContextForPathResolving('MyPackage', 'MySubPackage', 'My', 'html');
98 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('getTemplateRootPath', 'getPartialRootPath', 'getLayoutRootPath'), array(), '', FALSE);
99 $templateView->_set('controllerContext', $mockControllerContext);
100 $templateView->expects($this->any())->method('getTemplateRootPath')->will($this->returnValue('Resources/Private/'));
101 $actual = $templateView->_call('expandGenericPathPattern', '@templateRoot/Templates/@subpackage/@controller/@action.@format', TRUE, TRUE);
102 $expected = array(
103 'Resources/Private/Templates/MySubPackage/My/@action.html',
104 'Resources/Private/Templates/MySubPackage/My/@action',
105 'Resources/Private/Templates/MySubPackage/@action.html',
106 'Resources/Private/Templates/MySubPackage/@action',
107 'Resources/Private/Templates/@action.html',
108 'Resources/Private/Templates/@action'
109 );
110 $this->assertEquals($expected, $actual);
111 }
112
113 /**
114 * Helper to build mock controller context needed to test expandGenericPathPattern.
115 *
116 * @param $packageKey
117 * @param $subPackageKey
118 * @param $controllerClassName
119 * @param $format
120 */
121 protected function setupMockControllerContextForPathResolving($packageKey, $subPackageKey, $controllerName, $format) {
122 $controllerObjectName = "TYPO3\\{$packageKey}\\" . ($subPackageKey != $subPackageKey . '\\' ?: '') . 'Controller\\' . $controllerName . 'Controller';
123 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
124 $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
125 $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
126 $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
127 $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
128 $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));
129 $mockControllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext', array('getRequest'), array(), '', FALSE);
130 $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));
131 return $mockControllerContext;
132 }
133
134 /**
135 * @test
136 */
137 public function getTemplateRootPathReturnsUserSpecifiedTemplatePath() {
138 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
139 $templateView->setTemplateRootPath('/foo/bar');
140 $expected = '/foo/bar';
141 $actual = $templateView->_call('getTemplateRootPath');
142 $this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
143 }
144
145 /**
146 * @test
147 */
148 public function getPartialRootPathReturnsUserSpecifiedPartialPath() {
149 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
150 $templateView->setPartialRootPath('/foo/bar');
151 $expected = '/foo/bar';
152 $actual = $templateView->_call('getPartialRootPath');
153 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
154 }
155
156 /**
157 * @test
158 */
159 public function getLayoutRootPathReturnsUserSpecifiedPartialPath() {
160 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
161 $templateView->setLayoutRootPath('/foo/bar');
162 $expected = '/foo/bar';
163 $actual = $templateView->_call('getLayoutRootPath');
164 $this->assertEquals($expected, $actual, 'A set partial root path was not returned correctly.');
165 }
166
167 /**
168 * @test
169 */
170 public function pathToPartialIsResolvedCorrectly() {
171 $this->markTestSkipped('Needs to be finished');
172 \vfsStreamWrapper::register();
173 mkdir('vfs://MyTemplates');
174 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
175 $mockRootDirectory = \vfsStreamDirectory::create('ExamplePackagePath/Resources/Private/Partials');
176 $mockRootDirectory->getChild('Resources/Private/Partials')->addChild('Partials');
177 \vfsStreamWrapper::setRoot($mockRootDirectory);
178 $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser', array(''), array(), '', FALSE);
179 }
180
181 /**
182 * @test
183 */
184 public function resolveTemplatePathAndFilenameChecksDifferentPathPatternsAndReturnsTheFirstPathWhichExists() {
185 \vfsStreamWrapper::register();
186 mkdir('vfs://MyTemplates');
187 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
188 $paths = array(
189 'vfs://NonExistantDir/UnknowFile.html',
190 'vfs://MyTemplates/@action.html'
191 );
192 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('expandGenericPathPattern'), array(), '', FALSE);
193 $templateView->expects($this->once())->method('expandGenericPathPattern')->with('@templateRoot/@subpackage/@controller/@action.@format', FALSE, FALSE)->will($this->returnValue($paths));
194 $templateView->setTemplateRootPath('MyTemplates');
195 $templateView->setPartialRootPath('MyPartials');
196 $templateView->setLayoutRootPath('MyLayouts');
197 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource', 'myCoolAction'));
198 }
199
200 /**
201 * @test
202 */
203 public function resolveTemplatePathAndFilenameReturnsTheExplicitlyConfiguredTemplatePathAndFilename() {
204 \vfsStreamWrapper::register();
205 mkdir('vfs://MyTemplates');
206 file_put_contents('vfs://MyTemplates/MyCoolAction.html', 'contentsOfMyCoolAction');
207 $templateView = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\View\\TemplateView', array('dummy'), array(), '', FALSE);
208 $templateView->_set('templatePathAndFilename', 'vfs://MyTemplates/MyCoolAction.html');
209 $this->assertSame('contentsOfMyCoolAction', $templateView->_call('getTemplateSource'));
210 }
211
212 }
213
214
215 ?>