1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Core\Path; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
|
9
|
|
|
class PathTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Test paths are joined |
13
|
|
|
* |
14
|
|
|
* @dataProvider providerTestJoinPaths |
15
|
|
|
* @param array $args Arguments to pass to Path::join() |
16
|
|
|
* @param string $expected Expected path |
17
|
|
|
*/ |
18
|
|
|
public function testJoinPaths($args, $expected) |
19
|
|
|
{ |
20
|
|
|
$joined = Path::join($args); |
21
|
|
|
$this->assertEquals($expected, $joined); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of tests for testJoinPaths |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function providerTestJoinPaths() |
30
|
|
|
{ |
31
|
|
|
$tests = [ |
32
|
|
|
// Single arg |
33
|
|
|
[['/'], '/'], |
34
|
|
|
[['\\'], '/'], |
35
|
|
|
[['base'], 'base'], |
36
|
|
|
[['c:/base\\'], 'c:/base'], |
37
|
|
|
// Windows paths |
38
|
|
|
[['c:/', 'bob'], 'c:/bob'], |
39
|
|
|
[['c:/', '\\bob/'], 'c:/bob'], |
40
|
|
|
[['c:\\basedir', '/bob\\'], 'c:/basedir/bob'], |
41
|
|
|
// Empty-ish paths to clear out |
42
|
|
|
[['/root/dir', '/', ' ', 'next/', '\\'], '/root/dir/next'], |
43
|
|
|
[['/', '/', ' ', '/', '\\'], '/'], |
44
|
|
|
[['/', '', '',], '/'], |
45
|
|
|
[['/root', '/', ' ', '/', '\\'], '/root'], |
46
|
|
|
[['', '/root', '/', ' ', '/', '\\'], '/root'], |
47
|
|
|
[['', 'root', '/', ' ', '/', '\\'], 'root'], |
48
|
|
|
[['\\', '', '/root', '/', ' ', '/', '\\'], '/root'], |
49
|
|
|
// join blocks of paths |
50
|
|
|
[['/root/dir', 'another/path\\to/join'], '/root/dir/another/path/to/join'], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
// Rewrite tests for other filesystems (output arg only) |
54
|
|
|
if (DIRECTORY_SEPARATOR !== '/') { |
55
|
|
|
foreach ($tests as $index => $test) { |
56
|
|
|
$tests[$index][1] = str_replace('/', DIRECTORY_SEPARATOR, $tests[$index][1]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $tests; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test that joinPaths give the appropriate error |
64
|
|
|
* |
65
|
|
|
* @dataProvider providerTestJoinPathsErrors |
66
|
|
|
* @param array $args Arguments to pass to Filesystem::joinPath() |
67
|
|
|
* @param string $error Expected path |
68
|
|
|
*/ |
69
|
|
|
public function testJoinPathsErrors($args, $error) |
70
|
|
|
{ |
71
|
|
|
$this->expectException(InvalidArgumentException::class); |
72
|
|
|
$this->expectExceptionMessage($error); |
73
|
|
|
Path::join($args); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function providerTestJoinPathsErrors() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
[['/base', '../passwd'], 'Can not collapse relative folders'], |
80
|
|
|
[['/base/../', 'passwd/path'], 'Can not collapse relative folders'], |
81
|
|
|
[['../', 'passwd/path'], 'Can not collapse relative folders'], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider providerTestNormalise |
87
|
|
|
* @param string $input |
88
|
|
|
* @param string $expected |
89
|
|
|
*/ |
90
|
|
|
public function testNormalise($input, $expected) |
91
|
|
|
{ |
92
|
|
|
$output = Path::normalise($input); |
93
|
|
|
$this->assertEquals($expected, $output, "Expected $input to be normalised to $expected"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function providerTestNormalise() |
97
|
|
|
{ |
98
|
|
|
$tests = [ |
99
|
|
|
// Windows paths |
100
|
|
|
["c:/bob", "c:/bob"], |
101
|
|
|
["c://bob", "c:/bob"], |
102
|
|
|
["/root/dir/", "/root/dir"], |
103
|
|
|
["/root\\dir\\\\sub/", "/root/dir/sub"], |
104
|
|
|
[" /some/dir/ ", "/some/dir"], |
105
|
|
|
["", ""], |
106
|
|
|
["/", ""], |
107
|
|
|
["\\", ""], |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
// Rewrite tests for other filesystems (output arg only) |
111
|
|
|
if (DIRECTORY_SEPARATOR !== '/') { |
112
|
|
|
foreach ($tests as $index => $test) { |
113
|
|
|
$tests[$index][1] = str_replace('/', DIRECTORY_SEPARATOR, $tests[$index][1]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
return $tests; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|