18839e08d5fb4421480ac0db4d70d4ef02adf0d0
[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(StringValidator::class, $validators->current());
144
145 $validators->next();
146 static::assertInstanceOf(Fixture\Validation\Validator\CustomValidator::class, $validators->current());
147
148 $validators->next();
149
150 /** @var ConjunctionValidator $subConjunctionValidator */
151 $subConjunctionValidator = $validators->current();
152 static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
153
154 // todo: It doesn't make sense that there is another conjunction
155 // todo: valididator with a StringValidator attached. We need to
156 // todo: find out what causes that and how to fix this.
157
158 /** @var \SplObjectStorage $subValidators */
159 $subValidators = $subConjunctionValidator->getValidators();
160 static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
161
162 $subValidators->rewind();
163 static::assertInstanceOf(StringValidator::class, $subValidators->current());
164 }
165
166 /**
167 * @test
168 */
169 public function extbaseValidatorsAreProperlyResolved()
170 {
171 // Setup
172 $this->request->setControllerActionName('baz');
173 $this->request->setArgument('bazParam', [ 'notEmpty' ]);
174
175 // Test run
176 $this->controller->processRequest($this->request, $this->response);
177
178 // Open arguments property
179 $reflectionClass = new \ReflectionClass($this->controller);
180 $argumentsProperty = $reflectionClass->getProperty('arguments');
181 $argumentsProperty->setAccessible(true);
182
183 // Assertions
184
185 /** @var Arguments $arguments */
186 $arguments = $argumentsProperty->getValue($this->controller);
187 $argument = $arguments->getArgument('bazParam');
188
189 /** @var ConjunctionValidator $validator */
190 $conjunctionValidator = $argument->getValidator();
191 static::assertInstanceOf(ConjunctionValidator::class, $conjunctionValidator);
192
193 /** @var \SplObjectStorage $validators */
194 $validators = $conjunctionValidator->getValidators();
195 static::assertInstanceOf(\SplObjectStorage::class, $validators);
196
197 $validators->rewind();
198 static::assertInstanceOf(NotEmptyValidator::class, $validators->current());
199
200 $validators->next();
201
202 /** @var ConjunctionValidator $subConjunctionValidator */
203 $subConjunctionValidator = $validators->current();
204 static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
205
206 /** @var \SplObjectStorage $subValidators */
207 $subValidators = $subConjunctionValidator->getValidators();
208 static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
209 static::assertEmpty($subValidators);
210 }
211 }