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