[!!!][TASK] Remove support for non namespaced classes in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / UnitDeprecated / Validation / ValidatorResolverTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\UnitDeprecated\Validation;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException;
19
20 /**
21 * Test case
22 */
23 class ValidatorResolverTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver | \PHPUnit_Framework_MockObject_MockObject | \TYPO3\TestingFramework\Core\AccessibleObjectInterface
27 */
28 protected $validatorResolver;
29
30 protected function setUp()
31 {
32 $this->validatorResolver = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\ValidatorResolver::class, ['dummy']);
33 }
34
35 /**
36 * @test
37 */
38 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments()
39 {
40 $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['fooAction'], [], '', false);
41 $methodParameters = [];
42 $mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
43 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
44 $validatorResolver = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\ValidatorResolver::class, ['createValidator']);
45 $validatorResolver->_set('reflectionService', $mockReflectionService);
46 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
47 $this->assertSame([], $result);
48 }
49
50 /**
51 * @test
52 */
53 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod()
54 {
55 $mockObject = $this->getMockBuilder('stdClass')
56 ->setMethods(['fooMethod'])
57 ->disableOriginalConstructor()
58 ->getMock();
59 $methodParameters = [
60 'arg1' => [
61 'type' => 'string'
62 ],
63 'arg2' => [
64 'type' => 'array'
65 ]
66 ];
67 $methodTagsValues = [
68 'param' => [
69 'string $arg1',
70 'array $arg2'
71 ],
72 'validate' => [
73 '$arg1 Foo(bar = baz), Bar',
74 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
75 ]
76 ];
77 $mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
78 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
79 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
80 $mockStringValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
81 $mockArrayValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
82 $mockFooValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
83 $mockBarValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
84 $mockQuuxValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
85 $conjunction1 = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
86 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
87 $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
88 $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
89 $conjunction2 = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
90 $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
91 $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
92 $mockArguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
93 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg1', 'dummyValue'));
94 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg2', 'dummyValue'));
95 $validatorResolver = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\ValidatorResolver::class, ['createValidator']);
96 $validatorResolver->expects($this->at(0))->method('createValidator')->with(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction1));
97 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
98 $validatorResolver->expects($this->at(2))->method('createValidator')->with(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction2));
99 $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
100 $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', ['bar' => 'baz'])->will($this->returnValue($mockFooValidator));
101 $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
102 $validatorResolver->expects($this->at(6))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
103 $validatorResolver->_set('reflectionService', $mockReflectionService);
104 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
105 $this->assertEquals(['arg1' => $conjunction1, 'arg2' => $conjunction2], $result);
106 }
107
108 /**
109 * @test
110 */
111 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists()
112 {
113 $this->expectException(InvalidValidationConfigurationException::class);
114 $this->expectExceptionCode(1253172726);
115 $mockObject = $this->getMockBuilder('stdClass')
116 ->setMethods(['fooMethod'])
117 ->disableOriginalConstructor()
118 ->getMock();
119 $methodParameters = [
120 'arg1' => [
121 'type' => 'string'
122 ]
123 ];
124 $methodTagsValues = [
125 'param' => [
126 'string $arg1'
127 ],
128 'validate' => [
129 '$arg2 VENDOR\\ModelCollection\\Domain\\Model\\Model'
130 ]
131 ];
132 $mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
133 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
134 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
135 $mockStringValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
136 $mockQuuxValidator = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class);
137 $conjunction1 = $this->createMock(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class);
138 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
139 $validatorResolver = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Validation\ValidatorResolver::class, ['createValidator']);
140 $validatorResolver->expects($this->at(0))->method('createValidator')->with(\TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator::class)->will($this->returnValue($conjunction1));
141 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
142 $validatorResolver->expects($this->at(2))->method('createValidator')->with('VENDOR\\ModelCollection\\Domain\\Model\\Model')->will($this->returnValue($mockQuuxValidator));
143 $validatorResolver->_set('reflectionService', $mockReflectionService);
144 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
145 }
146
147 /**
148 * dataProvider for parseValidatorAnnotationCanParseAnnotations
149 * @return array
150 */
151 public function validatorAnnotations(): array
152 {
153 return [
154 [
155 '$var Bar',
156 [
157 'argumentName' => 'var',
158 'validators' => [
159 ['validatorName' => 'Bar', 'validatorOptions' => []]
160 ]
161 ]
162 ],
163 [
164 '$var Bar, Foo',
165 [
166 'argumentName' => 'var',
167 'validators' => [
168 ['validatorName' => 'Bar', 'validatorOptions' => []],
169 ['validatorName' => 'Foo', 'validatorOptions' => []]
170 ]
171 ]
172 ],
173 [
174 '$var Baz (Foo=Bar)',
175 [
176 'argumentName' => 'var',
177 'validators' => [
178 ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => 'Bar']]
179 ]
180 ]
181 ],
182 [
183 '$var Buzz (Foo="B=a, r", Baz=1)',
184 [
185 'argumentName' => 'var',
186 'validators' => [
187 ['validatorName' => 'Buzz', 'validatorOptions' => ['Foo' => 'B=a, r', 'Baz' => '1']]
188 ]
189 ]
190 ],
191 [
192 '$var Foo(Baz=1, Bar=Quux)',
193 [
194 'argumentName' => 'var',
195 'validators' => [
196 ['validatorName' => 'Foo', 'validatorOptions' => ['Baz' => '1', 'Bar' => 'Quux']]
197 ]
198 ]
199 ],
200 [
201 '$var Pax, Foo(Baz = \'1\', Bar = Quux)',
202 [
203 'argumentName' => 'var',
204 'validators' => [
205 ['validatorName' => 'Pax', 'validatorOptions' => []],
206 ['validatorName' => 'Foo', 'validatorOptions' => ['Baz' => '1', 'Bar' => 'Quux']]
207 ]
208 ]
209 ],
210 [
211 '$var Reg (P="[at]*(h|g)"), Quux',
212 [
213 'argumentName' => 'var',
214 'validators' => [
215 ['validatorName' => 'Reg', 'validatorOptions' => ['P' => '[at]*(h|g)']],
216 ['validatorName' => 'Quux', 'validatorOptions' => []]
217 ]
218 ]
219 ],
220 [
221 '$var Baz (Foo="B\\"ar")',
222 [
223 'argumentName' => 'var',
224 'validators' => [
225 ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => 'B"ar']]
226 ]
227 ]
228 ],
229 [
230 '$var Baz(Foo="5"), Bar(Quux="123")',
231 [
232 'argumentName' => 'var',
233 'validators' => [
234 ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => '5']],
235 ['validatorName' => 'Bar', 'validatorOptions' => ['Quux' => '123']]
236 ]
237 ]
238 ],
239 [
240 '$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \\"string\\" with *freaky* \\stuff")',
241 [
242 'argumentName' => 'var',
243 'validators' => [
244 ['validatorName' => 'Baz', 'validatorOptions' => ['Foo' => '2']],
245 ['validatorName' => 'Bar', 'validatorOptions' => ['Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff']]
246 ]
247 ]
248 ],
249 'namespaced validator class name' => [
250 'annotation' => '$var F3\TestPackage\Quux',
251 'expected' => [
252 'argumentName' => 'var',
253 'validators' => [
254 ['validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => []]
255 ]
256 ]
257 ],
258 'shorthand notation for system validator' => [
259 'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
260 'expected' => [
261 'argumentName' => 'var',
262 'validators' => [
263 ['validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => []]
264 ]
265 ]
266 ],
267 'shorthand notation for custom validator with parameter' => [
268 'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
269 'expected' => [
270 'argumentName' => 'var',
271 'validators' => [
272 ['validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => ['Foo' => '2']]
273 ]
274 ]
275 ],
276 ];
277 }
278
279 /**
280 * @test
281 * @dataProvider validatorAnnotations
282 *
283 * @param mixed $annotation
284 * @param mixed $expectedResult
285 */
286 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult)
287 {
288 $result = $this->validatorResolver->_call('parseValidatorAnnotation', $annotation);
289 static::assertEquals($result, $expectedResult);
290 }
291 }