1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Yiisoft\Validator\Rule\Url; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @group validators |
10
|
|
|
*/ |
11
|
|
|
class UrlTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testValidate() |
14
|
|
|
{ |
15
|
|
|
$val = new Url(); |
16
|
|
|
$this->assertFalse($val->validate('google.de')->isValid()); |
17
|
|
|
$this->assertTrue($val->validate('http://google.de')->isValid()); |
18
|
|
|
$this->assertTrue($val->validate('https://google.de')->isValid()); |
19
|
|
|
$this->assertFalse($val->validate('htp://yiiframework.com')->isValid()); |
20
|
|
|
$this->assertTrue($val->validate('https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8' |
21
|
|
|
. '&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr')->isValid()); |
22
|
|
|
$this->assertFalse($val->validate('ftp://ftp.ruhr-uni-bochum.de/')->isValid()); |
23
|
|
|
$this->assertFalse($val->validate('http://invalid,domain')->isValid()); |
24
|
|
|
$this->assertFalse($val->validate('http://example.com,')->isValid()); |
25
|
|
|
$this->assertFalse($val->validate('http://example.com*12')->isValid()); |
26
|
|
|
$this->assertTrue($val->validate('http://example.com/*12')->isValid()); |
27
|
|
|
$this->assertTrue($val->validate('http://example.com/?test')->isValid()); |
28
|
|
|
$this->assertTrue($val->validate('http://example.com/#test')->isValid()); |
29
|
|
|
$this->assertTrue($val->validate('http://example.com:80/#test')->isValid()); |
30
|
|
|
$this->assertTrue($val->validate('http://example.com:65535/#test')->isValid()); |
31
|
|
|
$this->assertTrue($val->validate('http://example.com:81/?good')->isValid()); |
32
|
|
|
$this->assertTrue($val->validate('http://example.com?test')->isValid()); |
33
|
|
|
$this->assertTrue($val->validate('http://example.com#test')->isValid()); |
34
|
|
|
$this->assertTrue($val->validate('http://example.com:81#test')->isValid()); |
35
|
|
|
$this->assertTrue($val->validate('http://example.com:81?good')->isValid()); |
36
|
|
|
$this->assertFalse($val->validate('http://example.com,?test')->isValid()); |
37
|
|
|
$this->assertFalse($val->validate('http://example.com:?test')->isValid()); |
38
|
|
|
$this->assertFalse($val->validate('http://example.com:test')->isValid()); |
39
|
|
|
$this->assertFalse($val->validate('http://example.com:123456/test')->isValid()); |
40
|
|
|
|
41
|
|
|
$this->assertFalse($val->validate('http://äüö?=!"§$%&/()=}][{³²€.edu')->isValid()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testValidateWithoutScheme() |
45
|
|
|
{ |
46
|
|
|
$val = (new Url()) |
47
|
|
|
->pattern('/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i'); |
48
|
|
|
|
49
|
|
|
$this->assertTrue($val->validate('yiiframework.com')->isValid()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testValidateWithCustomScheme() |
53
|
|
|
{ |
54
|
|
|
$val = (new Url()) |
55
|
|
|
->schemes(['http', 'https', 'ftp', 'ftps']); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($val->validate('ftp://ftp.ruhr-uni-bochum.de/')->isValid()); |
58
|
|
|
$this->assertTrue($val->validate('http://google.de')->isValid()); |
59
|
|
|
$this->assertTrue($val->validate('https://google.de')->isValid()); |
60
|
|
|
$this->assertFalse($val->validate('htp://yiiframework.com')->isValid()); |
61
|
|
|
// relative urls not supported |
62
|
|
|
$this->assertFalse($val->validate('//yiiframework.com')->isValid()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testValidateWithIdn() |
66
|
|
|
{ |
67
|
|
|
if (!function_exists('idn_to_ascii')) { |
68
|
|
|
$this->markTestSkipped('intl package required'); |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
$val = (new Url()) |
73
|
|
|
->enableIDN(); |
74
|
|
|
|
75
|
|
|
$this->assertTrue($val->validate('http://äüößìà.de')->isValid()); |
76
|
|
|
// converted via http://mct.verisign-grs.com/convertServlet |
77
|
|
|
$this->assertTrue($val->validate('http://xn--zcack7ayc9a.de')->isValid()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testValidateLength() |
81
|
|
|
{ |
82
|
|
|
$url = 'http://' . str_pad('base', 2000, 'url') . '.de'; |
83
|
|
|
$val = new Url(); |
84
|
|
|
$this->assertFalse($val->validate($url)->isValid()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|