]> git.mxchange.org Git - friendica.git/commitdiff
Added first version of ConfigCacheSaver
authorPhilipp Holzer <admin@philipp.info>
Sat, 23 Mar 2019 14:37:05 +0000 (15:37 +0100)
committerPhilipp Holzer <admin@philipp.info>
Sat, 23 Mar 2019 18:34:54 +0000 (19:34 +0100)
16 files changed:
src/App.php
src/Core/Config/Cache/ConfigCacheLoader.php [deleted file]
src/Core/Config/Configuration.php
src/Core/Console/AutomaticInstallation.php
src/Factory/ConfigFactory.php
src/Factory/DependencyFactory.php
src/Util/Config/ConfigCacheLoader.php [new file with mode: 0644]
src/Util/Config/ConfigCacheManager.php [new file with mode: 0644]
src/Util/Config/ConfigCacheSaver.php [new file with mode: 0644]
tests/DatabaseTest.php
tests/include/ApiTest.php
tests/src/Core/Config/Cache/ConfigCacheLoaderTest.php [deleted file]
tests/src/Database/DBATest.php
tests/src/Database/DBStructureTest.php
tests/src/Util/Config/ConfigCacheLoaderTest.php [new file with mode: 0644]
tests/src/Util/Config/ConfigCacheSaverTest.php [new file with mode: 0644]

index 7893cbe704eb6be664bb0d0beb1891e718de66c9..7c88f0f02152b6b66b8aaf90406dd451e0642132 100644 (file)
@@ -8,12 +8,12 @@ use Detection\MobileDetect;
 use DOMDocument;
 use DOMXPath;
 use Exception;
-use Friendica\Core\Config\Cache\ConfigCacheLoader;
 use Friendica\Core\Config\Cache\IConfigCache;
 use Friendica\Core\Config\Configuration;
 use Friendica\Database\DBA;
 use Friendica\Model\Profile;
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\Config\ConfigCacheLoader;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
diff --git a/src/Core/Config/Cache/ConfigCacheLoader.php b/src/Core/Config/Cache/ConfigCacheLoader.php
deleted file mode 100644 (file)
index 9e06d8f..0000000
+++ /dev/null
@@ -1,231 +0,0 @@
-<?php
-
-namespace Friendica\Core\Config\Cache;
-
-use Friendica\App;
-use Friendica\Core\Addon;
-
-/**
- * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
- *
- * It is capable of loading the following config files:
- * - *.config.php   (current)
- * - *.ini.php      (deprecated)
- * - *.htconfig.php (deprecated)
- */
-class ConfigCacheLoader
-{
-       /**
-        * The Sub directory of the config-files
-        * @var string
-        */
-       const SUBDIRECTORY = 'config';
-
-       private $baseDir;
-       private $configDir;
-
-       /**
-        * @var App\Mode
-        */
-       private $appMode;
-
-       public function __construct($baseDir, App\Mode $mode)
-       {
-               $this->appMode = $mode;
-               $this->baseDir = $baseDir;
-               $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
-       }
-
-       /**
-        * Load the configuration files
-        *
-        * First loads the default value for all the configuration keys, then the legacy configuration files, then the
-        * expected local.config.php
-        *
-        * @param IConfigCache The config cache to load to
-        *
-        * @throws \Exception
-        */
-       public function loadConfigFiles(IConfigCache $config)
-       {
-               $config->load($this->loadCoreConfig('defaults'));
-               $config->load($this->loadCoreConfig('settings'));
-
-               $config->load($this->loadLegacyConfig('htpreconfig'), true);
-               $config->load($this->loadLegacyConfig('htconfig'), true);
-
-               $config->load($this->loadCoreConfig('local'), true);
-
-               // In case of install mode, add the found basepath (because there isn't a basepath set yet
-               if ($this->appMode->isInstall()) {
-                       // Setting at least the basepath we know
-                       $config->set('system', 'basepath', $this->baseDir);
-               }
-       }
-
-       /**
-        * Tries to load the specified core-configuration and returns the config array.
-        *
-        * @param string $name The name of the configuration
-        *
-        * @return array The config array (empty if no config found)
-        *
-        * @throws \Exception if the configuration file isn't readable
-        */
-       public function loadCoreConfig($name)
-       {
-               if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
-                       return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
-               } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
-                       return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
-               } else {
-                       return [];
-               }
-       }
-
-       /**
-        * Tries to load the specified addon-configuration and returns the config array.
-        *
-        * @param string $name The name of the configuration
-        *
-        * @return array The config array (empty if no config found)
-        *
-        * @throws \Exception if the configuration file isn't readable
-        */
-       public function loadAddonConfig($name)
-       {
-               $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
-                       Addon::DIRECTORY       . DIRECTORY_SEPARATOR . // addon/
-                       $name                  . DIRECTORY_SEPARATOR . // openstreetmap/
-                       self::SUBDIRECTORY     . DIRECTORY_SEPARATOR . // config/
-                       $name . ".config.php";                         // openstreetmap.config.php
-
-               if (file_exists($filepath)) {
-                       return $this->loadConfigFile($filepath);
-               } else {
-                       return [];
-               }
-       }
-
-       /**
-        * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
-        *
-        * @param string $name The name of the config file
-        *
-        * @return array The configuration array (empty if no config found)
-        *
-        * @deprecated since version 2018.09
-        */
-       private function loadLegacyConfig($name)
-       {
-               $filePath = $this->baseDir  . DIRECTORY_SEPARATOR . '.' . $name . '.php';
-
-               $config = [];
-
-               if (file_exists($filePath)) {
-                       $a = new \stdClass();
-                       $a->config = [];
-                       include $filePath;
-
-                       $htConfigCategories = array_keys($a->config);
-
-                       // map the legacy configuration structure to the current structure
-                       foreach ($htConfigCategories as $htConfigCategory) {
-                               if (is_array($a->config[$htConfigCategory])) {
-                                       $keys = array_keys($a->config[$htConfigCategory]);
-
-                                       foreach ($keys as $key) {
-                                               $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
-                                       }
-                               } else {
-                                       $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
-                               }
-                       }
-
-                       unset($a);
-
-                       if (isset($db_host)) {
-                               $config['database']['hostname'] = $db_host;
-                               unset($db_host);
-                       }
-                       if (isset($db_user)) {
-                               $config['database']['username'] = $db_user;
-                               unset($db_user);
-                       }
-                       if (isset($db_pass)) {
-                               $config['database']['password'] = $db_pass;
-                               unset($db_pass);
-                       }
-                       if (isset($db_data)) {
-                               $config['database']['database'] = $db_data;
-                               unset($db_data);
-                       }
-                       if (isset($config['system']['db_charset'])) {
-                               $config['database']['charset'] = $config['system']['db_charset'];
-                       }
-                       if (isset($pidfile)) {
-                               $config['system']['pidfile'] = $pidfile;
-                               unset($pidfile);
-                       }
-                       if (isset($default_timezone)) {
-                               $config['system']['default_timezone'] = $default_timezone;
-                               unset($default_timezone);
-                       }
-                       if (isset($lang)) {
-                               $config['system']['language'] = $lang;
-                               unset($lang);
-                       }
-               }
-
-               return $config;
-       }
-
-       /**
-        * Tries to load the specified legacy configuration file and returns the config array.
-        *
-        * @deprecated since version 2018.12
-        * @param string $filepath
-        *
-        * @return array The configuration array
-        * @throws \Exception
-        */
-       private function loadINIConfigFile($filepath)
-       {
-               $contents = include($filepath);
-
-               $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
-
-               if ($config === false) {
-                       throw new \Exception('Error parsing INI config file ' . $filepath);
-               }
-
-               return $config;
-       }
-
-       /**
-        * Tries to load the specified configuration file and returns the config array.
-        *
-        * The config format is PHP array and the template for configuration files is the following:
-        *
-        * <?php return [
-        *      'section' => [
-        *          'key' => 'value',
-        *      ],
-        * ];
-        *
-        * @param  string $filepath The filepath of the
-        * @return array The config array0
-        *
-        * @throws \Exception if the config cannot get loaded.
-        */
-       private function loadConfigFile($filepath)
-       {
-               $config = include($filepath);
-
-               if (!is_array($config)) {
-                       throw new \Exception('Error loading config file ' . $filepath);
-               }
-
-               return $config;
-       }
-}
index 532ed982a9d46498d3de738e741f58db33b26cbc..abf6af37ca38e68a3056720f6594cc008bf82f03 100644 (file)
@@ -10,6 +10,16 @@ namespace Friendica\Core\Config;
  */
 class Configuration
 {
+       /**
+        * The blacklist of configuration settings, which should not get saved to the backend
+        * @var array
+        */
+       private $configSaveBlacklist = [
+               'config' => [
+                       'hostname' => true,
+               ]
+       ];
+
        /**
         * @var Cache\IConfigCache
         */
@@ -117,7 +127,7 @@ class Configuration
                $cached = $this->configCache->set($cat, $key, $value);
 
                // If there is no connected adapter, we're finished
-               if (!$this->configAdapter->isConnected()) {
+               if (!$this->configAdapter->isConnected() && !empty($this->configSaveBlacklist[$cat][$key])) {
                        return $cached;
                }
 
index 280e3d10710091ae33a902d11caacf076df31d51..0c73c82b1b670297c95cc2ee043247b980cd74f4 100644 (file)
@@ -7,6 +7,7 @@ use Friendica\BaseObject;
 use Friendica\Core\Config;
 use Friendica\Core\Installer;
 use Friendica\Core\Theme;
+use Friendica\Util\Config\ConfigCacheLoader;
 use RuntimeException;
 
 class AutomaticInstallation extends Console
@@ -103,7 +104,7 @@ HELP;
                        }
 
                        //reload the config cache
-                       $loader = new Config\Cache\ConfigCacheLoader($a->getBasePath(), $a->getMode());
+                       $loader = new ConfigCacheLoader($a->getBasePath(), $a->getMode());
                        $loader->loadConfigFiles($configCache);
 
                } else {
index 6a30cf0e0577d269b0468948ade177669d3826e3..7a281d97a54980840f0484c0879812ee0cb6e175 100644 (file)
@@ -6,15 +6,16 @@ use Friendica\Core;
 use Friendica\Core\Config;
 use Friendica\Core\Config\Adapter;
 use Friendica\Core\Config\Cache;
+use Friendica\Util\Config\ConfigCacheLoader;
 
 class ConfigFactory
 {
        /**
-        * @param Cache\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
+        * @param ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
         *
         * @return Cache\ConfigCache
         */
-       public static function createCache(Cache\ConfigCacheLoader $loader)
+       public static function createCache(ConfigCacheLoader $loader)
        {
                $configCache = new Cache\ConfigCache();
                $loader->loadConfigFiles($configCache);
index 265dca88ef7ba51db7702e4d3f792148c75a84b0..9322a44cfe69e5894aae976a95e2727a97dafa89 100644 (file)
@@ -3,9 +3,9 @@
 namespace Friendica\Factory;
 
 use Friendica\App;
-use Friendica\Core\Config\Cache;
 use Friendica\Factory;
 use Friendica\Util\BasePath;
+use Friendica\Util\Config;
 
 class DependencyFactory
 {
@@ -24,7 +24,7 @@ class DependencyFactory
        {
                $basePath = BasePath::create($directory, $_SERVER);
                $mode = new App\Mode($basePath);
-               $configLoader = new Cache\ConfigCacheLoader($basePath, $mode);
+               $configLoader = new Config\ConfigCacheLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
                Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
diff --git a/src/Util/Config/ConfigCacheLoader.php b/src/Util/Config/ConfigCacheLoader.php
new file mode 100644 (file)
index 0000000..f2f4f75
--- /dev/null
@@ -0,0 +1,219 @@
+<?php
+
+namespace Friendica\Util\Config;
+
+use Friendica\App;
+use Friendica\Core\Addon;
+use Friendica\Core\Config\Cache\IConfigCache;
+
+/**
+ * The ConfigCacheLoader loads config-files and stores them in a IConfigCache ( @see IConfigCache )
+ *
+ * It is capable of loading the following config files:
+ * - *.config.php   (current)
+ * - *.ini.php      (deprecated)
+ * - *.htconfig.php (deprecated)
+ */
+class ConfigCacheLoader extends ConfigCacheManager
+{
+       /**
+        * @var App\Mode
+        */
+       private $appMode;
+
+       public function __construct($baseDir, App\Mode $mode)
+       {
+               parent::__construct($baseDir);
+               $this->appMode = $mode;
+       }
+
+       /**
+        * Load the configuration files
+        *
+        * First loads the default value for all the configuration keys, then the legacy configuration files, then the
+        * expected local.config.php
+        *
+        * @param IConfigCache The config cache to load to
+        *
+        * @throws \Exception
+        */
+       public function loadConfigFiles(IConfigCache $config)
+       {
+               $config->load($this->loadCoreConfig('defaults'));
+               $config->load($this->loadCoreConfig('settings'));
+
+               $config->load($this->loadLegacyConfig('htpreconfig'), true);
+               $config->load($this->loadLegacyConfig('htconfig'), true);
+
+               $config->load($this->loadCoreConfig('local'), true);
+
+               // In case of install mode, add the found basepath (because there isn't a basepath set yet
+               if ($this->appMode->isInstall()) {
+                       // Setting at least the basepath we know
+                       $config->set('system', 'basepath', $this->baseDir);
+               }
+       }
+
+       /**
+        * Tries to load the specified core-configuration and returns the config array.
+        *
+        * @param string $name The name of the configuration
+        *
+        * @return array The config array (empty if no config found)
+        *
+        * @throws \Exception if the configuration file isn't readable
+        */
+       public function loadCoreConfig($name)
+       {
+               if (!empty($this->getConfigFullName($name))) {
+                       return $this->loadConfigFile($this->getConfigFullName($name));
+               } elseif (!empty($this->getIniFullName($name))) {
+                       return $this->loadINIConfigFile($this->getIniFullName($name));
+               } else {
+                       return [];
+               }
+       }
+
+       /**
+        * Tries to load the specified addon-configuration and returns the config array.
+        *
+        * @param string $name The name of the configuration
+        *
+        * @return array The config array (empty if no config found)
+        *
+        * @throws \Exception if the configuration file isn't readable
+        */
+       public function loadAddonConfig($name)
+       {
+               $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
+                       Addon::DIRECTORY       . DIRECTORY_SEPARATOR . // addon/
+                       $name                  . DIRECTORY_SEPARATOR . // openstreetmap/
+                       self::SUBDIRECTORY     . DIRECTORY_SEPARATOR . // config/
+                       $name . ".config.php";                         // openstreetmap.config.php
+
+               if (file_exists($filepath)) {
+                       return $this->loadConfigFile($filepath);
+               } else {
+                       return [];
+               }
+       }
+
+       /**
+        * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
+        *
+        * @param string $name The name of the config file
+        *
+        * @return array The configuration array (empty if no config found)
+        *
+        * @deprecated since version 2018.09
+        */
+       private function loadLegacyConfig($name)
+       {
+               $config = [];
+               if (!empty($this->getHtConfigFullName($name))) {
+                       $a = new \stdClass();
+                       $a->config = [];
+                       include $this->getHtConfigFullName($name);
+
+                       $htConfigCategories = array_keys($a->config);
+
+                       // map the legacy configuration structure to the current structure
+                       foreach ($htConfigCategories as $htConfigCategory) {
+                               if (is_array($a->config[$htConfigCategory])) {
+                                       $keys = array_keys($a->config[$htConfigCategory]);
+
+                                       foreach ($keys as $key) {
+                                               $config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
+                                       }
+                               } else {
+                                       $config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
+                               }
+                       }
+
+                       unset($a);
+
+                       if (isset($db_host)) {
+                               $config['database']['hostname'] = $db_host;
+                               unset($db_host);
+                       }
+                       if (isset($db_user)) {
+                               $config['database']['username'] = $db_user;
+                               unset($db_user);
+                       }
+                       if (isset($db_pass)) {
+                               $config['database']['password'] = $db_pass;
+                               unset($db_pass);
+                       }
+                       if (isset($db_data)) {
+                               $config['database']['database'] = $db_data;
+                               unset($db_data);
+                       }
+                       if (isset($config['system']['db_charset'])) {
+                               $config['database']['charset'] = $config['system']['db_charset'];
+                       }
+                       if (isset($pidfile)) {
+                               $config['system']['pidfile'] = $pidfile;
+                               unset($pidfile);
+                       }
+                       if (isset($default_timezone)) {
+                               $config['system']['default_timezone'] = $default_timezone;
+                               unset($default_timezone);
+                       }
+                       if (isset($lang)) {
+                               $config['system']['language'] = $lang;
+                               unset($lang);
+                       }
+               }
+
+               return $config;
+       }
+
+       /**
+        * Tries to load the specified legacy configuration file and returns the config array.
+        *
+        * @deprecated since version 2018.12
+        * @param string $filepath
+        *
+        * @return array The configuration array
+        * @throws \Exception
+        */
+       private function loadINIConfigFile($filepath)
+       {
+               $contents = include($filepath);
+
+               $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
+
+               if ($config === false) {
+                       throw new \Exception('Error parsing INI config file ' . $filepath);
+               }
+
+               return $config;
+       }
+
+       /**
+        * Tries to load the specified configuration file and returns the config array.
+        *
+        * The config format is PHP array and the template for configuration files is the following:
+        *
+        * <?php return [
+        *      'section' => [
+        *          'key' => 'value',
+        *      ],
+        * ];
+        *
+        * @param  string $filepath The filepath of the
+        * @return array The config array0
+        *
+        * @throws \Exception if the config cannot get loaded.
+        */
+       private function loadConfigFile($filepath)
+       {
+               $config = include($filepath);
+
+               if (!is_array($config)) {
+                       throw new \Exception('Error loading config file ' . $filepath);
+               }
+
+               return $config;
+       }
+}
diff --git a/src/Util/Config/ConfigCacheManager.php b/src/Util/Config/ConfigCacheManager.php
new file mode 100644 (file)
index 0000000..1981717
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace Friendica\Util\Config;
+
+abstract class ConfigCacheManager
+{
+       /**
+        * The Sub directory of the config-files
+        * @var string
+        */
+       const SUBDIRECTORY = 'config';
+
+       protected $baseDir;
+       protected $configDir;
+
+       public function __construct($baseDir)
+       {
+               $this->baseDir = $baseDir;
+               $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
+       }
+
+       protected function getConfigFullName($name)
+       {
+               $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php';
+               return file_exists($fullName) ? $fullName : '';
+       }
+
+       protected function getIniFullName($name)
+       {
+               $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
+               return file_exists($fullName) ? $fullName : '';
+       }
+
+       protected function getHtConfigFullName($name)
+       {
+               $fullName = $this->baseDir  . DIRECTORY_SEPARATOR . '.' . $name . '.php';
+               return file_exists($fullName) ? $fullName : '';
+       }
+}
diff --git a/src/Util/Config/ConfigCacheSaver.php b/src/Util/Config/ConfigCacheSaver.php
new file mode 100644 (file)
index 0000000..d84ec6a
--- /dev/null
@@ -0,0 +1,198 @@
+<?php
+
+namespace Friendica\Util\Config;
+
+/**
+ * The ConfigCacheSaver saves specific variables back from the ConfigCache (@see ConfigCache )
+ * into the config-files
+ *
+ * It is capable of loading the following config files:
+ * - *.config.php   (current)
+ * - *.ini.php      (deprecated)
+ * - *.htconfig.php (deprecated)
+ */
+class ConfigCacheSaver extends ConfigCacheManager
+{
+       /**
+        * The standard indentation for config files
+        * @var string
+        */
+       const INDENT = "\t";
+
+       /**
+        * Saves a given value to the config file
+        * Either it replaces the current value or it will get added
+        *
+        * @param string $cat   The configuration category
+        * @param string $key   The configuration key
+        * @param string $value The new value
+        */
+       public function saveToConfigFile($cat, $key, $value)
+       {
+               $this->saveToLegacyConfig('htpreconfig', $cat, $key, $value);
+               $this->saveToLegacyConfig('htconfig', $cat, $key, $value);
+               $this->saveToCoreConfig('local', $cat, $key, $value);
+       }
+
+       /**
+        * Saves a value to either an config or an ini file
+        *
+        * @param string $name  The configuration file name ('local', 'addon', ..)
+        * @param string $cat   The configuration category
+        * @param string $key   The configuration key
+        * @param string $value The new value
+        */
+       private function saveToCoreConfig($name, $cat, $key, $value)
+       {
+               if (!empty($this->getConfigFullName($name))) {
+                       $this->saveConfigFile($this->getConfigFullName($name), $cat, $key, $value);
+               } elseif (!empty($this->getIniFullName($name))) {
+                       $this->saveINIConfigFile($this->getIniFullName($name), $cat, $key, $value);
+               } else {
+                       return;
+               }
+       }
+
+       /**
+        * Saves a value to a config file
+        *
+        * @param string $fullName The configuration full name (including the path)
+        * @param string $cat   The configuration category
+        * @param string $key   The configuration key
+        * @param string $value The new value
+        *
+        * @throws \Exception In case a file operation doesn't work
+        */
+       private function saveConfigFile($fullName, $cat, $key, $value)
+       {
+               $reading = fopen($fullName, 'r');
+               if (!$reading) {
+                       throw new \Exception('Cannot open config file \'' . $fullName . '\'.');
+               }
+               $writing = fopen($fullName . '.tmp', 'w');
+               if (!$writing) {
+                       throw new \Exception('Cannot create temporary config file \'' . $fullName . '.tmp\'.');
+               }
+               $categoryFound = false;
+               $categoryBracketFound = false;
+               $lineFound = false;
+               $lineArrowFound = false;
+               while (!feof($reading)) {
+                       $line = fgets($reading);
+                       // find the first line like "'system' =>"
+                       if (!$categoryFound && stristr($line, sprintf('\'%s\'', $cat))) {
+                               $categoryFound = true;
+                       }
+                       // find the first line with a starting bracket ( "[" )
+                       if ($categoryFound && !$categoryBracketFound && stristr($line, '[')) {
+                               $categoryBracketFound = true;
+                       }
+                       // find the first line with the key like "'value'"
+                       if ($categoryBracketFound && !$lineFound && stristr($line, sprintf('\'%s\'', $key))) {
+                               $lineFound = true;
+                       }
+                       // find the first line with an arrow ("=>") after finding the key
+                       if ($lineFound && !$lineArrowFound && stristr($line, '=>')) {
+                               $lineArrowFound = true;
+                       }
+                       // find the current value and replace it
+                       if ($lineArrowFound && preg_match_all('/\'(.*?)\'/', $line, $matches, PREG_SET_ORDER)) {
+                               $lineVal = end($matches)[0];
+                               $writeLine = str_replace($lineVal, '\'' . $value . '\'', $line);
+                               $categoryFound = false;
+                               $categoryBracketFound = false;
+                               $lineFound = false;
+                               $lineArrowFound = false;
+                               // if a line contains a closing bracket for the category ( "]" ) and we didn't find the key/value pair,
+                               // add it as a new line before the closing bracket
+                       } elseif ($categoryBracketFound && !$lineArrowFound && stristr($line, ']')) {
+                               $categoryFound = false;
+                               $categoryBracketFound = false;
+                               $lineFound = false;
+                               $lineArrowFound = false;
+                               $writeLine = sprintf(self::INDENT . self::INDENT .'\'%s\' => \'%s\',' . PHP_EOL, $key, $value);
+                               $writeLine .= $line;
+                       } else {
+                               $writeLine = $line;
+                       }
+                       fputs($writing, $writeLine);
+               }
+               if (!fclose($reading)) {
+                       throw new \Exception('Cannot close config file \'' . $fullName . '\'.');
+               };
+               if (!fclose($writing)) {
+                       throw new \Exception('Cannot close temporary config file \'' . $fullName . '.tmp\'.');
+               };
+               if (!rename($fullName, $fullName . '.old')) {
+                       throw new \Exception('Cannot backup current config file \'' . $fullName . '\'.');
+               }
+               if (!rename($fullName . '.tmp', $fullName)) {
+                       throw new \Exception('Cannot move temporary config file \'' . $fullName . '.tmp\' to current.');
+               }
+       }
+
+       /**
+        * Saves a value to a ini file
+        *
+        * @param string $fullName The configuration full name (including the path)
+        * @param string $cat   The configuration category
+        * @param string $key   The configuration key
+        * @param string $value The new value
+        */
+       private function saveINIConfigFile($fullName, $cat, $key, $value)
+       {
+               $reading = fopen($fullName, 'r');
+               $writing = fopen($fullName . '.tmp', 'w');
+               $categoryFound = false;
+               while (!feof($reading)) {
+                       $line = fgets($reading);
+                       if (!$categoryFound && stristr($line, sprintf('[%s]', $cat))) {
+                               $categoryFound = true;
+                               $writeLine = $line;
+                       } elseif ($categoryFound && preg_match_all('/^' . $key . '\s*=\s*(.*?)$/', $line, $matches, PREG_SET_ORDER)) {
+                               $writeLine = $key . ' = ' . $value . PHP_EOL;
+                               $categoryFound = false;
+                       } elseif ($categoryFound && (preg_match_all('/^\[.*?\]$/', $line) || preg_match_all('/^INI;.*$/', $line))) {
+                               $categoryFound = false;
+                               $writeLine = $key . ' = ' .  $value . PHP_EOL;
+                               $writeLine .= $line;
+                       } else {
+                               $writeLine = $line;
+                       }
+                       fputs($writing, $writeLine);
+               }
+               fclose($reading);
+               fclose($writing);
+               rename($fullName, $fullName . '.old');
+               rename($fullName . '.tmp', $fullName);
+       }
+
+       private function saveToLegacyConfig($name, $cat, $key, $value)
+       {
+               if (empty($this->getHtConfigFullName($name))) {
+                       return;
+               }
+               $fullName = $this->getHtConfigFullName($name);
+               $reading = fopen($fullName, 'r');
+               $writing = fopen($fullName . '.tmp', 'w');
+               $found = false;
+               while (!feof($reading)) {
+                       $line = fgets($reading);
+                       if (preg_match_all('/^\$a\-\>config\[\'' . $cat . '\',\'' . $key . '\'\]\s*=\s\'*(.*?)\'$/', $line, $matches, PREG_SET_ORDER)) {
+                               $writeLine = $key . ' = ' . $value . PHP_EOL;
+                               $found = true;
+                       } else {
+                               $writeLine = $line;
+                       }
+                       fputs($writing, $writeLine);
+               }
+               if (!$found) {
+                       $writeLine = $key . ' = ' . $value . PHP_EOL;
+                       fputs($writing, $writeLine);
+               }
+               fclose($reading);
+               fclose($writing);
+               rename($fullName, $fullName . '.old');
+               rename($fullName . '.tmp', $fullName);
+       }
+}
index 6a64b28816bdd138f14d0cf3d3677a6e6a77ead4..d434674061ebba013c3c82786142390d8401b733 100644 (file)
@@ -6,10 +6,10 @@
 namespace Friendica\Test;
 
 use Friendica\App;
-use Friendica\Core\Config\Cache;
 use Friendica\Database\DBA;
 use Friendica\Factory;
 use Friendica\Util\BasePath;
+use Friendica\Util\Config\ConfigCacheLoader;
 use Friendica\Util\Profiler;
 use PHPUnit\DbUnit\DataSet\YamlDataSet;
 use PHPUnit\DbUnit\TestCaseTrait;
@@ -43,7 +43,7 @@ abstract class DatabaseTest extends MockedTest
 
                $basePath = BasePath::create(dirname(__DIR__));
                $mode = new App\Mode($basePath);
-               $configLoader = new Cache\ConfigCacheLoader($basePath, $mode);
+               $configLoader = new ConfigCacheLoader($basePath, $mode);
                $config = Factory\ConfigFactory::createCache($configLoader);
 
                $profiler = \Mockery::mock(Profiler::class);
index 3d7e5bcfb655ef066d7e789342d447c4cf9d05f6..bd1b041082818a52397eb52d600ee235a8693fed 100644 (file)
@@ -7,13 +7,13 @@ namespace Friendica\Test;
 
 use Friendica\App;
 use Friendica\Core\Config;
-use Friendica\Core\Config\Cache;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Factory;
 use Friendica\Network\HTTPException;
 use Friendica\Util\BasePath;
+use Friendica\Util\Config\ConfigCacheLoader;
 use Monolog\Handler\TestHandler;
 
 require_once __DIR__ . '/../../include/api.php';
@@ -38,7 +38,7 @@ class ApiTest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../');
                $mode = new App\Mode($basePath);
-               $configLoader = new Cache\ConfigCacheLoader($basePath, $mode);
+               $configLoader = new ConfigCacheLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
                Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
diff --git a/tests/src/Core/Config/Cache/ConfigCacheLoaderTest.php b/tests/src/Core/Config/Cache/ConfigCacheLoaderTest.php
deleted file mode 100644 (file)
index 39dc20e..0000000
+++ /dev/null
@@ -1,209 +0,0 @@
-<?php
-
-namespace Friendica\Test\src\Core\Config\Cache;
-
-use Friendica\App;
-use Friendica\Core\Config\Cache\ConfigCache;
-use Friendica\Core\Config\Cache\ConfigCacheLoader;
-use Friendica\Test\MockedTest;
-use Friendica\Test\Util\VFSTrait;
-use Mockery\MockInterface;
-use org\bovigo\vfs\vfsStream;
-
-class ConfigCacheLoaderTest extends MockedTest
-{
-       use VFSTrait;
-
-       /**
-        * @var App\Mode|MockInterface
-        */
-       private $mode;
-
-       protected function setUp()
-       {
-               parent::setUp();
-
-               $this->setUpVfsDir();
-
-               $this->mode = \Mockery::mock(App\Mode::class);
-               $this->mode->shouldReceive('isInstall')->andReturn(true);
-       }
-
-       /**
-        * Test the loadConfigFiles() method with default values
-        */
-       public function testLoadConfigFiles()
-       {
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-               $configCache = new ConfigCache();
-
-               $configCacheLoader->loadConfigFiles($configCache);
-
-               $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
-       }
-
-       /**
-        * Test the loadConfigFiles() method with a wrong local.config.php
-        * @expectedException \Exception
-        * @expectedExceptionMessageRegExp /Error loading config file \w+/
-        */
-       public function testLoadConfigWrong()
-       {
-               $this->delConfigFile('local.config.php');
-
-               vfsStream::newFile('local.config.php')
-                       ->at($this->root->getChild('config'))
-                       ->setContent('<?php return true;');
-
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-               $configCache = new ConfigCache();
-
-               $configCacheLoader->loadConfigFiles($configCache);
-       }
-
-       /**
-        * Test the loadConfigFiles() method with a local.config.php file
-        */
-       public function testLoadConfigFilesLocal()
-       {
-               $this->delConfigFile('local.config.php');
-
-               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       'datasets' . DIRECTORY_SEPARATOR .
-                       'config' . DIRECTORY_SEPARATOR .
-                       'local.config.php';
-
-               vfsStream::newFile('local.config.php')
-                       ->at($this->root->getChild('config'))
-                       ->setContent(file_get_contents($file));
-
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-               $configCache = new ConfigCache();
-
-               $configCacheLoader->loadConfigFiles($configCache);
-
-               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
-               $this->assertEquals('testuser', $configCache->get('database', 'username'));
-               $this->assertEquals('testpw', $configCache->get('database', 'password'));
-               $this->assertEquals('testdb', $configCache->get('database', 'database'));
-
-               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
-               $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
-       }
-
-       /**
-        * Test the loadConfigFile() method with a local.ini.php file
-        */
-       public function testLoadConfigFilesINI()
-       {
-               $this->delConfigFile('local.config.php');
-
-               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       'datasets' . DIRECTORY_SEPARATOR .
-                       'config' . DIRECTORY_SEPARATOR .
-                       'local.ini.php';
-
-               vfsStream::newFile('local.ini.php')
-                       ->at($this->root->getChild('config'))
-                       ->setContent(file_get_contents($file));
-
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-               $configCache = new ConfigCache();
-
-               $configCacheLoader->loadConfigFiles($configCache);
-
-               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
-               $this->assertEquals('testuser', $configCache->get('database', 'username'));
-               $this->assertEquals('testpw', $configCache->get('database', 'password'));
-               $this->assertEquals('testdb', $configCache->get('database', 'database'));
-
-               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
-       }
-
-       /**
-        * Test the loadConfigFile() method with a .htconfig.php file
-        */
-       public function testLoadConfigFilesHtconfig()
-       {
-               $this->delConfigFile('local.config.php');
-
-               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       'datasets' . DIRECTORY_SEPARATOR .
-                       'config' . DIRECTORY_SEPARATOR .
-                       '.htconfig.test.php';
-
-               vfsStream::newFile('.htconfig.php')
-                       ->at($this->root)
-                       ->setContent(file_get_contents($file));
-
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-               $configCache = new ConfigCache();
-
-               $configCacheLoader->loadConfigFiles($configCache);
-
-               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
-               $this->assertEquals('testuser', $configCache->get('database', 'username'));
-               $this->assertEquals('testpw', $configCache->get('database', 'password'));
-               $this->assertEquals('testdb', $configCache->get('database', 'database'));
-               $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
-
-               $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
-               $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
-               $this->assertEquals('fr', $configCache->get('system', 'language'));
-
-               $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
-               $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
-
-               $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
-               $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
-               $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
-
-               $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
-               $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
-       }
-
-       public function testLoadAddonConfig()
-       {
-               $structure = [
-                       'addon' => [
-                               'test' => [
-                                       'config' => [],
-                               ],
-                       ],
-               ];
-
-               vfsStream::create($structure, $this->root);
-
-               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       '..' . DIRECTORY_SEPARATOR .
-                       'datasets' . DIRECTORY_SEPARATOR .
-                       'config' . DIRECTORY_SEPARATOR .
-                       'local.config.php';
-
-               vfsStream::newFile('test.config.php')
-                       ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
-                       ->setContent(file_get_contents($file));
-
-               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
-
-               $conf = $configCacheLoader->loadAddonConfig('test');
-
-               $this->assertEquals('testhost', $conf['database']['hostname']);
-               $this->assertEquals('testuser', $conf['database']['username']);
-               $this->assertEquals('testpw', $conf['database']['password']);
-               $this->assertEquals('testdb', $conf['database']['database']);
-
-               $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
-       }
-}
index e638e2740568ca4978ecda19c8b68ed1193ba85b..28f5afbd778a4eef8a650f342e46170f07e42200 100644 (file)
@@ -3,11 +3,11 @@ namespace Friendica\Test\src\Database;
 
 use Friendica\App;
 use Friendica\Core\Config;
-use Friendica\Core\Config\Cache;
 use Friendica\Database\DBA;
 use Friendica\Factory;
 use Friendica\Test\DatabaseTest;
 use Friendica\Util\BasePath;
+use Friendica\Util\Config\ConfigCacheLoader;
 
 class DBATest extends DatabaseTest
 {
@@ -15,7 +15,7 @@ class DBATest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../../');
                $mode = new App\Mode($basePath);
-               $configLoader = new Cache\ConfigCacheLoader($basePath, $mode);
+               $configLoader = new ConfigCacheLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
                Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
index 34f659b51ce21fb088cf5fa70c7f52547cee2a4f..65d5c85a421a1b92cda70f57b41442cc866543d3 100644 (file)
@@ -3,11 +3,11 @@
 namespace Friendica\Test\src\Database;
 
 use Friendica\App;
-use Friendica\Core\Config\Cache;
 use Friendica\Database\DBStructure;
 use Friendica\Factory;
 use Friendica\Test\DatabaseTest;
 use Friendica\Util\BasePath;
+use Friendica\Util\Config\ConfigCacheLoader;
 
 class DBStructureTest extends DatabaseTest
 {
@@ -15,7 +15,7 @@ class DBStructureTest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../../');
                $mode = new App\Mode($basePath);
-               $configLoader = new Cache\ConfigCacheLoader($basePath, $mode);
+               $configLoader = new ConfigCacheLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
                Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
diff --git a/tests/src/Util/Config/ConfigCacheLoaderTest.php b/tests/src/Util/Config/ConfigCacheLoaderTest.php
new file mode 100644 (file)
index 0000000..be0c5c0
--- /dev/null
@@ -0,0 +1,185 @@
+<?php
+
+namespace Friendica\Test\src\Util\Config;
+
+use Friendica\App;
+use Friendica\Core\Config\Cache\ConfigCache;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
+use Friendica\Util\Config\ConfigCacheLoader;
+use Mockery\MockInterface;
+use org\bovigo\vfs\vfsStream;
+
+class ConfigCacheLoaderTest extends MockedTest
+{
+       use VFSTrait;
+
+       /**
+        * @var App\Mode|MockInterface
+        */
+       private $mode;
+
+       protected function setUp()
+       {
+               parent::setUp();
+
+               $this->setUpVfsDir();
+
+               $this->mode = \Mockery::mock(App\Mode::class);
+               $this->mode->shouldReceive('isInstall')->andReturn(true);
+       }
+
+       /**
+        * Test the loadConfigFiles() method with default values
+        */
+       public function testLoadConfigFiles()
+       {
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+
+               $configCacheLoader->loadConfigFiles($configCache);
+
+               $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
+       }
+
+       /**
+        * Test the loadConfigFiles() method with a wrong local.config.php
+        * @expectedException \Exception
+        * @expectedExceptionMessageRegExp /Error loading config file \w+/
+        */
+       public function testLoadConfigWrong()
+       {
+               $this->delConfigFile('local.config.php');
+
+               vfsStream::newFile('local.config.php')
+                       ->at($this->root->getChild('config'))
+                       ->setContent('<?php return true;');
+
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+
+               $configCacheLoader->loadConfigFiles($configCache);
+       }
+
+       /**
+        * Test the loadConfigFiles() method with a local.config.php file
+        */
+       public function testLoadConfigFilesLocal()
+       {
+               $this->delConfigFile('local.config.php');
+
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR;
+
+               vfsStream::newFile('local.config.php')
+                       ->at($this->root->getChild('config'))
+                       ->setContent(file_get_contents($file));
+
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+
+               $configCacheLoader->loadConfigFiles($configCache);
+
+               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
+               $this->assertEquals('testuser', $configCache->get('database', 'username'));
+               $this->assertEquals('testpw', $configCache->get('database', 'password'));
+               $this->assertEquals('testdb', $configCache->get('database', 'database'));
+
+               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
+               $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
+       }
+
+       /**
+        * Test the loadConfigFile() method with a local.ini.php file
+        */
+       public function testLoadConfigFilesINI()
+       {
+               $this->delConfigFile('local.config.php');
+
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR;
+
+               vfsStream::newFile('local.ini.php')
+                       ->at($this->root->getChild('config'))
+                       ->setContent(file_get_contents($file));
+
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+
+               $configCacheLoader->loadConfigFiles($configCache);
+
+               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
+               $this->assertEquals('testuser', $configCache->get('database', 'username'));
+               $this->assertEquals('testpw', $configCache->get('database', 'password'));
+               $this->assertEquals('testdb', $configCache->get('database', 'database'));
+
+               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
+       }
+
+       /**
+        * Test the loadConfigFile() method with a .htconfig.php file
+        */
+       public function testLoadConfigFilesHtconfig()
+       {
+               $this->delConfigFile('local.config.php');
+
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR;
+
+               vfsStream::newFile('.htconfig.php')
+                       ->at($this->root)
+                       ->setContent(file_get_contents($file));
+
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+
+               $configCacheLoader->loadConfigFiles($configCache);
+
+               $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
+               $this->assertEquals('testuser', $configCache->get('database', 'username'));
+               $this->assertEquals('testpw', $configCache->get('database', 'password'));
+               $this->assertEquals('testdb', $configCache->get('database', 'database'));
+               $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
+
+               $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
+               $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
+               $this->assertEquals('fr', $configCache->get('system', 'language'));
+
+               $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
+               $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
+
+               $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
+               $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
+               $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
+
+               $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
+               $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
+       }
+
+       public function testLoadAddonConfig()
+       {
+               $structure = [
+                       'addon' => [
+                               'test' => [
+                                       'config' => [],
+                               ],
+                       ],
+               ];
+
+               vfsStream::create($structure, $this->root);
+
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR;
+
+               vfsStream::newFile('test.config.php')
+                       ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
+                       ->setContent(file_get_contents($file));
+
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+
+               $conf = $configCacheLoader->loadAddonConfig('test');
+
+               $this->assertEquals('testhost', $conf['database']['hostname']);
+               $this->assertEquals('testuser', $conf['database']['username']);
+               $this->assertEquals('testpw', $conf['database']['password']);
+               $this->assertEquals('testdb', $conf['database']['database']);
+
+               $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
+       }
+}
diff --git a/tests/src/Util/Config/ConfigCacheSaverTest.php b/tests/src/Util/Config/ConfigCacheSaverTest.php
new file mode 100644 (file)
index 0000000..da2f050
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+
+namespace Friendica\Test\src\Util\Config;
+
+use Friendica\App;
+use Friendica\Core\Config\Cache\ConfigCache;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
+use Friendica\Util\Config\ConfigCacheLoader;
+use Friendica\Util\Config\ConfigCacheSaver;
+use Mockery\MockInterface;
+use org\bovigo\vfs\vfsStream;
+
+class ConfigCacheSaverTest extends MockedTest
+{
+       use VFSTrait;
+       /**
+        * @var App\Mode|MockInterface
+        */
+       private $mode;
+       protected function setUp()
+       {
+               parent::setUp();
+               $this->setUpVfsDir();
+               $this->mode = \Mockery::mock(App\Mode::class);
+               $this->mode->shouldReceive('isInstall')->andReturn(true);
+       }
+       /**
+        * Test the saveToConfigFile() method with a local.config.php file
+        */
+       public function testSaveToConfigFileLocal()
+       {
+               $this->delConfigFile('local.config.php');
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       'datasets' . DIRECTORY_SEPARATOR .
+                       'config' . DIRECTORY_SEPARATOR .
+                       'local.config.php';
+               vfsStream::newFile('local.config.php')
+                       ->at($this->root->getChild('config'))
+                       ->setContent(file_get_contents($file));
+               $configCacheSaver = new ConfigCacheSaver($this->root->url());
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+               $configCacheLoader->loadConfigFiles($configCache);
+               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
+               $this->assertEquals('!<unset>!', $configCache->get('config', 'test_val'));
+               $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
+               $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!');
+               $newConfigCache = new ConfigCache();
+               $configCacheLoader->loadConfigFiles($newConfigCache);
+               $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
+               $this->assertEquals('Testing$!"$with@all.we can!', $newConfigCache->get('config', 'test_val'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old'));
+               $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.tmp'));
+               $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old')->url()));
+       }
+       /**
+        * Test the saveToConfigFile() method with a local.ini.php file
+        */
+       public function testSaveToConfigFileINI()
+       {
+               $this->delConfigFile('local.config.php');
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       'datasets' . DIRECTORY_SEPARATOR .
+                       'config' . DIRECTORY_SEPARATOR .
+                       'local.ini.php';
+               vfsStream::newFile('local.ini.php')
+                       ->at($this->root->getChild('config'))
+                       ->setContent(file_get_contents($file));
+               $configCacheSaver = new ConfigCacheSaver($this->root->url());
+               $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+               $configCacheLoader->loadConfigFiles($configCache);
+               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
+               $this->assertEquals('!<unset>!', $configCache->get('config', 'test_val'));
+               $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
+               $configCacheSaver->saveToConfigFile('config', 'test_val', "Testing@with.all we can");
+               $newConfigCache = new ConfigCache();
+               $configCacheLoader->loadConfigFiles($newConfigCache);
+               $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
+               $this->assertEquals("Testing@with.all we can", $newConfigCache->get('config', 'test_val'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php.old'));
+               $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php.tmp'));
+               $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.old')->url()));
+       }
+       /**
+        * Test the saveToConfigFile() method with a .htconfig.php file
+        * @todo fix it after 2019.03 merge to develop
+        */
+       public function testSaveToConfigFileHtconfig()
+       {
+               $this->markTestSkipped('Needs 2019.03 merge to develop first');
+               $this->delConfigFile('local.config.php');
+               $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       'datasets' . DIRECTORY_SEPARATOR .
+                       'config' . DIRECTORY_SEPARATOR .
+                       '.htconfig.test.php';
+               vfsStream::newFile('.htconfig.php')
+                       ->at($this->root)
+                       ->setContent(file_get_contents($file));
+               $configCacheSaver = new ConfigCacheSaver($this->root->url(), $this->mode);
+               $configCache = new ConfigCache();
+               $configCacheSaver->loadConfigFiles($configCache);
+               $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
+               $this->assertEquals('!<unset>!', $configCache->get('config', 'test_val'));
+               $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
+               $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!');
+               $newConfigCache = new ConfigCache();
+               $configCacheSaver->loadConfigFiles($newConfigCache);
+               $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
+               $this->assertEquals('Testing$!"$with@all.we can!', $newConfigCache->get('config', 'test_val'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php'));
+               $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.old'));
+               $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.tmp'));
+               $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.old')->url()));
+       }
+}