‪TYPO3CMS  10.4
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 {
33  protected ‪$backupEnvironment = true;
34 
41  public function ‪isCommonPrefixResolvedCorrectly(array $paths, $expected)
42  {
43  $commonPrefix = ‪PathUtility::getCommonPrefix($paths);
44  self::assertEquals($expected, $commonPrefix);
45  }
46 
51  {
52  return [
53  [
54  [
55  '/var/www/myhost.com/t3lib/'
56  ],
57  '/var/www/myhost.com/t3lib/'
58  ],
59  [
60  [
61  '/var/www/myhost.com/t3lib/',
62  '/var/www/myhost.com/t3lib/'
63  ],
64  '/var/www/myhost.com/t3lib/'
65  ],
66  [
67  [
68  '/var/www/myhost.com/typo3/',
69  '/var/www/myhost.com/t3lib/'
70  ],
71  '/var/www/myhost.com/'
72  ],
73  [
74  [
75  '/var/www/myhost.com/typo3/',
76  '/var/www/myhost.com/typo3'
77  ],
78  '/var/www/myhost.com/typo3/'
79  ],
80  [
81  [
82  '/var/www/myhost.com/typo3',
83  '/var/www/myhost.com/typo3'
84  ],
85  '/var/www/myhost.com/typo3/'
86  ],
87  [
88  [
89  '/var/www/myhost.com/uploads/',
90  '/var/www/myhost.com/typo3/',
91  '/var/www/myhost.com/t3lib/'
92  ],
93  '/var/www/myhost.com/'
94  ],
95  [
96  [
97  '/var/www/myhost.com/uploads/directory/',
98  '/var/www/myhost.com/typo3/sysext/',
99  '/var/www/myhost.com/t3lib/utility/'
100  ],
101  '/var/www/myhost.com/'
102  ],
103  [
104  [
105  'C:\\www\\myhost.com\\t3lib\\'
106  ],
107  'C:/www/myhost.com/t3lib/'
108  ],
109  [
110  [
111  'C:\\www\\myhost.com\\t3lib\\',
112  'C:\\www\\myhost.com\\t3lib\\'
113  ],
114  'C:/www/myhost.com/t3lib/'
115  ],
116  [
117  [
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\\',
126  'C:\\www\\myhost.com\\typo3\\',
127  'C:\\www\\myhost.com\\t3lib\\'
128  ],
129  'C:/www/myhost.com/'
130  ],
131  [
132  [
133  'C:\\www\\myhost.com\\uploads\\directory\\',
134  'C:\\www\\myhost.com\\typo3\\sysext\\',
135  'C:\\www\\myhost.com\\t3lib\\utility\\'
136  ],
137  'C:/www/myhost.com/'
138  ]
139  ];
140  }
141 
149  public function ‪isRelativePathResolvedCorrectly($source, $target, $expected)
150  {
151  $relativePath = ‪PathUtility::getRelativePath($source, $target);
152  self::assertEquals($expected, $relativePath);
153  }
154 
159  {
160  return [
161  [
162  '/',
163  ‪Environment::getPublicPath() . '/directory',
164  null
165  ],
166  [
167  ‪Environment::getPublicPath() . '/t3lib/',
168  ‪Environment::getPublicPath() . '/t3lib/',
169  ''
170  ],
171  [
172  ‪Environment::getPublicPath() . '/typo3/',
173  ‪Environment::getPublicPath() . '/t3lib/',
174  '../t3lib/'
175  ],
176  [
178  ‪Environment::getPublicPath() . '/t3lib/',
179  't3lib/'
180  ],
181  [
182  ‪Environment::getPublicPath() . '/t3lib/',
183  ‪Environment::getPublicPath() . '/t3lib/stddb/',
184  'stddb/'
185  ],
186  [
187  ‪Environment::getPublicPath() . '/typo3/sysext/frontend/',
188  ‪Environment::getPublicPath() . '/t3lib/utility/',
189  '../../../t3lib/utility/'
190  ],
191  ];
192  }
193 
201  public function ‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
202  {
203  $sanitizedPath = ‪PathUtility::sanitizeTrailingSeparator($path, $separator);
204  self::assertEquals($expected, $sanitizedPath);
205  }
206 
211  {
212  return [
213  ['/var/www//', '/', '/var/www/'],
214  ['/var/www/', '/', '/var/www/'],
215  ['/var/www', '/', '/var/www/']
216  ];
217  }
218 
225  {
226  return [
227  'basic' => [
228  '/abc/def/one.txt',
229  '../two.txt',
230  '/abc/two.txt'
231  ],
232  'same folder' => [
233  '/abc/one.txt',
234  './two.txt',
235  '/abc/two.txt'
236  ],
237  'preserve relative path if path goes above start path' => [
238  'abc/one.txt',
239  '../../two.txt',
240  '../two.txt'
241  ],
242  'preserve absolute path even if path goes above start path' => [
243  '/abc/one.txt',
244  '../../two.txt',
245  '/two.txt',
246  ],
247  'base folder with same folder path' => [
248  '/abc/',
249  './two.txt',
250  '/abc/two.txt'
251  ],
252  'base folder with parent folder path' => [
253  '/abc/bar/',
254  '../foo.txt',
255  '/abc/foo.txt'
256  ],
257  ];
258  }
259 
267  public function ‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
268  {
269  $resolvedFilename = ‪PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
270  self::assertEquals($expectedFileName, $resolvedFilename);
271  }
272 
279  {
280  return [
281  'removes single-dot-elements' => [
282  'abc/./def/././ghi',
283  'abc/def/ghi'
284  ],
285  'removes ./ at beginning' => [
286  './abc/def/ghi',
287  'abc/def/ghi'
288  ],
289  'removes double-slashes' => [
290  'abc//def/ghi',
291  'abc/def/ghi'
292  ],
293  'removes double-slashes from front, but keeps absolute path' => [
294  '//abc/def/ghi',
295  '/abc/def/ghi'
296  ],
297  'makes double-dot-elements go one level higher, test #1' => [
298  'abc/def/ghi/../..',
299  'abc'
300  ],
301  'makes double-dot-elements go one level higher, test #2' => [
302  'abc/def/ghi/../123/456/..',
303  'abc/def/123'
304  ],
305  'makes double-dot-elements go one level higher, test #3' => [
306  'abc/../../def/ghi',
307  '../def/ghi'
308  ],
309  'makes double-dot-elements go one level higher, test #4' => [
310  'abc/def/ghi//../123/456/..',
311  'abc/def/123'
312  ],
313  'truncates slash at the end' => [
314  'abc/def/ghi/',
315  'abc/def/ghi'
316  ],
317  'keeps slash in front of absolute paths' => [
318  '/abc/def/ghi',
319  '/abc/def/ghi'
320  ],
321  'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => [
322  '/abc/../../def/ghi',
323  '/def/ghi'
324  ],
325  'works with EXT-syntax-paths' => [
326  'EXT:abc/def/ghi/',
327  'EXT:abc/def/ghi'
328  ],
329  'truncates ending slash with space' => [
330  'abc/def/ ',
331  'abc/def'
332  ],
333  'truncates ending space' => [
334  'abc/def ',
335  'abc/def'
336  ],
337  'truncates ending dot' => [
338  'abc/def/.',
339  'abc/def'
340  ],
341  'does not truncates ending dot if part of name' => [
342  'abc/def.',
343  'abc/def.'
344  ],
345  'protocol is not removed' => [
346  'vfs://def/../text.txt',
347  'vfs://text.txt'
348  ],
349  'works with filenames' => [
350  '/def/../text.txt',
351  '/text.txt'
352  ],
353  'absolute windows path' => [
354  'C:\def\..\..\test.txt',
355  'C:/test.txt'
356  ],
357  'double slashaes' => [
358  'abc//def',
359  'abc/def'
360  ],
361  'multiple slashes' => [
362  'abc///////def',
363  'abc/def'
364  ],
365  ];
366  }
367 
374  public function ‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult): void
375  {
376  // Ensure Environment runs as Windows test
379  true,
380  false,
386  'WINDOWS'
387  );
388  self::assertSame(
389  $expectedResult,
391  );
392  }
393 
400  {
401  return [
402  'relative path' => [
403  'abc/def/ghi',
404  'abc/def'
405  ],
406  'absolute path 1' => [
407  '/var/www/html/index.php',
408  '/var/www/html'
409  ],
410  'absolute path 2' => [
411  '/var/www/html/typo3/index.php',
412  '/var/www/html/typo3'
413  ],
414  'windows path' => [
415  'C:\\inetpub\\index.php',
416  'C:\\inetpub'
417  ],
418  ];
419  }
420 
427  public function ‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult): void
428  {
429  self::assertSame(
430  $expectedResult,
432  );
433  }
434 
441  {
442  return [
443  'relative path' => [
444  'abc/def/ghi',
445  'ghi'
446  ],
447  'absolute path 1' => [
448  '/var/www/html/index.php',
449  'index.php'
450  ],
451  'absolute path 2' => [
452  '/var/www/html/typo3/index.php',
453  'index.php'
454  ],
455  'windows path' => [
456  'C:\\inetpub\\index.php',
457  'index.php'
458  ],
459  ];
460  }
461 
468  public function ‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult): void
469  {
470  self::assertSame(
471  $expectedResult,
473  );
474  }
475 
482  {
483  return [
484  'starting slash' => [
485  '/path',
486  false,
487  true
488  ],
489  'starting slash on windows' => [
490  '/path',
491  true,
492  true
493  ],
494  'no match' => [
495  'path',
496  false,
497  false
498  ],
499  'no match on windows' => [
500  'path',
501  true,
502  false
503  ],
504  'path starts with C:/' => [
505  'C:/folder',
506  false,
507  false
508  ],
509  'path starts with C:/ on windows' => [
510  'C:/folder',
511  true,
512  true
513  ],
514  'path starts with C:\\' => [
515  'C:\\folder',
516  false,
517  false
518  ],
519  'path starts with C:\\ on windows' => [
520  'C:\\folder',
521  true,
522  true
523  ],
524  ];
525  }
526 
534  public function ‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult): void
535  {
536  if ($isWindows) {
537  // Ensure Environment runs as Windows test
540  true,
541  false,
547  'WINDOWS'
548  );
549  }
550 
551  self::assertSame($expectedResult, ‪PathUtility::isAbsolutePath($inputPath));
552  }
553 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest
Definition: PathUtilityTest.php:28
‪TYPO3\CMS\Core\Utility\PathUtility\basenameDuringBootstrap
‪static string basenameDuringBootstrap($path)
Definition: PathUtility.php:291
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParent
‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:426
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectly
‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
Definition: PathUtilityTest.php:200
‪TYPO3\CMS\Core\Utility\PathUtility\getRelativePath
‪static string null getRelativePath($sourcePath, $targetPath)
Definition: PathUtility.php:73
‪TYPO3\CMS\Core\Utility\PathUtility\dirnameDuringBootstrap
‪static string dirnameDuringBootstrap($path)
Definition: PathUtility.php:276
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPathDataProvider
‪string[][] getCanonicalPathCorrectlyCleansPathDataProvider()
Definition: PathUtilityTest.php:277
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:307
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystemsPathDataProvider
‪array[] isAbsolutePathRespectsAllOperatingSystemsPathDataProvider()
Definition: PathUtilityTest.php:480
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystems
‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult)
Definition: PathUtilityTest.php:533
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly
‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
Definition: PathUtilityTest.php:266
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasename
‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:467
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParentDataProvider
‪string[][] dirnameDuringBootstrapCorrectlyFetchesParentDataProvider()
Definition: PathUtilityTest.php:398
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider
‪array getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider()
Definition: PathUtilityTest.php:223
‪TYPO3\CMS\Core\Utility\PathUtility\getCommonPrefix
‪static string null getCommonPrefix(array $paths)
Definition: PathUtility.php:110
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectly
‪isRelativePathResolvedCorrectly($source, $target, $expected)
Definition: PathUtilityTest.php:148
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectlyDataProvider
‪array isCommonPrefixResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:49
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectlyDataProvider
‪array isRelativePathResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:157
‪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:104
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider
‪array basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider()
Definition: PathUtilityTest.php:439
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static bool isAbsolutePath($path)
Definition: PathUtility.php:223
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectlyDataProvider
‪array isTrailingSeparatorSanitizedCorrectlyDataProvider()
Definition: PathUtilityTest.php:209
‪TYPO3\CMS\Core\Utility\PathUtility\sanitizeTrailingSeparator
‪static string sanitizeTrailingSeparator($path, $separator='/')
Definition: PathUtility.php:148
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\$backupEnvironment
‪bool $backupEnvironment
Definition: PathUtilityTest.php:32
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:210
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectly
‪isCommonPrefixResolvedCorrectly(array $paths, $expected)
Definition: PathUtilityTest.php:40
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsolutePathOfRelativeReferencedFileOrPath
‪static string getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
Definition: PathUtility.php:256
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPath
‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult)
Definition: PathUtilityTest.php:373
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192