[TASK] Replace TYPO3_OS constant with Environment check
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / PathUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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 use TYPO3\CMS\Core\Core\Environment;
17 use TYPO3\CMS\Core\Utility\PathUtility;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Testcase for class \TYPO3\CMS\Core\Utility\PathUtility
22 */
23 class PathUtilityTest extends UnitTestCase
24 {
25 /**
26 * Restore Environment after the test
27 * @var bool
28 */
29 protected $backupEnvironment = true;
30
31 /**
32 * @param array $paths
33 * @param string $expected
34 * @dataProvider isCommonPrefixResolvedCorrectlyDataProvider
35 * @test
36 */
37 public function isCommonPrefixResolvedCorrectly(array $paths, $expected)
38 {
39 $commonPrefix = PathUtility::getCommonPrefix($paths);
40 $this->assertEquals($expected, $commonPrefix);
41 }
42
43 /**
44 * @return array
45 */
46 public function isCommonPrefixResolvedCorrectlyDataProvider()
47 {
48 return [
49 [
50 [
51 '/var/www/myhost.com/t3lib/'
52 ],
53 '/var/www/myhost.com/t3lib/'
54 ],
55 [
56 [
57 '/var/www/myhost.com/t3lib/',
58 '/var/www/myhost.com/t3lib/'
59 ],
60 '/var/www/myhost.com/t3lib/'
61 ],
62 [
63 [
64 '/var/www/myhost.com/typo3/',
65 '/var/www/myhost.com/t3lib/'
66 ],
67 '/var/www/myhost.com/'
68 ],
69 [
70 [
71 '/var/www/myhost.com/uploads/',
72 '/var/www/myhost.com/typo3/',
73 '/var/www/myhost.com/t3lib/'
74 ],
75 '/var/www/myhost.com/'
76 ],
77 [
78 [
79 '/var/www/myhost.com/uploads/directory/',
80 '/var/www/myhost.com/typo3/sysext/',
81 '/var/www/myhost.com/t3lib/utility/'
82 ],
83 '/var/www/myhost.com/'
84 ],
85 [
86 [
87 'C:\\www\\myhost.com\\t3lib\\'
88 ],
89 'C:/www/myhost.com/t3lib/'
90 ],
91 [
92 [
93 'C:\\www\\myhost.com\\t3lib\\',
94 'C:\\www\\myhost.com\\t3lib\\'
95 ],
96 'C:/www/myhost.com/t3lib/'
97 ],
98 [
99 [
100 'C:\\www\\myhost.com\\typo3\\',
101 'C:\\www\\myhost.com\\t3lib\\'
102 ],
103 'C:/www/myhost.com/'
104 ],
105 [
106 [
107 'C:\\www\\myhost.com\\uploads\\',
108 'C:\\www\\myhost.com\\typo3\\',
109 'C:\\www\\myhost.com\\t3lib\\'
110 ],
111 'C:/www/myhost.com/'
112 ],
113 [
114 [
115 'C:\\www\\myhost.com\\uploads\\directory\\',
116 'C:\\www\\myhost.com\\typo3\\sysext\\',
117 'C:\\www\\myhost.com\\t3lib\\utility\\'
118 ],
119 'C:/www/myhost.com/'
120 ]
121 ];
122 }
123
124 /**
125 * @param string $source
126 * @param string $target
127 * @param string $expected
128 * @dataProvider isRelativePathResolvedCorrectlyDataProvider
129 * @test
130 */
131 public function isRelativePathResolvedCorrectly($source, $target, $expected)
132 {
133 $relativePath = PathUtility::getRelativePath($source, $target);
134 $this->assertEquals($expected, $relativePath);
135 }
136
137 /**
138 * @return array
139 */
140 public function isRelativePathResolvedCorrectlyDataProvider()
141 {
142 return [
143 [
144 '/',
145 PATH_site . 'directory',
146 null
147 ],
148 [
149 PATH_site . 't3lib/',
150 PATH_site . 't3lib/',
151 ''
152 ],
153 [
154 PATH_site . 'typo3/',
155 PATH_site . 't3lib/',
156 '../t3lib/'
157 ],
158 [
159 PATH_site,
160 PATH_site . 't3lib/',
161 't3lib/'
162 ],
163 [
164 PATH_site . 't3lib/',
165 PATH_site . 't3lib/stddb/',
166 'stddb/'
167 ],
168 [
169 PATH_site . 'typo3/sysext/frontend/',
170 PATH_site . 't3lib/utility/',
171 '../../../t3lib/utility/'
172 ],
173 ];
174 }
175
176 /**
177 * @param string $path
178 * @param string $separator
179 * @param string $expected
180 * @dataProvider isTrailingSeparatorSanitizedCorrectlyDataProvider
181 * @test
182 */
183 public function isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
184 {
185 $sanitizedPath = PathUtility::sanitizeTrailingSeparator($path, $separator);
186 $this->assertEquals($expected, $sanitizedPath);
187 }
188
189 /**
190 * @return array
191 */
192 public function isTrailingSeparatorSanitizedCorrectlyDataProvider()
193 {
194 return [
195 ['/var/www//', '/', '/var/www/'],
196 ['/var/www/', '/', '/var/www/'],
197 ['/var/www', '/', '/var/www/']
198 ];
199 }
200
201 /**
202 * Data Provider for getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly
203 *
204 * @return array
205 */
206 public function getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider()
207 {
208 return [
209 'basic' => [
210 '/abc/def/one.txt',
211 '../two.txt',
212 '/abc/two.txt'
213 ],
214 'same folder' => [
215 '/abc/one.txt',
216 './two.txt',
217 '/abc/two.txt'
218 ],
219 'preserve relative path if path goes above start path' => [
220 'abc/one.txt',
221 '../../two.txt',
222 '../two.txt'
223 ],
224 'preserve absolute path even if path goes above start path' => [
225 '/abc/one.txt',
226 '../../two.txt',
227 '/two.txt',
228 ],
229 'base folder with same folder path' => [
230 '/abc/',
231 './two.txt',
232 '/abc/two.txt'
233 ],
234 'base folder with parent folder path' => [
235 '/abc/bar/',
236 '../foo.txt',
237 '/abc/foo.txt'
238 ],
239 ];
240 }
241
242 /**
243 * @param $baseFileName
244 * @param $includeFileName
245 * @param $expectedFileName
246 * @test
247 * @dataProvider getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider
248 */
249 public function getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
250 {
251 $resolvedFilename = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
252 $this->assertEquals($expectedFileName, $resolvedFilename);
253 }
254
255 /**
256 * Data provider for getCanonicalPathCorrectlyCleansPath
257 *
258 * @return array
259 */
260 public function getCanonicalPathCorrectlyCleansPathDataProvider()
261 {
262 return [
263 'removes single-dot-elements' => [
264 'abc/./def/././ghi',
265 'abc/def/ghi'
266 ],
267 'removes ./ at beginning' => [
268 './abc/def/ghi',
269 'abc/def/ghi'
270 ],
271 'removes double-slashes' => [
272 'abc//def/ghi',
273 'abc/def/ghi'
274 ],
275 'removes double-slashes from front, but keeps absolute path' => [
276 '//abc/def/ghi',
277 '/abc/def/ghi'
278 ],
279 'makes double-dot-elements go one level higher, test #1' => [
280 'abc/def/ghi/../..',
281 'abc'
282 ],
283 'makes double-dot-elements go one level higher, test #2' => [
284 'abc/def/ghi/../123/456/..',
285 'abc/def/123'
286 ],
287 'makes double-dot-elements go one level higher, test #3' => [
288 'abc/../../def/ghi',
289 '../def/ghi'
290 ],
291 'makes double-dot-elements go one level higher, test #4' => [
292 'abc/def/ghi//../123/456/..',
293 'abc/def/123'
294 ],
295 'truncates slash at the end' => [
296 'abc/def/ghi/',
297 'abc/def/ghi'
298 ],
299 'keeps slash in front of absolute paths' => [
300 '/abc/def/ghi',
301 '/abc/def/ghi'
302 ],
303 'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => [
304 '/abc/../../def/ghi',
305 '/def/ghi'
306 ],
307 'works with EXT-syntax-paths' => [
308 'EXT:abc/def/ghi/',
309 'EXT:abc/def/ghi'
310 ],
311 'truncates ending slash with space' => [
312 'abc/def/ ',
313 'abc/def'
314 ],
315 'truncates ending space' => [
316 'abc/def ',
317 'abc/def'
318 ],
319 'truncates ending dot' => [
320 'abc/def/.',
321 'abc/def'
322 ],
323 'does not truncates ending dot if part of name' => [
324 'abc/def.',
325 'abc/def.'
326 ],
327 'protocol is not removed' => [
328 'vfs://def/../text.txt',
329 'vfs://text.txt'
330 ],
331 'works with filenames' => [
332 '/def/../text.txt',
333 '/text.txt'
334 ],
335 'absolute windwos path' => [
336 'C:\def\..\..\test.txt',
337 'C:/test.txt'
338 ],
339 'double slashaes' => [
340 'abc//def',
341 'abc/def'
342 ],
343 'multiple slashes' => [
344 'abc///////def',
345 'abc/def'
346 ],
347 ];
348 }
349
350 /**
351 * @test
352 * @dataProvider getCanonicalPathCorrectlyCleansPathDataProvider
353 */
354 public function getCanonicalPathCorrectlyCleansPath($inputName, $expectedResult)
355 {
356 // Ensure Environment runs as Windows test
357 Environment::initialize(
358 Environment::getContext(),
359 true,
360 false,
361 Environment::getProjectPath(),
362 Environment::getPublicPath(),
363 Environment::getVarPath(),
364 Environment::getConfigPath(),
365 Environment::getCurrentScript(),
366 'WINDOWS'
367 );
368 $this->assertEquals(
369 $expectedResult,
370 PathUtility::getCanonicalPath($inputName)
371 );
372 }
373 }