[TASK] Use name-resolution instead of strings where possible: 3
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class BackendConfigurationManagerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $backendConfigurationManager;
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Service\TypoScriptService|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
29 */
30 protected $mockTypoScriptService;
31
32 /**
33 * Sets up this testcase
34 */
35 public function setUp() {
36 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array());
37 $this->backendConfigurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class, array('getTypoScriptSetup'));
38 $this->mockTypoScriptService = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Service\TypoScriptService::class);
39 $this->backendConfigurationManager->_set('typoScriptService', $this->mockTypoScriptService);
40 }
41
42 /**
43 * @test
44 */
45 public function getCurrentPageIdReturnsPageIdFromGet() {
46 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('id' => 123));
47 $expectedResult = 123;
48 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
49 $this->assertEquals($expectedResult, $actualResult);
50 }
51
52 /**
53 * @test
54 */
55 public function getCurrentPageIdReturnsPageIdFromPost() {
56 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('id' => 123));
57 $_POST['id'] = 321;
58 $expectedResult = 321;
59 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
60 $this->assertEquals($expectedResult, $actualResult);
61 }
62
63 /**
64 * @test
65 */
66 public function getCurrentPageIdReturnsPidFromFirstRootTemplateIfIdIsNotSetAndNoRootPageWasFound() {
67 $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()));
68 $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(
69 array('pid' => 123)
70 ));
71 $expectedResult = 123;
72 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
73 $this->assertEquals($expectedResult, $actualResult);
74 }
75
76 /**
77 * @test
78 */
79 public function getCurrentPageIdReturnsUidFromFirstRootPageIfIdIsNotSet() {
80 $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(
81 array('uid' => 321)
82 ));
83 $expectedResult = 321;
84 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
85 $this->assertEquals($expectedResult, $actualResult);
86 }
87
88 /**
89 * @test
90 */
91 public function getCurrentPageIdReturnsDefaultStoragePidIfIdIsNotSetNoRootTemplateAndRootPageWasFound() {
92 $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()));
93 $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()));
94 $expectedResult = \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager::DEFAULT_BACKEND_STORAGE_PID;
95 $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
96 $this->assertEquals($expectedResult, $actualResult);
97 }
98
99 /**
100 * @test
101 */
102 public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
103 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue(array('foo' => 'bar')));
104 $expectedResult = array();
105 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
106 $this->assertEquals($expectedResult, $actualResult);
107 }
108
109 /**
110 * @test
111 */
112 public function getPluginConfigurationReturnsExtensionConfiguration() {
113 $testSettings = array(
114 'settings.' => array(
115 'foo' => 'bar'
116 )
117 );
118 $testSettingsConverted = array(
119 'settings' => array(
120 'foo' => 'bar'
121 )
122 );
123 $testSetup = array(
124 'module.' => array(
125 'tx_someextensionname.' => $testSettings
126 )
127 );
128 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
129 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
130 $expectedResult = array(
131 'settings' => array(
132 'foo' => 'bar'
133 )
134 );
135 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName');
136 $this->assertEquals($expectedResult, $actualResult);
137 }
138
139 /**
140 * @test
141 */
142 public function getPluginConfigurationReturnsPluginConfiguration() {
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_somepluginname.' => $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', 'SomePluginName');
166 $this->assertEquals($expectedResult, $actualResult);
167 }
168
169 /**
170 * @test
171 */
172 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
173 $testExtensionSettings = array(
174 'settings.' => array(
175 'foo' => 'bar',
176 'some.' => array(
177 'nested' => 'value'
178 )
179 )
180 );
181 $testExtensionSettingsConverted = array(
182 'settings' => array(
183 'foo' => 'bar',
184 'some' => array(
185 'nested' => 'value'
186 )
187 )
188 );
189 $testPluginSettings = array(
190 'settings.' => array(
191 'some.' => array(
192 'nested' => 'valueOverridde',
193 'new' => 'value'
194 )
195 )
196 );
197 $testPluginSettingsConverted = array(
198 'settings' => array(
199 'some' => array(
200 'nested' => 'valueOverridde',
201 'new' => 'value'
202 )
203 )
204 );
205 $testSetup = array(
206 'module.' => array(
207 'tx_someextensionname.' => $testExtensionSettings,
208 'tx_someextensionname_somepluginname.' => $testPluginSettings
209 )
210 );
211 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
212 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
213 $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
214 $expectedResult = array(
215 'settings' => array(
216 'foo' => 'bar',
217 'some' => array(
218 'nested' => 'valueOverridde',
219 'new' => 'value'
220 )
221 )
222 );
223 $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
224 $this->assertEquals($expectedResult, $actualResult);
225 }
226
227 /**
228 * @test
229 */
230 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
231 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
232 $expectedResult = array();
233 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
234 $this->assertEquals($expectedResult, $actualResult);
235 }
236
237 /**
238 * @test
239 */
240 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
241 $testSwitchableControllerActions = array(
242 'Controller1' => array(
243 'actions' => array(
244 'action1',
245 'action2'
246 ),
247 'nonCacheableActions' => array(
248 'action1'
249 )
250 ),
251 'Controller2' => array(
252 'actions' => array(
253 'action3',
254 'action4'
255 )
256 )
257 );
258 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['modules']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
259 $expectedResult = $testSwitchableControllerActions;
260 $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
261 $this->assertEquals($expectedResult, $actualResult);
262 }
263
264 /**
265 * @test
266 */
267 public function getContextSpecificFrameworkConfigurationReturnsUnmodifiedFrameworkConfigurationIfRequestHandlersAreConfigured() {
268 $frameworkConfiguration = array(
269 'pluginName' => 'Pi1',
270 'extensionName' => 'SomeExtension',
271 'foo' => array(
272 'bar' => array(
273 'baz' => 'Foo'
274 )
275 ),
276 'mvc' => array(
277 'requestHandlers' => array(
278 \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class => 'SomeRequestHandler'
279 )
280 )
281 );
282 $expectedResult = $frameworkConfiguration;
283 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
284 $this->assertEquals($expectedResult, $actualResult);
285 }
286
287 /**
288 * @test
289 */
290 public function getContextSpecificFrameworkConfigurationSetsDefaultRequestHandlersIfRequestHandlersAreNotConfigured() {
291 $frameworkConfiguration = array(
292 'pluginName' => 'Pi1',
293 'extensionName' => 'SomeExtension',
294 'foo' => array(
295 'bar' => array(
296 'baz' => 'Foo'
297 )
298 )
299 );
300 $expectedResult = array(
301 'pluginName' => 'Pi1',
302 'extensionName' => 'SomeExtension',
303 'foo' => array(
304 'bar' => array(
305 'baz' => 'Foo'
306 )
307 ),
308 'mvc' => array(
309 'requestHandlers' => array(
310 \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class,
311 \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class
312 )
313 )
314 );
315 $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
316 $this->assertEquals($expectedResult, $actualResult);
317 }
318
319 /**
320 * @test
321 */
322 public function storagePidsAreExtendedIfRecursiveSearchIsConfigured() {
323 $storagePid = '1,2,3';
324 $recursive = 99;
325 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager */
326 $abstractConfigurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class, array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
327 $queryGenerator = $this->getMock(\TYPO3\CMS\Core\Database\QueryGenerator::class);
328 $queryGenerator->expects($this->any())
329 ->method('getTreeList')
330 ->will($this->onConsecutiveCalls('4', '', '5,6'));
331 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
332
333 $expectedResult = '4,5,6';
334 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
335 $this->assertEquals($expectedResult, $actualResult);
336 }
337
338 /**
339 * @test
340 */
341 public function storagePidsAreExtendedIfRecursiveSearchIsConfiguredAndWithPidIncludedForNegativePid() {
342 $storagePid = '1,2,-3';
343 $recursive = 99;
344 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager */
345 $abstractConfigurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class, array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
346 $queryGenerator = $this->getMock(\TYPO3\CMS\Core\Database\QueryGenerator::class);
347 $queryGenerator->expects($this->any())
348 ->method('getTreeList')
349 ->will($this->onConsecutiveCalls('4', '', '3,5,6'));
350 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
351
352 $expectedResult = '4,3,5,6';
353 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
354 $this->assertEquals($expectedResult, $actualResult);
355 }
356
357 /**
358 * @test
359 */
360 public function storagePidsAreNotExtendedIfRecursiveSearchIsNotConfigured() {
361 $storagePid = '1,2,3';
362
363 $abstractConfigurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class, array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
364
365 $queryGenerator = $this->getMock(\TYPO3\CMS\Core\Database\QueryGenerator::class);
366 $queryGenerator->expects($this->never())->method('getTreeList');
367 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
368
369 $expectedResult = '1,2,3';
370 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid);
371 $this->assertEquals($expectedResult, $actualResult);
372 }
373
374 /**
375 * @test
376 */
377 public function storagePidsAreNotExtendedIfRecursiveSearchIsConfiguredForZeroLevels() {
378 $storagePid = '1,2,3';
379 $recursive = 0;
380
381 $abstractConfigurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager::class, array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
382
383 $queryGenerator = $this->getMock(\TYPO3\CMS\Core\Database\QueryGenerator::class);
384 $queryGenerator->expects($this->never())->method('getTreeList');
385 $abstractConfigurationManager->_set('queryGenerator', $queryGenerator);
386
387 $expectedResult = '1,2,3';
388 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
389 $this->assertEquals($expectedResult, $actualResult);
390 }
391 }