Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / UuidValidatorTest.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\Component\Validator\Constraints\Uuid;
15 use Symfony\Component\Validator\Constraints\UuidValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18 /**
19  * @author Colin O'Dell <colinodell@gmail.com>
20  */
21 class UuidValidatorTest extends ConstraintValidatorTestCase
22 {
23     protected function createValidator()
24     {
25         return new UuidValidator();
26     }
27
28     public function testNullIsValid()
29     {
30         $this->validator->validate(null, new Uuid());
31
32         $this->assertNoViolation();
33     }
34
35     public function testEmptyStringIsValid()
36     {
37         $this->validator->validate('', new Uuid());
38
39         $this->assertNoViolation();
40     }
41
42     /**
43      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
44      */
45     public function testExpectsUuidConstraintCompatibleType()
46     {
47         $constraint = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\Constraint');
48
49         $this->validator->validate('216fff40-98d9-11e3-a5e2-0800200c9a66', $constraint);
50     }
51
52     /**
53      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
54      */
55     public function testExpectsStringCompatibleType()
56     {
57         $this->validator->validate(new \stdClass(), new Uuid());
58     }
59
60     /**
61      * @dataProvider getValidStrictUuids
62      */
63     public function testValidStrictUuids($uuid, $versions = null)
64     {
65         $constraint = new Uuid();
66
67         if (null !== $versions) {
68             $constraint->versions = $versions;
69         }
70
71         $this->validator->validate($uuid, $constraint);
72
73         $this->assertNoViolation();
74     }
75
76     public function getValidStrictUuids()
77     {
78         return array(
79             array('216fff40-98d9-11e3-a5e2-0800200c9a66'), // Version 1 UUID in lowercase
80             array('216fff40-98d9-11e3-a5e2-0800200c9a66', array(Uuid::V1_MAC)),
81             array('216FFF40-98D9-11E3-A5E2-0800200C9A66'), // Version 1 UUID in UPPERCASE
82             array('456daefb-5aa6-41b5-8dbc-068b05a8b201'), // Version 4 UUID in lowercase
83             array('456daEFb-5AA6-41B5-8DBC-068B05A8B201'), // Version 4 UUID in mixed case
84             array('456daEFb-5AA6-41B5-8DBC-068B05A8B201', array(Uuid::V4_RANDOM)),
85         );
86     }
87
88     /**
89      * @dataProvider getInvalidStrictUuids
90      */
91     public function testInvalidStrictUuids($uuid, $code, $versions = null)
92     {
93         $constraint = new Uuid(array(
94             'message' => 'testMessage',
95         ));
96
97         if (null !== $versions) {
98             $constraint->versions = $versions;
99         }
100
101         $this->validator->validate($uuid, $constraint);
102
103         $this->buildViolation('testMessage')
104             ->setParameter('{{ value }}', '"'.$uuid.'"')
105             ->setCode($code)
106             ->assertRaised();
107     }
108
109     public function getInvalidStrictUuids()
110     {
111         return array(
112             array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
113             array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
114             array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
115             array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
116             array('216f-ff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
117             array('216fff40-98d9-11e3-a5e2-0800-200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
118             array('216fff40-98d9-11e3-a5e2-0800200c-9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
119             array('216fff40-98d9-11e3-a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
120             array('216fff4098d911e3a5e20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
121             array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
122             array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),
123             array('216fff40-98d9-01e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
124             array('216fff40-98d9-61e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
125             array('216fff40-98d9-71e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
126             array('216fff40-98d9-81e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
127             array('216fff40-98d9-91e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
128             array('216fff40-98d9-a1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
129             array('216fff40-98d9-b1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
130             array('216fff40-98d9-c1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
131             array('216fff40-98d9-d1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
132             array('216fff40-98d9-e1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
133             array('216fff40-98d9-f1e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR),
134             array('216fff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V2_DCE, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
135             array('216fff40-98d9-21e3-a5e2-0800200c9a66', Uuid::INVALID_VERSION_ERROR, array(Uuid::V1_MAC, Uuid::V3_MD5, Uuid::V4_RANDOM, Uuid::V5_SHA1)),
136             array('216fff40-98d9-11e3-05e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
137             array('216fff40-98d9-11e3-15e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
138             array('216fff40-98d9-11e3-25e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
139             array('216fff40-98d9-11e3-35e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
140             array('216fff40-98d9-11e3-45e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
141             array('216fff40-98d9-11e3-55e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
142             array('216fff40-98d9-11e3-65e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
143             array('216fff40-98d9-11e3-75e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
144             array('216fff40-98d9-11e3-c5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
145             array('216fff40-98d9-11e3-d5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
146             array('216fff40-98d9-11e3-e5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
147             array('216fff40-98d9-11e3-f5e2-0800200c9a66', Uuid::INVALID_VARIANT_ERROR),
148
149             // Non-standard UUID allowed by some other systems
150             array('{216fff40-98d9-11e3-a5e2-0800200c9a66}', Uuid::INVALID_CHARACTERS_ERROR),
151             array('[216fff40-98d9-11e3-a5e2-0800200c9a66]', Uuid::INVALID_CHARACTERS_ERROR),
152         );
153     }
154
155     /**
156      * @dataProvider getValidNonStrictUuids
157      */
158     public function testValidNonStrictUuids($uuid)
159     {
160         $constraint = new Uuid(array(
161             'strict' => false,
162         ));
163
164         $this->validator->validate($uuid, $constraint);
165
166         $this->assertNoViolation();
167     }
168
169     public function getValidNonStrictUuids()
170     {
171         return array(
172             array('216fff40-98d9-11e3-a5e2-0800200c9a66'),    // Version 1 UUID in lowercase
173             array('216FFF40-98D9-11E3-A5E2-0800200C9A66'),    // Version 1 UUID in UPPERCASE
174             array('456daefb-5aa6-41b5-8dbc-068b05a8b201'),    // Version 4 UUID in lowercase
175             array('456DAEFb-5AA6-41B5-8DBC-068b05a8B201'),    // Version 4 UUID in mixed case
176
177             // Non-standard UUIDs allowed by some other systems
178             array('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'), // Non-standard dash positions (every 4 chars)
179             array('216fff40-98d911e3-a5e20800-200c9a66'),     // Non-standard dash positions (every 8 chars)
180             array('216fff4098d911e3a5e20800200c9a66'),        // No dashes at all
181             array('{216fff40-98d9-11e3-a5e2-0800200c9a66}'),  // Wrapped with curly braces
182             array('[216fff40-98d9-11e3-a5e2-0800200c9a66]'),  // Wrapped with squared braces
183         );
184     }
185
186     /**
187      * @dataProvider getInvalidNonStrictUuids
188      */
189     public function testInvalidNonStrictUuids($uuid, $code)
190     {
191         $constraint = new Uuid(array(
192             'strict' => false,
193             'message' => 'myMessage',
194         ));
195
196         $this->validator->validate($uuid, $constraint);
197
198         $this->buildViolation('myMessage')
199             ->setParameter('{{ value }}', '"'.$uuid.'"')
200             ->setCode($code)
201             ->assertRaised();
202     }
203
204     public function getInvalidNonStrictUuids()
205     {
206         return array(
207             array('216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
208             array('216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
209             array('216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR),
210             array('216fff40-98d9-11e3-a5e2_0800200c9a6', Uuid::INVALID_CHARACTERS_ERROR),
211             array('216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR),
212             array('216fff40-98d9-11e3-a5e2-0800200c9a6', Uuid::TOO_SHORT_ERROR),
213             array('216fff40-98d9-11e3-a5e2-0800200c9a666', Uuid::TOO_LONG_ERROR),
214         );
215     }
216 }