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