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