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