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