[TASK] Add unit tests for TemplateService->splitConfArray()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / TypoScript / TemplateServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\TypoScript;
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\Utility\ExtensionManagementUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Testcase for \TYPO3\CMS\Core\TypoScript\TemplateService
22 */
23 class TemplateServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @var \TYPO3\CMS\Core\TypoScript\TemplateService
27 */
28 protected $templateService;
29
30 /**
31 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Core\TypoScript\TemplateService
32 */
33 protected $templateServiceMock;
34
35 /**
36 * Sets up this test case.
37 *
38 * @return void
39 */
40 protected function setUp() {
41 $GLOBALS['TYPO3_LOADED_EXT'] = array();
42 $this->templateService = new \TYPO3\CMS\Core\TypoScript\TemplateService();
43 $this->templateService->tt_track = FALSE;
44 $this->templateServiceMock = $this->getAccessibleMock(\TYPO3\CMS\Core\TypoScript\TemplateService::class, array('dummy'));
45 $this->templateServiceMock->tt_track = FALSE;
46 }
47
48 /**
49 * @test
50 */
51 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow() {
52 $row = array('foo');
53 $GLOBALS['TSFE'] = new \stdClass();
54 $sysPageMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class);
55 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
56 $GLOBALS['TSFE']->sys_page = $sysPageMock;
57 $this->templateService->versionOL($row);
58 }
59
60 /**
61 * @test
62 */
63 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested() {
64 $identifier = $this->getUniqueId('test');
65 $GLOBALS['TYPO3_LOADED_EXT'] = array(
66 $identifier => array(
67 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
68 'core', 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
69 ),
70 ),
71 );
72
73 $this->templateService->runThroughTemplates(array(), 0);
74 $this->assertFalse(
75 in_array('test.Core.TypoScript = 1', $this->templateService->config)
76 );
77 }
78
79 /**
80 * @test
81 */
82 public function extensionStaticsAreProcessedIfExplicitlyRequested() {
83 $identifier = $this->getUniqueId('test');
84 $GLOBALS['TYPO3_LOADED_EXT'] = array(
85 $identifier => array(
86 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
87 'core', 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
88 ),
89 'ext_typoscript_constants.txt' => ''
90 ),
91 );
92
93 $mockPackage = $this->getMock(\TYPO3\CMS\Core\Package\Package::class, array('getPackagePath'), array(), '', FALSE);
94 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(''));
95
96 $mockPackageManager = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class, array('isPackageActive' , 'getPackage'));
97 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(TRUE));
98 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
99 ExtensionManagementUtility::setPackageManager($mockPackageManager);
100
101 $this->templateService->setProcessExtensionStatics(TRUE);
102 $this->templateService->runThroughTemplates(array(), 0);
103
104 $this->assertTrue(
105 in_array('test.Core.TypoScript = 1', $this->templateService->config)
106 );
107
108 ExtensionManagementUtility::setPackageManager(GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class));
109 }
110
111 /**
112 * @test
113 */
114 public function updateRootlineDataOverwritesOwnArrayData() {
115 $originalRootline = array(
116 0 => array('uid' => 2, 'title' => 'originalTitle'),
117 1 => array('uid' => 3, 'title' => 'originalTitle2'),
118 );
119
120 $updatedRootline = array(
121 0 => array('uid' => 1, 'title' => 'newTitle'),
122 1 => array('uid' => 2, 'title' => 'newTitle2'),
123 2 => array('uid' => 3, 'title' => 'newTitle3'),
124 );
125
126 $expectedRootline = array(
127 0 => array('uid' => 2, 'title' => 'newTitle2'),
128 1 => array('uid' => 3, 'title' => 'newTitle3'),
129 );
130
131 $this->templateServiceMock->_set('rootLine', $originalRootline);
132 $this->templateServiceMock->updateRootlineData($updatedRootline);
133 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
134 }
135
136 /**
137 * @test
138 * @expectedException \RuntimeException
139 */
140 public function updateRootlineDataWithInvalidNewRootlineThrowsException() {
141 $originalRootline = array(
142 0 => array('uid' => 2, 'title' => 'originalTitle'),
143 1 => array('uid' => 3, 'title' => 'originalTitle2'),
144 );
145
146 $newInvalidRootline = array(
147 0 => array('uid' => 1, 'title' => 'newTitle'),
148 1 => array('uid' => 2, 'title' => 'newTitle2'),
149 );
150
151 $this->templateServiceMock->_set('rootLine', $originalRootline);
152 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
153 }
154
155 /**
156 * @test
157 */
158 public function getFileNameReturnsUrlCorrectly() {
159 $this->assertSame('http://example.com', $this->templateService->getFileName('http://example.com'));
160 $this->assertSame('https://example.com', $this->templateService->getFileName('https://example.com'));
161 }
162
163 /**
164 * @test
165 */
166 public function getFileNameReturnsFileCorrectly() {
167 $this->assertSame('typo3/index.php', $this->templateService->getFileName('typo3/index.php'));
168 }
169
170 /**
171 * @test
172 */
173 public function getFileNameReturnsNullIfDirectory() {
174 $this->assertNull($this->templateService->getFileName(__DIR__));
175 }
176
177 /**
178 * @test
179 */
180 public function getFileNameReturnsNullWithInvalidFileName() {
181 $this->assertNull($this->templateService->getFileName(' '));
182 $this->assertNull($this->templateService->getFileName('something/../else'));
183 }
184
185 public function splitConfDataProvider() {
186 return array(
187 array(
188 array('splitConfiguration' => 'a'),
189 3,
190 array(
191 0 => array('splitConfiguration' => 'a'),
192 1 => array('splitConfiguration' => 'a'),
193 2 => array('splitConfiguration' => 'a')
194 )
195 ),
196 array(
197 array('splitConfiguration' => 'a || b || c'),
198 5,
199 array(
200 0 => array('splitConfiguration' => 'a'),
201 1 => array('splitConfiguration' => 'b'),
202 2 => array('splitConfiguration' => 'c'),
203 3 => array('splitConfiguration' => 'c'),
204 4 => array('splitConfiguration' => 'c')
205 )
206 ),
207 array(
208 array('splitConfiguration' => 'a || b |*| c'),
209 5,
210 array(
211 0 => array('splitConfiguration' => 'a'),
212 1 => array('splitConfiguration' => 'b'),
213 2 => array('splitConfiguration' => 'c'),
214 3 => array('splitConfiguration' => 'c'),
215 4 => array('splitConfiguration' => 'c')
216 )
217 ),
218 array(
219 array('splitConfiguration' => 'a || b |*| c |*| d || e'),
220 7,
221 array(
222 0 => array('splitConfiguration' => 'a'),
223 1 => array('splitConfiguration' => 'b'),
224 2 => array('splitConfiguration' => 'c'),
225 3 => array('splitConfiguration' => 'c'),
226 4 => array('splitConfiguration' => 'c'),
227 5 => array('splitConfiguration' => 'd'),
228 6 => array('splitConfiguration' => 'e')
229 )
230 ),
231 array(
232 array('splitConfiguration' => 'a || b |*| c |*| d || e'),
233 4,
234 array(
235 0 => array('splitConfiguration' => 'a'),
236 1 => array('splitConfiguration' => 'b'),
237 2 => array('splitConfiguration' => 'd'),
238 3 => array('splitConfiguration' => 'e')
239 )
240 ),
241 array(
242 array('splitConfiguration' => 'a || b |*| c |*| d || e'),
243 3,
244 array(
245 0 => array('splitConfiguration' => 'a'),
246 1 => array('splitConfiguration' => 'd'),
247 2 => array('splitConfiguration' => 'e')
248 )
249 ),
250 array(
251 array('splitConfiguration' => 'a || b |*||*| c || d'),
252 7,
253 array(
254 0 => array('splitConfiguration' => 'a'),
255 1 => array('splitConfiguration' => 'b'),
256 2 => array('splitConfiguration' => 'b'),
257 3 => array('splitConfiguration' => 'b'),
258 4 => array('splitConfiguration' => 'b'),
259 5 => array('splitConfiguration' => 'c'),
260 6 => array('splitConfiguration' => 'd')
261 )
262 ),
263 array(
264 array('splitConfiguration' => '|*||*| a || b'),
265 7,
266 array(
267 0 => array('splitConfiguration' => 'a'),
268 1 => array('splitConfiguration' => 'a'),
269 2 => array('splitConfiguration' => 'a'),
270 3 => array('splitConfiguration' => 'a'),
271 4 => array('splitConfiguration' => 'a'),
272 5 => array('splitConfiguration' => 'a'),
273 6 => array('splitConfiguration' => 'b')
274 )
275 ),
276 array(
277 array('splitConfiguration' => 'a |*| b || c |*|'),
278 7,
279 array(
280 0 => array('splitConfiguration' => 'a'),
281 1 => array('splitConfiguration' => 'b'),
282 2 => array('splitConfiguration' => 'c'),
283 3 => array('splitConfiguration' => 'b'),
284 4 => array('splitConfiguration' => 'c'),
285 5 => array('splitConfiguration' => 'b'),
286 6 => array('splitConfiguration' => 'c')
287 )
288 ),
289 );
290 }
291
292 /**
293 * @test
294 * @dataProvider splitConfDataProvider
295 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ObjectsAndProperties/Index.html#objects-optionsplit
296 */
297 public function splitConfArraytest($configuration, $splitCount, $expected) {
298 $actual = $this->templateService->splitConfArray($configuration, $splitCount);
299 $this->assertSame($expected, $actual);
300 }
301 }