[+BUGFIX] Extbase (Configuration): slightly tweaked FrontendConfigurationManager...
authorBastian Waidelich <bastian@typo3.org>
Tue, 30 Nov 2010 15:37:12 +0000 (15:37 +0000)
committerBastian Waidelich <bastian@typo3.org>
Tue, 30 Nov 2010 15:37:12 +0000 (15:37 +0000)
[+TASK] Extbase (Tests): Added some unit tests for Backend- and FrontendConfigurationManager. To be continued

typo3/sysext/extbase/Classes/Configuration/FrontendConfigurationManager.php
typo3/sysext/extbase/Tests/Unit/Configuration/BackendConfigurationManagerTest.php
typo3/sysext/extbase/Tests/Unit/Configuration/FrontendConfigurationManagerTest.php

index c9e744c..bc11396 100644 (file)
@@ -269,33 +269,31 @@ class Tx_Extbase_Configuration_FrontendConfigurationManager extends Tx_Extbase_C
                return $frameworkConfiguration;
        }
 
-
        /**
         * Overrides the switchable controller actions from the flexform.
         *
         * @param array $frameworkConfiguration The original framework configuration
         * @param array $flexformConfiguration The full flexform configuration
         * @return array the modified framework configuration, if needed
-        * @todo: Check that the controller has been before inside the switchableControllerActions.
         */
        protected function overrideSwitchableControllerActionsFromFlexform(array $frameworkConfiguration, array $flexformConfiguration) {
                if (!isset($flexformConfiguration['switchableControllerActions']) || is_array($flexformConfiguration['switchableControllerActions'])) {
                        return $frameworkConfiguration;
                }
 
-               // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by  , first.
+                       // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by  , first.
+                       // The expected format is: "Controller1->action2;Controller2->action3;Controller2->action1"
                $switchableControllerActionPartsFromFlexform = t3lib_div::trimExplode(',', str_replace(';', ',', $flexformConfiguration['switchableControllerActions']), TRUE);
 
                $newSwitchableControllerActionsFromFlexform = array();
                foreach ($switchableControllerActionPartsFromFlexform as $switchableControllerActionPartFromFlexform) {
-                       list($controller, $action) = explode('->', $switchableControllerActionPartFromFlexform);
+                       list($controller, $action) = t3lib_div::trimExplode('->', $switchableControllerActionPartFromFlexform);
                        if (empty($controller) || empty($action)) {
                                throw new Tx_Extbase_Configuration_Exception_ParseError('Controller or action were empty when overriding switchableControllerActions from flexform.', 1257146403);
                        }
                        $newSwitchableControllerActionsFromFlexform[$controller][] = $action;
                }
-
-               if (count($newSwitchableControllerActionsFromFlexform)) {
+               if (count($newSwitchableControllerActionsFromFlexform) > 0) {
                        $this->overrideSwitchableControllerActions($frameworkConfiguration, $newSwitchableControllerActionsFromFlexform);
                }
                return $frameworkConfiguration;
index 1152734..611de91 100644 (file)
@@ -25,7 +25,7 @@
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
 
-class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManager_testcase extends Tx_Extbase_Tests_Unit_BaseTestCase {
+class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManagerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
 
        /**
         * @var array
@@ -43,6 +43,11 @@ class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManager_testcase e
        protected $typo3DbBackup;
 
        /**
+        * @var array
+        */
+       protected $extConfBackup;
+
+       /**
         * @var Tx_Extbase_Configuration_BackendConfigurationManager
         */
        protected $backendConfigurationManager;
@@ -57,7 +62,9 @@ class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManager_testcase e
                $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
                $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array());
 
-               $this->backendConfigurationManager = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Configuration_BackendConfigurationManager'), array('dummy'));
+               $this->extConfBackup = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'];
+
+               $this->backendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_BackendConfigurationManager', array('getTypoScriptSetup'));
        }
 
        /**
@@ -66,6 +73,7 @@ class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManager_testcase e
        public function tearDown() {
                t3lib_div::_GETset($this->getBackup);
                $_POST = $this->postBackup;
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = $this->extConfBackup;
        }
 
        /**
@@ -161,10 +169,157 @@ class Tx_Extbase_Tests_Unit_Configuration_BackendConfigurationManager_testcase e
                        ->with('uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', '', '1')
                        ->will($this->returnValue(array()));
 
-               $expectedResult = 0;
+               $expectedResult = Tx_Extbase_Configuration_AbstractConfigurationManager::DEFAULT_BACKEND_STORAGE_PID;
                $actualResult = $this->backendConfigurationManager->_call('getCurrentPageId');
 
                $this->assertEquals($expectedResult, $actualResult);
        }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
+               $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue(array('foo' => 'bar')));
+               $expectedResult = array();
+               $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationReturnsExtensionConfiguration() {
+               $testSetup = array(
+                       'module.' => array(
+                               'tx_someextensionname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar'
+                                       )
+                               ),
+                       ),
+               );
+               $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar'
+                       )
+               );
+               $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationReturnsPluginConfiguration() {
+               $testSetup = array(
+                       'module.' => array(
+                               'tx_someextensionname_somepluginname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar'
+                                       )
+                               ),
+                       ),
+               );
+               $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar'
+                       )
+               );
+               $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
+               $testSetup = array(
+                       'module.' => array(
+                               'tx_someextensionname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar',
+                                               'some.' => array(
+                                                       'nested' => 'value'
+                                               ),
+                                       ),
+                               ),
+                               'tx_someextensionname_somepluginname.' => array(
+                                       'settings.' => array(
+                                               'some.' => array(
+                                                       'nested' => 'valueOverridde',
+                                                       'new' => 'value',
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+               $this->backendConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($testSetup));
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar',
+                               'some' => array(
+                                       'nested' => 'valueOverridde',
+                                       'new' => 'value'
+                               ),
+                       ),
+               );
+               $actualResult = $this->backendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
+               $expectedResult = array();
+               $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
+               $testSwitchableControllerActions = array(
+                       'Controller1' => array(
+                               'actions' => array(
+                                       'action1', 'action2'
+                               ),
+                               'nonCacheableActions' => array(
+                                       'action1'
+                               ),
+                       ),
+                       'Controller2' => array(
+                               'actions' => array(
+                                       'action3', 'action4'
+                               ),
+                       )
+               );
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['modules']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
+               $expectedResult = $testSwitchableControllerActions;
+               $actualResult = $this->backendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getContextSpecificFrameworkConfigurationReturnsUnmodifiedFrameworkConfiguration() {
+               $frameworkConfiguration = array(
+                       'pluginName' => 'Pi1',
+                       'extensionName' => 'SomeExtension',
+                       'foo' => array(
+                               'bar' => array(
+                                       'baz' => 'Foo',
+                               ),
+                       )
+               );
+               $expectedResult = $frameworkConfiguration;
+               $actualResult = $this->backendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
+               $this->assertEquals($expectedResult, $actualResult);
+       }
 }
 ?>
\ No newline at end of file
index f585a05..529a583 100644 (file)
@@ -25,7 +25,7 @@
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
 
-class Tx_Extbase_Configuration_FrontendConfigurationManager_testcase extends Tx_Extbase_Tests_Unit_BaseTestCase {
+class Tx_Extbase_Configuration_FrontendConfigurationManagerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
 
        /**
         * @var tslib_fe
@@ -33,15 +33,29 @@ class Tx_Extbase_Configuration_FrontendConfigurationManager_testcase extends Tx_
        protected $tsfeBackup;
 
        /**
+        * @var tslib_cObj
+        */
+       protected $mockContentObject;
+
+       /**
         * @var Tx_Extbase_Configuration_FrontendConfigurationManager
         */
        protected $frontendConfigurationManager;
+
+       /**
+        * @var array
+        */
+       protected $extConfBackup;
+
        /**
         * Sets up this testcase
         */
        public function setUp() {
                $this->tsfeBackup = $GLOBALS['TSFE'];
-               $this->frontendConfigurationManager = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Configuration_FrontendConfigurationManager'), array('dummy'));
+               $this->mockContentObject = $this->getMock('tslib_cObj');
+               $this->extConfBackup = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'];
+               $this->frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('dummy'));
+               $this->frontendConfigurationManager->_set('contentObject', $this->mockContentObject);
        }
 
        /**
@@ -49,6 +63,7 @@ class Tx_Extbase_Configuration_FrontendConfigurationManager_testcase extends Tx_
         */
        public function tearDown() {
                $GLOBALS['TSFE']->tmpl->setup;
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = $this->extConfBackup;
        }
 
        /**
@@ -56,18 +71,146 @@ class Tx_Extbase_Configuration_FrontendConfigurationManager_testcase extends Tx_
         */
        public function getTypoScriptSetupReturnsSetupFromTSFE() {
                $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
-               $this->assertEquals(array('foo' => 'bar'), $this->frontendConfigurationManager->_callRef('getTypoScriptSetup'));
+               $this->assertEquals(array('foo' => 'bar'), $this->frontendConfigurationManager->_call('getTypoScriptSetup'));
        }
 
        /**
         * @test
         */
-       public function overrideSwitchableControllerActionsFromFlexformMergesNonCacheableActions() {
+       public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
+               $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
+               $expectedResult = array();
+               $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationReturnsExtensionConfiguration() {
+               $testSetup = array(
+                       'plugin.' => array(
+                               'tx_someextensionname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar'
+                                       )
+                               ),
+                       ),
+               );
+               $GLOBALS['TSFE']->tmpl->setup = $testSetup;
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar'
+                       )
+               );
+               $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationReturnsPluginConfiguration() {
+               $testSetup = array(
+                       'plugin.' => array(
+                               'tx_someextensionname_somepluginname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar'
+                                       )
+                               ),
+                       ),
+               );
+               $GLOBALS['TSFE']->tmpl->setup = $testSetup;
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar'
+                       )
+               );
+               $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
+               $testSetup = array(
+                       'plugin.' => array(
+                               'tx_someextensionname.' => array(
+                                       'settings.' => array(
+                                               'foo' => 'bar',
+                                               'some.' => array(
+                                                       'nested' => 'value'
+                                               ),
+                                       ),
+                               ),
+                               'tx_someextensionname_somepluginname.' => array(
+                                       'settings.' => array(
+                                               'some.' => array(
+                                                       'nested' => 'valueOverridde',
+                                                       'new' => 'value',
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+               $GLOBALS['TSFE']->tmpl->setup = $testSetup;
+               $expectedResult = array(
+                       'settings' => array(
+                               'foo' => 'bar',
+                               'some' => array(
+                                       'nested' => 'valueOverridde',
+                                       'new' => 'value'
+                               ),
+                       ),
+               );
+               $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
+               $expectedResult = array();
+               $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
+               $testSwitchableControllerActions = array(
+                       'Controller1' => array(
+                               'actions' => array(
+                                       'action1', 'action2'
+                               ),
+                               'nonCacheableActions' => array(
+                                       'action1'
+                               ),
+                       ),
+                       'Controller2' => array(
+                               'actions' => array(
+                                       'action3', 'action4'
+                               ),
+                       )
+               );
+               $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['plugins']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
+               $expectedResult = $testSwitchableControllerActions;
+               $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function overrideSwitchableControllerActionsFromFlexformReturnsUnchangedFrameworkConfigurationIfNoFlexformConfigurationIsFound() {
                $frameworkConfiguration = array(
-                       'userFunc' => 'tx_extbase_dispatcher->dispatch',
                        'pluginName' => 'Pi1',
                        'extensionName' => 'SomeExtension',
-                       'switchableControllerActions' => array(
+                       'controllerConfiguration' => array(
                                'Controller1' => array(
                                        'controller' => 'Controller1',
                                        'actions' => 'action1 , action2'
@@ -79,26 +222,87 @@ class Tx_Extbase_Configuration_FrontendConfigurationManager_testcase extends Tx_
                                )
                        )
                );
+               $flexformConfiguration = array();
+               $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexform', $frameworkConfiguration, $flexformConfiguration);
+               $this->assertSame($frameworkConfiguration, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function overrideSwitchableControllerActionsFromFlexformMergesNonCacheableActions() {
+               $frameworkConfiguration = array(
+                       'pluginName' => 'Pi1',
+                       'extensionName' => 'SomeExtension',
+                       'controllerConfiguration' => array(
+                               'Controller1' => array(
+                                       'actions' => array('action1 , action2')
+                               ),
+                               'Controller2' => array(
+                                       'actions' => array('action2', 'action1','action3'),
+                                       'nonCacheableActions' => array('action2', 'action3')
+                               )
+                       )
+               );
                $flexformConfiguration = array(
-                       'switchableControllerActions' => 'Controller1->action2;Controller2->action3;Controller2->action1'
+                       'switchableControllerActions' => 'Controller1  -> action2;Controller2->action3;  Controller2->action1'
                );
                $expectedResult = array(
-                       'userFunc' => 'tx_extbase_dispatcher->dispatch',
                        'pluginName' => 'Pi1',
                        'extensionName' => 'SomeExtension',
-                       'switchableControllerActions' => array(
+                       'controllerConfiguration' => array(
                                'Controller1' => array(
-                                       'controller' => 'Controller1',
-                                       'actions' => 'action2'
+                                       'actions' => array('action2'),
                                ),
                                'Controller2' => array(
-                                       'controller' => 'Controller2',
-                                       'actions' => 'action3,action1',
-                                       'nonCacheableActions' => 'action3'
+                                       'actions' => array('action3', 'action1'),
+                                       'nonCacheableActions' => array(1 => 'action3'),
+                               )
+                       )
+               );
+               $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexform', $frameworkConfiguration, $flexformConfiguration);
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        * @expectedException Tx_Extbase_Configuration_Exception_ParseError
+        */
+       public function overrideSwitchableControllerActionsThrowsExceptionIfFlexformConfigurationIsInvalid() {
+               $frameworkConfiguration = array(
+                       'pluginName' => 'Pi1',
+                       'extensionName' => 'SomeExtension',
+                       'controllerConfiguration' => array(
+                               'Controller1' => array(
+                                       'actions' => array('action1 , action2')
+                               ),
+                               'Controller2' => array(
+                                       'actions' => array('action2', 'action1','action3'),
+                                       'nonCacheableActions' => array('action2', 'action3')
                                )
                        )
                );
-               $actualResult = $this->frontendConfigurationManager->_callRef('overrideSwitchableControllerActionsFromFlexform', $frameworkConfiguration, $flexformConfiguration);
+               $flexformConfiguration = array(
+                       'switchableControllerActions' => 'Controller1->;Controller2->action3;Controller2->action1'
+               );
+               $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexform', $frameworkConfiguration, $flexformConfiguration);
+       }
+
+       /**
+        * @test
+        */
+       public function getContextSpecificFrameworkConfigurationCorrectlyCallsOverrideMethods() {
+               $frameworkConfiguration = array(
+                       'some' => array(
+                               'framework' => 'configuration'
+                       ),
+               );
+               $frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('overrideStoragePidIfStartingPointIsSet', 'overrideConfigurationFromPlugin', 'overrideConfigurationFromFlexform'));
+               $frontendConfigurationManager->expects($this->at(0))->method('overrideStoragePidIfStartingPointIsSet')->with($frameworkConfiguration)->will($this->returnValue(array('overridden' => 'storagePid')));
+               $frontendConfigurationManager->expects($this->at(1))->method('overrideConfigurationFromPlugin')->with(array('overridden' => 'storagePid'))->will($this->returnValue(array('overridden' => 'pluginConfiguration')));
+               $frontendConfigurationManager->expects($this->at(2))->method('overrideConfigurationFromFlexform')->with(array('overridden' => 'pluginConfiguration'))->will($this->returnValue(array('overridden' => 'flexformConfiguration')));
+               $expectedResult = array('overridden' => 'flexformConfiguration');
+               $actualResult = $frontendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
                $this->assertEquals($expectedResult, $actualResult);
        }
 }