[TASK] Improve performance of Fluid TemplatePaths
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / View / TemplatePathsTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\View;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
18 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Fluid\View\TemplatePaths;
20
21 /**
22 * Test case
23 */
24 class TemplatePathsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @return array
28 */
29 public function getPathSetterMethodTestValues()
30 {
31 $generator = function ($method, $indexType = 'numeric') {
32 switch ($indexType) {
33 default:
34 case 'numeric':
35 $set = [
36 20 => 'bar',
37 0 => 'baz',
38 100 => 'boz',
39 10 => 'foo',
40 ];
41 $expected = [
42 0 => 'baz',
43 10 => 'foo',
44 20 => 'bar',
45 100 => 'boz',
46 ];
47 break;
48 case 'alpha':
49 $set = [
50 'bcd' => 'bar',
51 'abc' => 'foo',
52 ];
53 $expected = [
54 'bcd' => 'bar',
55 'abc' => 'foo',
56 ];
57 break;
58 case 'alphanumeric':
59 $set = [
60 0 => 'baz',
61 'bcd' => 'bar',
62 15 => 'boz',
63 'abc' => 'foo',
64 ];
65 $expected = [
66 0 => 'baz',
67 'bcd' => 'bar',
68 15 => 'boz',
69 'abc' => 'foo',
70 ];
71 break;
72 }
73 return [$method, $set, $expected];
74 };
75 return [
76 'simple numeric index, template' => $generator(TemplatePaths::CONFIG_TEMPLATEROOTPATHS, 'numeric'),
77 'alpha index, template' => $generator(TemplatePaths::CONFIG_TEMPLATEROOTPATHS, 'alpha'),
78 'alpha-numeric index, template' => $generator(TemplatePaths::CONFIG_TEMPLATEROOTPATHS, 'alphanumeric'),
79 'simple numeric index, partial' => $generator(TemplatePaths::CONFIG_PARTIALROOTPATHS, 'numeric'),
80 'alpha index, partial' => $generator(TemplatePaths::CONFIG_PARTIALROOTPATHS, 'alpha'),
81 'alpha-numeric index, partial' => $generator(TemplatePaths::CONFIG_PARTIALROOTPATHS, 'alphanumeric'),
82 'simple numeric index, layout' => $generator(TemplatePaths::CONFIG_LAYOUTROOTPATHS, 'numeric'),
83 'alpha index, layout' => $generator(TemplatePaths::CONFIG_LAYOUTROOTPATHS, 'alpha'),
84 'alpha-numeric index, layout' => $generator(TemplatePaths::CONFIG_LAYOUTROOTPATHS, 'alphanumeric'),
85 ];
86 }
87
88 /**
89 * @test
90 * @dataProvider getPathSetterMethodTestValues
91 * @param string $method
92 * @param array $paths
93 * @param array $expected
94 */
95 public function pathSetterMethodSortsPathsByKeyDescending($method, array $paths, array $expected)
96 {
97 $setter = 'set' . ucfirst($method);
98 $subject = $this->getMockBuilder(TemplatePaths::class)->setMethods(['sanitizePath'])->getMock();
99 $subject->expects($this->any())->method('sanitizePath')->willReturnArgument(0);
100 $subject->$setter($paths);
101 $this->assertAttributeSame($expected, $method, $subject);
102 }
103
104 /**
105 * @test
106 */
107 public function getContextSpecificViewConfigurationSortsTypoScriptConfiguredPathsCorrectlyInFrontendMode()
108 {
109 $configurationManager = $this->getMockBuilder(ConfigurationManagerInterface::class)->getMockForAbstractClass();
110 $configurationManager->expects($this->once())->method('getConfiguration')->willReturn([
111 'plugin.' => [
112 'tx_test.' => [
113 'view.' => [
114 'templateRootPaths.' => [
115 '30' => 'third',
116 '10' => 'first',
117 '20' => 'second'
118 ],
119 'partialRootPaths.' => [
120 '20' => '2',
121 '30' => '3',
122 '10' => '1'
123 ],
124 'layoutRootPaths.' => [
125 '130' => '3.',
126 '10' => '1.',
127 '120' => '2.'
128 ],
129 ]
130 ]
131 ]
132 ]);
133 $cache = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
134 $cache->expects($this->once())->method('get')->willReturn(false);
135 $cache->expects($this->once())->method('set');
136 $subject = $this->getMockBuilder(TemplatePaths::class)->setMethods(['getConfigurationManager', 'getExtensionPrivateResourcesPath', 'getRuntimeCache', 'isBackendMode', 'isFrontendMode'])->getMock();
137 $subject->expects($this->once())->method('getExtensionPrivateResourcesPath')->with('test')->willReturn('test/');
138 $subject->expects($this->once())->method('getConfigurationManager')->willReturn($configurationManager);
139 $subject->expects($this->once())->method('getRuntimeCache')->willReturn($cache);
140 $subject->expects($this->once())->method('isBackendMode')->willReturn(false);
141 $subject->expects($this->once())->method('isFrontendMode')->willReturn(true);
142 $result = $this->callInaccessibleMethod($subject, 'getContextSpecificViewConfiguration', 'test');
143 $this->assertSame([
144 'templateRootPaths' => [
145 'test/Templates/',
146 'first',
147 'second',
148 'third'
149 ],
150 'partialRootPaths' => [
151 'test/Partials/',
152 '1',
153 '2',
154 '3'
155 ],
156 'layoutRootPaths' => [
157 'test/Layouts/',
158 '1.',
159 '2.',
160 '3.'
161 ]
162 ], $result);
163 }
164
165 /**
166 * @test
167 */
168 public function getContextSpecificViewConfigurationSortsTypoScriptConfiguredPathsCorrectlyInBackendMode()
169 {
170 $configurationManager = $this->getMockBuilder(ConfigurationManagerInterface::class)->getMockForAbstractClass();
171 $configurationManager->expects($this->once())->method('getConfiguration')->willReturn([
172 'module.' => [
173 'tx_test2.' => [
174 'view.' => [
175 'templateRootPaths.' => [
176 '30' => 'third',
177 '10' => 'first',
178 '20' => 'second'
179 ],
180 'partialRootPaths.' => [
181 '20' => '2',
182 '30' => '3',
183 '10' => '1'
184 ],
185 'layoutRootPaths.' => [
186 '130' => '3.',
187 '10' => '1.',
188 '120' => '2.'
189 ],
190 ]
191 ]
192 ]
193 ]);
194 $cache = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
195 $cache->expects($this->once())->method('get')->willReturn(false);
196 $cache->expects($this->once())->method('set');
197 $subject = $this->getMockBuilder(TemplatePaths::class)->setMethods(['getConfigurationManager', 'getExtensionPrivateResourcesPath', 'getRuntimeCache', 'isBackendMode', 'isFrontendMode'])->getMock();
198 $subject->expects($this->once())->method('getExtensionPrivateResourcesPath')->with('test2')->willReturn('test/');
199 $subject->expects($this->once())->method('getConfigurationManager')->willReturn($configurationManager);
200 $subject->expects($this->once())->method('getRuntimeCache')->willReturn($cache);
201 $subject->expects($this->once())->method('isBackendMode')->willReturn(true);
202 $subject->expects($this->never())->method('isFrontendMode');
203 $result = $this->callInaccessibleMethod($subject, 'getContextSpecificViewConfiguration', 'test2');
204 $this->assertSame([
205 'templateRootPaths' => [
206 'test/Templates/',
207 'first',
208 'second',
209 'third'
210 ],
211 'partialRootPaths' => [
212 'test/Partials/',
213 '1',
214 '2',
215 '3'
216 ],
217 'layoutRootPaths' => [
218 'test/Layouts/',
219 '1.',
220 '2.',
221 '3.'
222 ]
223 ], $result);
224 }
225
226 /**
227 * @test
228 */
229 public function getContextSpecificViewConfigurationDoesNotResolveFromTypoScriptAndDoesNotSortInUnspecifiedMode()
230 {
231 $configurationManager = $this->getMockBuilder(ConfigurationManagerInterface::class)->getMockForAbstractClass();
232 $configurationManager->expects($this->once())->method('getConfiguration')->willReturn([
233 'plugin.' => [
234 'tx_test.' => [
235 'view.' => [
236 'templateRootPaths.' => [
237 '30' => 'third',
238 '10' => 'first',
239 '20' => 'second'
240 ],
241 'partialRootPaths.' => [
242 '20' => '2',
243 '30' => '3',
244 '10' => '1'
245 ],
246 'layoutRootPaths.' => [
247 '130' => '3.',
248 '10' => '1.',
249 '120' => '2.'
250 ],
251 ]
252 ]
253 ]
254 ]);
255 $cache = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
256 $cache->expects($this->once())->method('get')->willReturn(false);
257 $cache->expects($this->once())->method('set');
258 $subject = $this->getMockBuilder(TemplatePaths::class)->setMethods(['getConfigurationManager', 'getExtensionPrivateResourcesPath', 'getRuntimeCache', 'isBackendMode', 'isFrontendMode'])->getMock();
259 $subject->expects($this->once())->method('getExtensionPrivateResourcesPath')->with('test')->willReturn('test/');
260 $subject->expects($this->once())->method('getConfigurationManager')->willReturn($configurationManager);
261 $subject->expects($this->once())->method('getRuntimeCache')->willReturn($cache);
262 $subject->expects($this->once())->method('isBackendMode')->willReturn(false);
263 $subject->expects($this->once())->method('isFrontendMode')->willReturn(false);
264 $result = $this->callInaccessibleMethod($subject, 'getContextSpecificViewConfiguration', 'test');
265 $this->assertSame([
266 'templateRootPaths' => [
267 'test/Templates/'
268 ],
269 'partialRootPaths' => [
270 'test/Partials/'
271 ],
272 'layoutRootPaths' => [
273 'test/Layouts/'
274 ]
275 ], $result);
276 }
277 }