[BUGFIX] ConfigurationManager use FrontendSimulator only if neccessary
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / Web / RequestBuilderTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @var Tx_Extbase_MVC_Web_RequestBuilder
32 */
33 protected $requestBuilder;
34
35 /**
36 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
37 */
38 protected $mockConfigurationManager;
39
40 /**
41 * @var array
42 */
43 protected $configuration;
44
45 /**
46 * @var Tx_Extbase_Object_ObjectManagerInterface
47 */
48 protected $mockObjectManager;
49
50 /**
51 * @var Tx_Extbase_Service_ExtensionService
52 */
53 protected $mockExtensionService;
54
55 /**
56 * @var Tx_Extbase_MVC_Web_Request
57 */
58 protected $mockRequest;
59
60 /**
61 * @var array
62 */
63 protected $getBackup = array();
64
65 /**
66 * @var array
67 */
68 protected $postBackup = array();
69
70 /**
71 * @var array
72 */
73 protected $serverBackup = array();
74
75 public function setUp() {
76 $this->requestBuilder = $this->getAccessibleMock('Tx_Extbase_MVC_Web_RequestBuilder', array('dummy'));
77 $this->configuration = array(
78 'userFunc' => 'Tx_Extbase_Dispatcher->dispatch',
79 'pluginName' => 'Pi1',
80 'extensionName' => 'MyExtension',
81 'controller' => 'TheFirstController',
82 'action' => 'show',
83 'controllerConfiguration' => array(
84 'TheFirstController' => array(
85 'actions' => array('show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test')
86 ),
87 'TheSecondController' => array(
88 'actions' => array('show', 'index')
89 ),
90 'TheThirdController' => array(
91 'actions' => array('delete', 'create')
92 )
93 )
94 );
95 $this->mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
96 $this->mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
97 $this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
98 $this->mockExtensionService = $this->getMock('Tx_Extbase_Service_ExtensionService');
99
100 $this->getBackup = $_GET;
101 $this->postBackup = $_POST;
102 $this->serverBackup = $_SERVER;
103 }
104
105 /**
106 * @return void
107 */
108 protected function injectDependencies() {
109 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
110 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
111
112 $this->mockObjectManager->expects($this->any())->method('create')->with('Tx_Extbase_MVC_Web_Request')->will($this->returnValue($this->mockRequest));
113 $this->requestBuilder->injectObjectManager($this->mockObjectManager);
114
115 $pluginNamespace = 'tx_' . strtolower($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']);
116 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
117 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
118 }
119
120 public function tearDown() {
121 $_GET = $this->getBackup;
122 $_POST = $this->postBackup;
123 $_SERVER = $this->serverBackup;
124 }
125
126 /**
127 * @test
128 */
129 public function buildReturnsAWebRequestObject() {
130 $this->injectDependencies();
131 $request = $this->requestBuilder->build();
132 $this->assertSame($this->mockRequest, $request);
133 }
134
135 /**
136 * @test
137 */
138 public function buildSetsRequestPluginName() {
139 $this->injectDependencies();
140 $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1');
141 $this->requestBuilder->build();
142 }
143
144 /**
145 * @test
146 */
147 public function buildSetsRequestControllerExtensionName() {
148 $this->injectDependencies();
149 $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension');
150 $this->requestBuilder->build();
151 }
152
153 /**
154 * @test
155 */
156 public function buildSetsRequestControllerName() {
157 $this->injectDependencies();
158 $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
159 $this->requestBuilder->build();
160 }
161
162 /**
163 * @test
164 */
165 public function buildSetsRequestControllerActionName() {
166 $this->injectDependencies();
167 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show');
168 $this->requestBuilder->build();
169 }
170
171 /**
172 * @test
173 */
174 public function buildSetsRequestRequestUri() {
175 $this->injectDependencies();
176 $expectedRequestUri = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
177 $this->mockRequest->expects($this->once())->method('setRequestUri')->with($expectedRequestUri);
178 $this->requestBuilder->build();
179 }
180
181 /**
182 * @test
183 */
184 public function buildSetsRequestBaseUri() {
185 $this->injectDependencies();
186 $expectedBaseUri = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
187 $this->mockRequest->expects($this->once())->method('setBaseUri')->with($expectedBaseUri);
188 $this->requestBuilder->build();
189 }
190
191 /**
192 * @test
193 */
194 public function buildSetsRequestMethod() {
195 $this->injectDependencies();
196 $_SERVER['REQUEST_METHOD'] = 'SomeRequestMethod';
197 $expectedMethod = 'SomeRequestMethod';
198 $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
199 $this->requestBuilder->build();
200 }
201
202
203 /**
204 * @test
205 * @expectedException Tx_Extbase_MVC_Exception
206 */
207 public function buildThrowsExceptionIfExtensionNameIsNotConfigured() {
208 unset($this->configuration['extensionName']);
209 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
210 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
211 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager);
212 $this->requestBuilder->build();
213 }
214
215 /**
216 * @test
217 * @expectedException Tx_Extbase_MVC_Exception
218 */
219 public function buildThrowsExceptionIfPluginNameIsNotConfigured() {
220 unset($this->configuration['pluginName']);
221 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
222 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
223 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager);
224 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
225 $this->requestBuilder->build();
226 }
227
228 /**
229 * @test
230 * @expectedException Tx_Extbase_MVC_Exception
231 */
232 public function buildThrowsExceptionIfControllerConfigurationIsEmptyOrNotSet() {
233 unset($this->configuration['controllerConfiguration']);
234 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
235 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
236 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
237 $this->requestBuilder->build();
238 }
239
240 /**
241 * @test
242 * @expectedException Tx_Extbase_MVC_Exception
243 */
244 public function buildThrowsExceptionIfControllerConfigurationHasNoDefaultActionDefined() {
245 $this->configuration['controllerConfiguration']['TheFirstController'] = array();
246 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
247 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
248 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
249 $this->requestBuilder->build();
250 }
251
252 /**
253 * @test
254 */
255 public function buildSetsParametersFromGetAndPostVariables() {
256 $this->configuration['extensionName'] = 'SomeExtensionName';
257 $this->configuration['pluginName'] = 'SomePluginName';
258 $this->injectDependencies();
259
260 $_GET = array(
261 'tx_someotherextensionname_somepluginname' => array(
262 'foo' => 'bar'
263 ),
264 'tx_someextensionname_somepluginname' => array(
265 'parameter1' => 'valueGetsOverwritten',
266 'parameter2' => array(
267 'parameter3' => 'value3'
268 )
269 )
270 );
271 $_POST = array(
272 'tx_someextensionname_someotherpluginname' => array(
273 'foo' => 'bar'
274 ),
275 'tx_someextensionname_somepluginname' => array(
276 'parameter1' => 'value1',
277 'parameter2' => array(
278 'parameter4' => 'value4'
279 )
280 )
281 );
282 $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter1', 'value1');
283 $this->mockRequest->expects($this->at(9))->method('setArgument')->with('parameter2', array('parameter3' => 'value3', 'parameter4' => 'value4'));
284
285 $this->requestBuilder->build();
286 }
287
288 /**
289 * @test
290 */
291 public function buildSetsFormatFromGetAndPostVariables() {
292 $this->configuration['extensionName'] = 'SomeExtensionName';
293 $this->configuration['pluginName'] = 'SomePluginName';
294 $this->injectDependencies();
295
296 $_GET = array(
297 'tx_someextensionname_somepluginname' => array(
298 'format' => 'GET',
299 )
300 );
301 $_POST = array(
302 'tx_someextensionname_somepluginname' => array(
303 'format' => 'POST',
304 )
305 );
306 $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST');
307
308 $this->requestBuilder->build();
309 }
310
311 /**
312 * @test
313 */
314 public function buildCorrectlySetsAllowedControllerActions() {
315 $this->injectDependencies();
316 $expectedResult = array(
317 'TheFirstController' => array(
318 'show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test'
319 ),
320 'TheSecondController' => array(
321 'show', 'index'
322 ),
323 'TheThirdController' => array(
324 'delete', 'create'
325 )
326 );
327 $this->requestBuilder->build();
328 $actualResult = $this->requestBuilder->_get('allowedControllerActions');
329 $this->assertEquals($expectedResult, $actualResult);
330 }
331 }
332 ?>