TYPO3 CMS  TYPO3_8-7
PathUtilityTest.php
Go to the documentation of this file.
1 <?php
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  */
17 
21 class PathUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
29  public function isCommonPrefixResolvedCorrectly(array $paths, $expected)
30  {
32  $this->assertEquals($expected, $commonPrefix);
33  }
34 
39  {
40  return [
41  [
42  [
43  '/var/www/myhost.com/t3lib/'
44  ],
45  '/var/www/myhost.com/t3lib/'
46  ],
47  [
48  [
49  '/var/www/myhost.com/t3lib/',
50  '/var/www/myhost.com/t3lib/'
51  ],
52  '/var/www/myhost.com/t3lib/'
53  ],
54  [
55  [
56  '/var/www/myhost.com/typo3/',
57  '/var/www/myhost.com/t3lib/'
58  ],
59  '/var/www/myhost.com/'
60  ],
61  [
62  [
63  '/var/www/myhost.com/uploads/',
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/directory/',
72  '/var/www/myhost.com/typo3/sysext/',
73  '/var/www/myhost.com/t3lib/utility/'
74  ],
75  '/var/www/myhost.com/'
76  ],
77  [
78  [
79  'C:\\www\\myhost.com\\t3lib\\'
80  ],
81  'C:/www/myhost.com/t3lib/'
82  ],
83  [
84  [
85  'C:\\www\\myhost.com\\t3lib\\',
86  'C:\\www\\myhost.com\\t3lib\\'
87  ],
88  'C:/www/myhost.com/t3lib/'
89  ],
90  [
91  [
92  'C:\\www\\myhost.com\\typo3\\',
93  'C:\\www\\myhost.com\\t3lib\\'
94  ],
95  'C:/www/myhost.com/'
96  ],
97  [
98  [
99  'C:\\www\\myhost.com\\uploads\\',
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\\directory\\',
108  'C:\\www\\myhost.com\\typo3\\sysext\\',
109  'C:\\www\\myhost.com\\t3lib\\utility\\'
110  ],
111  'C:/www/myhost.com/'
112  ]
113  ];
114  }
115 
123  public function isRelativePathResolvedCorrectly($source, $target, $expected)
124  {
125  $relativePath = \TYPO3\CMS\Core\Utility\PathUtility::getRelativePath($source, $target);
126  $this->assertEquals($expected, $relativePath);
127  }
128 
133  {
134  return [
135  [
136  '/',
137  PATH_site . 'directory',
138  null
139  ],
140  [
141  PATH_site . 't3lib/',
142  PATH_site . 't3lib/',
143  ''
144  ],
145  [
146  PATH_site . 'typo3/',
147  PATH_site . 't3lib/',
148  '../t3lib/'
149  ],
150  [
151  PATH_site,
152  PATH_site . 't3lib/',
153  't3lib/'
154  ],
155  [
156  PATH_site . 't3lib/',
157  PATH_site . 't3lib/stddb/',
158  'stddb/'
159  ],
160  [
161  PATH_site . 'typo3/sysext/frontend/',
162  PATH_site . 't3lib/utility/',
163  '../../../t3lib/utility/'
164  ],
165  ];
166  }
167 
175  public function isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
176  {
177  $sanitizedPath = \TYPO3\CMS\Core\Utility\PathUtility::sanitizeTrailingSeparator($path, $separator);
178  $this->assertEquals($expected, $sanitizedPath);
179  }
180 
185  {
186  return [
187  ['/var/www//', '/', '/var/www/'],
188  ['/var/www/', '/', '/var/www/'],
189  ['/var/www', '/', '/var/www/']
190  ];
191  }
192 
199  {
200  return [
201  'basic' => [
202  '/abc/def/one.txt',
203  '../two.txt',
204  '/abc/two.txt'
205  ],
206  'same folder' => [
207  '/abc/one.txt',
208  './two.txt',
209  '/abc/two.txt'
210  ],
211  'preserve relative path if path goes above start path' => [
212  'abc/one.txt',
213  '../../two.txt',
214  '../two.txt'
215  ],
216  'preserve absolute path even if path goes above start path' => [
217  '/abc/one.txt',
218  '../../two.txt',
219  '/two.txt',
220  ],
221  'base folder with same folder path' => [
222  '/abc/',
223  './two.txt',
224  '/abc/two.txt'
225  ],
226  'base folder with parent folder path' => [
227  '/abc/bar/',
228  '../foo.txt',
229  '/abc/foo.txt'
230  ],
231  ];
232  }
233 
241  public function getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
242  {
243  $resolvedFilename = \TYPO3\CMS\Core\Utility\PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
244  $this->assertEquals($expectedFileName, $resolvedFilename);
245  }
246 
253  {
254  return [
255  'removes single-dot-elements' => [
256  'abc/./def/././ghi',
257  'abc/def/ghi'
258  ],
259  'removes ./ at beginning' => [
260  './abc/def/ghi',
261  'abc/def/ghi'
262  ],
263  'removes double-slashes' => [
264  'abc//def/ghi',
265  'abc/def/ghi'
266  ],
267  'removes double-slashes from front, but keeps absolute path' => [
268  '//abc/def/ghi',
269  '/abc/def/ghi'
270  ],
271  'makes double-dot-elements go one level higher, test #1' => [
272  'abc/def/ghi/../..',
273  'abc'
274  ],
275  'makes double-dot-elements go one level higher, test #2' => [
276  'abc/def/ghi/../123/456/..',
277  'abc/def/123'
278  ],
279  'makes double-dot-elements go one level higher, test #3' => [
280  'abc/../../def/ghi',
281  '../def/ghi'
282  ],
283  'makes double-dot-elements go one level higher, test #4' => [
284  'abc/def/ghi//../123/456/..',
285  'abc/def/123'
286  ],
287  'truncates slash at the end' => [
288  'abc/def/ghi/',
289  'abc/def/ghi'
290  ],
291  'keeps slash in front of absolute paths' => [
292  '/abc/def/ghi',
293  '/abc/def/ghi'
294  ],
295  'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => [
296  '/abc/../../def/ghi',
297  '/def/ghi'
298  ],
299  'works with EXT-syntax-paths' => [
300  'EXT:abc/def/ghi/',
301  'EXT:abc/def/ghi'
302  ],
303  'truncates ending slash with space' => [
304  'abc/def/ ',
305  'abc/def'
306  ],
307  'truncates ending space' => [
308  'abc/def ',
309  'abc/def'
310  ],
311  'truncates ending dot' => [
312  'abc/def/.',
313  'abc/def'
314  ],
315  'does not truncates ending dot if part of name' => [
316  'abc/def.',
317  'abc/def.'
318  ],
319  'protocol is not removed' => [
320  'vfs://def/../text.txt',
321  'vfs://text.txt'
322  ],
323  'works with filenames' => [
324  '/def/../text.txt',
325  '/text.txt'
326  ],
327  'absolute windwos path' => [
328  'C:\def\..\..\test.txt',
329  'C:/test.txt'
330  ],
331  'double slashaes' => [
332  'abc//def',
333  'abc/def'
334  ],
335  'multiple slashes' => [
336  'abc///////def',
337  'abc/def'
338  ],
339  ];
340  }
341 
346  public function getCanonicalPathCorrectlyCleansPath($inputName, $expectedResult)
347  {
348  $this->assertEquals(
349  $expectedResult,
351  );
352  }
353 }
isRelativePathResolvedCorrectly($source, $target, $expected)
static getCommonPrefix(array $paths)
static getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected)
static getRelativePath($sourcePath, $targetPath)
Definition: PathUtility.php:70
getCanonicalPathCorrectlyCleansPath($inputName, $expectedResult)
getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName)
static sanitizeTrailingSeparator($path, $separator='/')