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