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