1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use stdClass; |
9
|
|
|
use Yiisoft\Validator\Rule\Url; |
10
|
|
|
use Yiisoft\Validator\Rule\UrlHandler; |
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
16
|
|
|
|
17
|
|
|
final class UrlTest extends RuleTestCase |
18
|
|
|
{ |
19
|
|
|
use DifferentRuleInHandlerTestTrait; |
20
|
|
|
use RuleWithOptionsTestTrait; |
21
|
|
|
use SkipOnErrorTestTrait; |
22
|
|
|
use WhenTestTrait; |
23
|
|
|
|
24
|
|
|
public function testNumberEmptyPattern(): void |
25
|
|
|
{ |
26
|
|
|
$this->expectException(InvalidArgumentException::class); |
27
|
|
|
$this->expectExceptionMessage('Pattern can\'t be empty.'); |
28
|
|
|
new Url(pattern: ''); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testDefaultValues(): void |
32
|
|
|
{ |
33
|
|
|
$rule = new Url(); |
34
|
|
|
|
35
|
|
|
$this->assertSame('url', $rule->getName()); |
36
|
|
|
$this->assertSame(['http', 'https'], $rule->getValidSchemes()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetValidSchemes(): void |
40
|
|
|
{ |
41
|
|
|
$rule = new Url(validSchemes: ['http', 'https', 'ftp', 'ftps']); |
42
|
|
|
$this->assertSame(['http', 'https', 'ftp', 'ftps'], $rule->getValidSchemes()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function dataOptions(): array |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[ |
49
|
|
|
new Url(), |
50
|
|
|
[ |
51
|
|
|
'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', |
52
|
|
|
'validSchemes' => ['http', 'https'], |
53
|
|
|
'enableIdn' => false, |
54
|
|
|
'incorrectInputMessage' => [ |
55
|
|
|
'template' => 'The value must be a string.', |
56
|
|
|
'parameters' => [], |
57
|
|
|
], |
58
|
|
|
'message' => [ |
59
|
|
|
'template' => 'This value is not a valid URL.', |
60
|
|
|
'parameters' => [], |
61
|
|
|
], |
62
|
|
|
'skipOnEmpty' => false, |
63
|
|
|
'skipOnError' => false, |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
[ |
67
|
|
|
new Url(enableIdn: true), |
68
|
|
|
[ |
69
|
|
|
'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', |
70
|
|
|
'validSchemes' => ['http', 'https'], |
71
|
|
|
'enableIdn' => true, |
72
|
|
|
'incorrectInputMessage' => [ |
73
|
|
|
'template' => 'The value must be a string.', |
74
|
|
|
'parameters' => [], |
75
|
|
|
], |
76
|
|
|
'message' => [ |
77
|
|
|
'template' => 'This value is not a valid URL.', |
78
|
|
|
'parameters' => [], |
79
|
|
|
], |
80
|
|
|
'skipOnEmpty' => false, |
81
|
|
|
'skipOnError' => false, |
82
|
|
|
], |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
new Url(validSchemes: ['http']), |
86
|
|
|
[ |
87
|
|
|
'pattern' => '/^((?i)http):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', |
88
|
|
|
'validSchemes' => ['http'], |
89
|
|
|
'enableIdn' => false, |
90
|
|
|
'incorrectInputMessage' => [ |
91
|
|
|
'template' => 'The value must be a string.', |
92
|
|
|
'parameters' => [], |
93
|
|
|
], |
94
|
|
|
'message' => [ |
95
|
|
|
'template' => 'This value is not a valid URL.', |
96
|
|
|
'parameters' => [], |
97
|
|
|
], |
98
|
|
|
'skipOnEmpty' => false, |
99
|
|
|
'skipOnError' => false, |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/', enableIdn: true), |
104
|
|
|
[ |
105
|
|
|
'pattern' => '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/', |
106
|
|
|
'validSchemes' => ['http', 'https'], |
107
|
|
|
'enableIdn' => true, |
108
|
|
|
'incorrectInputMessage' => [ |
109
|
|
|
'template' => 'The value must be a string.', |
110
|
|
|
'parameters' => [], |
111
|
|
|
], |
112
|
|
|
'message' => [ |
113
|
|
|
'template' => 'This value is not a valid URL.', |
114
|
|
|
'parameters' => [], |
115
|
|
|
], |
116
|
|
|
'skipOnEmpty' => false, |
117
|
|
|
'skipOnError' => false, |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function dataValidationPassed(): array |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
|
|
['http://google.de', [new Url()]], |
127
|
|
|
['https://google.de', [new Url()]], |
128
|
|
|
[ |
129
|
|
|
'https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr', |
130
|
|
|
[new Url()], |
131
|
|
|
], |
132
|
|
|
['http://example.com/*12', [new Url()]], |
133
|
|
|
['http://example.com/?test', [new Url()]], |
134
|
|
|
['http://example.com/#test', [new Url()]], |
135
|
|
|
['http://example.com:80/#test', [new Url()]], |
136
|
|
|
['http://example.com:65535/#test', [new Url()]], |
137
|
|
|
['http://example.com:81/?good', [new Url()]], |
138
|
|
|
['http://example.com?test', [new Url()]], |
139
|
|
|
['http://example.com#test', [new Url()]], |
140
|
|
|
['http://example.com:81#test', [new Url()]], |
141
|
|
|
['http://example.com:81?good', [new Url()]], |
142
|
|
|
|
143
|
|
|
['yiiframework.com', [new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)/')]], |
144
|
|
|
|
145
|
|
|
['ftp://ftp.ruhr-uni-bochum.de/', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]], |
146
|
|
|
['http://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]], |
147
|
|
|
['https://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]], |
148
|
|
|
|
149
|
|
|
['HtTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]], |
150
|
|
|
['fTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]], |
151
|
|
|
|
152
|
|
|
['http://äüößìà.de', [new Url(enableIdn: true)]], |
153
|
|
|
['http://xn--zcack7ayc9a.de', [new Url(enableIdn: true)]], |
154
|
|
|
['домен.рф', [new Url(pattern: '/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i', enableIdn: true)]], |
155
|
|
|
|
156
|
|
|
['http://' . str_repeat('a', 1989) . '.de', [new Url()]], |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function dataValidationFailed(): array |
161
|
|
|
{ |
162
|
|
|
$incorrectInputErrors = ['' => ['The value must be a string.']]; |
163
|
|
|
$errors = ['' => ['This value is not a valid URL.']]; |
164
|
|
|
$longUrl = 'http://' . str_repeat('u', 1990) . '.de'; |
165
|
|
|
|
166
|
|
|
return [ |
167
|
|
|
'incorrect input, integer' => [1, [new Url()], $incorrectInputErrors], |
168
|
|
|
'incorrect input, string in array' => [['yiiframework.com'], [new Url()], $incorrectInputErrors], |
169
|
|
|
'incorrect input, object' => [new stdClass(), [new Url()], $incorrectInputErrors], |
170
|
|
|
'custom incorrect input message' => [ |
171
|
|
|
1, |
172
|
|
|
[new Url(incorrectInputMessage: 'Custom incorrect input message.')], |
173
|
|
|
['' => ['Custom incorrect input message.']], |
174
|
|
|
], |
175
|
|
|
'custom incorrect input message with parameters' => [ |
176
|
|
|
1, |
177
|
|
|
[new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
178
|
|
|
['' => ['Attribute - , type - int.']], |
179
|
|
|
], |
180
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
181
|
|
|
['attribute' => 1], |
182
|
|
|
['attribute' => [new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')]], |
183
|
|
|
['attribute' => ['Attribute - attribute, type - int.']], |
184
|
|
|
], |
185
|
|
|
|
186
|
|
|
['google.de', [new Url()], $errors], |
187
|
|
|
['htp://yiiframework.com', [new Url()], $errors], |
188
|
|
|
['ftp://ftp.ruhr-uni-bochum.de/', [new Url()], $errors], |
189
|
|
|
['http://invalid,domain', [new Url()], $errors], |
190
|
|
|
['http://example.com,', [new Url()], $errors], |
191
|
|
|
['http://example.com*12', [new Url()], $errors], |
192
|
|
|
['http://example.com,?test', [new Url()], $errors], |
193
|
|
|
['http://example.com:?test', [new Url()], $errors], |
194
|
|
|
['http://example.com:test', [new Url()], $errors], |
195
|
|
|
['http://example.com:123456/test', [new Url()], $errors], |
196
|
|
|
['http://äüö?=!"§$%&/()=}][{³²€.edu', [new Url()], $errors], |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
['htp://yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors], |
200
|
|
|
// Relative URLs are not supported |
201
|
|
|
['//yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors], |
202
|
|
|
|
203
|
|
|
['', [new Url(enableIdn: true)], $errors], |
204
|
|
|
[$longUrl, [new Url(enableIdn: true)], $errors], |
205
|
|
|
[$longUrl, [new Url()], $errors], |
206
|
|
|
|
207
|
|
|
'custom message' => [ |
208
|
|
|
'', |
209
|
|
|
[new Url(enableIdn: true, message: 'Custom message.')], |
210
|
|
|
['' => ['Custom message.']], |
211
|
|
|
], |
212
|
|
|
'custom message with parameters' => [ |
213
|
|
|
'not a url', |
214
|
|
|
[new Url(enableIdn: true, message: 'Attribute - {attribute}, value - {value}.')], |
215
|
|
|
['' => ['Attribute - , value - not a url.']], |
216
|
|
|
], |
217
|
|
|
'custom message with parameters, attribute set' => [ |
218
|
|
|
['attribute' => 'not a url'], |
219
|
|
|
['attribute' => new Url(enableIdn: true, message: 'Attribute - {attribute}, value - {value}.')], |
220
|
|
|
['attribute' => ['Attribute - attribute, value - not a url.']], |
221
|
|
|
], |
222
|
|
|
]; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testSkipOnError(): void |
226
|
|
|
{ |
227
|
|
|
$this->testSkipOnErrorInternal(new Url(), new Url(skipOnError: true)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testWhen(): void |
231
|
|
|
{ |
232
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
233
|
|
|
$this->testWhenInternal(new Url(), new Url(when: $when)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
237
|
|
|
{ |
238
|
|
|
return [Url::class, UrlHandler::class]; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|