[BUGFIX] Set correct default value for TSFE properties
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / CollectionValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Test case
22 */
23 class CollectionValidatorTest extends UnitTestCase
24 {
25 /**
26 * @var string
27 */
28 protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class;
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
32 */
33 protected $mockValidatorResolver;
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
37 */
38 protected $validator;
39
40 /**
41 * @param array $options
42 * @param array $mockedMethods
43 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
44 */
45 protected function getValidator(array $options = [], array $mockedMethods = ['translateErrorMessage'])
46 {
47 return $this->getAccessibleMock($this->validatorClassName, $mockedMethods, [$options], '', true);
48 }
49
50 protected function setUp(): void
51 {
52 parent::setUp();
53 $this->mockValidatorResolver = $this->getAccessibleMock(
54 \TYPO3\CMS\Extbase\Validation\ValidatorResolver::class,
55 ['createValidator', 'buildBaseValidatorConjunction', 'getBaseValidatorConjunction']
56 );
57 $this->validator = $this->getValidator();
58 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
59 }
60
61 /**
62 * @test
63 */
64 public function collectionValidatorReturnsNoErrorsForANullValue()
65 {
66 $this->assertFalse($this->validator->validate(null)->hasErrors());
67 }
68
69 /**
70 * @test
71 */
72 public function collectionValidatorFailsForAValueNotBeingACollection()
73 {
74 $this->assertTrue($this->validator->validate(new \stdClass())->hasErrors());
75 }
76
77 /**
78 * @test
79 */
80 public function collectionValidatorValidatesEveryElementOfACollectionWithTheGivenElementValidator()
81 {
82 // todo: this test is rather complex, consider making it a functional test with fixtures
83
84 $this->validator->_set('options', ['elementValidator' => 'EmailAddress']);
85 $this->mockValidatorResolver->expects($this->exactly(4))
86 ->method('createValidator')
87 ->with('EmailAddress')
88 ->will($this->returnValue(
89 $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator::class)
90 ->setMethods(['translateErrorMessage'])
91 ->getMock()
92 ));
93 $this->validator->_set('validatorResolver', $this->mockValidatorResolver);
94 $arrayOfEmailAddresses = [
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->assertCount(2, $result->getFlattenedErrors());
105 }
106
107 /**
108 * @test
109 */
110 public function collectionValidatorValidatesNestedObjectStructuresWithoutEndlessLooping()
111 {
112 // todo: this test is rather complex, consider making it a functional test with fixtures
113
114 $A = new class() {
115 public $b = [];
116 public $integer = 5;
117 };
118
119 $B = new class() {
120 public $a;
121 public $c;
122 public $integer = 'Not an integer';
123 };
124
125 $A->b = [$B];
126 $B->a = $A;
127 $B->c = [$A];
128
129 // Create validators
130 $aValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class)
131 ->setMethods(['translateErrorMessage'])
132 ->setConstructorArgs([[]])
133 ->getMock();
134 $this->validator->_set('options', ['elementValidator' => 'Integer']);
135 $integerValidator = $this->getMockBuilder(\TYPO3\CMS\Extbase\Validation\Validator\IntegerValidator::class)
136 ->setMethods(['translateErrorMessage'])
137 ->setConstructorArgs([[]])
138 ->getMock();
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 {
161 // todo: this test is rather complex, consider making it a functional test with fixtures
162
163 $parentObject = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
164 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
165 $lazyObjectStorage = new \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage(
166 $parentObject,
167 'someProperty',
168 ['someNotEmptyValue']
169 );
170 // only in this test case we want to mock the isValid method
171 $validator = $this->getValidator(['elementType' => $elementType], ['isValid']);
172 $validator->expects($this->never())->method('isValid');
173 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
174 $validator->validate($lazyObjectStorage);
175 }
176
177 /**
178 * @test
179 */
180 public function collectionValidatorCallsCollectionElementValidatorWhenValidatingObjectStorages()
181 {
182 // todo: this test is rather complex, consider making it a functional test with fixtures
183
184 $entity = new \TYPO3\CMS\Extbase\Tests\Fixture\Entity('Foo');
185 $elementType = \TYPO3\CMS\Extbase\Tests\Fixture\Entity::class;
186 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
187 $objectStorage->attach($entity);
188 $aValidator = new \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator([]);
189
190 $this->mockValidatorResolver->expects($this->never())->method('createValidator');
191 $this->mockValidatorResolver->expects($this->once())
192 ->method('getBaseValidatorConjunction')
193 ->with($elementType)
194 ->will($this->returnValue($aValidator));
195
196 $this->validator->_set('options', ['elementType' => $elementType]);
197
198 $this->validator->validate($objectStorage);
199 }
200 }