0459dd02201ed692289a6245aae3403db3acca74
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / UrlValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
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;
18
19 /**
20  * @group dns-sensitive
21  */
22 class UrlValidatorTest extends ConstraintValidatorTestCase
23 {
24     protected function createValidator()
25     {
26         return new UrlValidator();
27     }
28
29     public function testNullIsValid()
30     {
31         $this->validator->validate(null, new Url());
32
33         $this->assertNoViolation();
34     }
35
36     public function testEmptyStringIsValid()
37     {
38         $this->validator->validate('', new Url());
39
40         $this->assertNoViolation();
41     }
42
43     public function testEmptyStringFromObjectIsValid()
44     {
45         $this->validator->validate(new EmailProvider(), new Url());
46
47         $this->assertNoViolation();
48     }
49
50     /**
51      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
52      */
53     public function testExpectsStringCompatibleType()
54     {
55         $this->validator->validate(new \stdClass(), new Url());
56     }
57
58     /**
59      * @dataProvider getValidUrls
60      */
61     public function testValidUrls($url)
62     {
63         $this->validator->validate($url, new Url());
64
65         $this->assertNoViolation();
66     }
67
68     public function getValidUrls()
69     {
70         return array(
71             array('http://a.pl'),
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'),
127         );
128     }
129
130     /**
131      * @dataProvider getInvalidUrls
132      */
133     public function testInvalidUrls($url)
134     {
135         $constraint = new Url(array(
136             'message' => 'myMessage',
137         ));
138
139         $this->validator->validate($url, $constraint);
140
141         $this->buildViolation('myMessage')
142             ->setParameter('{{ value }}', '"'.$url.'"')
143             ->setCode(Url::INVALID_URL_ERROR)
144             ->assertRaised();
145     }
146
147     public function getInvalidUrls()
148     {
149         return array(
150             array('google.com'),
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'),
170             array('http://'),
171         );
172     }
173
174     /**
175      * @dataProvider getValidCustomUrls
176      */
177     public function testCustomProtocolIsValid($url)
178     {
179         $constraint = new Url(array(
180             'protocols' => array('ftp', 'file', 'git'),
181         ));
182
183         $this->validator->validate($url, $constraint);
184
185         $this->assertNoViolation();
186     }
187
188     public function getValidCustomUrls()
189     {
190         return array(
191             array('ftp://google.com'),
192             array('file://127.0.0.1'),
193             array('git://[::1]/'),
194         );
195     }
196
197     /**
198      * @dataProvider getCheckDns
199      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
200      */
201     public function testCheckDns($violation)
202     {
203         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
204
205         $constraint = new Url(array(
206             'checkDNS' => true,
207             'dnsMessage' => 'myMessage',
208         ));
209
210         $this->validator->validate('http://example.com', $constraint);
211
212         if (!$violation) {
213             $this->assertNoViolation();
214         } else {
215             $this->buildViolation('myMessage')
216                 ->setParameter('{{ value }}', '"example.com"')
217                 ->setCode(Url::INVALID_URL_ERROR)
218                 ->assertRaised();
219         }
220     }
221
222     public function getCheckDns()
223     {
224         return array(array(true), array(false));
225     }
226 }
227
228 class EmailProvider
229 {
230     public function __toString()
231     {
232         return '';
233     }
234 }