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