From: Philipp Holzer <admin@philipp.info>
Date: Sun, 10 Feb 2019 12:09:38 +0000 (+0100)
Subject: Adding ConfigCacheLoaderTest
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=199ceeadbf0a3bcc5f184ca714facc3bec402b91;p=friendica.git

Adding ConfigCacheLoaderTest
---

diff --git a/src/Core/Config/ConfigCacheLoader.php b/src/Core/Config/ConfigCacheLoader.php
index 0b8761415c..3a6a3c803e 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 50999b984d..320d989cce 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 0000000000..88ba4e3ef3
--- /dev/null
+++ b/tests/datasets/config/.htconfig.test.php
@@ -0,0 +1,13 @@
+<?php
+/**
+ * A test .htconfig file
+ */
+
+$db_host = 'testhost';
+$db_user = 'testuser';
+$db_pass = 'testpw';
+$db_data = 'testdb';
+
+$pidfile = '/var/run/friendica.pid';
+$default_timezone = 'Europe/Berlin';
+$lang = 'fr';
diff --git a/tests/datasets/config/local.config.php b/tests/datasets/config/local.config.php
new file mode 100644
index 0000000000..8a392909f2
--- /dev/null
+++ b/tests/datasets/config/local.config.php
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * A test file for local configuration
+ *
+ */
+
+return [
+	'database' => [
+		'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 0000000000..1fea0b028e
--- /dev/null
+++ b/tests/datasets/config/local.ini.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * A test local ini file
+ */
+
+return <<<INI
+
+[database]
+hostname = testhost
+username = testuser
+password = testpw
+database = testdb
+
+[config]
+admin_email = admin@test.it
+INI;
diff --git a/tests/src/Core/Config/ConfigCacheLoaderTest.php b/tests/src/Core/Config/ConfigCacheLoaderTest.php
new file mode 100644
index 0000000000..6be89bc40f
--- /dev/null
+++ b/tests/src/Core/Config/ConfigCacheLoaderTest.php
@@ -0,0 +1,184 @@
+<?php
+
+namespace Friendica\Test\Core\Config;
+
+use Friendica\Core\Config\ConfigCache;
+use Friendica\Core\Config\ConfigCacheLoader;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
+use org\bovigo\vfs\vfsStream;
+
+class ConfigCacheLoaderTest extends MockedTest
+{
+	use VFSTrait;
+
+	protected function setUp()
+	{
+		parent::setUp();
+
+		$this->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('<?php return true;');
+
+		$configCacheLoader = new ConfigCacheLoader($this->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']);
+	}
+}