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\Validation;
20 * @group dns-sensitive
22 class UrlValidatorTest extends AbstractConstraintValidatorTest
24 protected function getApiVersion()
26 return Validation::API_VERSION_2_5;
29 protected function createValidator()
31 return new UrlValidator();
34 public function testNullIsValid()
36 $this->validator->validate(null, new Url());
38 $this->assertNoViolation();
41 public function testEmptyStringIsValid()
43 $this->validator->validate('', new Url());
45 $this->assertNoViolation();
48 public function testEmptyStringFromObjectIsValid()
50 $this->validator->validate(new EmailProvider(), new Url());
52 $this->assertNoViolation();
56 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
58 public function testExpectsStringCompatibleType()
60 $this->validator->validate(new \stdClass(), new Url());
64 * @dataProvider getValidUrls
66 public function testValidUrls($url)
68 $this->validator->validate($url, new Url());
70 $this->assertNoViolation();
73 public function getValidUrls()
77 array('http://www.google.com'),
78 array('http://www.google.com.'),
79 array('http://www.google.museum'),
80 array('https://google.com/'),
81 array('https://google.com:80/'),
82 array('http://www.example.coop/'),
83 array('http://www.test-example.com/'),
84 array('http://www.symfony.com/'),
85 array('http://symfony.fake/blog/'),
86 array('http://symfony.com/?'),
87 array('http://symfony.com/search?type=&q=url+validator'),
88 array('http://symfony.com/#'),
89 array('http://symfony.com/#?'),
90 array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
91 array('http://very.long.domain.name.com/'),
92 array('http://localhost/'),
93 array('http://myhost123/'),
94 array('http://127.0.0.1/'),
95 array('http://127.0.0.1:80/'),
96 array('http://[::1]/'),
97 array('http://[::1]:80/'),
98 array('http://[1:2:3::4:5:6:7]/'),
99 array('http://sãopaulo.com/'),
100 array('http://xn--sopaulo-xwa.com/'),
101 array('http://sãopaulo.com.br/'),
102 array('http://xn--sopaulo-xwa.com.br/'),
103 array('http://пример.испытание/'),
104 array('http://xn--e1afmkfd.xn--80akhbyknj4f/'),
105 array('http://مثال.إختبار/'),
106 array('http://xn--mgbh0fb.xn--kgbechtv/'),
107 array('http://例子.测试/'),
108 array('http://xn--fsqu00a.xn--0zwm56d/'),
109 array('http://例子.測試/'),
110 array('http://xn--fsqu00a.xn--g6w251d/'),
111 array('http://例え.テスト/'),
112 array('http://xn--r8jz45g.xn--zckzah/'),
113 array('http://مثال.آزمایشی/'),
114 array('http://xn--mgbh0fb.xn--hgbk6aj7f53bba/'),
115 array('http://실례.테스트/'),
116 array('http://xn--9n2bp8q.xn--9t4b11yi5a/'),
117 array('http://العربية.idn.icann.org/'),
118 array('http://xn--ogb.idn.icann.org/'),
119 array('http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/'),
120 array('http://xn--espaa-rta.xn--ca-ol-fsay5a/'),
121 array('http://xn--d1abbgf6aiiy.xn--p1ai/'),
122 array('http://☎.com/'),
123 array('http://username:password@symfony.com'),
124 array('http://user-name@symfony.com'),
125 array('http://symfony.com?'),
126 array('http://symfony.com?query=1'),
127 array('http://symfony.com/?query=1'),
128 array('http://symfony.com#'),
129 array('http://symfony.com#fragment'),
130 array('http://symfony.com/#fragment'),
131 array('http://symfony.com/#one_more%20test'),
136 * @dataProvider getInvalidUrls
138 public function testInvalidUrls($url)
140 $constraint = new Url(array(
141 'message' => 'myMessage',
144 $this->validator->validate($url, $constraint);
146 $this->buildViolation('myMessage')
147 ->setParameter('{{ value }}', '"'.$url.'"')
148 ->setCode(Url::INVALID_URL_ERROR)
152 public function getInvalidUrls()
156 array('://google.com'),
157 array('http ://google.com'),
158 array('http:/google.com'),
159 array('http://goog_le.com'),
160 array('http://google.com::aa'),
161 array('http://google.com:aa'),
162 array('ftp://google.fr'),
163 array('faked://google.fr'),
164 array('http://127.0.0.1:aa/'),
165 array('ftp://[::1]/'),
166 array('http://[::1'),
167 array('http://hello.☎/'),
168 array('http://:password@symfony.com'),
169 array('http://:password@@symfony.com'),
170 array('http://username:passwordsymfony.com'),
171 array('http://usern@me:password@symfony.com'),
172 array('http://example.com/exploit.html?<script>alert(1);</script>'),
173 array('http://example.com/exploit.html?hel lo'),
174 array('http://example.com/exploit.html?not_a%hex'),
179 * @dataProvider getValidCustomUrls
181 public function testCustomProtocolIsValid($url)
183 $constraint = new Url(array(
184 'protocols' => array('ftp', 'file', 'git'),
187 $this->validator->validate($url, $constraint);
189 $this->assertNoViolation();
192 public function getValidCustomUrls()
195 array('ftp://google.com'),
196 array('file://127.0.0.1'),
197 array('git://[::1]/'),
202 * @dataProvider getCheckDns
203 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
205 public function testCheckDns($violation)
207 DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
209 $constraint = new Url(array(
211 'dnsMessage' => 'myMessage',
214 $this->validator->validate('http://example.com', $constraint);
217 $this->assertNoViolation();
219 $this->buildViolation('myMessage')
220 ->setParameter('{{ value }}', '"example.com"')
221 ->setCode(Url::INVALID_URL_ERROR)
226 public function getCheckDns()
228 return array(array(true), array(false));
234 public function __toString()