|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Utility\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Utility\Url; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class StrTest |
|
9
|
|
|
* @package Nip\Utility\Tests |
|
10
|
|
|
*/ |
|
11
|
|
|
class UrlTest extends AbstractTest |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function test_addArg() |
|
15
|
|
|
{ |
|
16
|
|
|
// Regular tests |
|
17
|
|
|
self::assertSame('user=5', Url::addArg(['user' => 5], '')); |
|
18
|
|
|
self::assertSame('/app/admin/users?user=5', Url::addArg(['user' => 5], '/app/admin/users')); |
|
19
|
|
|
|
|
20
|
|
|
self::assertSame( |
|
21
|
|
|
'/app/admin/users?action=edit&user=5', |
|
22
|
|
|
Url::addArg(['user' => 5], '/app/admin/users?action=edit') |
|
23
|
|
|
); |
|
24
|
|
|
self::assertSame( |
|
25
|
|
|
'/app/admin/users?action=edit&tab=personal&user=5', |
|
26
|
|
|
Url::addArg(['user' => 5], '/app/admin/users?action=edit&tab=personal') |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
// Ensure strips false. |
|
30
|
|
|
self::assertSame('/index.php', Url::addArg(['debug' => false], '/index.php')); |
|
31
|
|
|
|
|
32
|
|
|
// With valueless parameters. |
|
33
|
|
|
self::assertSame('/index.php?debug=', Url::addArg(['debug' => null], '/index.php')); |
|
34
|
|
|
self::assertSame('/index.php?debug=#hash', Url::addArg(['debug' => null], '/index.php#hash')); |
|
35
|
|
|
|
|
36
|
|
|
// With a URL fragment |
|
37
|
|
|
self::assertSame('/app/admin/users?user=5#test', Url::addArg(['user' => 5], '/app/admin/users#test')); |
|
38
|
|
|
|
|
39
|
|
|
// Full URL |
|
40
|
|
|
self::assertSame('http://example.com/?a=b', Url::addArg(['a' => 'b'], 'http://example.com')); |
|
41
|
|
|
|
|
42
|
|
|
// Only the query string |
|
43
|
|
|
self::assertSame('?a=b&c=d', Url::addArg(['c' => 'd'], '?a=b')); |
|
44
|
|
|
self::assertSame('a=b&c=d', Url::addArg(['c' => 'd'], 'a=b')); |
|
45
|
|
|
|
|
46
|
|
|
// Url encoding test |
|
47
|
|
|
self::assertSame( |
|
48
|
|
|
'/app/admin/users?param=containsa%26sym', |
|
49
|
|
|
Url::addArg(['param' => 'containsa&sym'], '/app/admin/users') |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
// If not provided, grab the URI from the server. |
|
53
|
|
|
$_SERVER['REQUEST_URI'] = '/app/admin/users'; |
|
54
|
|
|
self::assertSame('/app/admin/users?user=6', Url::addArg(['user' => 6])); |
|
55
|
|
|
self::assertSame('/app/admin/users?user=7', Url::addArg(['user' => 7])); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testRemoveArg() |
|
59
|
|
|
{ |
|
60
|
|
|
self::assertSame('/app/admin/users', Url::delArg('user', '/app/admin/users?user=5')); |
|
61
|
|
|
self::assertSame('/app/admin/users?action=edit', Url::delArg('user', '/app/admin/users?action=edit&user=5')); |
|
62
|
|
|
self::assertSame( |
|
63
|
|
|
'/app/admin/users?user=5', |
|
64
|
|
|
Url::delArg(['tab', 'action'], '/app/admin/users?action=edit&tab=personal&user=5') |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function test_build() |
|
69
|
|
|
{ |
|
70
|
|
|
self::assertSame( |
|
71
|
|
|
'http://example.com:8080/path/?query#fragment', |
|
72
|
|
|
Url::build('http://example.com:8080/path/?query#fragment') |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
self::assertSame( |
|
76
|
|
|
'https://dev.example.com/', |
|
77
|
|
|
Url::build('http://example.com/', ['scheme' => 'https', 'host' => 'dev.example.com']) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
self::assertSame( |
|
81
|
|
|
'http://example.com/#hi', |
|
82
|
|
|
Url::build('http://example.com/', ['fragment' => 'hi']) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
self::assertSame( |
|
86
|
|
|
'http://example.com/page', |
|
87
|
|
|
Url::build('http://example.com', ['path' => 'page']) |
|
88
|
|
|
); |
|
89
|
|
|
self::assertSame( |
|
90
|
|
|
'http://example.com/page', |
|
91
|
|
|
Url::build('http://example.com/', ['path' => 'page']) |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
self::assertSame( |
|
95
|
|
|
'http://example.com/?hi=Bro', |
|
96
|
|
|
Url::build('http://example.com/', ['query' => 'hi=Bro']) |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
self::assertSame( |
|
100
|
|
|
'http://example.com/?show=1&hi=Bro', |
|
101
|
|
|
Url::build('http://example.com/?show=1', ['query' => 'hi=Bro'], Url::URL_JOIN_QUERY) |
|
102
|
|
|
); |
|
103
|
|
|
self::assertSame('http://[email protected]/', Url::build('http://example.com/', ['user' => 'admin'])); |
|
104
|
|
|
self::assertSame( |
|
105
|
|
|
'http://admin:[email protected]/', |
|
106
|
|
|
Url::build( |
|
107
|
|
|
'http://example.com/', |
|
108
|
|
|
['user' => 'admin', 'pass' => '1'] |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testCreate() |
|
114
|
|
|
{ |
|
115
|
|
|
self::assertSame( |
|
116
|
|
|
'https://example.com/?foo=bar', |
|
117
|
|
|
Url::create( |
|
118
|
|
|
[ |
|
119
|
|
|
'host' => 'example.com', |
|
120
|
|
|
'user' => '', |
|
121
|
|
|
'pass' => '123456', |
|
122
|
|
|
'query' => [ |
|
123
|
|
|
'foo' => 'bar', |
|
124
|
|
|
], |
|
125
|
|
|
] |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
self::assertSame( |
|
130
|
|
|
'https://example.com/', |
|
131
|
|
|
Url::create( |
|
132
|
|
|
[ |
|
133
|
|
|
'host' => 'example.com', |
|
134
|
|
|
'part' => '', |
|
135
|
|
|
] |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
self::assertSame( |
|
140
|
|
|
'ssh://example.com/', |
|
141
|
|
|
Url::create( |
|
142
|
|
|
[ |
|
143
|
|
|
'host' => 'example.com', |
|
144
|
|
|
'scheme' => 'ssh', |
|
145
|
|
|
'part' => '', |
|
146
|
|
|
] |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
self::assertSame( |
|
151
|
|
|
'http://example.com/', |
|
152
|
|
|
Url::create( |
|
153
|
|
|
[ |
|
154
|
|
|
'host' => 'example.com', |
|
155
|
|
|
'port' => 80, |
|
156
|
|
|
] |
|
157
|
|
|
) |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
self::assertSame( |
|
161
|
|
|
'https://example.com/', |
|
162
|
|
|
Url::create( |
|
163
|
|
|
[ |
|
164
|
|
|
'host' => 'example.com', |
|
165
|
|
|
'port' => 443, |
|
166
|
|
|
] |
|
167
|
|
|
) |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
self::assertSame( |
|
171
|
|
|
'https://example.com/page#hash', |
|
172
|
|
|
Url::create( |
|
173
|
|
|
[ |
|
174
|
|
|
'host' => 'example.com', |
|
175
|
|
|
'path' => 'page', |
|
176
|
|
|
'fragment' => 'hash', |
|
177
|
|
|
] |
|
178
|
|
|
) |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
self::assertSame( |
|
182
|
|
|
'https://user:[email protected]/page?foo=bar#hash', |
|
183
|
|
|
Url::create( |
|
184
|
|
|
[ |
|
185
|
|
|
'scheme' => 'https', |
|
186
|
|
|
'host' => 'example.com', |
|
187
|
|
|
'user' => 'user', |
|
188
|
|
|
'pass' => '123456', |
|
189
|
|
|
'path' => 'page', |
|
190
|
|
|
'query' => [ |
|
191
|
|
|
'foo' => 'bar', |
|
192
|
|
|
], |
|
193
|
|
|
'fragment' => '#hash', |
|
194
|
|
|
] |
|
195
|
|
|
) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
public function testIsAbsolute() |
|
201
|
|
|
{ |
|
202
|
|
|
self::assertTrue(Url::isAbsolute('https://site.com')); |
|
203
|
|
|
self::assertTrue(Url::isAbsolute('http://site.com')); |
|
204
|
|
|
self::assertTrue(Url::isAbsolute('//site.com')); |
|
205
|
|
|
self::assertTrue(Url::isAbsolute('ftp://site.com')); |
|
206
|
|
|
|
|
207
|
|
|
self::assertFalse(Url::isAbsolute('/path/to/file')); |
|
208
|
|
|
self::assertFalse(Url::isAbsolute('w:/path/to/file')); |
|
209
|
|
|
self::assertFalse(Url::isAbsolute('W:/path/to/file')); |
|
210
|
|
|
self::assertFalse(Url::isAbsolute('W:\path\to\file')); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @dataProvider data_isValid() |
|
215
|
|
|
* |
|
216
|
|
|
* @param $url |
|
217
|
|
|
* @param $output |
|
218
|
|
|
*/ |
|
219
|
|
|
public function test_isValid($url, $absolute, $output) |
|
220
|
|
|
{ |
|
221
|
|
|
self::assertSame($output, Url::isValid($url, $absolute)); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
public function data_isValid() |
|
228
|
|
|
{ |
|
229
|
|
|
return [ |
|
230
|
|
|
['test', true, false], |
|
231
|
|
|
['google.ro', true, false], |
|
232
|
|
|
['http://google.ro', true, true], |
|
233
|
|
|
]; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function test_linkify() |
|
237
|
|
|
{ |
|
238
|
|
|
$input = 'great websites: http://www.google.com?param=test and http://yahoo.com/a/nested/folder'; |
|
239
|
|
|
$expect = 'great websites: <a href="http://www.google.com?param=test">http://www.google.com?param=test</a> and <a href="http://yahoo.com/a/nested/folder">http://yahoo.com/a/nested/folder</a>'; |
|
240
|
|
|
static::assertEquals($expect, Url::linkify($input)); |
|
241
|
|
|
static::assertEquals($expect, Url::linkify($expect), 'linkify() tried to double linkify an href.'); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|