[TASK] EM: Refactor Configuration handling 31/17731/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Sat, 26 Jan 2013 16:29:33 +0000 (17:29 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 28 Jan 2013 11:59:55 +0000 (12:59 +0100)
Classes ConfigurationUtility and ConfigurationItemRepository handle
the extension managers extension configuration settings.

Configuration sources are ext_conf_template.txt files,
LocalConfiguration.php and the extension manager configuration
forms. They all have different formats and structures, thus
the code to merge all that is not exactly easy.

This patch shifts some code around in order to make the current
handling better understandable for easier debugging. The API is
still not as simple as it should be, but for now some
code smell is solved:

* Use @inject annotation for dependency injection to reduce
  number of code lines.
* Remove dependency injected objects if possible and rely on
  injection of objectManager only if the object is not widely
  used in the class.
* Disentangle a method call chain from InstallUtility to
  ConfigurationUtility back to InstallUtility by removing method
  writeExtensionTypoScriptStyleConfigurationToLocalconf() from
  InstallUtility and merging functionality with
  ConfigurationUtility->writeConfiguration().
* Remove public method createArrayFromConstants() from
  ConfigurationItemRepository and move to ConfigurationUtility
  under new name
  getDefaultConfigurationFromExtConfTemplateAsFlatItemArray().
* Rename ConfigurationItemRepository->findByExtension() to
  findByExtensionKey() and always let it return an
  ObjectStorage, even if empty.
* Simplify several method signatures, mostly of protected methods
  to one parameter only, moving only the extension key around.
* Better encapsulating of low level code in protected methods
  instead of duplicated low level code in in entry methods
  or controller logic.
* Resolve code smell in unit tests with better mocking and
  structured tests.

Change-Id: I441280a3907e2487cd7fe7ba1856e9ef04c72a42
Resolves: #44818
Releases: 6.1, 6.0
Reviewed-on: https://review.typo3.org/17731
Reviewed-by: Jan-Erik Revsbech
Tested-by: Jan-Erik Revsbech
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/extensionmanager/Classes/Controller/ConfigurationController.php
typo3/sysext/extensionmanager/Classes/Domain/Repository/ConfigurationItemRepository.php
typo3/sysext/extensionmanager/Classes/Utility/ConfigurationUtility.php
typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
typo3/sysext/extensionmanager/Tests/Unit/Domain/Repository/ConfigurationItemRepositoryTest.php
typo3/sysext/extensionmanager/Tests/Unit/Utility/ConfigurationUtilityTest.php

index f14c7a8..ae7c93e 100644 (file)
@@ -50,12 +50,20 @@ class ConfigurationController extends \TYPO3\CMS\Extensionmanager\Controller\Abs
         * Show the extension configuration form. The whole form field handling is done
         * in the corresponding view helper
         *
-        * @param array $extension
+        * @param array $extension Extension information, must contain at least the key
+        * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
+        * @return void
         */
-       public function showConfigurationFormAction($extension) {
-               $extension = array_merge($extension, $GLOBALS['TYPO3_LOADED_EXT'][$extension['key']]);
-               $configuration = $this->configurationItemRepository->findByExtension($extension);
-               $this->view->assign('configuration', $configuration)->assign('extension', $extension);
+       public function showConfigurationFormAction(array $extension) {
+               if (!array_key_exists('key', $extension)) {
+                       throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
+                               'Extension key not found.',
+                               1359206803
+                       );
+               }
+               $this->view
+                       ->assign('configuration', $this->configurationItemRepository->findByExtensionKey($extension['key']))
+                       ->assign('extension', $extension);
        }
 
        /**
index da468ff..6cd04db 100644 (file)
@@ -32,104 +32,80 @@ class ConfigurationItemRepository {
 
        /**
         * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+        * @inject
         */
        protected $objectManager;
 
        /**
-        * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
-        */
-       protected $configurationManager;
-
-       /**
-        * Injects the object manager
-        *
-        * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
-        * @return void
-        */
-       public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
-               $this->objectManager = $objectManager;
-       }
-
-       /**
-        * Inject configuration manager
-        *
-        * @param \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager
-        * @return void
-        */
-       public function injectConfigurationManager(\TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager) {
-               $this->configurationManager = $configurationManager;
-       }
-
-       /**
         * Find configuration options by extension
         *
-        * @param array $extension array with extension information
-        * @return null|\SplObjectStorage
+        * @param string $extensionKey Extension key
+        * @return \SplObjectStorage
         */
-       public function findByExtension(array $extension) {
-               $configRaw = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(
-                       PATH_site . $extension['siteRelPath'] . '/ext_conf_template.txt'
-               );
-               $configurationObjectStorage = NULL;
-               if ($configRaw) {
-                       $configurationArray = $this->convertRawConfigurationToArray($configRaw, $extension);
-                       $configurationObjectStorage = $this->convertHierarchicArrayToObject($configurationArray);
-               }
-               return $configurationObjectStorage;
+       public function findByExtensionKey($extensionKey) {
+               $configurationArray = $this->getConfigurationArrayFromExtensionKey($extensionKey);
+               return $this->convertHierarchicArrayToObject($configurationArray);
        }
 
        /**
         * Converts the raw configuration file content to an configuration object storage
         *
-        * @param string $configRaw
-        * @param array $extension array with extension information
+        * @param string $extensionKey Extension key
         * @return array
         */
-       protected function convertRawConfigurationToArray($configRaw, array $extension) {
-               $defaultConfiguration = $this->createArrayFromConstants($configRaw, $extension);
-               $metaInformation = $this->addMetaInformation($defaultConfiguration);
-               $configuration = $this->mergeWithExistingConfiguration($defaultConfiguration, $extension);
-               $hierarchicConfiguration = array();
-               foreach ($configuration as $configurationOption) {
-                       $hierarchicConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
-                               $this->buildConfigurationArray($configurationOption, $extension),
-                               $hierarchicConfiguration
-                       );
-               }
+       protected function getConfigurationArrayFromExtensionKey($extensionKey) {
+               /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
+               $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
+               $defaultConfiguration = $configurationUtility->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
+
+               $resultArray = array();
+               if (count($defaultConfiguration) > 0) {
+                       $metaInformation = $this->addMetaInformation($defaultConfiguration);
+                       $configuration = $this->mergeWithExistingConfiguration($defaultConfiguration, $extensionKey);
+                       $hierarchicConfiguration = array();
+                       foreach ($configuration as $configurationOption) {
+                               $hierarchicConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
+                                       $this->buildConfigurationArray($configurationOption, $extensionKey),
+                                       $hierarchicConfiguration
+                               );
+                       }
 
-               // Flip category array as it was merged the other way around
-               $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
+                       // Flip category array as it was merged the other way around
+                       $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
 
-               // Sort configurations of each subcategory
-               foreach ($hierarchicConfiguration as &$catConfigurationArray) {
-                       foreach ($catConfigurationArray as &$subcatConfigurationArray) {
-                               uasort($subcatConfigurationArray, function ($a, $b) {
-                                       return strnatcmp($a['subcat'], $b['subcat']);
-                               });
+                       // Sort configurations of each subcategory
+                       foreach ($hierarchicConfiguration as &$catConfigurationArray) {
+                               foreach ($catConfigurationArray as &$subcatConfigurationArray) {
+                                       uasort($subcatConfigurationArray, function ($a, $b) {
+                                               return strnatcmp($a['subcat'], $b['subcat']);
+                                       });
+                               }
+                               unset($subcatConfigurationArray);
                        }
-                       unset($subcatConfigurationArray);
+                       unset($tempConfiguration);
+
+                       $resultArray = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($hierarchicConfiguration, $metaInformation);
                }
-               unset($tempConfiguration);
 
-               return \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($hierarchicConfiguration, $metaInformation);
+               return $resultArray;
        }
 
        /**
         * Builds a configuration array from each line (option) of the config file
         *
         * @param string $configurationOption config file line representing one setting
-        * @param array $extension
+        * @param string $extensionKey Extension key
         * @return array
         */
-       protected function buildConfigurationArray($configurationOption, $extension) {
+       protected function buildConfigurationArray($configurationOption, $extensionKey) {
                $hierarchicConfiguration = array();
                if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
                        $configurationOption = $this->extractInformationForConfigFieldsOfTypeUser($configurationOption);
                } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
                        $configurationOption = $this->extractInformationForConfigFieldsOfTypeOptions($configurationOption);
                }
-               if (\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extension['key'])) {
-                       $configurationOption['label'] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extension['key']);
+               if (\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extensionKey)) {
+                       $configurationOption['label'] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extensionKey);
                }
                $configurationOption['labels'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $configurationOption['label'], FALSE, 2);
                $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ? $configurationOption['subcat_name'] : '__default';
@@ -187,91 +163,17 @@ class ConfigurationItemRepository {
        }
 
        /**
-        * Create a flat array of configuration options from
-        * incoming raw configuration file using core's typoscript parser.
-        *
-        * Generates an array from the typoscript style constants and
-        * adds meta data like TSConstantEditor comments
-        *
-        * Result is an array, with configuration item as array keys,
-        * and item properties as key-value sub-array:
-        *
-        * array(
-        *   'fooOption' => array(
-        *     'type' => 'string',
-        *     'value' => 'foo',
-        *     ...
-        *   ),
-        *   'barOption' => array(
-        *     'type' => boolean,
-        *     'default_value' => 0,
-        *     ...
-        *   ),
-        *   ...
-        * )
-        *
-        * @param string $configRaw Raw configuration string
-        * @param array $extension Extension name
-        * @return array
-        *
-        * @TODO: Code smell, this method is used in ConfigurationUtility as well.
-        *              This code should be moved elsewhere, it is not cool to have this
-        *              public method here in the repository class.
-        */
-       public function createArrayFromConstants($configRaw, array $extension) {
-               $tsStyleConfig = $this->getT3libTsStyleConfig();
-               $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
-               $theConstants = $tsStyleConfig->ext_initTSstyleConfig($configRaw, $extension['siteRelPath'], PATH_site . $extension['siteRelPath'], $GLOBALS['BACK_PATH']);
-
-               // Loop through configuration items, see if it is assigned to a sub category
-               // and add the sub category label to the item property if so.
-               foreach ($theConstants as $configurationOptionName => $configurationOption) {
-                       if (
-                               array_key_exists('subcat_name', $configurationOption)
-                               && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
-                               && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
-                       ) {
-                               $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
-                       }
-               }
-
-               // Set up the additional descriptions
-               if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
-                       foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
-                               $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
-                               foreach ($highlights as $highlightNumber => $value) {
-                                       if (rtrim($category, '.') == $theConstants[$value]['cat']) {
-                                               $theConstants[$value]['highlight'] = $highlightNumber;
-                                       }
-                               }
-                       }
-               }
-               return $theConstants;
-       }
-
-       /**
-        * Wrapper for makeInstance to make it possible to mock
-        * the class
-        *
-        * @return \TYPO3\CMS\Core\TypoScript\ConfigurationForm
-        */
-       protected function getT3libTsStyleConfig() {
-               return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
-       }
-
-       /**
-        * Merge new configuration with existing configuration
+        * Merge current local configuration over default configuration
         *
-        * @param array $configuration the new configuration array
-        * @param array $extension the extension information
+        * @param array $defaultConfiguration Default configuration from ext_conf_template.txt
+        * @param string $extensionKey the extension information
         * @return array
         */
-       protected function mergeWithExistingConfiguration(array $configuration, array $extension) {
+       protected function mergeWithExistingConfiguration(array $defaultConfiguration, $extensionKey) {
                try {
                        $currentExtensionConfig = unserialize(
-                               $this->configurationManager->getConfigurationValueByPath(
-                                       'EXT/extConf/' . $extension['key']
-                               )
+                               $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')
+                                       ->getConfigurationValueByPath('EXT/extConf/' . $extensionKey)
                        );
                } catch (\RuntimeException $e) {
                        $currentExtensionConfig = array();
@@ -281,7 +183,7 @@ class ConfigurationItemRepository {
                foreach ($flatExtensionConfig as $key => $value) {
                        $valuedCurrentExtensionConfig[$key]['value'] = $value;
                }
-               $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($configuration, $valuedCurrentExtensionConfig);
+               $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($defaultConfiguration, $valuedCurrentExtensionConfig);
                return $configuration;
        }
 
@@ -310,7 +212,7 @@ class ConfigurationItemRepository {
                                        // Set sub category label if configuration item contains a subcat label.
                                        // The sub category label is set multiple times if there is more than one item
                                        // in a sub category, but that is ok since all items of one sub category
-                                       // share the same label. @see createArrayFromConstants()
+                                       // share the same label.
                                        if (array_key_exists('subcat_label', $configurationItem)) {
                                                $configurationSubcategoryObject->setLabel($configurationItem['subcat_label']);
                                        }
index eecb8c4..59375a7 100644 (file)
@@ -27,7 +27,7 @@ namespace TYPO3\CMS\Extensionmanager\Utility;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 /**
- * Utility for dealing with ext_emconf
+ * Utility for dealing with ext_emconf and ext_conf_template settings
  *
  * @author Susanne Moog <susanne.moog@typo3.org>
  */
@@ -35,34 +35,17 @@ class ConfigurationUtility implements \TYPO3\CMS\Core\SingletonInterface {
 
        /**
         * @var \TYPO3\CMS\Extbase\Object\ObjectManager
+        * @inject
         */
        protected $objectManager;
 
        /**
-        * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository
-        */
-       protected $configurationItemRepository;
-
-       /**
-        * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
-        * @return void
-        */
-       public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
-               $this->objectManager = $objectManager;
-       }
-
-       /**
-        * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository $configurationItemRepository
-        * @return void
-        */
-       public function injectConfigurationItemRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository $configurationItemRepository) {
-               $this->configurationItemRepository = $configurationItemRepository;
-       }
-
-       /**
-        * Saves default configuration of an extension to localConfiguration
+        * Get default configuration from ext_conf_template of an extension
+        * and save as initial configuration to LocalConfiguration ['EXT']['extConf'].
         *
-        * @param string $extensionKey
+        * Used by the InstallUtility to initialize local extension config.
+        *
+        * @param string $extensionKey Extension key
         * @return void
         */
        public function saveDefaultConfiguration($extensionKey) {
@@ -72,16 +55,23 @@ class ConfigurationUtility implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
-        * Write extension specific configuration to localconf
+        * Writes extension specific configuration to LocalConfiguration file
+        * in array ['EXT']['extConf'][$extensionKey].
+        *
+        * Removes core cache files afterwards.
         *
-        * @param array $configuration
-        * @param $extensionKey
+        * This low level method expects a nested configuration array that
+        * was already merged with default configuration and maybe new form values.
+        *
+        * @param array $configuration Configuration to save
+        * @param string $extensionKey Extension key
         * @return void
         */
-       public function writeConfiguration(array $configuration, $extensionKey) {
-               /** @var $installUtility \TYPO3\CMS\Extensionmanager\Utility\InstallUtility */
-               $installUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility');
-               $installUtility->writeExtensionTypoScriptStyleConfigurationToLocalconf($extensionKey, $configuration);
+       public function writeConfiguration(array $configuration = array(), $extensionKey) {
+               /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
+               $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
+               $configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($configuration));
+               \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
        }
 
        /**
@@ -91,16 +81,110 @@ class ConfigurationUtility implements \TYPO3\CMS\Core\SingletonInterface {
         * @return array
         */
        public function getCurrentConfiguration($extensionKey) {
-               $extension = $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey];
-               $defaultConfig = $this->configurationItemRepository->createArrayFromConstants(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(PATH_site . $extension['siteRelPath'] . '/ext_conf_template.txt'), $extension);
+               $defaultConfiguration = $this->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
                $currentExtensionConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey]);
                $currentExtensionConfig = is_array($currentExtensionConfig) ? $currentExtensionConfig : array();
                $currentExtensionConfig = $this->convertNestedToValuedConfiguration($currentExtensionConfig);
-               $currentFullConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($defaultConfig, $currentExtensionConfig);
+               $currentFullConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
+                       $defaultConfiguration,
+                       $currentExtensionConfig
+               );
                return $currentFullConfiguration;
        }
 
        /**
+        * Create a flat array of configuration options from
+        * ext_conf_template.txt of an extension using core's typoscript parser.
+        *
+        * Generates an array from the typoscript style constants and
+        * adds meta data like TSConstantEditor comments
+        *
+        * Result is an array, with configuration item as array keys,
+        * and item properties as key-value sub-array:
+        *
+        * array(
+        *   'fooOption' => array(
+        *     'type' => 'string',
+        *     'value' => 'foo',
+        *     ...
+        *   ),
+        *   'barOption' => array(
+        *     'type' => boolean,
+        *     'default_value' => 0,
+        *     ...
+        *   ),
+        *   ...
+        * )
+        *
+        * @param string $extensionKey Extension key
+        * @return array
+        */
+       public function getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey) {
+               $rawConfigurationString = $this->getDefaultConfigurationRawString($extensionKey);
+
+               $theConstants = array();
+
+               if (strlen($rawConfigurationString) > 0) {
+                       $extensionPathInformation = $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey];
+
+                       $tsStyleConfig = $this->objectManager->create('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
+                       $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
+
+                       $theConstants = $tsStyleConfig->ext_initTSstyleConfig(
+                               $rawConfigurationString,
+                               $extensionPathInformation['siteRelPath'],
+                               PATH_site . $extensionPathInformation['siteRelPath'],
+                               $GLOBALS['BACK_PATH']
+                       );
+
+                       // Loop through configuration items, see if it is assigned to a sub category
+                       // and add the sub category label to the item property if so.
+                       foreach ($theConstants as $configurationOptionName => $configurationOption) {
+                               if (
+                                       array_key_exists('subcat_name', $configurationOption)
+                                       && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
+                                       && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
+                               ) {
+                                       $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
+                               }
+                       }
+
+                       // Set up the additional descriptions
+                       if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
+                               foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
+                                       $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
+                                       foreach ($highlights as $highlightNumber => $value) {
+                                               if (rtrim($category, '.') == $theConstants[$value]['cat']) {
+                                                       $theConstants[$value]['highlight'] = $highlightNumber;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               return $theConstants;
+       }
+
+       /**
+        * Return content of an extensions ext_conf_template.txt file if
+        * the file exists, empty string if file does not exist.
+        *
+        * @param string $extensionKey Extension key
+        * @return string
+        */
+       protected function getDefaultConfigurationRawString($extensionKey) {
+               $rawString = '';
+               $extConfTemplateFileLocation = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
+                       'EXT:' . $extensionKey . '/ext_conf_template.txt',
+                       FALSE
+               );
+               if (file_exists($extConfTemplateFileLocation)) {
+                       $rawString = file_get_contents($extConfTemplateFileLocation);
+               }
+               return $rawString;
+       }
+
+       /**
         * Converts a valued configuration to a nested configuration.
         *
         * array('first.second' => array('value' => 1))
@@ -138,5 +222,4 @@ class ConfigurationUtility implements \TYPO3\CMS\Core\SingletonInterface {
        }
 }
 
-
 ?>
index 8096b3a..1ba097a 100644 (file)
@@ -64,11 +64,6 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
        protected $databaseUtility;
 
        /**
-        * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
-        */
-       protected $configurationManager;
-
-       /**
         * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
         */
        public $extensionRepository;
@@ -106,16 +101,6 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
-        * Inject configuration manager
-        *
-        * @param \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager
-        * @return void
-        */
-       public function injectConfigurationManager(\TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager) {
-               $this->configurationManager = $configurationManager;
-       }
-
-       /**
         * Inject emConfUtility
         *
         * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
@@ -272,7 +257,9 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
        public function reloadCaches() {
                \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
                // Set new extlist / extlistArray for extension load changes at runtime
-               $localConfiguration = $this->configurationManager->getLocalConfiguration();
+               /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
+               $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
+               $localConfiguration = $configurationManager->getLocalConfiguration();
                $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray'] = $localConfiguration['EXT']['extListArray'];
                $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = implode(',', $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray']);
                \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->reloadTypo3LoadedExtAndClassLoaderAndExtLocalconf();
@@ -337,19 +324,6 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
-        * Writes the TSstyleconf values to "localconf.php"
-        * Removes the temp_CACHED* files before return.
-        *
-        * @param string $extensionKey Extension key
-        * @param array $newConfiguration Configuration array to write back
-        * @return void
-        */
-       public function writeExtensionTypoScriptStyleConfigurationToLocalconf($extensionKey, $newConfiguration) {
-               $this->configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($newConfiguration));
-               \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
-       }
-
-       /**
         * Remove an extension (delete the directory)
         *
         * @param string $extension
index 6674fd9..3765b6f 100644 (file)
@@ -31,40 +31,159 @@ namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Domain\Repository;
 class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
 
        /**
-        * @var boolean Backup globals
+        * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject
         */
-       protected $backupGlobals = TRUE;
+       protected $configurationItemRepository;
 
        /**
-        * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
+        * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
         */
-       public $configurationItemRepository;
+       protected $injectedObjectManagerMock;
 
        /**
+        * Set up
         *
         * @return void
         */
        public function setUp() {
+               // Mock system under test to make protected methods accessible
                $this->configurationItemRepository = $this->getAccessibleMock(
                        'TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository',
-                       array('dummy',)
-               );
-               $configurationManagerMock = $this->getAccessibleMock(
-                       'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager',
                        array('dummy')
                );
-               $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
+
+               $this->injectedObjectManagerMock = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
+               $this->configurationItemRepository->_set(
+                       'objectManager',
+                       $this->injectedObjectManagerMock
+               );
        }
 
        /**
-        * @return void
+        * @test
         */
-       public function tearDown() {
-               unset($this->configurationItemRepository);
+       public function getConfigurationArrayFromExtensionKeyReturnsSortedHierarchicArray() {
+               $flatConfigurationItemArray = array(
+                       'item1' => array(
+                               'cat' => 'basic',
+                               'subcat_name' => 'enable',
+                               'subcat' => 'a/enable/10z',
+                               'type' => 'string',
+                               'label' => 'Item 1: This is the first configuration item',
+                               'name' =>'item1',
+                               'value' => 'one',
+                               'default_value' => 'one',
+                               'subcat_label' => 'Enable features',
+                       ),
+                       'integerValue' => array(
+                               'cat' => 'basic',
+                               'subcat_name' => 'enable',
+                               'subcat' => 'a/enable/20z',
+                               'type' => 'int+',
+                               'label' => 'Integer Value: Please insert a positive integer value',
+                               'name' =>'integerValue',
+                               'value' => '1',
+                               'default_value' => '1',
+                               'subcat_label' => 'Enable features',
+                       ),
+                       'enableJquery' => array(
+                               'cat' => 'advanced',
+                               'subcat_name' => 'file',
+                               'subcat' => 'c/file/10z',
+                               'type' => 'boolean',
+                               'label' => 'enableJquery: Insert jQuery plugin',
+                               'name' =>'enableJquery',
+                               'value' => '1',
+                               'default_value' => '1',
+                               'subcat_label' => 'Files',
+                       ),
+               );
+
+               $configurationItemRepository = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository',
+                       array('mergeWithExistingConfiguration')
+               );
+               $configurationItemRepository->_set(
+                       'objectManager',
+                       $this->injectedObjectManagerMock
+               );
+               $configurationUtilityMock = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
+               $configurationUtilityMock
+                       ->expects($this->once())
+                       ->method('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
+                       ->will($this->returnValue($flatConfigurationItemArray));
+               $this->injectedObjectManagerMock
+                       ->expects($this->any())
+                       ->method('get')
+                       ->will($this->returnValue($configurationUtilityMock));
+               $configurationItemRepository
+                       ->expects($this->any())
+                       ->method('mergeWithExistingConfiguration')
+                       ->will($this->returnValue($flatConfigurationItemArray));
+
+               $expectedArray = array(
+                       'basic' => array(
+                               'enable' => array(
+                                       'item1' => array(
+                                               'cat' => 'basic',
+                                               'subcat_name' => 'enable',
+                                               'subcat' => 'a/enable/10z',
+                                               'type' => 'string',
+                                               'label' => 'Item 1: This is the first configuration item',
+                                               'name' =>'item1',
+                                               'value' => 'one',
+                                               'default_value' => 'one',
+                                               'subcat_label' => 'Enable features',
+                                               'labels' => array(
+                                                       0 => 'Item 1',
+                                                       1 => 'This is the first configuration item'
+                                               )
+                                       ),
+                                       'integerValue' => array(
+                                               'cat' => 'basic',
+                                               'subcat_name' => 'enable',
+                                               'subcat' => 'a/enable/20z',
+                                               'type' => 'int+',
+                                               'label' => 'Integer Value: Please insert a positive integer value',
+                                               'name' =>'integerValue',
+                                               'value' => '1',
+                                               'default_value' => '1',
+                                               'subcat_label' => 'Enable features',
+                                               'labels' => array(
+                                                       0 => 'Integer Value',
+                                                       1 => 'Please insert a positive integer value'
+                                               )
+                                       )
+                               )
+                       ),
+                       'advanced' => array(
+                               'file' => array(
+                                       'enableJquery' => array(
+                                               'cat' => 'advanced',
+                                               'subcat_name' => 'file',
+                                               'subcat' => 'c/file/10z',
+                                               'type' => 'boolean',
+                                               'label' => 'enableJquery: Insert jQuery plugin',
+                                               'name' =>'enableJquery',
+                                               'value' => '1',
+                                               'default_value' => '1',
+                                               'subcat_label' => 'Files',
+                                               'labels' => array(
+                                                       0 => 'enableJquery',
+                                                       1 => 'Insert jQuery plugin'
+                                               )
+                                       ),
+                               )
+                       )
+               );
+
+               $this->assertSame(
+                       $expectedArray,
+                       $configurationItemRepository->_call('getConfigurationArrayFromExtensionKey', uniqid('some_extension'))
+               );
        }
 
        /**
-        *
         * @test
         * @return void
         */
@@ -91,7 +210,6 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
        }
 
        /**
-        *
         * @return array
         */
        public function extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider() {
@@ -139,7 +257,6 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
        }
 
        /**
-        *
         * @test
         * @dataProvider extractInformationForConfigFieldsOfTypeUserAddsGenericAndTypeInformationDataProvider
         * @param $configurationOption
@@ -152,7 +269,6 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
        }
 
        /**
-        *
         * @test
         * @return void
         */
@@ -182,7 +298,6 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
        }
 
        /**
-        *
         * @test
         * @return void
         */
@@ -212,68 +327,80 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
        }
 
        /**
-        *
         * @test
         * @return void
         */
-       public function mergeDefaultConfigurationWithNoCurrentValuesReturnsTheDefaultConfiguration() {
-
-                       // @TODO: Possible tests that can be added if ConfigurationManager is not static
-                       // and can be mocked:
-               /*
-                       // Value is set to null
-               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = NULL;
-               $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
-               $this->assertEquals($defaultConfiguration, $configuration);
-
-                       // Value is set to integer
-               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 123;
-               $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
-               $this->assertEquals($defaultConfiguration, $configuration);
-
-                       // valid configuration value - an empty serialized array
-               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = 'a:0:{}';
-               $configuration = $this->configurationItemRepository->mergeWithExistingConfiguration($defaultConfiguration, $extension);
-               $this->assertEquals($defaultConfiguration, $configuration);
-               */
-
-               $extensionKey = 'some_non_existing_extension';
-               $extension = array(
-                       'key' => $extensionKey
+       public function mergeDefaultConfigurationCatchesExceptionOfConfigurationManagerIfNoLocalConfigurationExists() {
+               $exception = $this->getMock('RuntimeException');
+               $configurationManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
+               $configurationManagerMock
+                       ->expects($this->once())
+                       ->method('getConfigurationValueByPath')
+                       ->will($this->throwException($exception));
+               $this->injectedObjectManagerMock
+                       ->expects($this->any())
+                       ->method('get')
+                       ->will($this->returnValue($configurationManagerMock));
+
+               $this->configurationItemRepository->_call(
+                       'mergeWithExistingConfiguration',
+                       array(),
+                       uniqid('not_existing_extension')
                );
+       }
+
+       /**
+        * @test
+        * @return void
+        */
+       public function mergeDefaultConfigurationWithNoCurrentValuesReturnsTheDefaultConfiguration() {
+               $exception = $this->getMock('RuntimeException');
+               $configurationManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
+               $configurationManagerMock
+                       ->expects($this->once())
+                       ->method('getConfigurationValueByPath')
+                       ->will($this->throwException($exception));
+               $this->injectedObjectManagerMock
+                       ->expects($this->any())
+                       ->method('get')
+                       ->will($this->returnValue($configurationManagerMock));
                $defaultConfiguration = array(
                        'foo' => 'bar'
                );
-
-               // No value is set
-               $configuration = $this->configurationItemRepository->_callRef('mergeWithExistingConfiguration', $defaultConfiguration, $extension);
+               $configuration = $this->configurationItemRepository->_call(
+                       'mergeWithExistingConfiguration',
+                       $defaultConfiguration,
+                       uniqid('not_existing_extension')
+               );
                $this->assertEquals($defaultConfiguration, $configuration);
        }
 
        /**
-        *
         * @test
         * @return void
         */
        public function mergeWithExistingConfigurationOverwritesDefaultKeysWithCurrent() {
-               $configurationManagerMock = $this->getAccessibleMock(
-                       'TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
+               $localConfiguration = serialize(array(
+                       'FE.' => array(
+                               'enabled' => '1',
+                               'saltedPWHashingMethod' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
+                       ),
+                       'CLI.' => array(
+                               'enabled' => '0'
+                       )
+               ));
+
+               $configurationManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
                $configurationManagerMock
-                               ->expects($this->once())
-                               ->method('getConfigurationValueByPath')
-                               ->with('EXT/extConf/testextensionkey')
-                               ->will($this->returnValue(
-                                       serialize(array(
-                                               'FE.' => array(
-                                                       'enabled' => '1',
-                                                       'saltedPWHashingMethod' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\SaltInterface_sha1'
-                                               ),
-                                               'CLI.' => array(
-                                                       'enabled' => '0'
-                                               )
-                                       ))
-                               ));
-               $this->configurationItemRepository->injectConfigurationManager($configurationManagerMock);
+                       ->expects($this->once())
+                       ->method('getConfigurationValueByPath')
+                       ->with('EXT/extConf/testextensionkey')
+                       ->will($this->returnValue($localConfiguration));
+
+               $this->injectedObjectManagerMock
+                       ->expects($this->any())
+                       ->method('get')
+                       ->will($this->returnValue($configurationManagerMock));
 
                $defaultConfiguration = array(
                        'FE.enabled' => array(
@@ -289,6 +416,7 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
                                'value' => 'TYPO3\\CMS\\Saltedpasswords\\Salt\\Md5Salt'
                        )
                );
+
                $expectedResult = array(
                        'FE.enabled' => array(
                                'value' => '1'
@@ -306,203 +434,15 @@ class ConfigurationItemRepositoryTest extends \TYPO3\CMS\Extbase\Tests\Unit\Base
                                'value' => '0'
                        )
                );
-               $result = $this->configurationItemRepository->_call('mergeWithExistingConfiguration', $defaultConfiguration, array('key' => 'testextensionkey'));
-               $this->assertEquals($expectedResult, $result);
-       }
-
-       /**
-        *
-        * @return array
-        */
-       public function createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider() {
-               return array(
-                       'demo data from salted passwords' => array(
-                               '
-# cat=basic/enable; type=user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]; label=Frontend configuration check
-checkConfigurationFE=0
 
-# cat=advancedBackend; type=boolean; label=Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.
-BE.forceSalted = 0
-
-TSConstantEditor.advancedbackend {
-  description = <span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!
-                       1=BE.forceSalted
-}
-                               ',
-                               array(
-                                       'checkConfigurationFE' => array(
-                                               'cat' => 'basic',
-                                               'subcat_name' => 'enable',
-                                               'subcat' => 'a/enable/z',
-                                               'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
-                                               'label' => 'Frontend configuration check',
-                                               'name' => 'checkConfigurationFE',
-                                               'value' => '0',
-                                               'default_value' => '0'
-                                       ),
-                                       'BE.forceSalted' => array(
-                                               'cat' => 'advancedbackend',
-                                               'subcat' => 'x/z',
-                                               'type' => 'boolean',
-                                               'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
-                                               'name' => 'BE.forceSalted',
-                                               'value' => '0',
-                                               'default_value' => '0'
-                                       )
-                               ),
-                               array(
-                                       'advancedbackend.' => array(
-                                               'description' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!',
-                                               1 => 'BE.forceSalted'
-                                       )
-                               ),
-                               array(
-                                       'checkConfigurationFE' => array(
-                                               'cat' => 'basic',
-                                               'subcat_name' => 'enable',
-                                               'subcat' => 'a/enable/z',
-                                               'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
-                                               'label' => 'Frontend configuration check',
-                                               'name' => 'checkConfigurationFE',
-                                               'value' => '0',
-                                               'default_value' => '0',
-                                               'subcat_label' => 'Enable features',
-                                       ),
-                                       'BE.forceSalted' => array(
-                                               'cat' => 'advancedbackend',
-                                               'subcat' => 'x/z',
-                                               'type' => 'boolean',
-                                               'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
-                                               'name' => 'BE.forceSalted',
-                                               'value' => '0',
-                                               'default_value' => '0',
-                                               'highlight' => 1,
-                                       ),
-                                       '__meta__' => array(
-                                               'advancedbackend' => array(
-                                                       'highlightText' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!'
-                                               )
-                                       )
-                               )
-                       )
+               $actualResult = $this->configurationItemRepository->_call(
+                       'mergeWithExistingConfiguration',
+                       $defaultConfiguration,
+                       'testextensionkey'
                );
-       }
-
-       /**
-        *
-        * @test
-        * @dataProvider createArrayFromConstantsCreatesAnArrayWithMetaInformationDataProvider
-        * @param $raw
-        * @param $constants
-        * @param $setupTsConstantEditor
-        * @param $expected
-        * @return void
-        *
-        * @TODO: Split this test to multiple tests:
-        *              test getT3libTsStyleConfig is called with $raw
-        *              test TSConstantEditor. is considered in result
-        *              test subcat_label is correctly extracted (see method)
-        */
-       public function createArrayFromConstantsCreatesAnArrayWithMetaInformation($raw, $constants, $setupTsConstantEditor, $expected) {
-               $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
-               $configurationItemRepositoryMock = $this->getMock(
-                       'TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ConfigurationItemRepository',
-                       array('getT3libTsStyleConfig')
-               );
-               $configurationItemRepositoryMock
-                       ->expects($this->once())
-                       ->method('getT3libTsStyleConfig')
-                       ->will($this->returnValue($tsStyleConfig));
-               $tsStyleConfig
-                       ->expects($this->once())
-                       ->method('ext_initTSstyleConfig')
-                       ->with($raw, $this->anything(), $this->anything(), $this->anything())
-                       ->will($this->returnValue($constants));
-               $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
 
-               $constantsResult = $configurationItemRepositoryMock->createArrayFromConstants($raw, array());
-               $this->assertEquals($expected, $constantsResult);
+               $this->assertEquals($expectedResult, $actualResult);
        }
-
-       /**
-        * @test
-        *
-        * @TODO: This test still uses internal knowledge of createArrayFromConstants() which it shouldn't
-        *              Instead, it should mock createArrayFromConstants(), use a given result array and not the raw config string.
-        */
-       public function convertRawConfigurationToArrayReturnsSortedHierarchicArray() {
-               $configRaw = '# cat=basic/enable/10; type=string; label=Item 1: This is the first configuration item
-item1 = one
-
-# cat=basic/enable/20; type=int+; label=Integer Value: Please insert a positive integer value
-integerValue = 1
-
-# cat=advanced/file/10; type=boolean; label=enableJquery: Insert jQuery plugin
-enableJquery = 1';
-               $extension = array();
-
-               $expectedArray = array(
-                       'basic' => array(
-                               'enable' => array(
-                                       'item1' => array(
-                                               'cat' => 'basic',
-                                               'subcat_name' => 'enable',
-                                               'subcat' => 'a/enable/10z',
-                                               'type' => 'string',
-                                               'label' => 'Item 1: This is the first configuration item',
-                                               'name' =>'item1',
-                                               'value' => 'one',
-                                               'default_value' => 'one',
-                                               'subcat_label' => 'Enable features',
-                                               'labels' => array(
-                                                       0 => 'Item 1',
-                                                       1 => 'This is the first configuration item'
-                                               )
-                                       ),
-                                       'integerValue' => array(
-                                               'cat' => 'basic',
-                                               'subcat_name' => 'enable',
-                                               'subcat' => 'a/enable/20z',
-                                               'type' => 'int+',
-                                               'label' => 'Integer Value: Please insert a positive integer value',
-                                               'name' =>'integerValue',
-                                               'value' => '1',
-                                               'default_value' => '1',
-                                               'subcat_label' => 'Enable features',
-                                               'labels' => array(
-                                                       0 => 'Integer Value',
-                                                       1 => 'Please insert a positive integer value'
-                                               )
-                                       )
-                               )
-                       ),
-                       'advanced' => array(
-                               'file' => array(
-                                       'enableJquery' => array(
-                                               'cat' => 'advanced',
-                                               'subcat_name' => 'file',
-                                               'subcat' => 'c/file/10z',
-                                               'type' => 'boolean',
-                                               'label' => 'enableJquery: Insert jQuery plugin',
-                                               'name' =>'enableJquery',
-                                               'value' => '1',
-                                               'default_value' => '1',
-                                               'subcat_label' => 'Files',
-                                               'labels' => array(
-                                                       0 => 'enableJquery',
-                                                       1 => 'Insert jQuery plugin'
-                                               )
-                                       ),
-                               )
-                       )
-               );
-
-               $this->assertSame(
-                       $expectedArray,
-                       $this->configurationItemRepository->_callRef('convertRawConfigurationToArray', $configRaw, $extension)
-               );
-       }
-
 }
 
 
index f0124f4..bb81e5c 100644 (file)
@@ -31,18 +31,136 @@ namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Utility;
 class ConfigurationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
 
        /**
-        * @param array $configuration
-        * @param array $expected
-        * @dataProvider convertValuedToNestedConfigurationDataProvider
         * @test
         */
-       public function convertValuedToNestedConfiguration(array $configuration, array $expected) {
-               /** @var $fixture \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
-               $fixture = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
-               $this->assertEquals($expected, $fixture->convertValuedToNestedConfiguration($configuration));
+       public function getCurrentConfigurationReturnsExtensionConfigurationAsValuedConfiguration() {
+               /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
+               $configurationUtility = $this->getMock(
+                       'TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility',
+                       array('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
+               );
+               $configurationUtility
+                       ->expects($this->once())
+                       ->method('getDefaultConfigurationFromExtConfTemplateAsValuedArray')
+                       ->will($this->returnValue(array()));
+               $extensionKey = uniqid('some-extension');
+
+               $currentConfiguration = array(
+                       'key1' => 'value1',
+                       'key2.' => array(
+                               'subkey1' => 'value2'
+                       )
+               );
+
+               $expected = array(
+                       'key1' => array(
+                               'value' => 'value1',
+                       ),
+                       'key2.subkey1' => array(
+                               'value' => 'value2',
+                       ),
+               );
+
+               $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey]= array();
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey] = serialize($currentConfiguration);
+               $actual = $configurationUtility->getCurrentConfiguration($extensionKey);
+               $this->assertEquals($expected, $actual);
+       }
+
+       /**
+        * @test
+        */
+       public function getDefaultConfigurationFromExtConfTemplateAsValuedArrayReturnsExpectedExampleArray() {
+               /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
+               $configurationUtility = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility',
+                       array('getDefaultConfigurationRawString')
+               );
+               $configurationUtility
+                       ->expects($this->once())
+                       ->method('getDefaultConfigurationRawString')
+                       ->will($this->returnValue('foo'));
+
+               $tsStyleConfig = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
+
+               $objectManagerMock = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
+               $configurationUtility->_set('objectManager', $objectManagerMock);
+               $objectManagerMock
+                       ->expects($this->once())
+                       ->method('create')
+                       ->with('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm')
+                       ->will($this->returnValue($tsStyleConfig));
+
+               $constants = array(
+                       'checkConfigurationFE' => array(
+                               'cat' => 'basic',
+                               'subcat_name' => 'enable',
+                               'subcat' => 'a/enable/z',
+                               'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
+                               'label' => 'Frontend configuration check',
+                               'name' => 'checkConfigurationFE',
+                               'value' => '0',
+                               'default_value' => '0'
+                       ),
+                       'BE.forceSalted' => array(
+                               'cat' => 'advancedbackend',
+                               'subcat' => 'x/z',
+                               'type' => 'boolean',
+                               'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
+                               'name' => 'BE.forceSalted',
+                               'value' => '0',
+                               'default_value' => '0'
+                       )
+               );
+               $tsStyleConfig
+                       ->expects($this->once())
+                       ->method('ext_initTSstyleConfig')
+                       ->will($this->returnValue($constants));
+
+               $setupTsConstantEditor = array(
+                       'advancedbackend.' => array(
+                               'description' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!',
+                               1 => 'BE.forceSalted'
+                       )
+               );
+               $tsStyleConfig->setup['constants']['TSConstantEditor.'] = $setupTsConstantEditor;
+
+               $expected = array(
+                       'checkConfigurationFE' => array(
+                               'cat' => 'basic',
+                               'subcat_name' => 'enable',
+                               'subcat' => 'a/enable/z',
+                               'type' => 'user[EXT:saltedpasswords/classes/class.tx_saltedpasswords_emconfhelper.php:TYPO3\\CMS\\Saltedpasswords\\Utility\\ExtensionManagerConfigurationUtility->checkConfigurationFrontend]',
+                               'label' => 'Frontend configuration check',
+                               'name' => 'checkConfigurationFE',
+                               'value' => '0',
+                               'default_value' => '0',
+                               'subcat_label' => 'Enable features',
+                       ),
+                       'BE.forceSalted' => array(
+                               'cat' => 'advancedbackend',
+                               'subcat' => 'x/z',
+                               'type' => 'boolean',
+                               'label' => 'Force salted passwords: Enforce usage of SaltedPasswords. Old MD5 hashed passwords will stop working.',
+                               'name' => 'BE.forceSalted',
+                               'value' => '0',
+                               'default_value' => '0',
+                               'highlight' => 1,
+                       ),
+                       '__meta__' => array(
+                               'advancedbackend' => array(
+                                       'highlightText' => '<span style="background:red; padding:1px 2px; color:#fff; font-weight:bold;">1</span> Install tool has hardcoded md5 hashing, enabling this setting will prevent use of a install-tool-created BE user.<br />Currently same is for changin password with user setup module unless you use pending patch!'
+                               )
+                       )
+               );
+
+               $result = $configurationUtility->getDefaultConfigurationFromExtConfTemplateAsValuedArray(uniqid('some_extension'));
+               $this->assertEquals($expected, $result);
        }
 
        /**
+        * Data provider for convertValuedToNestedConfiguration
+        *
         * @return array
         */
        public function convertValuedToNestedConfigurationDataProvider() {
@@ -127,18 +245,22 @@ class ConfigurationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCas
        }
 
        /**
+        * @test
+        * @dataProvider convertValuedToNestedConfigurationDataProvider
+        *
         * @param array $configuration
         * @param array $expected
-        * @test
-        * @dataProvider convertNestedToValuedConfigurationDataProvider
+        * @return void
         */
-       public function convertNestedToValuedConfiguration(array $configuration, array $expected) {
+       public function convertValuedToNestedConfiguration(array $configuration, array $expected) {
                /** @var $fixture \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
                $fixture = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
-               $this->assertEquals($expected, $fixture->convertNestedToValuedConfiguration($configuration));
+               $this->assertEquals($expected, $fixture->convertValuedToNestedConfiguration($configuration));
        }
 
        /**
+        * Data provider for convertNestedToValuedConfiguration
+        *
         * @return array
         */
        public function convertNestedToValuedConfigurationDataProvider() {
@@ -190,23 +312,16 @@ class ConfigurationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCas
 
        /**
         * @test
+        * @dataProvider convertNestedToValuedConfigurationDataProvider
+        *
+        * @param array $configuration
+        * @param array $expected
+        * @return void
         */
-       public function thatGetCurrentConfigurationReturnsCorrectExtensionConfigurationAsValuedConfiguration() {
-               $mockedConfigurationItemRepository = $this->getAccessibleMock('\TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository', array(
-                       'createArrayFromConstants'
-               ));
-               $mockedConfigurationItemRepository->expects($this->any())
-                       ->method('createArrayFromConstants')
-                       ->will($this->returnValue(array()));
-               $GLOBALS['TYPO3_LOADED_EXT']['MY_DUMMY_EXT']= array();
-               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['MY_DUMMY_EXT'] = serialize(array('key1' => 'value1', 'key2.' => array('subkey1' => 'value2')));
-               $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
-               $configurationUtility->injectConfigurationItemRepository($mockedConfigurationItemRepository);
-
-               $configuration = $configurationUtility->getCurrentConfiguration('MY_DUMMY_EXT');
-               $this->assertEquals('value1', $configuration['key1']['value']);
-               $this->assertEquals('value2', $configuration['key2.subkey1']['value']);
+       public function convertNestedToValuedConfiguration(array $configuration, array $expected) {
+               /** @var $fixture \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
+               $fixture = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
+               $this->assertEquals($expected, $fixture->convertNestedToValuedConfiguration($configuration));
        }
-
 }
 ?>