[BUGFIX] Assume pid 0 for global backend context
[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 getCurrentPageIdReturnsZeroIfIdIsNotSet() {
79 $expectedResult = 0;
80 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
81 $this->assertEquals($expectedResult, $actualResult);
82 }
83
84 /**
85 * @test
86 */
87 public function getPluginConfigurationReturnsPluginConfiguration() {
88 $testSettings = array(
89 'settings.' => array(
90 'foo' => 'bar'
91 )
92 );
93 $testSettingsConverted = array(
94 'settings' => array(
95 'foo' => 'bar'
96 )
97 );
98 $testSetup = array(
99 'module.' => array(
100 'tx_someextensionname_somepluginname.' => $testSettings
101 )
102 );
103 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
104 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
105 $expectedResult = array(
106 'settings' => array(
107 'foo' => 'bar'
108 )
109 );
110 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
111 $this->assertEquals($expectedResult, $actualResult);
112 }
113
114 /**
115 * @test
116 */
117 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
118 $testExtensionSettings = array(
119 'settings.' => array(
120 'foo' => 'bar',
121 'some.' => array(
122 'nested' => 'value'
123 )
124 )
125 );
126 $testExtensionSettingsConverted = array(
127 'settings' => array(
128 'foo' => 'bar',
129 'some' => array(
130 'nested' => 'value'
131 )
132 )
133 );
134 $testPluginSettings = array(
135 'settings.' => array(
136 'some.' => array(
137 'nested' => 'valueOverridde',
138 'new' => 'value'
139 )
140 )
141 );
142 $testPluginSettingsConverted = array(
143 'settings' => array(
144 'some' => array(
145 'nested' => 'valueOverridde',
146 'new' => 'value'
147 )
148 )
149 );
150 $testSetup = array(
151 'module.' => array(
152 'tx_someextensionname.' => $testExtensionSettings,
153 'tx_someextensionname_somepluginname.' => $testPluginSettings
154 )
155 );
156 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
157 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
158 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
159 $expectedResult = array(
160 'settings' => array(
161 'foo' => 'bar',
162 'some' => array(
163 'nested' => 'valueOverridde',
164 'new' => 'value'
165 )
166 )
167 );
168 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
169 $this->assertEquals($expectedResult, $actualResult);
170 }
171
172 /**
173 * @test
174 */
175 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
176 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
177 $expectedResult = array();
178 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
179 $this->assertEquals($expectedResult, $actualResult);
180 }
181
182 /**
183 * @test
184 */
185 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
186 $testSwitchableControllerActions = array(
187 'Controller1' => array(
188 'actions' => array(
189 'action1',
190 'action2'
191 ),
192 'nonCacheableActions' => array(
193 'action1'
194 )
195 ),
196 'Controller2' => array(
197 'actions' => array(
198 'action3',
199 'action4'
200 )
201 )
202 );
203 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['modules']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
204 $expectedResult = $testSwitchableControllerActions;
205 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
206 $this->assertEquals($expectedResult, $actualResult);
207 }
208
209 /**
210 * @test
211 */
212 public function getContextSpecificFrameworkConfigurationReturnsUnmodifiedFrameworkConfigurationIfRequestHandlersAreConfigured() {
213 $frameworkConfiguration = array(
214 'pluginName' => 'Pi1',
215 'extensionName' => 'SomeExtension',
216 'foo' => array(
217 'bar' => array(
218 'baz' => 'Foo'
219 )
220 ),
221 'mvc' => array(
222 'requestHandlers' => array(
223 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'SomeRequestHandler'
224 )
225 )
226 );
227 $expectedResult = $frameworkConfiguration;
228 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
229 $this->assertEquals($expectedResult, $actualResult);
230 }
231
232 /**
233 * @test
234 */
235 public function getContextSpecificFrameworkConfigurationSetsDefaultRequestHandlersIfRequestHandlersAreNotConfigured() {
236 $frameworkConfiguration = array(
237 'pluginName' => 'Pi1',
238 'extensionName' => 'SomeExtension',
239 'foo' => array(
240 'bar' => array(
241 'baz' => 'Foo'
242 )
243 )
244 );
245 $expectedResult = array(
246 'pluginName' => 'Pi1',
247 'extensionName' => 'SomeExtension',
248 'foo' => array(
249 'bar' => array(
250 'baz' => 'Foo'
251 )
252 ),
253 'mvc' => array(
254 'requestHandlers' => array(
255 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\FrontendRequestHandler',
256 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler' => 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\BackendRequestHandler'
257 )
258 )
259 );
260 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
261 $this->assertEquals($expectedResult, $actualResult);
262 }
263
264 /**
265 * @test
266 */
267 public function storagePidsAreExtendedIfRecursiveSearchIsConfigured() {
268 $storagePid = '1,2,3';
269 $recursive = 99;
270 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager */
271 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
272 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
273 $queryGenerator->expects($this->any())
274 ->method('getTreeList')
275 ->will($this->onConsecutiveCalls('4', '', '5,6'));
276 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
277
278 $expectedResult = '4,5,6';
279 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
280 $this->assertEquals($expectedResult, $actualResult);
281 }
282
283 /**
284 * @test
285 */
286 public function storagePidsAreExtendedIfRecursiveSearchIsConfiguredAndWithPidIncludedForNegativePid() {
287 $storagePid = '1,2,-3';
288 $recursive = 99;
289 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager */
290 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
291 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
292 $queryGenerator->expects($this->any())
293 ->method('getTreeList')
294 ->will($this->onConsecutiveCalls('4', '', '3,5,6'));
295 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
296
297 $expectedResult = '4,3,5,6';
298 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
299 $this->assertEquals($expectedResult, $actualResult);
300 }
301
302 /**
303 * @test
304 */
305 public function storagePidsAreNotExtendedIfRecursiveSearchIsNotConfigured() {
306 $storagePid = '1,2,3';
307
308 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
309
310 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
311 $queryGenerator->expects($this->never())->method('getTreeList');
312 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
313
314 $expectedResult = '1,2,3';
315 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid);
316 $this->assertEquals($expectedResult, $actualResult);
317 }
318
319 /**
320 * @test
321 */
322 public function storagePidsAreNotExtendedIfRecursiveSearchIsConfiguredForZeroLevels() {
323 $storagePid = '1,2,3';
324 $recursive = 0;
325
326 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
327
328 $queryGenerator = $this->getMock('TYPO3\\CMS\\Core\\Database\\QueryGenerator');
329 $queryGenerator->expects($this->never())->method('getTreeList');
330 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
331
332 $expectedResult = '1,2,3';
333 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
334 $this->assertEquals($expectedResult, $actualResult);
335 }
336 }