‪TYPO3CMS  9.5
PathUtilityTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪PathUtilityTest extends UnitTestCase
26 {
31  protected ‪$backupEnvironment = true;
32 
39  public function ‪isCommonPrefixResolvedCorrectly(array $paths, $expected)
40  {
41  $commonPrefix = ‪PathUtility::getCommonPrefix($paths);
42  $this->assertEquals($expected, $commonPrefix);
43  }
44 
49  {
50  return [
51  [
52  [
53  '/var/www/myhost.com/t3lib/'
54  ],
55  '/var/www/myhost.com/t3lib/'
56  ],
57  [
58  [
59  '/var/www/myhost.com/t3lib/',
60  '/var/www/myhost.com/t3lib/'
61  ],
62  '/var/www/myhost.com/t3lib/'
63  ],
64  [
65  [
66  '/var/www/myhost.com/typo3/',
67  '/var/www/myhost.com/t3lib/'
68  ],
69  '/var/www/myhost.com/'
70  ],
71  [
72  [
73  '/var/www/myhost.com/typo3/',
74  '/var/www/myhost.com/typo3'
75  ],
76  '/var/www/myhost.com/typo3/'
77  ],
78  [
79  [
80  '/var/www/myhost.com/typo3',
81  '/var/www/myhost.com/typo3'
82  ],
83  '/var/www/myhost.com/typo3/'
84  ],
85  [
86  [
87  '/var/www/myhost.com/uploads/',
88  '/var/www/myhost.com/typo3/',
89  '/var/www/myhost.com/t3lib/'
90  ],
91  '/var/www/myhost.com/'
92  ],
93  [
94  [
95  '/var/www/myhost.com/uploads/directory/',
96  '/var/www/myhost.com/typo3/sysext/',
97  '/var/www/myhost.com/t3lib/utility/'
98  ],
99  '/var/www/myhost.com/'
100  ],
101  [
102  [
103  'C:\\www\\myhost.com\\t3lib\\'
104  ],
105  'C:/www/myhost.com/t3lib/'
106  ],
107  [
108  [
109  'C:\\www\\myhost.com\\t3lib\\',
110  'C:\\www\\myhost.com\\t3lib\\'
111  ],
112  'C:/www/myhost.com/t3lib/'
113  ],
114  [
115  [
116  'C:\\www\\myhost.com\\typo3\\',
117  'C:\\www\\myhost.com\\t3lib\\'
118  ],
119  'C:/www/myhost.com/'
120  ],
121  [
122  [
123  'C:\\www\\myhost.com\\uploads\\',
124  'C:\\www\\myhost.com\\typo3\\',
125  'C:\\www\\myhost.com\\t3lib\\'
126  ],
127  'C:/www/myhost.com/'
128  ],
129  [
130  [
131  'C:\\www\\myhost.com\\uploads\\directory\\',
132  'C:\\www\\myhost.com\\typo3\\sysext\\',
133  'C:\\www\\myhost.com\\t3lib\\utility\\'
134  ],
135  'C:/www/myhost.com/'
136  ]
137  ];
138  }
139 
147  public function ‪isRelativePathResolvedCorrectly($source, $target, $expected)
148  {
149  $relativePath = ‪PathUtility::getRelativePath($source, $target);
150  $this->assertEquals($expected, $relativePath);
151  }
152 
157  {
158  return [
159  [
160  '/',
161  ‪Environment::getPublicPath() . '/directory',
162  null
163  ],
164  [
165  ‪Environment::getPublicPath() . '/t3lib/',
166  ‪Environment::getPublicPath() . '/t3lib/',
167  ''
168  ],
169  [
170  ‪Environment::getPublicPath() . '/typo3/',
171  ‪Environment::getPublicPath() . '/t3lib/',
172  '../t3lib/'
173  ],
174  [
176  ‪Environment::getPublicPath() . '/t3lib/',
177  't3lib/'
178  ],
179  [
180  ‪Environment::getPublicPath() . '/t3lib/',
181  ‪Environment::getPublicPath() . '/t3lib/stddb/',
182  'stddb/'
183  ],
184  [
185  ‪Environment::getPublicPath() . '/typo3/sysext/frontend/',
186  ‪Environment::getPublicPath() . '/t3lib/utility/',
187  '../../../t3lib/utility/'
188  ],
189  ];
190  }
191 
199  public function ‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
200  {
201  $sanitizedPath = ‪PathUtility::sanitizeTrailingSeparator($path, $separator);
202  $this->assertEquals($expected, $sanitizedPath);
203  }
204 
209  {
210  return [
211  ['/var/www//', '/', '/var/www/'],
212  ['/var/www/', '/', '/var/www/'],
213  ['/var/www', '/', '/var/www/']
214  ];
215  }
216 
223  {
224  return [
225  'basic' => [
226  '/abc/def/one.txt',
227  '../two.txt',
228  '/abc/two.txt'
229  ],
230  'same folder' => [
231  '/abc/one.txt',
232  './two.txt',
233  '/abc/two.txt'
234  ],
235  'preserve relative path if path goes above start path' => [
236  'abc/one.txt',
237  '../../two.txt',
238  '../two.txt'
239  ],
240  'preserve absolute path even if path goes above start path' => [
241  '/abc/one.txt',
242  '../../two.txt',
243  '/two.txt',
244  ],
245  'base folder with same folder path' => [
246  '/abc/',
247  './two.txt',
248  '/abc/two.txt'
249  ],
250  'base folder with parent folder path' => [
251  '/abc/bar/',
252  '../foo.txt',
253  '/abc/foo.txt'
254  ],
255  ];
256  }
257 
265  public function ‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
266  {
267  $resolvedFilename = ‪PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
268  $this->assertEquals($expectedFileName, $resolvedFilename);
269  }
270 
277  {
278  return [
279  'removes single-dot-elements' => [
280  'abc/./def/././ghi',
281  'abc/def/ghi'
282  ],
283  'removes ./ at beginning' => [
284  './abc/def/ghi',
285  'abc/def/ghi'
286  ],
287  'removes double-slashes' => [
288  'abc//def/ghi',
289  'abc/def/ghi'
290  ],
291  'removes double-slashes from front, but keeps absolute path' => [
292  '//abc/def/ghi',
293  '/abc/def/ghi'
294  ],
295  'makes double-dot-elements go one level higher, test #1' => [
296  'abc/def/ghi/../..',
297  'abc'
298  ],
299  'makes double-dot-elements go one level higher, test #2' => [
300  'abc/def/ghi/../123/456/..',
301  'abc/def/123'
302  ],
303  'makes double-dot-elements go one level higher, test #3' => [
304  'abc/../../def/ghi',
305  '../def/ghi'
306  ],
307  'makes double-dot-elements go one level higher, test #4' => [
308  'abc/def/ghi//../123/456/..',
309  'abc/def/123'
310  ],
311  'truncates slash at the end' => [
312  'abc/def/ghi/',
313  'abc/def/ghi'
314  ],
315  'keeps slash in front of absolute paths' => [
316  '/abc/def/ghi',
317  '/abc/def/ghi'
318  ],
319  'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => [
320  '/abc/../../def/ghi',
321  '/def/ghi'
322  ],
323  'works with EXT-syntax-paths' => [
324  'EXT:abc/def/ghi/',
325  'EXT:abc/def/ghi'
326  ],
327  'truncates ending slash with space' => [
328  'abc/def/ ',
329  'abc/def'
330  ],
331  'truncates ending space' => [
332  'abc/def ',
333  'abc/def'
334  ],
335  'truncates ending dot' => [
336  'abc/def/.',
337  'abc/def'
338  ],
339  'does not truncates ending dot if part of name' => [
340  'abc/def.',
341  'abc/def.'
342  ],
343  'protocol is not removed' => [
344  'vfs://def/../text.txt',
345  'vfs://text.txt'
346  ],
347  'works with filenames' => [
348  '/def/../text.txt',
349  '/text.txt'
350  ],
351  'absolute windwos path' => [
352  'C:\def\..\..\test.txt',
353  'C:/test.txt'
354  ],
355  'double slashaes' => [
356  'abc//def',
357  'abc/def'
358  ],
359  'multiple slashes' => [
360  'abc///////def',
361  'abc/def'
362  ],
363  ];
364  }
365 
372  public function ‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult): void
373  {
374  // Ensure Environment runs as Windows test
377  true,
378  false,
384  'WINDOWS'
385  );
386  $this->assertSame(
387  $expectedResult,
389  );
390  }
391 
398  {
399  return [
400  'relative path' => [
401  'abc/def/ghi',
402  'abc/def'
403  ],
404  'absolute path 1' => [
405  '/var/www/html/index.php',
406  '/var/www/html'
407  ],
408  'absolute path 2' => [
409  '/var/www/html/typo3/index.php',
410  '/var/www/html/typo3'
411  ],
412  'windows path' => [
413  'C:\\inetpub\\index.php',
414  'C:\\inetpub'
415  ],
416  ];
417  }
418 
425  public function ‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult): void
426  {
427  $this->assertSame(
428  $expectedResult,
430  );
431  }
432 
439  {
440  return [
441  'relative path' => [
442  'abc/def/ghi',
443  'ghi'
444  ],
445  'absolute path 1' => [
446  '/var/www/html/index.php',
447  'index.php'
448  ],
449  'absolute path 2' => [
450  '/var/www/html/typo3/index.php',
451  'index.php'
452  ],
453  'windows path' => [
454  'C:\\inetpub\\index.php',
455  'index.php'
456  ],
457  ];
458  }
459 
466  public function ‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult): void
467  {
468  $this->assertSame(
469  $expectedResult,
471  );
472  }
473 
480  {
481  return [
482  'starting slash' => [
483  '/path',
484  false,
485  true
486  ],
487  'starting slash on windows' => [
488  '/path',
489  true,
490  true
491  ],
492  'no match' => [
493  'path',
494  false,
495  false
496  ],
497  'no match on windows' => [
498  'path',
499  true,
500  false
501  ],
502  'path starts with C:/' => [
503  'C:/folder',
504  false,
505  false
506  ],
507  'path starts with C:/ on windows' => [
508  'C:/folder',
509  true,
510  true
511  ],
512  'path starts with C:\\' => [
513  'C:\\folder',
514  false,
515  false
516  ],
517  'path starts with C:\\ on windows' => [
518  'C:\\folder',
519  true,
520  true
521  ],
522  ];
523  }
524 
532  public function ‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult): void
533  {
534  if ($isWindows) {
535  // Ensure Environment runs as Windows test
538  true,
539  false,
545  'WINDOWS'
546  );
547  }
548 
549  $this->assertSame($expectedResult, ‪PathUtility::isAbsolutePath($inputPath));
550  }
551 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest
Definition: PathUtilityTest.php:26
‪TYPO3\CMS\Core\Utility\PathUtility\basenameDuringBootstrap
‪static string basenameDuringBootstrap($path)
Definition: PathUtility.php:290
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParent
‪dirnameDuringBootstrapCorrectlyFetchesParent(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:424
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectly
‪isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
Definition: PathUtilityTest.php:198
‪TYPO3\CMS\Core\Utility\PathUtility\getRelativePath
‪static string null getRelativePath($sourcePath, $targetPath)
Definition: PathUtility.php:72
‪TYPO3\CMS\Core\Utility\PathUtility\dirnameDuringBootstrap
‪static string dirnameDuringBootstrap($path)
Definition: PathUtility.php:275
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:193
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPathDataProvider
‪string[][] getCanonicalPathCorrectlyCleansPathDataProvider()
Definition: PathUtilityTest.php:275
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:306
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystemsPathDataProvider
‪array[] isAbsolutePathRespectsAllOperatingSystemsPathDataProvider()
Definition: PathUtilityTest.php:478
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isAbsolutePathRespectsAllOperatingSystems
‪isAbsolutePathRespectsAllOperatingSystems(string $inputPath, bool $isWindows, bool $expectedResult)
Definition: PathUtilityTest.php:531
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:106
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly
‪getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
Definition: PathUtilityTest.php:264
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:142
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasename
‪basenameDuringBootstrapCorrectlyFetchesBasename(string $inputPath, string $expectedResult)
Definition: PathUtilityTest.php:465
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\dirnameDuringBootstrapCorrectlyFetchesParentDataProvider
‪string[][] dirnameDuringBootstrapCorrectlyFetchesParentDataProvider()
Definition: PathUtilityTest.php:396
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider
‪array getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider()
Definition: PathUtilityTest.php:221
‪TYPO3\CMS\Core\Utility\PathUtility\getCommonPrefix
‪static string null getCommonPrefix(array $paths)
Definition: PathUtility.php:109
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectly
‪isRelativePathResolvedCorrectly($source, $target, $expected)
Definition: PathUtilityTest.php:146
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectlyDataProvider
‪array isCommonPrefixResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isRelativePathResolvedCorrectlyDataProvider
‪array isRelativePathResolvedCorrectlyDataProvider()
Definition: PathUtilityTest.php:155
‪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:77
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider
‪array basenameDuringBootstrapCorrectlyFetchesBasenameDataProvider()
Definition: PathUtilityTest.php:437
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static bool isAbsolutePath($path)
Definition: PathUtility.php:222
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isTrailingSeparatorSanitizedCorrectlyDataProvider
‪array isTrailingSeparatorSanitizedCorrectlyDataProvider()
Definition: PathUtilityTest.php:207
‪TYPO3\CMS\Core\Utility\PathUtility\sanitizeTrailingSeparator
‪static string sanitizeTrailingSeparator($path, $separator='/')
Definition: PathUtility.php:147
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\$backupEnvironment
‪bool $backupEnvironment
Definition: PathUtilityTest.php:30
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:183
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\isCommonPrefixResolvedCorrectly
‪isCommonPrefixResolvedCorrectly(array $paths, $expected)
Definition: PathUtilityTest.php:38
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsolutePathOfRelativeReferencedFileOrPath
‪static string getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
Definition: PathUtility.php:255
‪TYPO3\CMS\Core\Tests\Unit\Utility\PathUtilityTest\getCanonicalPathCorrectlyCleansPath
‪getCanonicalPathCorrectlyCleansPath(string $inputName, string $expectedResult)
Definition: PathUtilityTest.php:371
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165