‪TYPO3CMS  ‪main
PathUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 final class ‪PathUtilityTest extends UnitTestCase
28 {
29  protected bool ‪$backupEnvironment = true;
30 
36  public function ‪isCommonPrefixResolvedCorrectly(array $paths, $expected): void
37  {
38  $commonPrefix = ‪PathUtility::getCommonPrefix($paths);
39  self::assertEquals($expected, $commonPrefix);
40  }
41 
42  public static function ‪isCommonPrefixResolvedCorrectlyDataProvider(): array
43  {
44  return [
45  [
46  [
47  '/var/www/myhost.com/t3lib/',
48  ],
49  '/var/www/myhost.com/t3lib/',
50  ],
51  [
52  [
53  '/var/www/myhost.com/t3lib/',
54  '/var/www/myhost.com/t3lib/',
55  ],
56  '/var/www/myhost.com/t3lib/',
57  ],
58  [
59  [
60  '/var/www/myhost.com/typo3/',
61  '/var/www/myhost.com/t3lib/',
62  ],
63  '/var/www/myhost.com/',
64  ],
65  [
66  [
67  '/var/www/myhost.com/typo3/',
68  '/var/www/myhost.com/typo3',
69  ],
70  '/var/www/myhost.com/typo3/',
71  ],
72  [
73  [
74  '/var/www/myhost.com/typo3',
75  '/var/www/myhost.com/typo3',
76  ],
77  '/var/www/myhost.com/typo3/',
78  ],
79  [
80  [
81  '/var/www/myhost.com/uploads/',
82  '/var/www/myhost.com/typo3/',
83  '/var/www/myhost.com/t3lib/',
84  ],
85  '/var/www/myhost.com/',
86  ],
87  [
88  [
89  '/var/www/myhost.com/uploads/directory/',
90  '/var/www/myhost.com/typo3/sysext/',
91  '/var/www/myhost.com/t3lib/utility/',
92  ],
93  '/var/www/myhost.com/',
94  ],
95  [
96  [
97  'C:\\www\\myhost.com\\t3lib\\',
98  ],
99  'C:/www/myhost.com/t3lib/',
100  ],
101  [
102  [
103  'C:\\www\\myhost.com\\t3lib\\',
104  'C:\\www\\myhost.com\\t3lib\\',
105  ],
106  'C:/www/myhost.com/t3lib/',
107  ],
108  [
109  [
110  'C:\\www\\myhost.com\\typo3\\',
111  'C:\\www\\myhost.com\\t3lib\\',
112  ],
113  'C:/www/myhost.com/',
114  ],
115  [
116  [
117  'C:\\www\\myhost.com\\uploads\\',
118  'C:\\www\\myhost.com\\typo3\\',
119  'C:\\www\\myhost.com\\t3lib\\',
120  ],
121  'C:/www/myhost.com/',
122  ],
123  [
124  [
125  'C:\\www\\myhost.com\\uploads\\directory\\',
126  'C:\\www\\myhost.com\\typo3\\sysext\\',
127  'C:\\www\\myhost.com\\t3lib\\utility\\',
128  ],
129  'C:/www/myhost.com/',
130  ],
131  ];
132  }
133 
141  public function ‪isRelativePathResolvedCorrectly($source, $target, $expected): void
142  {
143  $relativePath = ‪PathUtility::getRelativePath($source, $target);
144  self::assertEquals($expected, $relativePath);
145  }
146 
147  public static function ‪isRelativePathResolvedCorrectlyDataProvider(): array
148  {
149  return [
150  [
151  '/',
152  ‪Environment::getPublicPath() . '/directory',
153  null,
154  ],
155  [
156  ‪Environment::getPublicPath() . '/t3lib/',
157  ‪Environment::getPublicPath() . '/t3lib/',
158  '',
159  ],
160  [
161  ‪Environment::getPublicPath() . '/typo3/',
162  ‪Environment::getPublicPath() . '/t3lib/',
163  '../t3lib/',
164  ],
165  [
167  ‪Environment::getPublicPath() . '/t3lib/',
168  't3lib/',
169  ],
170  [
171  ‪Environment::getPublicPath() . '/t3lib/',
172  ‪Environment::getPublicPath() . '/t3lib/stddb/',
173  'stddb/',
174  ],
175  [
176  ‪Environment::getPublicPath() . '/typo3/sysext/frontend/',
177  ‪Environment::getPublicPath() . '/t3lib/utility/',
178  '../../../t3lib/utility/',
179  ],
180  ];
181  }
182 
190  public function ‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected): void
191  {
192  $sanitizedPath = ‪PathUtility::sanitizeTrailingSeparator($path, $separator);
193  self::assertEquals($expected, $sanitizedPath);
194  }
195 
197  {
198  return [
199  ['/var/www//', '/', '/var/www/'],
200  ['/var/www/', '/', '/var/www/'],
201  ['/var/www', '/', '/var/www/'],
202  ];
203  }
204 
209  {
210  return [
211  'basic' => [
212  '/abc/def/one.txt',
213  '../two.txt',
214  '/abc/two.txt',
215  ],
216  'same folder' => [
217  '/abc/one.txt',
218  './two.txt',
219  '/abc/two.txt',
220  ],
221  'preserve relative path if path goes above start path' => [
222  'abc/one.txt',
223  '../../two.txt',
224  '../two.txt',
225  ],
226  'preserve absolute path even if path goes above start path' => [
227  '/abc/one.txt',
228  '../../two.txt',
229  '/two.txt',
230  ],
231  'base folder with same folder path' => [
232  '/abc/',
233  './two.txt',
234  '/abc/two.txt',
235  ],
236  'base folder with parent folder path' => [
237  '/abc/bar/',
238  '../foo.txt',
239  '/abc/foo.txt',
240  ],
241  ];
242  }
243 
248  public function ‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly(string $baseFileName, string $includeFileName, string $expectedFileName): void
249  {
250  $resolvedFilename = ‪PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
251  self::assertEquals($expectedFileName, $resolvedFilename);
252  }
253 
260  {
261  return [
262  'removes single-dot-elements' => [
263  'abc/./def/././ghi',
264  'abc/def/ghi',
265  ],
266  'removes ./ at beginning' => [
267  './abc/def/ghi',
268  'abc/def/ghi',
269  ],
270  'removes double-slashes' => [
271  'abc//def/ghi',
272  'abc/def/ghi',
273  ],
274  'removes double-slashes from front, but keeps absolute path' => [
275  '//abc/def/ghi',
276  '/abc/def/ghi',
277  ],
278  'makes double-dot-elements go one level higher, test #1' => [
279  'abc/def/ghi/../..',
280  'abc',
281  ],
282  'makes double-dot-elements go one level higher, test #2' => [
283  'abc/def/ghi/../123/456/..',
284  'abc/def/123',
285  ],
286  'makes double-dot-elements go one level higher, test #3' => [
287  'abc/../../def/ghi',
288  '../def/ghi',
289  ],
290  'makes double-dot-elements go one level higher, test #4' => [
291  'abc/def/ghi//../123/456/..',
292  'abc/def/123',
293  ],
294  'truncates slash at the end' => [
295  'abc/def/ghi/',
296  'abc/def/ghi',
297  ],
298  'keeps slash in front of absolute paths' => [
299  '/abc/def/ghi',
300  '/abc/def/ghi',
301  ],
302  'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => [
303  '/abc/../../def/ghi',
304  '/def/ghi',
305  ],
306  'double-dot-elements want to go higher, more than one segment' => [
307  '/abc/../../../../def/ghi',
308  '/def/ghi',
309  ],
310  'works with EXT-syntax-paths' => [
311  'EXT:abc/def/ghi/',
312  'EXT:abc/def/ghi',
313  ],
314  'truncates ending slash with space' => [
315  'abc/def/ ',
316  'abc/def',
317  ],
318  'truncates ending space' => [
319  'abc/def ',
320  'abc/def',
321  ],
322  'truncates ending dot' => [
323  'abc/def/.',
324  'abc/def',
325  ],
326  'does not truncates ending dot if part of name' => [
327  'abc/def.',
328  'abc/def.',
329  ],
330  'protocol is not removed' => [
331  'vfs://def/../text.txt',
332  'vfs://text.txt',
333  ],
334  'works with filenames' => [
335  '/def/../text.txt',
336  '/text.txt',
337  ],
338  'absolute windows path' => [
339  'C:\def\..\..\test.txt',
340  'C:/test.txt',
341  ],
342  'absolute windows path with more segments' => [
343  'C:\def\def2\def3\..\..\folder\subfolder\test.txt',
344  'C:/def/folder/subfolder/test.txt',
345  ],
346  'double slashaes' => [
347  'abc//def',
348  'abc/def',
349  ],
350  'multiple slashes' => [
351  'abc///////def',
352  'abc/def',
353  ],
354  ];
355  }
356 
361  public function ‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult): void
362  {
363  // Ensure Environment runs as Windows test
366  true,
367  false,
373  'WINDOWS'
374  );
375  self::assertSame(
376  $expectedResult,
378  );
379  }
380 
387  {
388  return [
389  'relative path' => [
390  'abc/def/ghi',
391  'abc/def',
392  ],
393  'absolute path 1' => [
394  '/var/www/html/index.php',
395  '/var/www/html',
396  ],
397  'absolute path 2' => [
398  '/var/www/html/typo3/index.php',
399  '/var/www/html/typo3',
400  ],
401  'windows path' => [
402  'C:\\inetpub\\index.php',
403  'C:\\inetpub',
404  ],
405  ];
406  }
407 
412  public function ‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult): void
413  {
414  self::assertSame(
415  $expectedResult,
417  );
418  }
419 
424  {
425  return [
426  'relative path' => [
427  'abc/def/ghi',
428  'ghi',
429  ],
430  'absolute path 1' => [
431  '/var/www/html/index.php',
432  'index.php',
433  ],
434  'absolute path 2' => [
435  '/var/www/html/typo3/index.php',
436  'index.php',
437  ],
438  'windows path' => [
439  'C:\\inetpub\\index.php',
440  'index.php',
441  ],
442  ];
443  }
444 
449  public function ‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult): void
450  {
451  self::assertSame(
452  $expectedResult,
454  );
455  }
456 
463  {
464  return [
465  'starting slash' => [
466  '/path',
467  false,
468  true,
469  ],
470  'starting slash on windows' => [
471  '/path',
472  true,
473  true,
474  ],
475  'no match' => [
476  'path',
477  false,
478  false,
479  ],
480  'no match on windows' => [
481  'path',
482  true,
483  false,
484  ],
485  'path starts with C:/' => [
486  'C:/folder',
487  false,
488  false,
489  ],
490  'path starts with C:/ on windows' => [
491  'C:/folder',
492  true,
493  true,
494  ],
495  'path starts with C:\\' => [
496  'C:\\folder',
497  false,
498  false,
499  ],
500  'path starts with C:\\ on windows' => [
501  'C:\\folder',
502  true,
503  true,
504  ],
505  'path empty' => [
506  '',
507  false,
508  false,
509  ],
510  ];
511  }
512 
517  public function ‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult): void
518  {
519  if ($isWindows) {
520  // Ensure Environment runs as Windows test
523  true,
524  false,
530  'WINDOWS'
531  );
532  }
533 
534  self::assertSame($expectedResult, ‪PathUtility::isAbsolutePath($inputPath));
535  }
536 
537  public static function ‪hasProtocolAndSchemeDataProvider(): array
538  {
539  return [
540  ['//example.com/demo.html', true],
541  ['http://example.com/demo.html', true],
542  ['https://example.com/demo.html', true],
543  ['f://example.com/demo.html', true],
544  ['f:/example.com/demo.html', false],
545  ['://example.com/demo.html', false],
546  [':/example.com/demo.html', false],
547  ['/example.com/demo.html', false],
548  ['example.com/demo.html', false],
549  ['demo.html', false],
550  ['\\\\server\\unc-path\\demo.html', false],
551  ['\\\\example.com\\demo.html', false],
552  ];
553  }
554 
559  public function ‪hasProtocolAndScheme(string ‪$url, bool $result): void
560  {
561  self::assertSame($result, ‪PathUtility::hasProtocolAndScheme(‪$url));
562  }
563 }
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath(string $path)
Definition: PathUtility.php:364
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest
Definition: PathUtilityTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParent
‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:412
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static isAbsolutePath(string $path)
Definition: PathUtility.php:286
‪TYPO3\CMS\Core\Utility\PathUtility\sanitizeTrailingSeparator
‪static sanitizeTrailingSeparator(string $path, string $separator='/')
Definition: PathUtility.php:203
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectly
‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
Definition: PathUtilityTest.php:190
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider
‪static getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider()
Definition: PathUtilityTest.php:208
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Utility\PathUtility\getCommonPrefix
‪static getCommonPrefix(array $paths)
Definition: PathUtility.php:165
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystemsPathDataProvider
‪static array[] isAbsolutePathRespectsAllOperatingSystemsPathDataProvider()
Definition: PathUtilityTest.php:462
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParentDataProvider
‪static string[][] dirnameDuringBootstrapCorrectlyFetchesParentDataProvider()
Definition: PathUtilityTest.php:386
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystems
‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult)
Definition: PathUtilityTest.php:517
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider
‪static basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider()
Definition: PathUtilityTest.php:423
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectlyDataProvider
‪static isRelativePathResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:147
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsolutePathOfRelativeReferencedFileOrPath
‪static string getAbsolutePathOfRelativeReferencedFileOrPath(string $baseFilenameOrPath, string $includeFileName)
Definition: PathUtility.php:319
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasename
‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:449
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly
‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly(string $baseFileName, string $includeFileName, string $expectedFileName)
Definition: PathUtilityTest.php:248
‪TYPO3\CMS\Core\Utility\PathUtility\getRelativePath
‪static getRelativePath(string $sourcePath, string $targetPath)
Definition: PathUtility.php:129
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectly
‪isRelativePathResolvedCorrectly($source, $target, $expected)
Definition: PathUtilityTest.php:141
‪TYPO3\CMS\Core\Utility\PathUtility\basenameDuringBootstrap
‪static basenameDuringBootstrap(string $path)
Definition: PathUtility.php:348
‪TYPO3\CMS\Core\Utility\PathUtility\dirnameDuringBootstrap
‪static string dirnameDuringBootstrap(string $path)
Definition: PathUtility.php:337
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:100
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectlyDataProvider
‪static isCommonPrefixResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:42
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\$backupEnvironment
‪bool $backupEnvironment
Definition: PathUtilityTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectly
‪isCommonPrefixResolvedCorrectly(array $paths, $expected)
Definition: PathUtilityTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\hasProtocolAndScheme
‪hasProtocolAndScheme(string $url, bool $result)
Definition: PathUtilityTest.php:559
‪TYPO3\CMS\Core\Utility\PathUtility\hasProtocolAndScheme
‪static hasProtocolAndScheme(string $path)
Definition: PathUtility.php:445
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\hasProtocolAndSchemeDataProvider
‪static hasProtocolAndSchemeDataProvider()
Definition: PathUtilityTest.php:537
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPathDataProvider
‪static string[][] getCanonicalPathCorrectlyCleansPathDataProvider()
Definition: PathUtilityTest.php:259
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectlyDataProvider
‪static isTrailingSeparatorSanitizedCorrectlyDataProvider()
Definition: PathUtilityTest.php:196
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPath
‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult)
Definition: PathUtilityTest.php:361