[BUGFIX] Files with unclean path indexed multiple times
[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 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31 /**
32 * Testcase for class \TYPO3\CMS\Core\Utility\PathUtility
33 *
34 * @author Oliver Hader <oliver.hader@typo3.org>
35 */
36 class PathUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
37
38 /**
39 * @param array $paths
40 * @param string $expected
41 * @dataProvider isCommonPrefixResolvedCorrectlyDataProvider
42 * @test
43 */
44 public function isCommonPrefixResolvedCorrectly(array $paths, $expected) {
45 $commonPrefix = \TYPO3\CMS\Core\Utility\PathUtility::getCommonPrefix($paths);
46 $this->assertEquals($expected, $commonPrefix);
47 }
48
49 /**
50 * @return array
51 */
52 public function isCommonPrefixResolvedCorrectlyDataProvider() {
53 return array(
54 array(
55 array(
56 '/var/www/myhost.com/t3lib/'
57 ),
58 '/var/www/myhost.com/t3lib/'
59 ),
60 array(
61 array(
62 '/var/www/myhost.com/t3lib/',
63 '/var/www/myhost.com/t3lib/'
64 ),
65 '/var/www/myhost.com/t3lib/'
66 ),
67 array(
68 array(
69 '/var/www/myhost.com/typo3/',
70 '/var/www/myhost.com/t3lib/'
71 ),
72 '/var/www/myhost.com/'
73 ),
74 array(
75 array(
76 '/var/www/myhost.com/uploads/',
77 '/var/www/myhost.com/typo3/',
78 '/var/www/myhost.com/t3lib/'
79 ),
80 '/var/www/myhost.com/'
81 ),
82 array(
83 array(
84 '/var/www/myhost.com/uploads/directory/',
85 '/var/www/myhost.com/typo3/sysext/',
86 '/var/www/myhost.com/typo3/contrib/',
87 '/var/www/myhost.com/t3lib/utility/'
88 ),
89 '/var/www/myhost.com/'
90 ),
91 array(
92 array(
93 'C:\\www\\myhost.com\\t3lib\\'
94 ),
95 'C:/www/myhost.com/t3lib/'
96 ),
97 array(
98 array(
99 'C:\\www\\myhost.com\\t3lib\\',
100 'C:\\www\\myhost.com\\t3lib\\'
101 ),
102 'C:/www/myhost.com/t3lib/'
103 ),
104 array(
105 array(
106 'C:\\www\\myhost.com\\typo3\\',
107 'C:\\www\\myhost.com\\t3lib\\'
108 ),
109 'C:/www/myhost.com/'
110 ),
111 array(
112 array(
113 'C:\\www\\myhost.com\\uploads\\',
114 'C:\\www\\myhost.com\\typo3\\',
115 'C:\\www\\myhost.com\\t3lib\\'
116 ),
117 'C:/www/myhost.com/'
118 ),
119 array(
120 array(
121 'C:\\www\\myhost.com\\uploads\\directory\\',
122 'C:\\www\\myhost.com\\typo3\\sysext\\',
123 'C:\\www\\myhost.com\\typo3\\contrib\\',
124 'C:\\www\\myhost.com\\t3lib\\utility\\'
125 ),
126 'C:/www/myhost.com/'
127 )
128 );
129 }
130
131 /**
132 * @param string $source
133 * @param string $target
134 * @param string $expected
135 * @dataProvider isRelativePathResolvedCorrectlyDataProvider
136 * @test
137 */
138 public function isRelativePathResolvedCorrectly($source, $target, $expected) {
139 $relativePath = \TYPO3\CMS\Core\Utility\PathUtility::getRelativePath($source, $target);
140 $this->assertEquals($expected, $relativePath);
141 }
142
143 /**
144 * @return array
145 */
146 public function isRelativePathResolvedCorrectlyDataProvider() {
147 return array(
148 array(
149 '/',
150 PATH_site . 'directory',
151 NULL
152 ),
153 array(
154 PATH_site . 't3lib/',
155 PATH_site . 't3lib/',
156 ''
157 ),
158 array(
159 PATH_site . 'typo3/',
160 PATH_site . 't3lib/',
161 '../t3lib/'
162 ),
163 array(
164 PATH_site,
165 PATH_site . 't3lib/',
166 't3lib/'
167 ),
168 array(
169 PATH_site . 't3lib/',
170 PATH_site . 't3lib/stddb/',
171 'stddb/'
172 ),
173 array(
174 PATH_site . 'typo3/sysext/cms/',
175 PATH_site . 't3lib/utility/',
176 '../../../t3lib/utility/'
177 ),
178 );
179 }
180
181 /**
182 * @param string $path
183 * @param string $separator
184 * @param string $expected
185 * @dataProvider isTrailingSeparatorSanitizedCorrectlyDataProvider
186 * @test
187 */
188 public function isTrailingSeparatorSanitizedCorrectly($path, $separator, $expected) {
189 $sanitizedPath = \TYPO3\CMS\Core\Utility\PathUtility::sanitizeTrailingSeparator($path, $separator);
190 $this->assertEquals($expected, $sanitizedPath);
191 }
192
193 /**
194 * @return array
195 */
196 public function isTrailingSeparatorSanitizedCorrectlyDataProvider() {
197 return array(
198 array('/var/www//', '/', '/var/www/'),
199 array('/var/www/', '/', '/var/www/'),
200 array('/var/www', '/', '/var/www/')
201 );
202 }
203
204 /**
205 * Data provider for getCanonicalPathCorrectlyCleansPath
206 *
207 * @return array
208 */
209 public function getCanonicalPathCorrectlyCleansPathDataProvider() {
210 return array(
211 'removes single-dot-elements' => array(
212 'abc/./def/././ghi',
213 'abc/def/ghi'
214 ),
215 'removes ./ at beginning' => array(
216 './abc/def/ghi',
217 'abc/def/ghi'
218 ),
219 'removes double-slashes' => array(
220 'abc//def/ghi',
221 'abc/def/ghi'
222 ),
223 'removes double-slashes from front, but keeps absolute path' => array(
224 '//abc/def/ghi',
225 '/abc/def/ghi'
226 ),
227 'makes double-dot-elements go one level higher, test #1' => array(
228 'abc/def/ghi/../..',
229 'abc'
230 ),
231 'makes double-dot-elements go one level higher, test #2' => array(
232 'abc/def/ghi/../123/456/..',
233 'abc/def/123'
234 ),
235 'makes double-dot-elements go one level higher, test #3' => array(
236 'abc/../../def/ghi',
237 '../def/ghi'
238 ),
239 'makes double-dot-elements go one level higher, test #4' => array(
240 'abc/def/ghi//../123/456/..',
241 'abc/def/123'
242 ),
243 'truncates slash at the end' => array(
244 'abc/def/ghi/',
245 'abc/def/ghi'
246 ),
247 'keeps slash in front of absolute paths' => array(
248 '/abc/def/ghi',
249 '/abc/def/ghi'
250 ),
251 'keeps slash in front of absolute paths even if double-dot-elements want to go higher' => array(
252 '/abc/../../def/ghi',
253 '/def/ghi'
254 ),
255 'works with EXT-syntax-paths' => array(
256 'EXT:abc/def/ghi/',
257 'EXT:abc/def/ghi'
258 ),
259 'truncates ending slash with space' => array(
260 'abc/def/ ',
261 'abc/def'
262 ),
263 'truncates ending space' => array(
264 'abc/def ',
265 'abc/def'
266 ),
267 'truncates ending dot' => array(
268 'abc/def/.',
269 'abc/def'
270 ),
271 'does not truncates ending dot if part of name' => array(
272 'abc/def.',
273 'abc/def.'
274 ),
275 'protocol is not removed' => array(
276 'vfs://def/../text.txt',
277 'vfs://text.txt'
278 ),
279 'works with filenames' => array(
280 '/def/../text.txt',
281 '/text.txt'
282 ),
283 'absolute windwos path' => array(
284 'C:\def\..\..\test.txt',
285 'C:/test.txt'
286 ),
287 'double slashaes' => array(
288 'abc//def',
289 'abc/def'
290 ),
291 'multiple slashes' => array(
292 'abc///////def',
293 'abc/def'
294 ),
295 );
296 }
297
298 /**
299 * @test
300 * @dataProvider getCanonicalPathCorrectlyCleansPathDataProvider
301 */
302 public function getCanonicalPathCorrectlyCleansPath($inputName, $expectedResult) {
303 $className = uniqid('PathUtilityFixture');
304 $fixtureClassString = '
305 namespace ' . ltrim(__NAMESPACE__, '\\') . ';
306 class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\PathUtility {
307 static public function isWindows() {
308 return TRUE;
309 }
310 }
311 ';
312 eval($fixtureClassString);
313 $fullyQualifiedClassName = __NAMESPACE__ . '\\' . $className;
314
315 $this->assertEquals(
316 $expectedResult,
317 $fullyQualifiedClassName::getCanonicalPath($inputName)
318 );
319 }
320
321 }
322
323 ?>