Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / ExpressionValidatorTest.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\Expression;
15 use Symfony\Component\Validator\Constraints\ExpressionValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17 use Symfony\Component\Validator\Tests\Fixtures\Entity;
18 use Symfony\Component\Validator\Tests\Fixtures\ToString;
19
20 class ExpressionValidatorTest extends ConstraintValidatorTestCase
21 {
22     protected function createValidator()
23     {
24         return new ExpressionValidator();
25     }
26
27     public function testExpressionIsEvaluatedWithNullValue()
28     {
29         $constraint = new Expression(array(
30             'expression' => 'false',
31             'message' => 'myMessage',
32         ));
33
34         $this->validator->validate(null, $constraint);
35
36         $this->buildViolation('myMessage')
37             ->setParameter('{{ value }}', 'null')
38             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
39             ->assertRaised();
40     }
41
42     public function testExpressionIsEvaluatedWithEmptyStringValue()
43     {
44         $constraint = new Expression(array(
45             'expression' => 'false',
46             'message' => 'myMessage',
47         ));
48
49         $this->validator->validate('', $constraint);
50
51         $this->buildViolation('myMessage')
52             ->setParameter('{{ value }}', '""')
53             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
54             ->assertRaised();
55     }
56
57     public function testSucceedingExpressionAtObjectLevel()
58     {
59         $constraint = new Expression('this.data == 1');
60
61         $object = new Entity();
62         $object->data = '1';
63
64         $this->setObject($object);
65
66         $this->validator->validate($object, $constraint);
67
68         $this->assertNoViolation();
69     }
70
71     public function testFailingExpressionAtObjectLevel()
72     {
73         $constraint = new Expression(array(
74             'expression' => 'this.data == 1',
75             'message' => 'myMessage',
76         ));
77
78         $object = new Entity();
79         $object->data = '2';
80
81         $this->setObject($object);
82
83         $this->validator->validate($object, $constraint);
84
85         $this->buildViolation('myMessage')
86             ->setParameter('{{ value }}', 'object')
87             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
88             ->assertRaised();
89     }
90
91     public function testSucceedingExpressionAtObjectLevelWithToString()
92     {
93         $constraint = new Expression('this.data == 1');
94
95         $object = new ToString();
96         $object->data = '1';
97
98         $this->setObject($object);
99
100         $this->validator->validate($object, $constraint);
101
102         $this->assertNoViolation();
103     }
104
105     public function testFailingExpressionAtObjectLevelWithToString()
106     {
107         $constraint = new Expression(array(
108             'expression' => 'this.data == 1',
109             'message' => 'myMessage',
110         ));
111
112         $object = new ToString();
113         $object->data = '2';
114
115         $this->setObject($object);
116
117         $this->validator->validate($object, $constraint);
118
119         $this->buildViolation('myMessage')
120             ->setParameter('{{ value }}', 'toString')
121             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
122             ->assertRaised();
123     }
124
125     public function testSucceedingExpressionAtPropertyLevel()
126     {
127         $constraint = new Expression('value == this.data');
128
129         $object = new Entity();
130         $object->data = '1';
131
132         $this->setRoot($object);
133         $this->setPropertyPath('data');
134         $this->setProperty($object, 'data');
135
136         $this->validator->validate('1', $constraint);
137
138         $this->assertNoViolation();
139     }
140
141     public function testFailingExpressionAtPropertyLevel()
142     {
143         $constraint = new Expression(array(
144             'expression' => 'value == this.data',
145             'message' => 'myMessage',
146         ));
147
148         $object = new Entity();
149         $object->data = '1';
150
151         $this->setRoot($object);
152         $this->setPropertyPath('data');
153         $this->setProperty($object, 'data');
154
155         $this->validator->validate('2', $constraint);
156
157         $this->buildViolation('myMessage')
158             ->atPath('data')
159             ->setParameter('{{ value }}', '"2"')
160             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
161             ->assertRaised();
162     }
163
164     public function testSucceedingExpressionAtNestedPropertyLevel()
165     {
166         $constraint = new Expression('value == this.data');
167
168         $object = new Entity();
169         $object->data = '1';
170
171         $root = new Entity();
172         $root->reference = $object;
173
174         $this->setRoot($root);
175         $this->setPropertyPath('reference.data');
176         $this->setProperty($object, 'data');
177
178         $this->validator->validate('1', $constraint);
179
180         $this->assertNoViolation();
181     }
182
183     public function testFailingExpressionAtNestedPropertyLevel()
184     {
185         $constraint = new Expression(array(
186             'expression' => 'value == this.data',
187             'message' => 'myMessage',
188         ));
189
190         $object = new Entity();
191         $object->data = '1';
192
193         $root = new Entity();
194         $root->reference = $object;
195
196         $this->setRoot($root);
197         $this->setPropertyPath('reference.data');
198         $this->setProperty($object, 'data');
199
200         $this->validator->validate('2', $constraint);
201
202         $this->buildViolation('myMessage')
203             ->atPath('reference.data')
204             ->setParameter('{{ value }}', '"2"')
205             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
206             ->assertRaised();
207     }
208
209     /**
210      * When validatePropertyValue() is called with a class name
211      * https://github.com/symfony/symfony/pull/11498.
212      */
213     public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
214     {
215         $constraint = new Expression('value == "1"');
216
217         $this->setRoot('1');
218         $this->setPropertyPath('');
219         $this->setProperty(null, 'property');
220
221         $this->validator->validate('1', $constraint);
222
223         $this->assertNoViolation();
224     }
225
226     /**
227      * When validatePropertyValue() is called with a class name
228      * https://github.com/symfony/symfony/pull/11498.
229      */
230     public function testFailingExpressionAtPropertyLevelWithoutRoot()
231     {
232         $constraint = new Expression(array(
233             'expression' => 'value == "1"',
234             'message' => 'myMessage',
235         ));
236
237         $this->setRoot('2');
238         $this->setPropertyPath('');
239         $this->setProperty(null, 'property');
240
241         $this->validator->validate('2', $constraint);
242
243         $this->buildViolation('myMessage')
244             ->atPath('')
245             ->setParameter('{{ value }}', '"2"')
246             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
247             ->assertRaised();
248     }
249
250     public function testExpressionLanguageUsage()
251     {
252         $constraint = new Expression(array(
253             'expression' => 'false',
254         ));
255
256         $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
257
258         $used = false;
259
260         $expressionLanguage->method('evaluate')
261             ->will($this->returnCallback(function () use (&$used) {
262                 $used = true;
263
264                 return true;
265             }));
266
267         $validator = new ExpressionValidator(null, $expressionLanguage);
268         $validator->initialize($this->createContext());
269         $validator->validate(null, $constraint);
270
271         $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
272     }
273 }