Restructure (P)Config to follow new paradigm

This commit is contained in:
Philipp 2021-10-23 11:29:16 +02:00
parent 68046573a4
commit ab83d0dd27
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
49 changed files with 368 additions and 331 deletions

View file

@ -0,0 +1,345 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Core\Config\Cache;
use Friendica\Core\Config\Cache;
use Friendica\Test\MockedTest;
use ParagonIE\HiddenString\HiddenString;
use stdClass;
class CacheTest extends MockedTest
{
public function dataTests()
{
return [
'normal' => [
'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, Cache\Cache $configCache)
{
foreach ($data as $cat => $values) {
foreach ($values as $key => $value) {
self::assertEquals($data[$cat][$key], $configCache->get($cat, $key));
}
}
}
/**
* Test the loadConfigArray() method without override
* @dataProvider dataTests
*/
public function testLoadConfigArray($data)
{
$configCache = new Cache\Cache();
$configCache->load($data);
self::assertConfigValues($data, $configCache);
}
/**
* Test the loadConfigArray() method with overrides
* @dataProvider dataTests
*/
public function testLoadConfigArrayOverride($data)
{
$override = [
'system' => [
'test' => 'not',
'boolTrue' => false,
]
];
$configCache = new Cache\Cache();
$configCache->load($data, Cache\Cache::SOURCE_DB);
// doesn't override - Low Priority due Config file
$configCache->load($override, Cache\Cache::SOURCE_FILE);
self::assertConfigValues($data, $configCache);
// override the value - High Prio due Server Env
$configCache->load($override, Cache\Cache::SOURCE_ENV);
self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
// Don't overwrite server ENV variables - even in load mode
$configCache->load($data, Cache\Cache::SOURCE_DB);
self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
// Overwrite ENV variables with ENV variables
$configCache->load($data, Cache\Cache::SOURCE_ENV);
self::assertConfigValues($data, $configCache);
self::assertNotEquals($override['system']['test'], $configCache->get('system', 'test'));
self::assertNotEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
}
/**
* Test the loadConfigArray() method with wrong/empty datasets
*/
public function testLoadConfigArrayWrong()
{
$configCache = new Cache\Cache();
// empty dataset
$configCache->load([]);
self::assertEmpty($configCache->getAll());
// wrong dataset
$configCache->load(['system' => 'not_array']);
self::assertEmpty($configCache->getAll());
// incomplete dataset (key is integer ID of the array)
$configCache->load(['system' => ['value']]);
self::assertEquals('value', $configCache->get('system', 0));
}
/**
* Test the getAll() method
* @dataProvider dataTests
*/
public function testGetAll($data)
{
$configCache = new Cache\Cache();
$configCache->load($data);
$all = $configCache->getAll();
self::assertContains($data['system'], $all);
self::assertContains($data['config'], $all);
}
/**
* Test the set() and get() method
* @dataProvider dataTests
*/
public function testSetGet($data)
{
$configCache = new Cache\Cache();
foreach ($data as $cat => $values) {
foreach ($values as $key => $value) {
$configCache->set($cat, $key, $value);
}
}
self::assertConfigValues($data, $configCache);
}
/**
* Test the get() method without a value
*/
public function testGetEmpty()
{
$configCache = new Cache\Cache();
self::assertNull($configCache->get('something', 'value'));
}
/**
* Test the get() method with a category
*/
public function testGetCat()
{
$configCache = new Cache\Cache([
'system' => [
'key1' => 'value1',
'key2' => 'value2',
],
'config' => [
'key3' => 'value3',
],
]);
self::assertEquals([
'key1' => 'value1',
'key2' => 'value2',
], $configCache->get('system'));
// explicit null as key
self::assertEquals([
'key1' => 'value1',
'key2' => 'value2',
], $configCache->get('system', null));
}
/**
* Test the delete() method
* @dataProvider dataTests
*/
public function testDelete($data)
{
$configCache = new Cache\Cache($data);
foreach ($data as $cat => $values) {
foreach ($values as $key => $value) {
$configCache->delete($cat, $key);
}
}
self::assertEmpty($configCache->getAll());
}
/**
* Test the keyDiff() method with result
* @dataProvider dataTests
*/
public function testKeyDiffWithResult($data)
{
$configCache = new Cache\Cache($data);
$diffConfig = [
'fakeCat' => [
'fakeKey' => 'value',
]
];
self::assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
}
/**
* Test the keyDiff() method without result
* @dataProvider dataTests
*/
public function testKeyDiffWithoutResult($data)
{
$configCache = new Cache\Cache($data);
$diffConfig = $configCache->getAll();
self::assertEmpty($configCache->keyDiff($diffConfig));
}
/**
* Test the default hiding of passwords inside the cache
*/
public function testPasswordHide()
{
$configCache = new Cache\Cache([
'database' => [
'password' => 'supersecure',
'username' => 'notsecured',
],
]);
self::assertEquals('supersecure', $configCache->get('database', 'password'));
self::assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
}
/**
* Test disabling the hiding of passwords inside the cache
*/
public function testPasswordShow()
{
$configCache = new Cache\Cache([
'database' => [
'password' => 'supersecure',
'username' => 'notsecured',
],
], false);
self::assertEquals('supersecure', $configCache->get('database', 'password'));
self::assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
}
/**
* Test a empty password
*/
public function testEmptyPassword()
{
$configCache = new Cache\Cache([
'database' => [
'password' => '',
'username' => '',
]
]);
self::assertNotEmpty($configCache->get('database', 'password'));
self::assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
self::assertEmpty($configCache->get('database', 'username'));
}
public function testWrongTypePassword()
{
$configCache = new Cache\Cache([
'database' => [
'password' => new stdClass(),
'username' => '',
]
]);
self::assertNotEmpty($configCache->get('database', 'password'));
self::assertEmpty($configCache->get('database', 'username'));
$configCache = new Cache\Cache([
'database' => [
'password' => 23,
'username' => '',
]
]);
self::assertEquals(23, $configCache->get('database', 'password'));
self::assertEmpty($configCache->get('database', 'username'));
}
/**
* Test the set() method with overrides
* @dataProvider dataTests
*/
public function testSetOverrides($data)
{
$configCache = new Cache\Cache();
$configCache->load($data, Cache\Cache::SOURCE_DB);
// test with wrong override
self::assertFalse($configCache->set('system', 'test', '1234567', Cache\Cache::SOURCE_FILE));
self::assertEquals($data['system']['test'], $configCache->get('system', 'test'));
// test with override (equal)
self::assertTrue($configCache->set('system', 'test', '8910', Cache\Cache::SOURCE_DB));
self::assertEquals('8910', $configCache->get('system', 'test'));
// test with override (over)
self::assertTrue($configCache->set('system', 'test', '111213', Cache\Cache::SOURCE_ENV));
self::assertEquals('111213', $configCache->get('system', 'test'));
}
}

View file

@ -0,0 +1,389 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Core\Config\Cache;
use Friendica\Core\Config\Cache;
use Friendica\Core\Config\Factory\ConfigFactory;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
use Friendica\Core\Config\Cache\ConfigFileLoader;
use org\bovigo\vfs\vfsStream;
class ConfigFileLoaderTest extends MockedTest
{
use VFSTrait;
protected function setUp(): void
{
parent::setUp();
$this->setUpVfsDir();
}
/**
* Test the loadConfigFiles() method with default values
*/
public function testLoadConfigFiles()
{
$this->delConfigFile('local.config.php');
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
}
/**
* Test the loadConfigFiles() method with a wrong local.config.php
*
*/
public function testLoadConfigWrong()
{
$this->expectExceptionMessageMatches("/Error loading config file \w+/");
$this->expectException(\Exception::class);
$this->delConfigFile('local.config.php');
vfsStream::newFile('local.config.php')
->at($this->root->getChild('config'))
->setContent('<?php return true;');
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($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 .
'A.config.php';
vfsStream::newFile('local.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::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 .
'A.ini.php';
vfsStream::newFile('local.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
self::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.php';
vfsStream::newFile('.htconfig.php')
->at($this->root)
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
self::assertEquals('anotherCharset', $configCache->get('database', 'charset'));
self::assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
self::assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
self::assertEquals('fr', $configCache->get('system', 'language'));
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
self::assertEquals('/another/php', $configCache->get('config', 'php_path'));
self::assertEquals('999', $configCache->get('config', 'max_import_size'));
self::assertEquals('666', $configCache->get('system', 'maximagesize'));
self::assertEquals('frio,quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
self::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 .
'A.config.php';
vfsStream::newFile('test.config.php')
->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
->setContent(file_get_contents($file));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$conf = $configFileLoader->loadAddonConfig('test');
self::assertEquals('testhost', $conf['database']['hostname']);
self::assertEquals('testuser', $conf['database']['username']);
self::assertEquals('testpw', $conf['database']['password']);
self::assertEquals('testdb', $conf['database']['database']);
self::assertEquals('admin@test.it', $conf['config']['admin_email']);
}
/**
* test loading multiple config files - the last config should work
*/
public function testLoadMultipleConfigs()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.config.php'));
vfsStream::newFile('B.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
/**
* test loading multiple config files - the last config should work (INI-version)
*/
public function testLoadMultipleInis()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.ini.php'));
vfsStream::newFile('B.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
/**
* Test that sample-files (e.g. local-sample.config.php) is never loaded
*/
public function testNotLoadingSamples()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.ini.php'));
vfsStream::newFile('B-sample.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
$configFileLoader = new ConfigFileLoader(
$this->root->url(),
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEmpty($configCache->get('system', 'NewKey'));
}
/**
* Test that using a wrong configuration directory leads to the "normal" config path
*/
public function testWrongEnvDir()
{
$this->delConfigFile('local.config.php');
$configFileLoader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
}
/**
* Test that a different location of the configuration directory produces the expected output
*/
public function testRightEnvDir()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('B.config.php')
->at($this->root->getChild('config2'))
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileLoader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
$configCache = new Cache\Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
}