Yaffs site version 1.1
[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\Validation;
18
19 /**
20  * @group dns-sensitive
21  */
22 class UrlValidatorTest extends AbstractConstraintValidatorTest
23 {
24     protected function getApiVersion()
25     {
26         return Validation::API_VERSION_2_5;
27     }
28
29     protected function createValidator()
30     {
31         return new UrlValidator();
32     }
33
34     public function testNullIsValid()
35     {
36         $this->validator->validate(null, new Url());
37
38         $this->assertNoViolation();
39     }
40
41     public function testEmptyStringIsValid()
42     {
43         $this->validator->validate('', new Url());
44
45         $this->assertNoViolation();
46     }
47
48     public function testEmptyStringFromObjectIsValid()
49     {
50         $this->validator->validate(new EmailProvider(), new Url());
51
52         $this->assertNoViolation();
53     }
54
55     /**
56      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
57      */
58     public function testExpectsStringCompatibleType()
59     {
60         $this->validator->validate(new \stdClass(), new Url());
61     }
62
63     /**
64      * @dataProvider getValidUrls
65      */
66     public function testValidUrls($url)
67     {
68         $this->validator->validate($url, new Url());
69
70         $this->assertNoViolation();
71     }
72
73     public function getValidUrls()
74     {
75         return array(
76             array('http://a.pl'),
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'),
132         );
133     }
134
135     /**
136      * @dataProvider getInvalidUrls
137      */
138     public function testInvalidUrls($url)
139     {
140         $constraint = new Url(array(
141             'message' => 'myMessage',
142         ));
143
144         $this->validator->validate($url, $constraint);
145
146         $this->buildViolation('myMessage')
147             ->setParameter('{{ value }}', '"'.$url.'"')
148             ->setCode(Url::INVALID_URL_ERROR)
149             ->assertRaised();
150     }
151
152     public function getInvalidUrls()
153     {
154         return array(
155             array('google.com'),
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'),
175             array('http://'),
176         );
177     }
178
179     /**
180      * @dataProvider getValidCustomUrls
181      */
182     public function testCustomProtocolIsValid($url)
183     {
184         $constraint = new Url(array(
185             'protocols' => array('ftp', 'file', 'git'),
186         ));
187
188         $this->validator->validate($url, $constraint);
189
190         $this->assertNoViolation();
191     }
192
193     public function getValidCustomUrls()
194     {
195         return array(
196             array('ftp://google.com'),
197             array('file://127.0.0.1'),
198             array('git://[::1]/'),
199         );
200     }
201
202     /**
203      * @dataProvider getCheckDns
204      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
205      */
206     public function testCheckDns($violation)
207     {
208         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
209
210         $constraint = new Url(array(
211             'checkDNS' => true,
212             'dnsMessage' => 'myMessage',
213         ));
214
215         $this->validator->validate('http://example.com', $constraint);
216
217         if (!$violation) {
218             $this->assertNoViolation();
219         } else {
220             $this->buildViolation('myMessage')
221                 ->setParameter('{{ value }}', '"example.com"')
222                 ->setCode(Url::INVALID_URL_ERROR)
223                 ->assertRaised();
224         }
225     }
226
227     public function getCheckDns()
228     {
229         return array(array(true), array(false));
230     }
231 }
232
233 class EmailProvider
234 {
235     public function __toString()
236     {
237         return '';
238     }
239 }