52c3307e2c59636bb8612f40f4aabe28a96e326b
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / PathUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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 */
16 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\WindowsPathUtilityFixture;
17
18 /**
19 * Testcase for class \TYPO3\CMS\Core\Utility\PathUtility
20 */
21 class PathUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
22
23 /**
24 * @param array $paths
25 * @param string $expected
26 * @dataProvider isCommonPrefixResolvedCorrectlyDataProvider
27 * @test
28 */
29 public function isCommonPrefixResolvedCorrectly(array $paths, $expected) {
30 $commonPrefix = \TYPO3\CMS\Core\Utility\PathUtility::getCommonPrefix($paths);
31 $this->assertEquals($expected, $commonPrefix);
32 }
33
34 /**
35 * @return array
36 */
37 public function isCommonPrefixResolvedCorrectlyDataProvider() {
38 return array(
39 array(
40 array(
41 '/var/www/myhost.com/t3lib/'
42 ),
43 '/var/www/myhost.com/t3lib/'
44 ),
45 array(
46 array(
47 '/var/www/myhost.com/t3lib/',
48 '/var/www/myhost.com/t3lib/'
49 ),
50 '/var/www/myhost.com/t3lib/'
51 ),
52 array(
53 array(
54 '/var/www/myhost.com/typo3/',
55 '/var/www/myhost.com/t3lib/'
56 ),
57 '/var/www/myhost.com/'
58 ),
59 array(
60 array(
61 '/var/www/myhost.com/uploads/',
62 '/var/www/myhost.com/typo3/',
63 '/var/www/myhost.com/t3lib/'
64 ),
65 '/var/www/myhost.com/'
66 ),
67 array(
68 array(
69 '/var/www/myhost.com/uploads/directory/',
70 '/var/www/myhost.com/typo3/sysext/',
71 '/var/www/myhost.com/typo3/contrib/',
72 '/var/www/myhost.com/t3lib/utility/'
73 ),
74 '/var/www/myhost.com/'
75 ),
76 array(
77 array(
78 'C:\\www\\myhost.com\\t3lib\\'
79 ),
80 'C:/www/myhost.com/t3lib/'
81 ),
82 array(
83 array(
84 'C:\\www\\myhost.com\\t3lib\\',
85 'C:\\www\\myhost.com\\t3lib\\'
86 ),
87 'C:/www/myhost.com/t3lib/'
88 ),
89 array(
90 array(
91 'C:\\www\\myhost.com\\typo3\\',
92 'C:\\www\\myhost.com\\t3lib\\'
93 ),
94 'C:/www/myhost.com/'
95 ),
96 array(
97 array(
98 'C:\\www\\myhost.com\\uploads\\',
99 'C:\\www\\myhost.com\\typo3\\',
100 'C:\\www\\myhost.com\\t3lib\\'
101 ),
102 'C:/www/myhost.com/'
103 ),
104 array(
105 array(
106 'C:\\www\\myhost.com\\uploads\\directory\\',
107 'C:\\www\\myhost.com\\typo3\\sysext\\',
108 'C:\\www\\myhost.com\\typo3\\contrib\\',
109 'C:\\www\\myhost.com\\t3lib\\utility\\'
110 ),
111 'C:/www/myhost.com/'
112 )
113 );
114 }
115
116 /**
117 * @param string $source
118 * @param string $target
119 * @param string $expected
120 * @dataProvider isRelativePathResolvedCorrectlyDataProvider
121 * @test
122 */
123 public function isRelativePathResolvedCorrectly($source, $target, $expected) {
124 $relativePath = \TYPO3\CMS\Core\Utility\PathUtility::getRelativePath($source, $target);
125 $this->assertEquals($expected, $relativePath);
126 }
127
128 /**
129 * @return array
130 */
131 public function isRelativePathResolvedCorrectlyDataProvider() {
132 return array(
133 array(
134 '/',
135 PATH_site . 'directory',
136 NULL
137 ),
138 array(
139 PATH_site . 't3lib/',
140 PATH_site . 't3lib/',
141 ''
142 ),
143 array(
144 PATH_site . 'typo3/',
145 PATH_site . 't3lib/',
146 '../t3lib/'
147 ),
148 array(
149 PATH_site,
150 PATH_site . 't3lib/',
151 't3lib/'
152 ),
153 array(
154 PATH_site . 't3lib/',
155 PATH_site . 't3lib/stddb/',
156 'stddb/'
157 ),
158 array(
159 PATH_site . 'typo3/sysext/frontend/',
160 PATH_site . 't3lib/utility/',
161 '../../../t3lib/utility/'
162 ),
163 );
164 }
165
166 /**
167 * @param string $path
168 * @param string $separator
169 * @param string $expected
170 * @dataProvider isTrailingSeparatorSanitizedCorrectlyDataProvider
171 * @test
172 */
173 public function isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected) {
174 $sanitizedPath = \TYPO3\CMS\Core\Utility\PathUtility::sanitizeTrailingSeparator($path, $separator);
175 $this->assertEquals($expected, $sanitizedPath);
176 }
177
178 /**
179 * @return array
180 */
181 public function isTrailingSeparatorSanitizedCorrectlyDataProvider() {
182 return array(
183 array('/var/www//', '/', '/var/www/'),
184 array('/var/www/', '/', '/var/www/'),
185 array('/var/www', '/', '/var/www/')
186 );
187 }
188
189 /**
190 * Data Provider for getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly
191 *
192 * @return array
193 */
194 public function getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider() {
195 return array(
196 'basic' => array(
197 '/abc/def/one.txt',
198 '../two.txt',
199 '/abc/two.txt'
200 ),
201 'same folder' => array(
202 '/abc/one.txt',
203 './two.txt',
204 '/abc/two.txt'
205 ),
206 'preserve relative path if path goes above start path' => array(
207 'abc/one.txt',
208 '../../two.txt',
209 '../two.txt'
210 ),
211 'preserve absolute path even if path goes above start path' => array(
212 '/abc/one.txt',
213 '../../two.txt',
214 '/two.txt',
215 ),
216 'base folder with same folder path' => array(
217 '/abc/',
218 './two.txt',
219 '/abc/two.txt'
220 ),
221 'base folder with parent folder path' => array(
222 '/abc/bar/',
223 '../foo.txt',
224 '/abc/foo.txt'
225 ),
226 );
227 }
228
229 /**
230 * @param $baseFileName
231 * @param $includeFileName
232 * @param $expectedFileName
233 * @test
234 * @dataProvider getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectlyDataProvider
235 */
236 public function getAbsolutePathOfRelativeReferencedFileOrPathResolvesFileCorrectly($baseFileName, $includeFileName, $expectedFileName) {
237 $resolvedFilename = \TYPO3\CMS\Core\Utility\PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($baseFileName, $includeFileName);
238 $this->assertEquals($expectedFileName, $resolvedFilename);
239 }
240
241 /**
242 * Data provider for getCanonicalPathCorrectlyCleansPath
243 *
244 * @return array
245 */
246 public function getCanonicalPathCorrectlyCleansPathDataProvider() {
247 return array(
248 'removes single-dot-elements' => array(
249 'abc/./def/././ghi',
250 'abc/def/ghi'
251 ),
252 'removes ./ at beginning' => array(
253 './abc/def/ghi',
254 'abc/def/ghi'
255 ),
256 'removes double-slashes' => array(
257 'abc//def/ghi',
258 'abc/def/ghi'
259 ),
260 'removes double-slashes from front, but keeps absolute path' => array(
261 '//abc/def/ghi',
262 '/abc/def/ghi'
263 ),
264 'makes double-dot-elements go one level higher, test #1' => array(
265 'abc/def/ghi/../..',
266 'abc'
267 ),
268 'makes double-dot-elements go one level higher, test #2' => array(
269 'abc/def/ghi/../123/456/..',
270 'abc/def/123'
271 ),
272 'makes double-dot-elements go one level higher, test #3' => array(
273 'abc/../../def/ghi',
274 '../def/ghi'
275 ),
276 'makes double-dot-elements go one level higher, test #4' => array(
277 'abc/def/ghi//../123/456/..',
278 'abc/def/123'
279 ),
280 'truncates slash at the end' => array(
281 'abc/def/ghi/',
282 'abc/def/ghi'
283 ),
284 'keeps slash in front of absolute paths' => array(
285 '/abc/def/ghi',
286 '/abc/def/ghi'
287 ),
288 'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => array(
289 '/abc/../../def/ghi',
290 '/def/ghi'
291 ),
292 'works with EXT-syntax-paths' => array(
293 'EXT:abc/def/ghi/',
294 'EXT:abc/def/ghi'
295 ),
296 'truncates ending slash with space' => array(
297 'abc/def/ ',
298 'abc/def'
299 ),
300 'truncates ending space' => array(
301 'abc/def ',
302 'abc/def'
303 ),
304 'truncates ending dot' => array(
305 'abc/def/.',
306 'abc/def'
307 ),
308 'does not truncates ending dot if part of name' => array(
309 'abc/def.',
310 'abc/def.'
311 ),
312 'protocol is not removed' => array(
313 'vfs://def/../text.txt',
314 'vfs://text.txt'
315 ),
316 'works with filenames' => array(
317 '/def/../text.txt',
318 '/text.txt'
319 ),
320 'absolute windwos path' => array(
321 'C:\def\..\..\test.txt',
322 'C:/test.txt'
323 ),
324 'double slashaes' => array(
325 'abc//def',
326 'abc/def'
327 ),
328 'multiple slashes' => array(
329 'abc///////def',
330 'abc/def'
331 ),
332 );
333 }
334
335 /**
336 * @test
337 * @dataProvider getCanonicalPathCorrectlyCleansPathDataProvider
338 */
339 public function getCanonicalPathCorrectlyCleansPath($inputName, $expectedResult) {
340 $this->assertEquals(
341 $expectedResult,
342 WindowsPathUtilityFixture::getCanonicalPath($inputName)
343 );
344 }
345
346 }