[!!!][TASK] Remove support for non namespaced classes in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / RequestTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException;
17 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException;
18
19 /**
20 * Test case
21 */
22 class RequestTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function aSingleArgumentCanBeSetWithSetArgumentAndRetrievedWithGetArgument()
28 {
29 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
30 $request->setArgument('someArgumentName', 'theValue');
31 $this->assertEquals('theValue', $request->getArgument('someArgumentName'));
32 }
33
34 /**
35 * @test
36 */
37 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsNoString()
38 {
39 $this->expectException(InvalidArgumentNameException::class);
40 $this->expectExceptionCode(1210858767);
41 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
42 $request->setArgument(123, 'theValue');
43 }
44
45 /**
46 * @test
47 */
48 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsAnEmptyString()
49 {
50 $this->expectException(InvalidArgumentNameException::class);
51 $this->expectExceptionCode(1210858767);
52 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
53 $request->setArgument('', 'theValue');
54 }
55
56 /**
57 * @test
58 */
59 public function setArgumentThrowsExceptionIfTheGivenArgumentValueIsAnObject()
60 {
61 $this->expectException(InvalidArgumentTypeException::class);
62 $this->expectExceptionCode(1210858767);
63 $this->markTestSkipped('Differing behavior from TYPO3.Flow because of backwards compatibility reasons.');
64 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
65 $request->setArgument('theKey', new \stdClass());
66 }
67
68 /**
69 * @test
70 */
71 public function setArgumentsOverridesAllExistingArguments()
72 {
73 $arguments = ['key1' => 'value1', 'key2' => 'value2'];
74 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
75 $request->setArgument('someKey', 'shouldBeOverridden');
76 $request->setArguments($arguments);
77 $actualResult = $request->getArguments();
78 $this->assertEquals($arguments, $actualResult);
79 }
80
81 /**
82 * @test
83 */
84 public function setArgumentsCallsSetArgumentForEveryArrayEntry()
85 {
86 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
87 ->setMethods(['setArgument'])
88 ->getMock();
89 $request->expects($this->at(0))->method('setArgument')->with('key1', 'value1');
90 $request->expects($this->at(1))->method('setArgument')->with('key2', 'value2');
91 $request->setArguments(['key1' => 'value1', 'key2' => 'value2']);
92 }
93
94 /**
95 * @test
96 */
97 public function setArgumentShouldSetControllerExtensionNameIfPackageKeyIsGiven()
98 {
99 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
100 ->setMethods(['setControllerExtensionName'])
101 ->getMock();
102 $request->expects($this->any())->method('setControllerExtensionName')->with('MyExtension');
103 $request->setArgument('@extension', 'MyExtension');
104 $this->assertFalse($request->hasArgument('@extension'));
105 }
106
107 /**
108 * @test
109 */
110 public function setArgumentShouldSetControllerSubpackageKeyIfSubpackageKeyIsGiven()
111 {
112 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
113 ->setMethods(['setControllerSubpackageKey'])
114 ->getMock();
115 $request->expects($this->any())->method('setControllerSubpackageKey')->with('MySubPackage');
116 $request->setArgument('@subpackage', 'MySubPackage');
117 $this->assertFalse($request->hasArgument('@subpackage'));
118 }
119
120 /**
121 * @test
122 */
123 public function setArgumentShouldSetControllerNameIfControllerIsGiven()
124 {
125 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
126 ->setMethods(['setControllerName'])
127 ->getMock();
128 $request->expects($this->any())->method('setControllerName')->with('MyController');
129 $request->setArgument('@controller', 'MyController');
130 $this->assertFalse($request->hasArgument('@controller'));
131 }
132
133 /**
134 * @test
135 */
136 public function setArgumentShouldSetControllerActionNameIfActionIsGiven()
137 {
138 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
139 ->setMethods(['setControllerActionName'])
140 ->getMock();
141 $request->expects($this->any())->method('setControllerActionName')->with('foo');
142 $request->setArgument('@action', 'foo');
143 $this->assertFalse($request->hasArgument('@action'));
144 }
145
146 /**
147 * @test
148 */
149 public function setArgumentShouldSetFormatIfFormatIsGiven()
150 {
151 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
152 ->setMethods(['setFormat'])
153 ->getMock();
154 $request->expects($this->any())->method('setFormat')->with('txt');
155 $request->setArgument('@format', 'txt');
156 $this->assertFalse($request->hasArgument('@format'));
157 }
158
159 /**
160 * @test
161 */
162 public function setArgumentShouldSetVendorIfVendorIsGiven()
163 {
164 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
165 ->setMethods(['setFormat', 'setVendor'])
166 ->getMock();
167 $request->expects($this->any())->method('setVendor')->with('VENDOR');
168 $request->setArgument('@vendor', 'VENDOR');
169 $this->assertFalse($request->hasArgument('@vendor'));
170 }
171
172 /**
173 * @test
174 */
175 public function internalArgumentsShouldNotBeReturnedAsNormalArgument()
176 {
177 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
178 $request->setArgument('__referrer', 'foo');
179 $this->assertFalse($request->hasArgument('__referrer'));
180 }
181
182 /**
183 * @test
184 */
185 public function internalArgumentsShouldBeStoredAsInternalArguments()
186 {
187 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
188 $request->setArgument('__referrer', 'foo');
189 $this->assertSame('foo', $request->getInternalArgument('__referrer'));
190 }
191
192 /**
193 * @test
194 */
195 public function hasInternalArgumentShouldReturnNullIfArgumentNotFound()
196 {
197 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
198 $this->assertNull($request->getInternalArgument('__nonExistingInternalArgument'));
199 }
200
201 /**
202 * @test
203 */
204 public function setArgumentAcceptsObjectIfArgumentIsInternal()
205 {
206 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
207 $object = new \stdClass();
208 $request->setArgument('__theKey', $object);
209 $this->assertSame($object, $request->getInternalArgument('__theKey'));
210 }
211
212 /**
213 * @test
214 */
215 public function multipleArgumentsCanBeSetWithSetArgumentsAndRetrievedWithGetArguments()
216 {
217 $arguments = [
218 'firstArgument' => 'firstValue',
219 'dænishÅrgument' => 'görman välju',
220 '3a' => '3v'
221 ];
222 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
223 $request->setArguments($arguments);
224 $this->assertEquals($arguments, $request->getArguments());
225 }
226
227 /**
228 * @test
229 */
230 public function hasArgumentTellsIfAnArgumentExists()
231 {
232 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
233 $request->setArgument('existingArgument', 'theValue');
234 $this->assertTrue($request->hasArgument('existingArgument'));
235 $this->assertFalse($request->hasArgument('notExistingArgument'));
236 }
237
238 /**
239 * @test
240 */
241 public function theActionNameCanBeSetAndRetrieved()
242 {
243 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
244 ->setMethods(['getControllerObjectName'])
245 ->disableOriginalConstructor()
246 ->getMock();
247 $request->expects($this->once())->method('getControllerObjectName')->will($this->returnValue(''));
248 $request->setControllerActionName('theAction');
249 $this->assertEquals('theAction', $request->getControllerActionName());
250 }
251
252 /**
253 * @test
254 */
255 public function theRepresentationFormatCanBeSetAndRetrieved()
256 {
257 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
258 $request->setFormat('html');
259 $this->assertEquals('html', $request->getFormat());
260 }
261
262 /**
263 * @test
264 */
265 public function theRepresentationFormatIsAutomaticallyLowercased()
266 {
267 $this->markTestSkipped('Different behavior from TYPO3.Flow because of backwards compatibility.');
268 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
269 $request->setFormat('hTmL');
270 $this->assertEquals('html', $request->getFormat());
271 }
272
273 /**
274 * @test
275 */
276 public function aFlagCanBeSetIfTheRequestNeedsToBeDispatchedAgain()
277 {
278 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
279 $this->assertFalse($request->isDispatched());
280 $request->setDispatched(true);
281 $this->assertTrue($request->isDispatched());
282 }
283
284 /**
285 * DataProvider for explodeObjectControllerName
286 *
287 * @return array
288 */
289 public function controllerArgumentsAndExpectedObjectName()
290 {
291 return [
292 'Vendor TYPO3\CMS, extension, controller given' => [
293 [
294 'vendorName' => 'TYPO3\\CMS',
295 'extensionName' => 'Ext',
296 'subpackageKey' => '',
297 'controllerName' => 'Foo',
298 ],
299 'TYPO3\\CMS\\Ext\\Controller\\FooController',
300 ],
301 'Vendor TYPO3\CMS, extension, subpackage, controlle given' => [
302 [
303 'vendorName' => 'TYPO3\\CMS',
304 'extensionName' => 'Fluid',
305 'subpackageKey' => 'ViewHelpers\\Widget',
306 'controllerName' => 'Paginate',
307 ],
308 \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
309 ],
310 'Vendor VENDOR, extension, controller given' => [
311 [
312 'vendorName' => 'VENDOR',
313 'extensionName' => 'Ext',
314 'subpackageKey' => '',
315 'controllerName' => 'Foo',
316 ],
317 'VENDOR\\Ext\\Controller\\FooController',
318 ],
319 'Vendor VENDOR, extension subpackage, controller given' => [
320 [
321 'vendorName' => 'VENDOR',
322 'extensionName' => 'Ext',
323 'subpackageKey' => 'ViewHelpers\\Widget',
324 'controllerName' => 'Foo',
325 ],
326 'VENDOR\\Ext\\ViewHelpers\\Widget\\Controller\\FooController',
327 ],
328 ];
329 }
330
331 /**
332 * @dataProvider controllerArgumentsAndExpectedObjectName
333 *
334 * @param array $controllerArguments
335 * @param string $controllerObjectName
336 * @test
337 */
338 public function getControllerObjectNameResolvesControllerObjectNameCorrectly($controllerArguments, $controllerObjectName)
339 {
340 /** @var $request \TYPO3\CMS\Extbase\Mvc\Request */
341 $request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Request::class, ['dummy']);
342 $request->_set('controllerVendorName', $controllerArguments['vendorName']);
343 $request->_set('controllerExtensionName', $controllerArguments['extensionName']);
344 $request->_set('controllerSubpackageKey', $controllerArguments['subpackageKey']);
345 $request->_set('controllerName', $controllerArguments['controllerName']);
346
347 $this->assertEquals($controllerObjectName, $request->getControllerObjectName());
348 }
349
350 /**
351 * @dataProvider controllerArgumentsAndExpectedObjectName
352 *
353 * @param array $controllerArguments
354 * @param string $controllerObjectName
355 * @test
356 */
357 public function setControllerObjectNameResolvesControllerObjectNameArgumentsCorrectly($controllerArguments, $controllerObjectName)
358 {
359 /** @var $request \TYPO3\CMS\Extbase\Mvc\Request */
360 $request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Request::class, ['dummy']);
361 $request->setControllerObjectName($controllerObjectName);
362
363 $actualControllerArguments = [
364 'vendorName' => $request->_get('controllerVendorName'),
365 'extensionName' => $request->_get('controllerExtensionName'),
366 'subpackageKey' => $request->_get('controllerSubpackageKey'),
367 'controllerName' => $request->_get('controllerName'),
368 ];
369
370 $this->assertSame($controllerArguments, $actualControllerArguments);
371 }
372 }