Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[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';
32
33 /**
34 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
35 */
36 protected $configurationManager;
37
38 public function setUp() {
39 parent::setUp();
40
41 $this->configurationManager = $this->getMock('TYPO3\CMS\Extbase\Configuration\ConfigurationManager', array('isFeatureEnabled'), array(), '', FALSE);
42 $this->configurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(TRUE));
43 $this->validator->injectConfigurationManager($this->configurationManager);
44 }
45
46 /**
47 * @test
48 * @author Robert Lemke <robert@typo3.org>
49 */
50 public function validatorShouldReturnErrorsIfTheValueIsNoObjectAndNotNull() {
51 $this->assertTrue($this->validator->validate('foo')->hasErrors());
52 }
53
54 /**
55 * @test
56 * @author Robert Lemke <robert@typo3.org>
57 */
58 public function validatorShouldReturnNoErrorsIfTheValueIsNull() {
59 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
60 }
61
62 /**
63 * @return array
64 */
65 public function dataProviderForValidator() {
66 $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 1);
67 $error2 = new \TYPO3\CMS\Extbase\Error\Error('error2', 2);
68 $emptyResult1 = new \TYPO3\CMS\Extbase\Error\Result();
69 $emptyResult2 = new \TYPO3\CMS\Extbase\Error\Result();
70 $resultWithError1 = new \TYPO3\CMS\Extbase\Error\Result();
71 $resultWithError1->addError($error1);
72 $resultWithError2 = new \TYPO3\CMS\Extbase\Error\Result();
73 $resultWithError2->addError($error2);
74 $classNameForObjectWithPrivateProperties = $this->getUniqueId('B');
75 eval('class ' . $classNameForObjectWithPrivateProperties . '{ protected $foo = \'foovalue\'; protected $bar = \'barvalue\'; }');
76 $objectWithPrivateProperties = new $classNameForObjectWithPrivateProperties();
77 return array(
78 // If no errors happened, this is shown
79 array($objectWithPrivateProperties, $emptyResult1, $emptyResult2, array()),
80 // If errors on two properties happened, they are merged together.
81 array($objectWithPrivateProperties, $resultWithError1, $resultWithError2, array('foo' => array($error1), 'bar' => array($error2)))
82 );
83 }
84
85 /**
86 * @test
87 * @dataProvider dataProviderForValidator
88 * @author Robert Lemke <robert@typo3.org>
89 * @param mixed $mockObject
90 * @param mixed $validationResultForFoo
91 * @param mixed $validationResultForBar
92 * @param mixed $errors
93 */
94 public function validateChecksAllPropertiesForWhichAPropertyValidatorExists($mockObject, $validationResultForFoo, $validationResultForBar, $errors) {
95 $validatorForFoo = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
96 $validatorForFoo->expects($this->once())->method('validate')->with('foovalue')->will($this->returnValue($validationResultForFoo));
97 $validatorForBar = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
98 $validatorForBar->expects($this->once())->method('validate')->with('barvalue')->will($this->returnValue($validationResultForBar));
99 $this->validator->addPropertyValidator('foo', $validatorForFoo);
100 $this->validator->addPropertyValidator('bar', $validatorForBar);
101 $this->assertEquals($errors, $this->validator->validate($mockObject)->getFlattenedErrors());
102 }
103
104 /**
105 * @test
106 * @author Karsten Dambekalns <karsten@typo3.org>
107 */
108 public function validateCanHandleRecursiveTargetsWithoutEndlessLooping() {
109 $classNameA = $this->getUniqueId('B');
110 eval('class ' . $classNameA . '{ public $b; }');
111 $classNameB = $this->getUniqueId('B');
112 eval('class ' . $classNameB . '{ public $a; }');
113 $A = new $classNameA();
114 $B = new $classNameB();
115 $A->b = $B;
116 $B->a = $A;
117
118 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
119 $bValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
120
121 $aValidator->injectConfigurationManager($this->configurationManager);
122 $bValidator->injectConfigurationManager($this->configurationManager);
123
124 $aValidator->addPropertyValidator('b', $bValidator);
125 $bValidator->addPropertyValidator('a', $aValidator);
126 $this->assertFalse($aValidator->validate($A)->hasErrors());
127 }
128
129 /**
130 * @test
131 * @author Karsten Dambekalns <karsten@typo3.org>
132 */
133 public function validateDetectsFailuresInRecursiveTargetsI() {
134 $classNameA = $this->getUniqueId('A');
135 eval('class ' . $classNameA . '{ public $b; }');
136 $classNameB = $this->getUniqueId('B');
137 eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
138 $A = new $classNameA();
139 $B = new $classNameB();
140 $A->b = $B;
141 $B->a = $A;
142 $aValidator = $this->getValidator();
143 $bValidator = $this->getValidator();
144
145 $aValidator->injectConfigurationManager($this->configurationManager);
146 $bValidator->injectConfigurationManager($this->configurationManager);
147
148 $aValidator->addPropertyValidator('b', $bValidator);
149 $bValidator->addPropertyValidator('a', $aValidator);
150 $error = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
151 $result = new \TYPO3\CMS\Extbase\Error\Result();
152 $result->addError($error);
153 $mockUuidValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
154 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result));
155 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
156 $this->assertSame(array('b.uuid' => array($error)), $aValidator->validate($A)->getFlattenedErrors());
157 }
158
159 /**
160 * @test
161 * @author Karsten Dambekalns <karsten@typo3.org>
162 */
163 public function validateDetectsFailuresInRecursiveTargetsII() {
164 $classNameA = $this->getUniqueId('A');
165 eval('class ' . $classNameA . '{ public $b; public $uuid = 0xF; }');
166 $classNameB = $this->getUniqueId('B');
167 eval('class ' . $classNameB . '{ public $a; public $uuid = 0xF; }');
168 $A = new $classNameA();
169 $B = new $classNameB();
170 $A->b = $B;
171 $B->a = $A;
172 $aValidator = $this->getValidator();
173 $bValidator = $this->getValidator();
174
175 $aValidator->injectConfigurationManager($this->configurationManager);
176 $bValidator->injectConfigurationManager($this->configurationManager);
177
178 $aValidator->addPropertyValidator('b', $bValidator);
179 $bValidator->addPropertyValidator('a', $aValidator);
180 $error1 = new \TYPO3\CMS\Extbase\Error\Error('error1', 123);
181 $result1 = new \TYPO3\CMS\Extbase\Error\Result();
182 $result1->addError($error1);
183 $mockUuidValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ValidatorInterface', array('validate'));
184 $mockUuidValidator->expects($this->any())->method('validate')->with(15)->will($this->returnValue($result1));
185 $aValidator->addPropertyValidator('uuid', $mockUuidValidator);
186 $bValidator->addPropertyValidator('uuid', $mockUuidValidator);
187 $this->assertSame(array('b.uuid' => array($error1), 'uuid' => array($error1)), $aValidator->validate($A)->getFlattenedErrors());
188 }
189 }