[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Configuration / BackendConfigurationManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 class BackendConfigurationManagerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
34 */
35 protected $typo3DbBackup;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
39 */
40 protected $backendConfigurationManager;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Service\TypoScriptService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
44 */
45 protected $mockTypoScriptService;
46
47 /**
48 * Sets up this testcase
49 */
50 public function setUp() {
51 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
52 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array());
53 $this->backendConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('getTypoScriptSetup'));
54 $this->mockTypoScriptService = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
55 $this->backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService);
56 }
57
58 /**
59 * Tears down this testcase
60 */
61 public function tearDown() {
62 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
63 }
64
65 /**
66 * @test
67 */
68 public function getCurrentPageIdReturnsPageIdFromGet() {
69 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('id' => 123));
70 $expectedResult = 123;
71 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
72 $this->assertEquals($expectedResult, $actualResult);
73 }
74
75 /**
76 * @test
77 */
78 public function getCurrentPageIdReturnsPageIdFromPost() {
79 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('id' => 123));
80 $_POST['id'] = 321;
81 $expectedResult = 321;
82 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
83 $this->assertEquals($expectedResult, $actualResult);
84 }
85
86 /**
87 * @test
88 */
89 public function getCurrentPageIdReturnsPidFromFirstRootTemplateIfIdIsNotSetAndNoRootPageWasFound() {
90 $GLOBALS['TYPO3_DB']->expects($this->at(0))->method('exec_SELECTgetRows')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', '', '1')->will($this->returnValue(array()));
91 $GLOBALS['TYPO3_DB']->expects($this->at(1))->method('exec_SELECTgetRows')->with('pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', '', '1')->will($this->returnValue(array(
92 array('pid' => 123)
93 )));
94 $expectedResult = 123;
95 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
96 $this->assertEquals($expectedResult, $actualResult);
97 }
98
99 /**
100 * @test
101 */
102 public function getCurrentPageIdReturnsUidFromFirstRootPageIfIdIsNotSet() {
103 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', '', '1')->will($this->returnValue(array(
104 array('uid' => 321)
105 )));
106 $expectedResult = 321;
107 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
108 $this->assertEquals($expectedResult, $actualResult);
109 }
110
111 /**
112 * @test
113 */
114 public function getCurrentPageIdReturnsDefaultStoragePidIfIdIsNotSetNoRootTemplateAndRootPageWasFound() {
115 $GLOBALS['TYPO3_DB']->expects($this->at(0))->method('exec_SELECTgetRows')->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', '', '1')->will($this->returnValue(array()));
116 $GLOBALS['TYPO3_DB']->expects($this->at(1))->method('exec_SELECTgetRows')->with('pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', '', '1')->will($this->returnValue(array()));
117 $expectedResult = \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager::DEFAULT_BACKEND_STORAGE_PID;
118 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
119 $this->assertEquals($expectedResult, $actualResult);
120 }
121
122 /**
123 * @test
124 */
125 public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
126 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue(array('foo' => 'bar')));
127 $expectedResult = array();
128 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
129 $this->assertEquals($expectedResult, $actualResult);
130 }
131
132 /**
133 * @test
134 */
135 public function getPluginConfigurationReturnsExtensionConfiguration() {
136 $testSettings = array(
137 'settings.' => array(
138 'foo' => 'bar'
139 )
140 );
141 $testSettingsConverted = array(
142 'settings' => array(
143 'foo' => 'bar'
144 )
145 );
146 $testSetup = array(
147 'module.' => array(
148 'tx_someextensionname.' => $testSettings
149 )
150 );
151 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
152 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
153 $expectedResult = array(
154 'settings' => array(
155 'foo' => 'bar'
156 )
157 );
158 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName');
159 $this->assertEquals($expectedResult, $actualResult);
160 }
161
162 /**
163 * @test
164 */
165 public function getPluginConfigurationReturnsPluginConfiguration() {
166 $testSettings = array(
167 'settings.' => array(
168 'foo' => 'bar'
169 )
170 );
171 $testSettingsConverted = array(
172 'settings' => array(
173 'foo' => 'bar'
174 )
175 );
176 $testSetup = array(
177 'module.' => array(
178 'tx_someextensionname_somepluginname.' => $testSettings
179 )
180 );
181 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
182 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
183 $expectedResult = array(
184 'settings' => array(
185 'foo' => 'bar'
186 )
187 );
188 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
189 $this->assertEquals($expectedResult, $actualResult);
190 }
191
192 /**
193 * @test
194 */
195 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
196 $testExtensionSettings = array(
197 'settings.' => array(
198 'foo' => 'bar',
199 'some.' => array(
200 'nested' => 'value'
201 )
202 )
203 );
204 $testExtensionSettingsConverted = array(
205 'settings' => array(
206 'foo' => 'bar',
207 'some' => array(
208 'nested' => 'value'
209 )
210 )
211 );
212 $testPluginSettings = array(
213 'settings.' => array(
214 'some.' => array(
215 'nested' => 'valueOverridde',
216 'new' => 'value'
217 )
218 )
219 );
220 $testPluginSettingsConverted = array(
221 'settings' => array(
222 'some' => array(
223 'nested' => 'valueOverridde',
224 'new' => 'value'
225 )
226 )
227 );
228 $testSetup = array(
229 'module.' => array(
230 'tx_someextensionname.' => $testExtensionSettings,
231 'tx_someextensionname_somepluginname.' => $testPluginSettings
232 )
233 );
234 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
235 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
236 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
237 $expectedResult = array(
238 'settings' => array(
239 'foo' => 'bar',
240 'some' => array(
241 'nested' => 'valueOverridde',
242 'new' => 'value'
243 )
244 )
245 );
246 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
247 $this->assertEquals($expectedResult, $actualResult);
248 }
249
250 /**
251 * @test
252 */
253 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
254 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
255 $expectedResult = array();
256 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
257 $this->assertEquals($expectedResult, $actualResult);
258 }
259
260 /**
261 * @test
262 */
263 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
264 $testSwitchableControllerActions = array(
265 'Controller1' => array(
266 'actions' => array(
267 'action1',
268 'action2'
269 ),
270 'nonCacheableActions' => array(
271 'action1'
272 )
273 ),
274 'Controller2' => array(
275 'actions' => array(
276 'action3',
277 'action4'
278 )
279 )
280 );
281 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['modules']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
282 $expectedResult = $testSwitchableControllerActions;
283 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
284 $this->assertEquals($expectedResult, $actualResult);
285 }
286
287 /**
288 * @test
289 */
290 public function getContextSpecificFrameworkConfigurationReturnsUnmodifiedFrameworkConfigurationIfRequestHandlersAreConfigured() {
291 $frameworkConfiguration = array(
292 'pluginName' => 'Pi1',
293 'extensionName' => 'SomeExtension',
294 'foo' => array(
295 'bar' => array(
296 'baz' => 'Foo'
297 )
298 ),
299 'mvc' => array(
300 'requestHandlers' => array(
301 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'SomeRequestHandler'
302 )
303 )
304 );
305 $expectedResult = $frameworkConfiguration;
306 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
307 $this->assertEquals($expectedResult, $actualResult);
308 }
309
310 /**
311 * @test
312 */
313 public function getContextSpecificFrameworkConfigurationSetsDefaultRequestHandlersIfRequestHandlersAreNotConfigured() {
314 $frameworkConfiguration = array(
315 'pluginName' => 'Pi1',
316 'extensionName' => 'SomeExtension',
317 'foo' => array(
318 'bar' => array(
319 'baz' => 'Foo'
320 )
321 )
322 );
323 $expectedResult = array(
324 'pluginName' => 'Pi1',
325 'extensionName' => 'SomeExtension',
326 'foo' => array(
327 'bar' => array(
328 'baz' => 'Foo'
329 )
330 ),
331 'mvc' => array(
332 'requestHandlers' => array(
333 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler',
334 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler'
335 )
336 )
337 );
338 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
339 $this->assertEquals($expectedResult, $actualResult);
340 }
341
342 /**
343 * @test
344 */
345 public function storagePidsAreExtendedIfRecursiveSearchIsConfigured() {
346 $storagePid = '1,2,3';
347 $recursive = 99;
348 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager */
349 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
350 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
351 $queryGenerator->expects($this->any())
352 ->method('getTreeList')
353 ->will($this->onConsecutiveCalls('1,4', '2', '3,5,6'));
354 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
355
356 $expectedResult = '1,4,2,3,5,6';
357 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
358 $this->assertEquals($expectedResult, $actualResult);
359 }
360
361 /**
362 * @test
363 */
364 public function storagePidsAreNotExtendedIfRecursiveSearchIsNotConfigured() {
365 $storagePid = '1,2,3';
366
367 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
368
369 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
370 $queryGenerator->expects($this->never())->method('getTreeList');
371 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
372
373 $expectedResult = '1,2,3';
374 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid);
375 $this->assertEquals($expectedResult, $actualResult);
376 }
377
378 /**
379 * @test
380 */
381 public function storagePidsAreNotExtendedIfRecursiveSearchIsConfiguredForZeroLevels() {
382 $storagePid = '1,2,3';
383 $recursive = 0;
384
385 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
386
387 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
388 $queryGenerator->expects($this->never())->method('getTreeList');
389 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
390
391 $expectedResult = '1,2,3';
392 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
393 $this->assertEquals($expectedResult, $actualResult);
394 }
395 }
396
397 ?>