4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Bridge\PhpUnit\DnsMock;
15 use Symfony\Component\Validator\Constraints\Url;
16 use Symfony\Component\Validator\Constraints\UrlValidator;
17 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
20 * @group dns-sensitive
22 class UrlValidatorTest extends ConstraintValidatorTestCase
24 protected function createValidator()
26 return new UrlValidator();
29 public function testNullIsValid()
31 $this->validator->validate(null, new Url());
33 $this->assertNoViolation();
36 public function testEmptyStringIsValid()
38 $this->validator->validate('', new Url());
40 $this->assertNoViolation();
43 public function testEmptyStringFromObjectIsValid()
45 $this->validator->validate(new EmailProvider(), new Url());
47 $this->assertNoViolation();
51 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
53 public function testExpectsStringCompatibleType()
55 $this->validator->validate(new \stdClass(), new Url());
59 * @dataProvider getValidUrls
61 public function testValidUrls($url)
63 $this->validator->validate($url, new Url());
65 $this->assertNoViolation();
68 public function getValidUrls()
72 array('http://www.google.com'),
73 array('http://www.google.com.'),
74 array('http://www.google.museum'),
75 array('https://google.com/'),
76 array('https://google.com:80/'),
77 array('http://www.example.coop/'),
78 array('http://www.test-example.com/'),
79 array('http://www.symfony.com/'),
80 array('http://symfony.fake/blog/'),
81 array('http://symfony.com/?'),
82 array('http://symfony.com/search?type=&q=url+validator'),
83 array('http://symfony.com/#'),
84 array('http://symfony.com/#?'),
85 array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
86 array('http://very.long.domain.name.com/'),
87 array('http://localhost/'),
88 array('http://myhost123/'),
89 array('http://127.0.0.1/'),
90 array('http://127.0.0.1:80/'),
91 array('http://[::1]/'),
92 array('http://[::1]:80/'),
93 array('http://[1:2:3::4:5:6:7]/'),
94 array('http://sãopaulo.com/'),
95 array('http://xn--sopaulo-xwa.com/'),
96 array('http://sãopaulo.com.br/'),
97 array('http://xn--sopaulo-xwa.com.br/'),
98 array('http://пример.испытание/'),
99 array('http://xn--e1afmkfd.xn--80akhbyknj4f/'),
100 array('http://مثال.إختبار/'),
101 array('http://xn--mgbh0fb.xn--kgbechtv/'),
102 array('http://例子.测试/'),
103 array('http://xn--fsqu00a.xn--0zwm56d/'),
104 array('http://例子.測試/'),
105 array('http://xn--fsqu00a.xn--g6w251d/'),
106 array('http://例え.テスト/'),
107 array('http://xn--r8jz45g.xn--zckzah/'),
108 array('http://مثال.آزمایشی/'),
109 array('http://xn--mgbh0fb.xn--hgbk6aj7f53bba/'),
110 array('http://실례.테스트/'),
111 array('http://xn--9n2bp8q.xn--9t4b11yi5a/'),
112 array('http://العربية.idn.icann.org/'),
113 array('http://xn--ogb.idn.icann.org/'),
114 array('http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/'),
115 array('http://xn--espaa-rta.xn--ca-ol-fsay5a/'),
116 array('http://xn--d1abbgf6aiiy.xn--p1ai/'),
117 array('http://☎.com/'),
118 array('http://username:password@symfony.com'),
119 array('http://user.name:password@symfony.com'),
120 array('http://username:pass.word@symfony.com'),
121 array('http://user.name:pass.word@symfony.com'),
122 array('http://user-name@symfony.com'),
123 array('http://symfony.com?'),
124 array('http://symfony.com?query=1'),
125 array('http://symfony.com/?query=1'),
126 array('http://symfony.com#'),
127 array('http://symfony.com#fragment'),
128 array('http://symfony.com/#fragment'),
129 array('http://symfony.com/#one_more%20test'),
134 * @dataProvider getInvalidUrls
136 public function testInvalidUrls($url)
138 $constraint = new Url(array(
139 'message' => 'myMessage',
142 $this->validator->validate($url, $constraint);
144 $this->buildViolation('myMessage')
145 ->setParameter('{{ value }}', '"'.$url.'"')
146 ->setCode(Url::INVALID_URL_ERROR)
150 public function getInvalidUrls()
154 array('://google.com'),
155 array('http ://google.com'),
156 array('http:/google.com'),
157 array('http://goog_le.com'),
158 array('http://google.com::aa'),
159 array('http://google.com:aa'),
160 array('ftp://google.fr'),
161 array('faked://google.fr'),
162 array('http://127.0.0.1:aa/'),
163 array('ftp://[::1]/'),
164 array('http://[::1'),
165 array('http://hello.☎/'),
166 array('http://:password@symfony.com'),
167 array('http://:password@@symfony.com'),
168 array('http://username:passwordsymfony.com'),
169 array('http://usern@me:password@symfony.com'),
170 array('http://example.com/exploit.html?<script>alert(1);</script>'),
171 array('http://example.com/exploit.html?hel lo'),
172 array('http://example.com/exploit.html?not_a%hex'),
178 * @dataProvider getValidCustomUrls
180 public function testCustomProtocolIsValid($url)
182 $constraint = new Url(array(
183 'protocols' => array('ftp', 'file', 'git'),
186 $this->validator->validate($url, $constraint);
188 $this->assertNoViolation();
191 public function getValidCustomUrls()
194 array('ftp://google.com'),
195 array('file://127.0.0.1'),
196 array('git://[::1]/'),
201 * @dataProvider getCheckDns
202 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
204 public function testCheckDns($violation)
206 DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
208 $constraint = new Url(array(
210 'dnsMessage' => 'myMessage',
213 $this->validator->validate('http://example.com', $constraint);
216 $this->assertNoViolation();
218 $this->buildViolation('myMessage')
219 ->setParameter('{{ value }}', '"example.com"')
220 ->setCode(Url::INVALID_URL_ERROR)
225 public function getCheckDns()
227 return array(array(true), array(false));
231 * @dataProvider getCheckDnsTypes
232 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
234 public function testCheckDnsByType($type)
236 DnsMock::withMockedHosts(array('example.com' => array(array('type' => $type))));
238 $constraint = new Url(array(
240 'dnsMessage' => 'myMessage',
243 $this->validator->validate('http://example.com', $constraint);
245 $this->assertNoViolation();
248 public function getCheckDnsTypes()
269 public function testCheckDnsWithBoolean()
271 DnsMock::withMockedHosts(array('example.com' => array(array('type' => 'A'))));
273 $constraint = new Url(array(
275 'dnsMessage' => 'myMessage',
278 $this->validator->validate('http://example.com', $constraint);
280 $this->assertNoViolation();
284 * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
285 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
287 public function testCheckDnsWithInvalidType()
289 DnsMock::withMockedHosts(array('example.com' => array(array('type' => 'A'))));
291 $constraint = new Url(array(
292 'checkDNS' => 'BOGUS',
293 'dnsMessage' => 'myMessage',
296 $this->validator->validate('http://example.com', $constraint);
302 public function __toString()