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@symfony.com'),
120 array('http://symfony.com?'),
121 array('http://symfony.com?query=1'),
122 array('http://symfony.com/?query=1'),
123 array('http://symfony.com#'),
124 array('http://symfony.com#fragment'),
125 array('http://symfony.com/#fragment'),
126 array('http://symfony.com/#one_more%20test'),
131 * @dataProvider getInvalidUrls
133 public function testInvalidUrls($url)
135 $constraint = new Url(array(
136 'message' => 'myMessage',
139 $this->validator->validate($url, $constraint);
141 $this->buildViolation('myMessage')
142 ->setParameter('{{ value }}', '"'.$url.'"')
143 ->setCode(Url::INVALID_URL_ERROR)
147 public function getInvalidUrls()
151 array('://google.com'),
152 array('http ://google.com'),
153 array('http:/google.com'),
154 array('http://goog_le.com'),
155 array('http://google.com::aa'),
156 array('http://google.com:aa'),
157 array('ftp://google.fr'),
158 array('faked://google.fr'),
159 array('http://127.0.0.1:aa/'),
160 array('ftp://[::1]/'),
161 array('http://[::1'),
162 array('http://hello.☎/'),
163 array('http://:password@symfony.com'),
164 array('http://:password@@symfony.com'),
165 array('http://username:passwordsymfony.com'),
166 array('http://usern@me:password@symfony.com'),
167 array('http://example.com/exploit.html?<script>alert(1);</script>'),
168 array('http://example.com/exploit.html?hel lo'),
169 array('http://example.com/exploit.html?not_a%hex'),
175 * @dataProvider getValidCustomUrls
177 public function testCustomProtocolIsValid($url)
179 $constraint = new Url(array(
180 'protocols' => array('ftp', 'file', 'git'),
183 $this->validator->validate($url, $constraint);
185 $this->assertNoViolation();
188 public function getValidCustomUrls()
191 array('ftp://google.com'),
192 array('file://127.0.0.1'),
193 array('git://[::1]/'),
198 * @dataProvider getCheckDns
199 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
201 public function testCheckDns($violation)
203 DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
205 $constraint = new Url(array(
207 'dnsMessage' => 'myMessage',
210 $this->validator->validate('http://example.com', $constraint);
213 $this->assertNoViolation();
215 $this->buildViolation('myMessage')
216 ->setParameter('{{ value }}', '"example.com"')
217 ->setCode(Url::INVALID_URL_ERROR)
222 public function getCheckDns()
224 return array(array(true), array(false));
230 public function __toString()