[BUGFIX] ActionController must not register superfluous validators
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Mvc / Controller / ActionControllerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Functional\Mvc\Controller;
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\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
20 use TYPO3\CMS\Extbase\Mvc\Web\Request;
21 use TYPO3\CMS\Extbase\Mvc\Web\Response;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
23 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
24 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
25 use TYPO3\CMS\Extbase\Validation\Validator\StringValidator;
26
27 /**
28 * Test case
29 */
30 class ActionControllerTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
31 {
32 /**
33 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
34 */
35 protected $request;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Mvc\Web\Response
39 */
40 protected $response;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Tests\Functional\Mvc\Controller\Fixture\Controller\TestController
44 */
45 protected $controller;
46
47 protected function setUp()
48 {
49 parent::setUp();
50
51 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
52
53 $this->request = $objectManager->get(Request::class);
54 $this->request->setControllerVendorName('TYPO3\\CMS');
55 $this->request->setPluginName('Pi1');
56 $this->request->setControllerExtensionName('Extbase\\Tests\\Functional\\Mvc\\Controller\\Fixture');
57 $this->request->setControllerName('Test');
58 $this->request->setMethod('GET');
59 $this->request->setFormat('html');
60
61 $this->response = $objectManager->get(Response::class);
62
63 $this->controller = $objectManager->get(Fixture\Controller\TestController::class);
64 }
65
66 /**
67 * @test
68 */
69 public function modelValidatorsAreProperlyResolved()
70 {
71 // Setup
72 $this->request->setControllerActionName('foo');
73 $this->request->setArgument('fooParam', []);
74
75 // Test run
76 $this->controller->processRequest($this->request, $this->response);
77
78 // Open arguments property
79 $reflectionClass = new \ReflectionClass($this->controller);
80 $argumentsProperty = $reflectionClass->getProperty('arguments');
81 $argumentsProperty->setAccessible(true);
82
83 // Assertions
84
85 /** @var Arguments $arguments */
86 $arguments = $argumentsProperty->getValue($this->controller);
87 $argument = $arguments->getArgument('fooParam');
88
89 /** @var ConjunctionValidator $validator */
90 $conjunctionValidator = $argument->getValidator();
91 static::assertInstanceOf(ConjunctionValidator::class, $conjunctionValidator);
92
93 /** @var \SplObjectStorage $validators */
94 $validators = $conjunctionValidator->getValidators();
95 static::assertInstanceOf(\SplObjectStorage::class, $validators);
96
97 $validators->rewind();
98
99 /** @var ConjunctionValidator $subConjunctionValidator */
100 $subConjunctionValidator = $validators->current();
101 static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
102
103 /** @var \SplObjectStorage $subValidators */
104 $subValidators = $subConjunctionValidator->getValidators();
105 static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
106
107 $subValidators->rewind();
108 static::assertInstanceOf(Fixture\Domain\Validator\ModelValidator::class, $subValidators->current());
109 }
110
111 /**
112 * @test
113 */
114 public function customValidatorsAreProperlyResolved()
115 {
116 // Setup
117 $this->request->setControllerActionName('bar');
118 $this->request->setArgument('barParam', '');
119
120 // Test run
121 $this->controller->processRequest($this->request, $this->response);
122
123 // Open arguments property
124 $reflectionClass = new \ReflectionClass($this->controller);
125 $argumentsProperty = $reflectionClass->getProperty('arguments');
126 $argumentsProperty->setAccessible(true);
127
128 // Assertions
129
130 /** @var Arguments $arguments */
131 $arguments = $argumentsProperty->getValue($this->controller);
132 $argument = $arguments->getArgument('barParam');
133
134 /** @var ConjunctionValidator $validator */
135 $conjunctionValidator = $argument->getValidator();
136 static::assertInstanceOf(ConjunctionValidator::class, $conjunctionValidator);
137
138 /** @var \SplObjectStorage $validators */
139 $validators = $conjunctionValidator->getValidators();
140 static::assertInstanceOf(\SplObjectStorage::class, $validators);
141
142 $validators->rewind();
143 static::assertInstanceOf(Fixture\Validation\Validator\CustomValidator::class, $validators->current());
144
145 $validators->next();
146
147 /** @var ConjunctionValidator $subConjunctionValidator */
148 $subConjunctionValidator = $validators->current();
149 static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
150
151 /** @var \SplObjectStorage $subValidators */
152 $subValidators = $subConjunctionValidator->getValidators();
153 static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
154
155 $subValidators->rewind();
156 static::assertInstanceOf(StringValidator::class, $subValidators->current());
157 }
158
159 /**
160 * @test
161 */
162 public function extbaseValidatorsAreProperlyResolved()
163 {
164 // Setup
165 $this->request->setControllerActionName('baz');
166 $this->request->setArgument('bazParam', [ 'notEmpty' ]);
167
168 // Test run
169 $this->controller->processRequest($this->request, $this->response);
170
171 // Open arguments property
172 $reflectionClass = new \ReflectionClass($this->controller);
173 $argumentsProperty = $reflectionClass->getProperty('arguments');
174 $argumentsProperty->setAccessible(true);
175
176 // Assertions
177
178 /** @var Arguments $arguments */
179 $arguments = $argumentsProperty->getValue($this->controller);
180 $argument = $arguments->getArgument('bazParam');
181
182 /** @var ConjunctionValidator $validator */
183 $conjunctionValidator = $argument->getValidator();
184 static::assertInstanceOf(ConjunctionValidator::class, $conjunctionValidator);
185
186 /** @var \SplObjectStorage $validators */
187 $validators = $conjunctionValidator->getValidators();
188 static::assertInstanceOf(\SplObjectStorage::class, $validators);
189 static::assertCount(1, $validators);
190
191 $validators->rewind();
192 static::assertInstanceOf(NotEmptyValidator::class, $validators->current());
193 }
194 }