[BUGFIX] Skip validation of uninitialized collections
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / CollectionValidatorTest.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 Conjunction Validator
26 *
27 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
28 */
29 class CollectionValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var string
33 */
34 protected $validatorClassName = 'TYPO3\\CMS\\Extbase\\Validation\\Validator\\CollectionValidator';
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
38 */
39 protected $mockValidatorResolver;
40
41 /**
42 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
43 */
44 protected $configurationManager;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
48 */
49 protected $validator;
50
51 /**
52 * @param array $options
53 * @param array $mockedMethods
54 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
55 */
56 protected function getValidator(array $options = array(), array $mockedMethods = array('dummy')) {
57 return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, array($options), '', TRUE);
58 }
59
60 /**
61 * @return void
62 */
63 public function setUp() {
64 $this->mockValidatorResolver = $this->getAccessibleMock(
65 'TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver',
66 array('createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction')
67 );
68 $this->validator = $this->getValidator();
69 $this->configurationManager = $this->getMock(
70 'TYPO3\CMS\Extbase\Configuration\ConfigurationManager',
71 array('isFeatureEnabled'),
72 array(),
73 '',
74 FALSE
75 );
76 $this->configurationManager->expects($this->any())
77 ->method('isFeatureEnabled')
78 ->with('rewrittenPropertyMapper')
79 ->will($this->returnValue(TRUE));
80 $this->validator->injectConfigurationManager($this->configurationManager);
81 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
82 }
83
84 /**
85 * @test
86 */
87 public function collectionValidatorReturnsNoErrorsForANullValue() {
88 $this->assertFalse($this->validator->validate(NULL)->hasErrors());
89 }
90
91 /**
92 * @test
93 */
94 public function collectionValidatorFailsForAValueNotBeingACollection() {
95 $this->assertTrue($this->validator->validate(new \StdClass())->hasErrors());
96 }
97
98 /**
99 * @test
100 */
101 public function collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator() {
102 $this->validator->_set('options', array('elementValidator' => 'EmailAddress'));
103 $this->mockValidatorResolver->expects($this->exactly(4))
104 ->method('createValidator')
105 ->with('EmailAddress')
106 ->will($this->returnValue(new \TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator()));
107 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
108 $arrayOfEmailAddresses = array(
109 'foo@bar.de',
110 'not a valid address',
111 'dummy@typo3.org',
112 'also not valid'
113 );
114
115 $result = $this->validator->validate($arrayOfEmailAddresses);
116
117 $this->assertTrue($result->hasErrors());
118 $this->assertSame(2, count($result->getFlattenedErrors()));
119 }
120
121 /**
122 * @test
123 */
124 public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping() {
125 $classNameA = 'A' . md5(uniqid(mt_rand(), TRUE));
126 eval('class ' . $classNameA . '{ public $b = array(); public $integer = 5; }');
127 $classNameB = 'B' . md5(uniqid(mt_rand(), TRUE));
128 eval('class ' . $classNameB . '{ public $a; public $c; public $integer = "Not an integer"; }');
129 $A = new $classNameA();
130 $B = new $classNameB();
131 $A->b = array($B);
132 $B->a = $A;
133 $B->c = array($A);
134
135 $this->mockValidatorResolver->expects($this->any())
136 ->method('createValidator')
137 ->with('Integer')
138 ->will($this->returnValue(new \TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator()));
139 $this->mockValidatorResolver->expects($this->any())
140 ->method('buildBaseValidatorConjunction')
141 ->will($this->returnValue(new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator()));
142
143 // Create validators
144 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
145 $aValidator->injectConfigurationManager($this->configurationManager);
146 $this->validator->_set('options', array('elementValidator' => 'Integer'));
147 $integerValidator = new \TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator(array());
148
149 // Add validators to properties
150 $aValidator->addPropertyValidator('b', $this->validator);
151 $aValidator->addPropertyValidator('integer', $integerValidator);
152
153 $result = $aValidator->validate($A)->getFlattenedErrors();
154 $this->assertEquals('The given subject was not a valid integer', $result['b.0'][0]->getMessage());
155 }
156
157 /**
158 * @test
159 */
160 public function collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages() {
161 $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
162 $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
163 $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
164 $parentObject,
165 'someProperty',
166 array('someNotEmptyValue')
167 );
168 \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($lazyObjectStorage, 'isInitialized', FALSE, TRUE);
169 // only in this test case we want to mock the isValid method
170 $validator = $this->getValidator(array('elementType' => $elementType), array('isValid'));
171 $validator->expects($this->never())->method('isValid');
172 $validator->injectConfigurationManager($this->configurationManager);
173 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
174 $validator->validate($lazyObjectStorage);
175 }
176
177 /**
178 * @test
179 */
180 public function collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages() {
181 $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
182 $elementType = '\TYPO3\CMS\Extbase\Tests\Fixture\Entity';
183 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
184 $objectStorage->attach($entity);
185 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator(array());
186 $aValidator->injectConfigurationManager($this->configurationManager);
187
188 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
189 $this->mockValidatorResolver->expects($this->once())
190 ->method('getBaseValidatorConjunction')
191 ->with($elementType)
192 ->will($this->returnValue($aValidator));
193
194 $this->validator->_set('options', array('elementType' => $elementType));
195
196 $this->validator->validate($objectStorage);
197 }
198
199 }