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