From 2a42f8ddedf805ccc8bc42085965bed8c86c65a4 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 10 Feb 2019 12:30:13 +0100 Subject: [PATCH 1/2] Adding ConfigCacheTest --- src/Core/Config/AbstractDbaConfigAdapter.php | 2 - src/Core/Config/ConfigCache.php | 11 +- tests/src/Core/Config/ConfigCacheTest.php | 175 +++++++++++++++++++ 3 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 tests/src/Core/Config/ConfigCacheTest.php diff --git a/src/Core/Config/AbstractDbaConfigAdapter.php b/src/Core/Config/AbstractDbaConfigAdapter.php index c2e8bda5d..bae75122a 100644 --- a/src/Core/Config/AbstractDbaConfigAdapter.php +++ b/src/Core/Config/AbstractDbaConfigAdapter.php @@ -2,8 +2,6 @@ namespace Friendica\Core\Config; -use Friendica\Database\DBA; - abstract class AbstractDbaConfigAdapter { /** @var bool */ diff --git a/src/Core/Config/ConfigCache.php b/src/Core/Config/ConfigCache.php index 0c9e1f486..b86ec3860 100644 --- a/src/Core/Config/ConfigCache.php +++ b/src/Core/Config/ConfigCache.php @@ -18,7 +18,7 @@ class ConfigCache implements IConfigCache, IPConfigCache */ public function __construct(array $config = []) { - $this->config = $config; + $this->loadConfigArray($config); } /** @@ -110,6 +110,9 @@ class ConfigCache implements IConfigCache, IPConfigCache } else { if (isset($this->config[$cat][$key])) { unset($this->config[$cat][$key]); + if (count($this->config[$cat]) == 0) { + unset($this->config[$cat]); + } } } } @@ -160,6 +163,12 @@ class ConfigCache implements IConfigCache, IPConfigCache { if (isset($this->config[$uid][$cat][$key])) { unset($this->config[$uid][$cat][$key]); + if (count($this->config[$uid][$cat]) == 0) { + unset($this->config[$uid][$cat]); + if (count($this->config[$uid]) == 0) { + unset($this->config[$uid]); + } + } } } diff --git a/tests/src/Core/Config/ConfigCacheTest.php b/tests/src/Core/Config/ConfigCacheTest.php new file mode 100644 index 000000000..25ea6030f --- /dev/null +++ b/tests/src/Core/Config/ConfigCacheTest.php @@ -0,0 +1,175 @@ + [ + 'data' => [ + 'system' => [ + 'test' => 'it', + 'boolTrue' => true, + 'boolFalse' => false, + 'int' => 235, + 'dec' => 2.456, + 'array' => ['1', 2, '3', true, false], + ], + 'config' => [ + 'a' => 'value', + ], + ] + ] + ]; + } + + private function assertConfigValues($data, ConfigCache $configCache, $uid = null) + { + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + if (isset($uid)) { + $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key)); + } else { + $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key)); + } + } + } + } + + /** + * Test the loadConfigArray() method without override + * @dataProvider dataTests + */ + public function testLoadConfigArray($data) + { + $configCache = new ConfigCache(); + $configCache->loadConfigArray($data); + + $this->assertConfigValues($data, $configCache); + } + + /** + * Test the loadConfigArray() method with overrides + * @dataProvider dataTests + */ + public function testLoadConfigArrayOverride($data) + { + $override = [ + 'system' => [ + 'test' => 'not', + 'boolTrue' => false, + ] + ]; + + $configCache = new ConfigCache(); + $configCache->loadConfigArray($data); + $configCache->loadConfigArray($override); + + $this->assertConfigValues($data, $configCache); + + // override the value + $configCache->loadConfigArray($override, true); + + $this->assertEquals($override['system']['test'], $configCache->get('system', 'test')); + $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue')); + } + + /** + * Test the getAll() method + * @dataProvider dataTests + */ + public function testGetAll($data) + { + $configCache = new ConfigCache(); + $configCache->loadConfigArray($data); + + $all = $configCache->getAll(); + + $this->assertContains($data['system'], $all); + + // config values are stored directly in the array base + $this->assertEquals($data['config']['a'], $all['a']); + } + + /** + * Test the set() and get() method + * @dataProvider dataTests + */ + public function testSetGet($data) + { + $configCache = new ConfigCache(); + + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + $configCache->set($cat, $key, $value); + } + } + + $this->assertConfigValues($data, $configCache); + } + + /** + * Test the delete() method + * @dataProvider dataTests + */ + public function testDelete($data) + { + $configCache = new ConfigCache($data); + + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + $configCache->delete($cat, $key); + } + } + + $this->assertEmpty($configCache->getAll()); + } + + /** + * Test the setP() and getP() methods + * @dataProvider dataTests + */ + public function testSetGetP($data) + { + $configCache = new ConfigCache(); + $uid = 345; + + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + $configCache->setP($uid, $cat, $key, $value); + } + } + + $this->assertConfigValues($data, $configCache, $uid); + } + + + /** + * Test the deleteP() method + * @dataProvider dataTests + */ + public function testDeleteP($data) + { + $configCache = new ConfigCache(); + $uid = 345; + + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + $configCache->setP($uid, $cat, $key, $value); + } + } + + foreach ($data as $cat => $values) { + foreach ($values as $key => $value) { + $configCache->deleteP($uid, $cat, $key); + } + } + + $this->assertEmpty($configCache->getAll()); + } +} From 199ceeadbf0a3bcc5f184ca714facc3bec402b91 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 10 Feb 2019 13:09:38 +0100 Subject: [PATCH 2/2] Adding ConfigCacheLoaderTest --- src/Core/Config/ConfigCacheLoader.php | 8 - tests/Util/VFSTrait.php | 2 +- tests/datasets/config/.htconfig.test.php | 13 ++ tests/datasets/config/local.config.php | 27 +++ tests/datasets/config/local.ini.php | 16 ++ .../src/Core/Config/ConfigCacheLoaderTest.php | 184 ++++++++++++++++++ 6 files changed, 241 insertions(+), 9 deletions(-) create mode 100644 tests/datasets/config/.htconfig.test.php create mode 100644 tests/datasets/config/local.config.php create mode 100644 tests/datasets/config/local.ini.php create mode 100644 tests/src/Core/Config/ConfigCacheLoaderTest.php diff --git a/src/Core/Config/ConfigCacheLoader.php b/src/Core/Config/ConfigCacheLoader.php index 0b8761415..3a6a3c803 100644 --- a/src/Core/Config/ConfigCacheLoader.php +++ b/src/Core/Config/ConfigCacheLoader.php @@ -160,10 +160,6 @@ class ConfigCacheLoader */ private function loadINIConfigFile($filepath) { - if (!file_exists($filepath)) { - throw new \Exception('Error parsing non-existent INI config file ' . $filepath); - } - $contents = include($filepath); $config = parse_ini_string($contents, true, INI_SCANNER_TYPED); @@ -193,10 +189,6 @@ class ConfigCacheLoader */ private function loadConfigFile($filepath) { - if (!file_exists($filepath)) { - throw new \Exception('Error loading non-existent config file ' . $filepath); - } - $config = include($filepath); if (!is_array($config)) { diff --git a/tests/Util/VFSTrait.php b/tests/Util/VFSTrait.php index 50999b984..320d989cc 100644 --- a/tests/Util/VFSTrait.php +++ b/tests/Util/VFSTrait.php @@ -60,7 +60,7 @@ trait VFSTrait protected function delConfigFile($filename) { if ($this->root->hasChild('config/' . $filename)) { - $this->root->removeChild('config/' . $filename); + $this->root->getChild('config')->removeChild($filename); } } } diff --git a/tests/datasets/config/.htconfig.test.php b/tests/datasets/config/.htconfig.test.php new file mode 100644 index 000000000..88ba4e3ef --- /dev/null +++ b/tests/datasets/config/.htconfig.test.php @@ -0,0 +1,13 @@ + [ + 'hostname' => 'testhost', + 'username' => 'testuser', + 'password' => 'testpw', + 'database' => 'testdb', + 'charset' => 'utf8mb4', + ], + + 'config' => [ + 'admin_email' => 'admin@test.it', + 'sitename' => 'Friendica Social Network', + 'register_policy' => \Friendica\Module\Register::OPEN, + 'register_text' => '', + ], + 'system' => [ + 'default_timezone' => 'UTC', + 'language' => 'en', + ], +]; diff --git a/tests/datasets/config/local.ini.php b/tests/datasets/config/local.ini.php new file mode 100644 index 000000000..1fea0b028 --- /dev/null +++ b/tests/datasets/config/local.ini.php @@ -0,0 +1,16 @@ +setUpVfsDir(); + } + + /** + * Test the loadConfigFiles() method with default values + */ + public function testLoadConfigFiles() + { + $configCacheLoader = new ConfigCacheLoader($this->root->url()); + $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('root->url()); + $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 . + '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()); + $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 . + '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()); + $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 . + '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()); + $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('/var/run/friendica.pid', $configCache->get('system', 'pidfile')); + $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone')); + $this->assertEquals('fr', $configCache->get('system', 'language')); + } + + public function testLoadAddonConfig() + { + $structure = [ + 'addon' => [ + 'test' => [ + 'config' => [], + ], + ], + ]; + + vfsStream::create($structure, $this->root); + + $file = dirname(__DIR__) . 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()); + + $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']); + } +}