0ad4c35d6e6cfdf3f75c8c46529ebf5e881272c9
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Configuration / FrontendConfigurationManagerTest.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 FrontendConfigurationManagerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
34 */
35 protected $mockContentObject;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
39 */
40 protected $frontendConfigurationManager;
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 $GLOBALS['TSFE'] = new \stdClass();
52 $GLOBALS['TSFE']->tmpl = new \stdClass();
53 $this->mockContentObject = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer', array('getTreeList'));
54 $this->frontendConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('dummy'));
55 $this->frontendConfigurationManager->_set('contentObject', $this->mockContentObject);
56 $this->mockTypoScriptService = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
57 $this->frontendConfigurationManager->injectTypoScriptService($this->mockTypoScriptService);
58 }
59
60 /**
61 * @test
62 */
63 public function getTypoScriptSetupReturnsSetupFromTsfe() {
64 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
65 $this->assertEquals(array('foo' => 'bar'), $this->frontendConfigurationManager->_call('getTypoScriptSetup'));
66 }
67
68 /**
69 * @test
70 */
71 public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
72 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
73 $expectedResult = array();
74 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
75 $this->assertEquals($expectedResult, $actualResult);
76 }
77
78 /**
79 * @test
80 */
81 public function getPluginConfigurationReturnsExtensionConfiguration() {
82 $testSettings = array(
83 'settings.' => array(
84 'foo' => 'bar'
85 )
86 );
87 $testSettingsConverted = array(
88 'settings' => array(
89 'foo' => 'bar'
90 )
91 );
92 $testSetup = array(
93 'plugin.' => array(
94 'tx_someextensionname.' => $testSettings
95 )
96 );
97 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
98 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
99 $expectedResult = array(
100 'settings' => array(
101 'foo' => 'bar'
102 )
103 );
104 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName');
105 $this->assertEquals($expectedResult, $actualResult);
106 }
107
108 /**
109 * @test
110 */
111 public function getPluginConfigurationReturnsPluginConfiguration() {
112 $testSettings = array(
113 'settings.' => array(
114 'foo' => 'bar'
115 )
116 );
117 $testSettingsConverted = array(
118 'settings' => array(
119 'foo' => 'bar'
120 )
121 );
122 $testSetup = array(
123 'plugin.' => array(
124 'tx_someextensionname_somepluginname.' => $testSettings
125 )
126 );
127 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
128 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
129 $expectedResult = array(
130 'settings' => array(
131 'foo' => 'bar'
132 )
133 );
134 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
135 $this->assertEquals($expectedResult, $actualResult);
136 }
137
138 /**
139 * @test
140 */
141 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
142 $testExtensionSettings = array(
143 'settings.' => array(
144 'foo' => 'bar',
145 'some.' => array(
146 'nested' => 'value'
147 )
148 )
149 );
150 $testExtensionSettingsConverted = array(
151 'settings' => array(
152 'foo' => 'bar',
153 'some' => array(
154 'nested' => 'value'
155 )
156 )
157 );
158 $testPluginSettings = array(
159 'settings.' => array(
160 'some.' => array(
161 'nested' => 'valueOverridde',
162 'new' => 'value'
163 )
164 )
165 );
166 $testPluginSettingsConverted = array(
167 'settings' => array(
168 'some' => array(
169 'nested' => 'valueOverridde',
170 'new' => 'value'
171 )
172 )
173 );
174 $testSetup = array(
175 'plugin.' => array(
176 'tx_someextensionname.' => $testExtensionSettings,
177 'tx_someextensionname_somepluginname.' => $testPluginSettings
178 )
179 );
180 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
181 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
182 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
183 $expectedResult = array(
184 'settings' => array(
185 'foo' => 'bar',
186 'some' => array(
187 'nested' => 'valueOverridde',
188 'new' => 'value'
189 )
190 )
191 );
192 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
193 $this->assertEquals($expectedResult, $actualResult);
194 }
195
196 /**
197 * @test
198 */
199 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
200 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
201 $expectedResult = array();
202 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
203 $this->assertEquals($expectedResult, $actualResult);
204 }
205
206 /**
207 * @test
208 */
209 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
210 $testSwitchableControllerActions = array(
211 'Controller1' => array(
212 'actions' => array(
213 'action1',
214 'action2'
215 ),
216 'nonCacheableActions' => array(
217 'action1'
218 )
219 ),
220 'Controller2' => array(
221 'actions' => array(
222 'action3',
223 'action4'
224 )
225 )
226 );
227 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['plugins']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
228 $expectedResult = $testSwitchableControllerActions;
229 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
230 $this->assertEquals($expectedResult, $actualResult);
231 }
232
233 /**
234 * @test
235 */
236 public function overrideSwitchableControllerActionsFromFlexFormReturnsUnchangedFrameworkConfigurationIfNoFlexFormConfigurationIsFound() {
237 $frameworkConfiguration = array(
238 'pluginName' => 'Pi1',
239 'extensionName' => 'SomeExtension',
240 'controllerConfiguration' => array(
241 'Controller1' => array(
242 'controller' => 'Controller1',
243 'actions' => 'action1 , action2'
244 ),
245 'Controller2' => array(
246 'controller' => 'Controller2',
247 'actions' => 'action2 , action1,action3',
248 'nonCacheableActions' => 'action2, action3'
249 )
250 )
251 );
252 $flexFormConfiguration = array();
253 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
254 $this->assertSame($frameworkConfiguration, $actualResult);
255 }
256
257 /**
258 * @test
259 */
260 public function overrideSwitchableControllerActionsFromFlexFormMergesNonCacheableActions() {
261 $frameworkConfiguration = array(
262 'pluginName' => 'Pi1',
263 'extensionName' => 'SomeExtension',
264 'controllerConfiguration' => array(
265 'Controller1' => array(
266 'actions' => array('action1 , action2')
267 ),
268 'Controller2' => array(
269 'actions' => array('action2', 'action1', 'action3'),
270 'nonCacheableActions' => array('action2', 'action3')
271 )
272 )
273 );
274 $flexFormConfiguration = array(
275 'switchableControllerActions' => 'Controller1 -> action2;Controller2->action3; Controller2->action1'
276 );
277 $expectedResult = array(
278 'pluginName' => 'Pi1',
279 'extensionName' => 'SomeExtension',
280 'controllerConfiguration' => array(
281 'Controller1' => array(
282 'actions' => array('action2')
283 ),
284 'Controller2' => array(
285 'actions' => array('action3', 'action1'),
286 'nonCacheableActions' => array(1 => 'action3')
287 )
288 )
289 );
290 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
291 $this->assertEquals($expectedResult, $actualResult);
292 }
293
294 /**
295 * @test
296 * @expectedException \TYPO3\CMS\Extbase\Configuration\Exception\ParseErrorException
297 */
298 public function overrideSwitchableControllerActionsThrowsExceptionIfFlexFormConfigurationIsInvalid() {
299 $frameworkConfiguration = array(
300 'pluginName' => 'Pi1',
301 'extensionName' => 'SomeExtension',
302 'controllerConfiguration' => array(
303 'Controller1' => array(
304 'actions' => array('action1 , action2')
305 ),
306 'Controller2' => array(
307 'actions' => array('action2', 'action1', 'action3'),
308 'nonCacheableActions' => array('action2', 'action3')
309 )
310 )
311 );
312 $flexFormConfiguration = array(
313 'switchableControllerActions' => 'Controller1->;Controller2->action3;Controller2->action1'
314 );
315 $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
316 }
317
318 /**
319 * @test
320 */
321 public function getContextSpecificFrameworkConfigurationCorrectlyCallsOverrideMethods() {
322 $frameworkConfiguration = array(
323 'some' => array(
324 'framework' => 'configuration'
325 )
326 );
327 /** @var \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
328 $frontendConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('overrideStoragePidIfStartingPointIsSet', 'overrideConfigurationFromPlugin', 'overrideConfigurationFromFlexForm'));
329 $frontendConfigurationManager->expects($this->at(0))->method('overrideStoragePidIfStartingPointIsSet')->with($frameworkConfiguration)->will($this->returnValue(array('overridden' => 'storagePid')));
330 $frontendConfigurationManager->expects($this->at(1))->method('overrideConfigurationFromPlugin')->with(array('overridden' => 'storagePid'))->will($this->returnValue(array('overridden' => 'pluginConfiguration')));
331 $frontendConfigurationManager->expects($this->at(2))->method('overrideConfigurationFromFlexForm')->with(array('overridden' => 'pluginConfiguration'))->will($this->returnValue(array('overridden' => 'flexFormConfiguration')));
332 $expectedResult = array('overridden' => 'flexFormConfiguration');
333 $actualResult = $frontendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
334 $this->assertEquals($expectedResult, $actualResult);
335 }
336
337 /**
338 * @test
339 */
340 public function storagePidsAreExtendedIfRecursiveSearchIsConfigured() {
341 $storagePid = '3,5,9';
342 $recursive = 99;
343 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager */
344 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
345 /** @var $cObjectMock \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer */
346 $cObjectMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
347 $cObjectMock->expects($this->any())
348 ->method('getTreeList')
349 ->will($this->onConsecutiveCalls('3,4', '5', '9,898,12'));
350 $abstractConfigurationManager->setContentObject($cObjectMock);
351
352 $expectedResult = '3,4,5,9,898,12';
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 /** @var $abstractConfigurationManager \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager */
364 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
365 /** @var $cObjectMock \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer */
366 $cObjectMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
367 $cObjectMock->expects($this->never())->method('getTreeList');
368 $abstractConfigurationManager->setContentObject($cObjectMock);
369
370 $expectedResult = '1,2,3';
371 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid);
372 $this->assertEquals($expectedResult, $actualResult);
373 }
374
375 /**
376 * @test
377 */
378 public function storagePidsAreNotExtendedIfRecursiveSearchIsConfiguredForZeroLevels() {
379 $storagePid = '1,2,3';
380 $recursive = 0;
381
382 $abstractConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('overrideSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration', 'getSwitchableControllerActions'));
383
384 /** @var $cObjectMock \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer */
385 $cObjectMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
386 $cObjectMock->expects($this->never())->method('getTreeList');
387 $abstractConfigurationManager->setContentObject($cObjectMock);
388
389 $expectedResult = '1,2,3';
390 $actualResult = $abstractConfigurationManager->_call('getRecursiveStoragePids', $storagePid, $recursive);
391 $this->assertEquals($expectedResult, $actualResult);
392 }
393
394 /**
395 * @test
396 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
397 */
398 public function mergeConfigurationIntoFrameworkConfigurationWorksAsExpected() {
399 $configuration = array(
400 'persistence' => array(
401 'storagePid' => '0,1,2,3'
402 )
403 );
404
405 $frameworkConfiguration = array('persistence' => array('storagePid' => '98'));
406 $this->assertSame(
407 array('persistence' => array('storagePid' => '0,1,2,3')),
408 $this->frontendConfigurationManager->_call('mergeConfigurationIntoFrameworkConfiguration', $frameworkConfiguration, $configuration, 'persistence')
409 );
410 }
411
412 /**
413 * @test
414 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
415 */
416 public function overrideStoragePidIfStartingPointIsSetOverridesCorrectly() {
417 $this->mockContentObject->expects($this->any())->method('getTreeList')->will($this->returnValue('1,2,3'));
418 $this->mockContentObject->data = array('pages' => '0', 'recursive' => 1);
419
420 $frameworkConfiguration = array('persistence' => array('storagePid' => '98'));
421 $this->assertSame(
422 array('persistence' => array('storagePid' => '0,1,2,3')),
423 $this->frontendConfigurationManager->_call('overrideStoragePidIfStartingPointIsSet', $frameworkConfiguration)
424 );
425 }
426
427 /**
428 * @test
429 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
430 */
431 public function overrideConfigurationFromFlexFormOverridesCorrectly() {
432 /** @var $flexFormService \TYPO3\CMS\Extbase\Service\FlexFormService|\PHPUnit_Framework_MockObject_MockObject */
433 $flexFormService = $this->getMock('TYPO3\CMS\Extbase\Service\FlexFormService', array('convertFlexFormContentToArray'));
434 $flexFormService->expects($this->any())->method('convertFlexFormContentToArray')->will($this->returnValue(array(
435 'persistence' => array(
436 'storagePid' => '0,1,2,3'
437 )
438 )));
439
440 $this->frontendConfigurationManager->_set('flexFormService', $flexFormService);
441 $this->mockContentObject->data = array('pi_flexform' => 'foo');
442
443 $frameworkConfiguration = array('persistence' => array('storagePid' => '98'));
444 $this->assertSame(
445 array('persistence' => array('storagePid' => '0,1,2,3')),
446 $this->frontendConfigurationManager->_call('overrideConfigurationFromFlexForm', $frameworkConfiguration)
447 );
448 }
449
450 /**
451 * @test
452 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
453 */
454 public function overrideConfigurationFromPluginOverridesCorrectly() {
455 /** @var $frontendConfigurationManager \TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager */
456 $frontendConfigurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager', array('getTypoScriptSetup'));
457 $frontendConfigurationManager->_set('contentObject', $this->mockContentObject);
458 $frontendConfigurationManager->injectTypoScriptService($this->mockTypoScriptService);
459
460 $this->mockTypoScriptService->expects($this->once())->method('convertTypoScriptArrayToPlainArray')->will($this->returnValue(array(
461 'persistence' => array(
462 'storagePid' => '0,1,2,3'
463 ),
464 'settings' => array(
465 'foo' => 'bar'
466 ),
467 'view' => array(
468 'foo' => 'bar'
469 ),
470 )));
471 $frontendConfigurationManager->expects($this->any())->method('getTypoScriptSetup')->will($this->returnValue(array(
472 'plugin.' => array(
473 'tx_ext_pi1.' => array(
474 'persistence.' => array(
475 'storagePid' => '0,1,2,3'
476 ),
477 'settings.' => array(
478 'foo' => 'bar'
479 ),
480 'view.' => array(
481 'foo' => 'bar'
482 ),
483 )
484 )
485 )));
486
487 $frameworkConfiguration = array(
488 'extensionName' => 'ext',
489 'pluginName' => 'pi1',
490 'persistence' => array(
491 'storagePid' => '1'
492 ),
493 'settings' => array(
494 'foo' => 'qux'
495 ),
496 'view' => array(
497 'foo' => 'qux'
498 ),
499 );
500 $this->assertSame(
501 array(
502 'extensionName' => 'ext',
503 'pluginName' => 'pi1',
504 'persistence' => array(
505 'storagePid' => '0,1,2,3',
506 ),
507 'settings' => array(
508 'foo' => 'bar'
509 ),
510 'view' => array(
511 'foo' => 'bar'
512 ),
513 ),
514 $frontendConfigurationManager->_call('overrideConfigurationFromPlugin', $frameworkConfiguration)
515 );
516 }
517 }
518
519 ?>