Updated to Drupal 8.5. Core Media not yet in use.
[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: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'),
130         );
131     }
132
133     /**
134      * @dataProvider getInvalidUrls
135      */
136     public function testInvalidUrls($url)
137     {
138         $constraint = new Url(array(
139             'message' => 'myMessage',
140         ));
141
142         $this->validator->validate($url, $constraint);
143
144         $this->buildViolation('myMessage')
145             ->setParameter('{{ value }}', '"'.$url.'"')
146             ->setCode(Url::INVALID_URL_ERROR)
147             ->assertRaised();
148     }
149
150     public function getInvalidUrls()
151     {
152         return array(
153             array('google.com'),
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'),
173             array('http://'),
174         );
175     }
176
177     /**
178      * @dataProvider getValidCustomUrls
179      */
180     public function testCustomProtocolIsValid($url)
181     {
182         $constraint = new Url(array(
183             'protocols' => array('ftp', 'file', 'git'),
184         ));
185
186         $this->validator->validate($url, $constraint);
187
188         $this->assertNoViolation();
189     }
190
191     public function getValidCustomUrls()
192     {
193         return array(
194             array('ftp://google.com'),
195             array('file://127.0.0.1'),
196             array('git://[::1]/'),
197         );
198     }
199
200     /**
201      * @dataProvider getCheckDns
202      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
203      */
204     public function testCheckDns($violation)
205     {
206         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
207
208         $constraint = new Url(array(
209             'checkDNS' => 'ANY',
210             'dnsMessage' => 'myMessage',
211         ));
212
213         $this->validator->validate('http://example.com', $constraint);
214
215         if (!$violation) {
216             $this->assertNoViolation();
217         } else {
218             $this->buildViolation('myMessage')
219                 ->setParameter('{{ value }}', '"example.com"')
220                 ->setCode(Url::INVALID_URL_ERROR)
221                 ->assertRaised();
222         }
223     }
224
225     public function getCheckDns()
226     {
227         return array(array(true), array(false));
228     }
229
230     /**
231      * @dataProvider getCheckDnsTypes
232      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
233      */
234     public function testCheckDnsByType($type)
235     {
236         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $type))));
237
238         $constraint = new Url(array(
239             'checkDNS' => $type,
240             'dnsMessage' => 'myMessage',
241         ));
242
243         $this->validator->validate('http://example.com', $constraint);
244
245         $this->assertNoViolation();
246     }
247
248     public function getCheckDnsTypes()
249     {
250         return array(
251             array('ANY'),
252             array('A'),
253             array('A6'),
254             array('AAAA'),
255             array('CNAME'),
256             array('MX'),
257             array('NAPTR'),
258             array('NS'),
259             array('PTR'),
260             array('SOA'),
261             array('SRV'),
262             array('TXT'),
263         );
264     }
265
266     /**
267      * @group legacy
268      */
269     public function testCheckDnsWithBoolean()
270     {
271         DnsMock::withMockedHosts(array('example.com' => array(array('type' => 'A'))));
272
273         $constraint = new Url(array(
274             'checkDNS' => true,
275             'dnsMessage' => 'myMessage',
276         ));
277
278         $this->validator->validate('http://example.com', $constraint);
279
280         $this->assertNoViolation();
281     }
282
283     /**
284      * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
285      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
286      */
287     public function testCheckDnsWithInvalidType()
288     {
289         DnsMock::withMockedHosts(array('example.com' => array(array('type' => 'A'))));
290
291         $constraint = new Url(array(
292             'checkDNS' => 'BOGUS',
293             'dnsMessage' => 'myMessage',
294         ));
295
296         $this->validator->validate('http://example.com', $constraint);
297     }
298 }
299
300 class EmailProvider
301 {
302     public function __toString()
303     {
304         return '';
305     }
306 }