‪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 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  'works with EXT-syntax-paths' => [
307  'EXT:abc/def/ghi/',
308  'EXT:abc/def/ghi',
309  ],
310  'truncates ending slash with space' => [
311  'abc/def/ ',
312  'abc/def',
313  ],
314  'truncates ending space' => [
315  'abc/def ',
316  'abc/def',
317  ],
318  'truncates ending dot' => [
319  'abc/def/.',
320  'abc/def',
321  ],
322  'does not truncates ending dot if part of name' => [
323  'abc/def.',
324  'abc/def.',
325  ],
326  'protocol is not removed' => [
327  'vfs://def/../text.txt',
328  'vfs://text.txt',
329  ],
330  'works with filenames' => [
331  '/def/../text.txt',
332  '/text.txt',
333  ],
334  'absolute windows path' => [
335  'C:\def\..\..\test.txt',
336  'C:/test.txt',
337  ],
338  'double slashaes' => [
339  'abc//def',
340  'abc/def',
341  ],
342  'multiple slashes' => [
343  'abc///////def',
344  'abc/def',
345  ],
346  ];
347  }
348 
353  public function ‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult): void
354  {
355  // Ensure Environment runs as Windows test
358  true,
359  false,
365  'WINDOWS'
366  );
367  self::assertSame(
368  $expectedResult,
370  );
371  }
372 
379  {
380  return [
381  'relative path' => [
382  'abc/def/ghi',
383  'abc/def',
384  ],
385  'absolute path 1' => [
386  '/var/www/html/index.php',
387  '/var/www/html',
388  ],
389  'absolute path 2' => [
390  '/var/www/html/typo3/index.php',
391  '/var/www/html/typo3',
392  ],
393  'windows path' => [
394  'C:\\inetpub\\index.php',
395  'C:\\inetpub',
396  ],
397  ];
398  }
399 
404  public function ‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult): void
405  {
406  self::assertSame(
407  $expectedResult,
409  );
410  }
411 
416  {
417  return [
418  'relative path' => [
419  'abc/def/ghi',
420  'ghi',
421  ],
422  'absolute path 1' => [
423  '/var/www/html/index.php',
424  'index.php',
425  ],
426  'absolute path 2' => [
427  '/var/www/html/typo3/index.php',
428  'index.php',
429  ],
430  'windows path' => [
431  'C:\\inetpub\\index.php',
432  'index.php',
433  ],
434  ];
435  }
436 
441  public function ‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult): void
442  {
443  self::assertSame(
444  $expectedResult,
446  );
447  }
448 
455  {
456  return [
457  'starting slash' => [
458  '/path',
459  false,
460  true,
461  ],
462  'starting slash on windows' => [
463  '/path',
464  true,
465  true,
466  ],
467  'no match' => [
468  'path',
469  false,
470  false,
471  ],
472  'no match on windows' => [
473  'path',
474  true,
475  false,
476  ],
477  'path starts with C:/' => [
478  'C:/folder',
479  false,
480  false,
481  ],
482  'path starts with C:/ on windows' => [
483  'C:/folder',
484  true,
485  true,
486  ],
487  'path starts with C:\\' => [
488  'C:\\folder',
489  false,
490  false,
491  ],
492  'path starts with C:\\ on windows' => [
493  'C:\\folder',
494  true,
495  true,
496  ],
497  'path empty' => [
498  '',
499  false,
500  false,
501  ],
502  ];
503  }
504 
509  public function ‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult): void
510  {
511  if ($isWindows) {
512  // Ensure Environment runs as Windows test
515  true,
516  false,
522  'WINDOWS'
523  );
524  }
525 
526  self::assertSame($expectedResult, ‪PathUtility::isAbsolutePath($inputPath));
527  }
528 
529  public static function ‪hasProtocolAndSchemeDataProvider(): array
530  {
531  return [
532  ['//example.com/demo.html', true],
533  ['http://example.com/demo.html', true],
534  ['https://example.com/demo.html', true],
535  ['f://example.com/demo.html', true],
536  ['f:/example.com/demo.html', false],
537  ['://example.com/demo.html', false],
538  [':/example.com/demo.html', false],
539  ['/example.com/demo.html', false],
540  ['example.com/demo.html', false],
541  ['demo.html', false],
542  ['\\\\server\\unc-path\\demo.html', false],
543  ['\\\\example.com\\demo.html', false],
544  ];
545  }
546 
551  public function ‪hasProtocolAndScheme(string ‪$url, bool $result): void
552  {
553  self::assertSame($result, ‪PathUtility::hasProtocolAndScheme(‪$url));
554  }
555 }
‪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:404
‪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:454
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParentDataProvider
‪static string[][] dirnameDuringBootstrapCorrectlyFetchesParentDataProvider()
Definition: PathUtilityTest.php:378
‪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:509
‪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:415
‪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:441
‪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:551
‪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:529
‪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:353