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