[BUGFIX] Make ValidatorResolver respect namespaces
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / ValidatorResolverTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Testcase for the validator resolver
32 */
33 class ValidatorResolverTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
34
35 /**
36 * @test
37 */
38 public function resolveValidatorObjectNameReturnsFalseIfValidatorCantBeResolved() {
39 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
40 /** @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
41 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
42 $validatorResolver->_set('objectManager', $mockObjectManager);
43 $this->assertFalse($validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
44 }
45
46 /**
47 * @test
48 */
49 public function resolveValidatorObjectNameReturnsTheGivenArgumentIfAnObjectOfThatNameIsRegistered() {
50 $validatorName = uniqid('FooValidator_') . 'Validator';
51 eval('class ' . $validatorName . ' {}');
52 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
53 $this->assertSame($validatorName, $validatorResolver->_call('resolveValidatorObjectName', $validatorName));
54 }
55
56 /**
57 * @test
58 */
59 public function resolveValidatorObjectNameReturnsTheGivenArgumentIfANamespacedObjectOfThatNameIsRegistered() {
60 $namespace = 'Acme\\Bar';
61 $className = uniqid('FooValidator') . 'Validator';
62 $validatorName = $namespace . '\\' . $className;
63 eval('namespace ' . $namespace . '; ' . LF . 'class ' . $className . ' {}');
64 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
65 $this->assertSame($validatorName, $validatorResolver->_call('resolveValidatorObjectName', $validatorName));
66 }
67
68 /**
69 * @test
70 */
71 public function resolveValidatorObjectNameCanResolveShorthandNotNamespacedValidatorNames() {
72 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
73 eval('class Tx_Mypkg_Validation_Validator_MyFirstValidator {}');
74 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
75 $validatorResolver->_set('objectManager', $mockObjectManager);
76 $this->assertSame('Tx_Mypkg_Validation_Validator_MyFirstValidator', $validatorResolver->_call('resolveValidatorObjectName', 'Mypkg:MyFirst'));
77 }
78
79 /**
80 * @return array
81 */
82 public function namespacedShorthandValidatornames() {
83 return array(
84 array('TYPO3\\CMS\\Mypkg\\Validation\\Validator', 'MySecondValidator', 'TYPO3.CMS.Mypkg:MySecond'),
85 array('Acme\\Mypkg\\Validation\\Validator', 'MyThirdValidator', 'Acme.Mypkg:MyThird')
86 );
87 }
88
89 /**
90 * @test
91 * @dataProvider namespacedShorthandValidatornames
92 */
93 public function resolveValidatorObjectNameCanResolveNamespacedShorthandValidatornames($namespace, $className, $shorthandValidatorname) {
94 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
95 eval('namespace ' . $namespace . '; class ' . $className . ' {}');
96 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
97 $validatorResolver->_set('objectManager', $mockObjectManager);
98 $this->assertSame($namespace . '\\' . $className, $validatorResolver->_call('resolveValidatorObjectName', $shorthandValidatorname));
99 }
100
101 /**
102 * @test
103 */
104 public function resolveValidatorObjectNameCanResolveShortNamesOfBuiltInValidators() {
105 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
106 eval('namespace TYPO3\\CMS\\Extbase\\Validation\\Validator; class FooValidator {}');
107 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
108 $validatorResolver->_set('objectManager', $mockObjectManager);
109 $this->assertSame('TYPO3\\CMS\\Extbase\\Validation\\Validator\\FooValidator', $validatorResolver->_call('resolveValidatorObjectName', 'Foo'));
110 }
111
112 /**
113 * @test
114 */
115 public function createValidatorResolvesAndReturnsAValidatorAndPassesTheGivenOptions() {
116 $className = uniqid('Test');
117 $mockValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ObjectValidatorInterface', array('setOptions', 'canValidate', 'isPropertyValid'), array(), $className);
118 $mockValidator->expects($this->once())->method('setOptions')->with(array('foo' => 'bar'));
119 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
120 $mockObjectManager->expects($this->any())->method('get')->with($className)->will($this->returnValue($mockValidator));
121 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName'));
122 $validatorResolver->_set('objectManager', $mockObjectManager);
123 $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with($className)->will($this->returnValue($className));
124 $validator = $validatorResolver->createValidator($className, array('foo' => 'bar'));
125 $this->assertSame($mockValidator, $validator);
126 }
127
128 /**
129 * @test
130 */
131 public function createValidatorReturnsNullIfAValidatorCouldNotBeResolved() {
132 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName'), array(), '', FALSE);
133 $validatorResolver->expects($this->once())->method('resolveValidatorObjectName')->with('Foo')->will($this->returnValue(FALSE));
134 $validator = $validatorResolver->createValidator('Foo', array('foo' => 'bar'));
135 $this->assertNull($validator);
136 }
137
138 /**
139 * @test
140 */
141 public function getBaseValidatorCachesTheResultOfTheBuildBaseValidatorChainCalls() {
142 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
143 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('buildBaseValidatorConjunction'), array(), '', FALSE);
144 $validatorResolver->expects($this->once())->method('buildBaseValidatorConjunction')->with('Tx_Virtual_Foo')->will($this->returnValue($mockConjunctionValidator));
145 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
146 $this->assertSame($mockConjunctionValidator, $result, '#1');
147 $result = $validatorResolver->getBaseValidatorConjunction('Tx_Virtual_Foo');
148 $this->assertSame($mockConjunctionValidator, $result, '#2');
149 }
150
151 /**
152 * @test
153 * @author Sebastian Kurfürst <sbastian@typo3.org>
154 */
155 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments() {
156 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
157 $methodTagsValues = array();
158 $methodParameters = array();
159 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
160 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
161 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
162 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
163 $validatorResolver->injectReflectionService($mockReflectionService);
164 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
165 $this->assertSame(array(), $result);
166 }
167
168 /**
169 * @test
170 * @author Robert Lemke <robert@typo3.org>
171 * @author Bastian Waidelich <bastian@typo3.org>
172 */
173 public function buildMethodArgumentsValidatorConjunctionsBuildsAConjunctionFromValidateAnnotationsOfTheSpecifiedMethod() {
174 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
175 $methodParameters = array(
176 'arg1' => array(
177 'type' => 'string'
178 ),
179 'arg2' => array(
180 'type' => 'array'
181 )
182 );
183 $methodTagsValues = array(
184 'param' => array(
185 'string $arg1',
186 'array $arg2'
187 ),
188 'validate' => array(
189 '$arg1 Foo(bar = baz), Bar',
190 '$arg2 F3_TestPackage_Quux'
191 )
192 );
193 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
194 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
195 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
196 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
197 $mockArrayValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
198 $mockFooValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
199 $mockBarValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
200 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
201 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
202 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
203 $conjunction1->expects($this->at(1))->method('addValidator')->with($mockFooValidator);
204 $conjunction1->expects($this->at(2))->method('addValidator')->with($mockBarValidator);
205 $conjunction2 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
206 $conjunction2->expects($this->at(0))->method('addValidator')->with($mockArrayValidator);
207 $conjunction2->expects($this->at(1))->method('addValidator')->with($mockQuuxValidator);
208 $mockObjectFactory = $this->getMock('Tx_Extbase_Object_FactoryInterface');
209 $mockArguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
210 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg1', 'dummyValue'));
211 $mockArguments->addArgument(new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('arg2', 'dummyValue'));
212 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
213 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction1));
214 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
215 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction2));
216 $validatorResolver->expects($this->at(3))->method('createValidator')->with('array')->will($this->returnValue($mockArrayValidator));
217 $validatorResolver->expects($this->at(4))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockFooValidator));
218 $validatorResolver->expects($this->at(5))->method('createValidator')->with('Bar')->will($this->returnValue($mockBarValidator));
219 $validatorResolver->expects($this->at(6))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockQuuxValidator));
220 $validatorResolver->injectReflectionService($mockReflectionService);
221 $result = $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
222 $this->assertEquals(array('arg1' => $conjunction1, 'arg2' => $conjunction2), $result);
223 }
224
225 /**
226 * @test
227 * @author Sebastian Kurfürst <sbastian@typo3.org>
228 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
229 */
230 public function buildMethodArgumentsValidatorConjunctionsThrowsExceptionIfValidationAnnotationForNonExistingArgumentExists() {
231 $mockObject = $this->getMock('stdClass', array('fooMethod'), array(), '', FALSE);
232 $methodParameters = array(
233 'arg1' => array(
234 'type' => 'string'
235 )
236 );
237 $methodTagsValues = array(
238 'param' => array(
239 'string $arg1'
240 ),
241 'validate' => array(
242 '$arg2 F3_TestPackage_Quux'
243 )
244 );
245 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
246 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodTagsValues));
247 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockObject), 'fooAction')->will($this->returnValue($methodParameters));
248 $mockStringValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
249 $mockQuuxValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array(), array(), '', FALSE);
250 $conjunction1 = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
251 $conjunction1->expects($this->at(0))->method('addValidator')->with($mockStringValidator);
252 $validatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('createValidator'));
253 $validatorResolver->expects($this->at(0))->method('createValidator')->with('Conjunction')->will($this->returnValue($conjunction1));
254 $validatorResolver->expects($this->at(1))->method('createValidator')->with('string')->will($this->returnValue($mockStringValidator));
255 $validatorResolver->expects($this->at(2))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockQuuxValidator));
256 $validatorResolver->injectReflectionService($mockReflectionService);
257 $validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockObject), 'fooAction');
258 }
259
260 /**
261 * @test
262 * @author Robert Lemke <robert@typo3.org>
263 * @author Christopher Hlubek <hlubek@networkteam.com>
264 */
265 public function buildBaseValidatorConjunctionAddsValidatorsDefinedByAnnotationsInTheClassToTheReturnedConjunction() {
266 $mockObject = $this->getMock('stdClass');
267 $className = get_class($mockObject);
268 $propertyTagsValues = array(
269 'foo' => array(
270 'var' => array('string'),
271 'validate' => array(
272 'Foo(bar = baz), Bar',
273 'Baz'
274 )
275 ),
276 'bar' => array(
277 'var' => array('integer'),
278 'validate' => array(
279 'F3_TestPackage_Quux'
280 )
281 )
282 );
283 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
284 $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('foo', 'bar')));
285 $mockReflectionService->expects($this->at(1))->method('getPropertyTagsValues')->with($className, 'foo')->will($this->returnValue($propertyTagsValues['foo']));
286 $mockReflectionService->expects($this->at(2))->method('getPropertyTagsValues')->with($className, 'bar')->will($this->returnValue($propertyTagsValues['bar']));
287 $mockObjectValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator', array(), array(), '', FALSE);
288 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
289 $mockConjunctionValidator->expects($this->once())->method('addValidator')->with($mockObjectValidator);
290 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
291 $mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
292 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
293 $validatorResolver->injectReflectionService($mockReflectionService);
294 $validatorResolver->injectObjectManager($mockObjectManager);
295 $validatorResolver->expects($this->at(0))->method('createValidator')->with('GenericObject')->will($this->returnValue($mockObjectValidator));
296 $validatorResolver->expects($this->at(1))->method('createValidator')->with('Foo', array('bar' => 'baz'))->will($this->returnValue($mockObjectValidator));
297 $validatorResolver->expects($this->at(2))->method('createValidator')->with('Bar')->will($this->returnValue($mockObjectValidator));
298 $validatorResolver->expects($this->at(3))->method('createValidator')->with('Baz')->will($this->returnValue($mockObjectValidator));
299 $validatorResolver->expects($this->at(4))->method('createValidator')->with('F3_TestPackage_Quux')->will($this->returnValue($mockObjectValidator));
300 $validatorResolver->expects($this->at(5))->method('createValidator')->with($className . 'Validator')->will($this->returnValue(NULL));
301 $result = $validatorResolver->_call('buildBaseValidatorConjunction', $className);
302 $this->assertSame($mockConjunctionValidator, $result);
303 }
304
305 /**
306 * dataProvider for buildBaseValidatorConjunctionAddsValidatorFromConventionToTheReturnedConjunction
307 * @return array
308 */
309 public function modelNamesProvider() {
310 return array(
311 'no replace' => array('F3_TestPackage_Quux', 'F3_TestPackage_QuuxValidator'),
312 'replace in not namespaced class' => array('F3_TestPackage_Model_Quux', 'F3_TestPackage_Validator_QuuxValidator'),
313 'replace in namespaced class' => array('F3\TestPackage\Model\Quux', 'F3\TestPackage\Validator\QuuxValidator')
314 );
315 }
316
317 /**
318 * @test
319 * @dataProvider modelNamesProvider
320 */
321 public function buildBaseValidatorConjunctionCreatesValidatorFromClassName($modelClassName, $validatorClassName) {
322 $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
323 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
324 $mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
325 $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
326 //$validatorResolver->injectReflectionService($mockReflectionService);
327 $validatorResolver->injectObjectManager($mockObjectManager);
328 $validatorResolver->expects($this->once())->method('createValidator')->with($validatorClassName)->will($this->returnValue(NULL));
329 $result = $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName);
330 $this->assertSame($mockConjunctionValidator, $result);
331 }
332
333 /**
334 * @test
335 * @author Bastian Waidelich <bastian@typo3.org>
336 */
337 public function resolveValidatorObjectNameCallsUnifyDataType() {
338 $mockValidatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('unifyDataType'));
339 $mockValidatorResolver->expects($this->any())->method('unifyDataType')->with('someDataType');
340 $mockValidatorResolver->_call('resolveValidatorObjectName', 'someDataType');
341 }
342
343 /**
344 * @test
345 * @author Bastian Waidelich <bastian@typo3.org>
346 */
347 public function unifyDataTypeCorrectlyRenamesPhpDataTypes() {
348 $mockValidatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
349 $this->assertEquals('Integer', $mockValidatorResolver->_call('unifyDataType', 'integer'));
350 $this->assertEquals('Integer', $mockValidatorResolver->_call('unifyDataType', 'int'));
351 $this->assertEquals('String', $mockValidatorResolver->_call('unifyDataType', 'string'));
352 $this->assertEquals('Array', $mockValidatorResolver->_call('unifyDataType', 'array'));
353 $this->assertEquals('Float', $mockValidatorResolver->_call('unifyDataType', 'float'));
354 $this->assertEquals('Float', $mockValidatorResolver->_call('unifyDataType', 'double'));
355 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'boolean'));
356 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'bool'));
357 $this->assertEquals('Boolean', $mockValidatorResolver->_call('unifyDataType', 'bool'));
358 $this->assertEquals('Number', $mockValidatorResolver->_call('unifyDataType', 'number'));
359 $this->assertEquals('Number', $mockValidatorResolver->_call('unifyDataType', 'numeric'));
360 }
361
362 /**
363 * @test
364 * @author Bastian Waidelich <bastian@typo3.org>
365 */
366 public function unifyDataTypeRenamesMixedToRaw() {
367 $mockValidator = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
368 $this->assertEquals('Raw', $mockValidator->_call('unifyDataType', 'mixed'));
369 }
370
371 /**
372 * dataProvider for parseValidatorAnnotationCanParseAnnotations
373 *
374 * @author Karsten Dambekalns <karsten@typo3.org>
375 * @author Christopher Hlubek <hlubek@networkteam.com>
376 * @return array
377 */
378 public function validatorAnnotations() {
379 return array(
380 array(
381 '$var Bar',
382 array(
383 'argumentName' => 'var',
384 'validators' => array(
385 array('validatorName' => 'Bar', 'validatorOptions' => array())
386 )
387 )
388 ),
389 array(
390 '$var Bar, Foo',
391 array(
392 'argumentName' => 'var',
393 'validators' => array(
394 array('validatorName' => 'Bar', 'validatorOptions' => array()),
395 array('validatorName' => 'Foo', 'validatorOptions' => array())
396 )
397 )
398 ),
399 array(
400 '$var Baz (Foo=Bar)',
401 array(
402 'argumentName' => 'var',
403 'validators' => array(
404 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'Bar'))
405 )
406 )
407 ),
408 array(
409 '$var Buzz (Foo="B=a, r", Baz=1)',
410 array(
411 'argumentName' => 'var',
412 'validators' => array(
413 array('validatorName' => 'Buzz', 'validatorOptions' => array('Foo' => 'B=a, r', 'Baz' => '1'))
414 )
415 )
416 ),
417 array(
418 '$var Foo(Baz=1, Bar=Quux)',
419 array(
420 'argumentName' => 'var',
421 'validators' => array(
422 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
423 )
424 )
425 ),
426 array(
427 '$var Pax, Foo(Baz = \'1\', Bar = Quux)',
428 array(
429 'argumentName' => 'var',
430 'validators' => array(
431 array('validatorName' => 'Pax', 'validatorOptions' => array()),
432 array('validatorName' => 'Foo', 'validatorOptions' => array('Baz' => '1', 'Bar' => 'Quux'))
433 )
434 )
435 ),
436 array(
437 '$var Reg (P="[at]*(h|g)"), Quux',
438 array(
439 'argumentName' => 'var',
440 'validators' => array(
441 array('validatorName' => 'Reg', 'validatorOptions' => array('P' => '[at]*(h|g)')),
442 array('validatorName' => 'Quux', 'validatorOptions' => array())
443 )
444 )
445 ),
446 array(
447 '$var Baz (Foo="B\\"ar")',
448 array(
449 'argumentName' => 'var',
450 'validators' => array(
451 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => 'B"ar'))
452 )
453 )
454 ),
455 array(
456 '$var F3_TestPackage_Quux',
457 array(
458 'argumentName' => 'var',
459 'validators' => array(
460 array('validatorName' => 'F3_TestPackage_Quux', 'validatorOptions' => array())
461 )
462 )
463 ),
464 array(
465 '$var Baz(Foo="5"), Bar(Quux="123")',
466 array(
467 'argumentName' => 'var',
468 'validators' => array(
469 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '5')),
470 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123'))
471 )
472 )
473 ),
474 array(
475 '$var Baz(Foo="2"), Bar(Quux=123, Pax="a weird \\"string\\" with *freaky* \\stuff")',
476 array(
477 'argumentName' => 'var',
478 'validators' => array(
479 array('validatorName' => 'Baz', 'validatorOptions' => array('Foo' => '2')),
480 array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff'))
481 )
482 )
483 ),
484 'namespaced validator class name' => array(
485 'annotation' => '$var F3\TestPackage\Quux',
486 'expected' => array(
487 'argumentName' => 'var',
488 'validators' => array(
489 array('validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => array())
490 )
491 )
492 ),
493 'shorthand notation for system validator' => array(
494 'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
495 'expected' => array(
496 'argumentName' => 'var',
497 'validators' => array(
498 array('validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => array())
499 )
500 )
501 ),
502 'shorthand notation for custom validator with parameter' => array(
503 'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
504 'expected' => array(
505 'argumentName' => 'var',
506 'validators' => array(
507 array('validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => array('Foo' => '2'))
508 )
509 )
510 ),
511 );
512 }
513
514 /**
515 * @test
516 * @dataProvider validatorAnnotations
517 * @author Karsten Dambekalns <karsten@typo3.org>
518 * @param mixed $annotation
519 * @param mixed $expectedResult
520 */
521 public function parseValidatorAnnotationCanParseAnnotations($annotation, $expectedResult) {
522 $mockValidatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('dummy'));
523 $result = $mockValidatorResolver->_call('parseValidatorAnnotation', $annotation);
524 $this->assertEquals($result, $expectedResult);
525 }
526
527 }
528
529
530 ?>