[TASK] Use name-resolution instead of strings where possible: 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / GenericObjectValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * Testcase for the Generic Object Validator
26 *
27 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
28 */
29 class GenericObjectValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\AbstractValidatorTestcase {
30
31 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class;
32
33 public function setUp() {
34 parent::setUp();
35 }
36
37 /**
38 * @test
39 * @author Robert Lemke <robert@typo3.org>
40 */
41 public function validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull() {
42 $this->assertTrue($this->validator->validate('foo')->hasErrors());
43 }
44
45 /**
46 * @test
47 * @author Robert Lemke <robert@typo3.org>
48 */
49 public function validatorShouldReturnNoErrorsIfTheValueIsNull() {
50 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
51 }
52
53 /**
54 * @return array
55 */
56 public function dataProviderForValidator() {
57 $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 1);
58 $error2 = new \TYPO3\CMS\Extbase\Error\Error('error2', 2);
59 $emptyResult1 = new \TYPO3\CMS\Extbase\Error\Result();
60 $emptyResult2 = new \TYPO3\CMS\Extbase\Error\Result();
61 $resultWithError1 = new \TYPO3\CMS\Extbase\Error\Result();
62 $resultWithError1->addError($error1);
63 $resultWithError2 = new \TYPO3\CMS\Extbase\Error\Result();
64 $resultWithError2->addError($error2);
65 $classNameForObjectWithPrivateProperties = 'B' . md5(uniqid(mt_rand(), TRUE));
66 eval('class ' . $classNameForObjectWithPrivateProperties . '{ protected $foo = \'foovalue\'; protected $bar = \'barvalue\'; }');
67 $objectWithPrivateProperties = new $classNameForObjectWithPrivateProperties();
68 return array(
69 // If no errors happened, this is shown
70 array($objectWithPrivateProperties, $emptyResult1, $emptyResult2, array()),
71 // If errors on two properties happened, they are merged together.
72 array($objectWithPrivateProperties, $resultWithError1, $resultWithError2, array('foo' => array($error1), 'bar' => array($error2)))
73 );
74 }
75
76 /**
77 * @test
78 * @dataProvider dataProviderForValidator
79 * @author Robert Lemke <robert@typo3.org>
80 * @param mixed $mockObject
81 * @param mixed $validationResultForFoo
82 * @param mixed $validationResultForBar
83 * @param mixed $errors
84 */
85 public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors) {
86 $validatorForFoo = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, array('validate', 'getOptions'));
87 $validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));
88 $validatorForBar = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, array('validate', 'getOptions'));
89 $validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));
90 $this->validator->addPropertyValidator('foo', $validatorForFoo);
91 $this->validator->addPropertyValidator('bar', $validatorForBar);
92 $this->assertEquals($errors, $this->validator->validate($mockObject)->getFlattenedErrors());
93 }
94
95 /**
96 * @test
97 * @author Karsten Dambekalns <karsten@typo3.org>
98 */
99 public function validateCanHandleRecursiveTargetsWithoutEndlessLooping() {
100 $classNameA = 'B' . md5(uniqid(mt_rand(), TRUE));
101 eval('class ' . $classNameA . '{ public $b; }');
102 $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
103 eval('class ' . $classNameB . '{ public $a; }');
104 $A = new $classNameA();
105 $B = new $classNameB();
106 $A->b = $B;
107 $B->a = $A;
108
109 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
110 $bValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
111
112 $aValidator->addPropertyValidator('b', $bValidator);
113 $bValidator->addPropertyValidator('a', $aValidator);
114 $this->assertFalse($aValidator->validate($A)->hasErrors());
115 }
116
117 /**
118 * @test
119 * @author Karsten Dambekalns <karsten@typo3.org>
120 */
121 public function validateDetectsFailuresInRecursiveTargetsI() {
122 $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
123 eval('class ' . $classNameA . '{ public $b; }');
124 $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
125 eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
126 $A = new $classNameA();
127 $B = new $classNameB();
128 $A->b = $B;
129 $B->a = $A;
130 $aValidator = $this->getValidator();
131 $bValidator = $this->getValidator();
132
133 $aValidator->addPropertyValidator('b', $bValidator);
134 $bValidator->addPropertyValidator('a', $aValidator);
135 $error = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
136 $result = new \TYPO3\CMS\Extbase\Error\Result();
137 $result->addError($error);
138 $mockUuidValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, array('validate', 'getOptions'));
139 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result));
140 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
141 $this->assertSame(array('b.uuid' => array($error)), $aValidator->validate($A)->getFlattenedErrors());
142 }
143
144 /**
145 * @test
146 * @author Karsten Dambekalns <karsten@typo3.org>
147 */
148 public function validateDetectsFailuresInRecursiveTargetsII() {
149 $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
150 eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }');
151 $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
152 eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
153 $A = new $classNameA();
154 $B = new $classNameB();
155 $A->b = $B;
156 $B->a = $A;
157 $aValidator = $this->getValidator();
158 $bValidator = $this->getValidator();
159
160 $aValidator->addPropertyValidator('b', $bValidator);
161 $bValidator->addPropertyValidator('a', $aValidator);
162 $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
163 $result1 = new \TYPO3\CMS\Extbase\Error\Result();
164 $result1->addError($error1);
165 $mockUuidValidator = $this->getMock(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface::class, array('validate', 'getOptions'));
166 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result1));
167 $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
168 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
169 $this->assertSame(array('b.uuid' => array($error1), 'uuid' => array($error1)), $aValidator->validate($A)->getFlattenedErrors());
170 }
171 }