[BUGFIX] Make TemplatePaths->fillDefaultsByPackageName sort paths
[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 getContextSpecificViewConfigurationSortsTypoScriptConfiguredPathsCorrectly()
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'])->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 $result = $this->callInaccessibleMethod($subject, 'getContextSpecificViewConfiguration', 'test');
142 $this->assertSame([
143 'templateRootPaths' => [
144 'test/Templates/',
145 'first',
146 'second',
147 'third'
148 ],
149 'partialRootPaths' => [
150 'test/Partials/',
151 '1',
152 '2',
153 '3'
154 ],
155 'layoutRootPaths' => [
156 'test/Layouts/',
157 '1.',
158 '2.',
159 '3.'
160 ]
161 ], $result);
162 }
163 }