Adding ConfigFileSaver and tests

This commit is contained in:
Philipp Holzer 2019-03-24 12:54:26 +01:00
parent fa31bb6dde
commit 49def0dc27
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
21 changed files with 493 additions and 473 deletions

View file

@ -9,7 +9,7 @@ use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigCacheLoader;
use Friendica\Util\Config\ConfigFileLoader;
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 ConfigCacheLoader($basePath, $mode);
$configLoader = new ConfigFileLoader($basePath, $mode);
$config = Factory\ConfigFactory::createCache($configLoader);
$profiler = \Mockery::mock(Profiler::class);

View file

@ -1,63 +0,0 @@
<?php
/**
* A test .htconfig file
*/
$db_host = 'testhost';
$db_user = 'testuser';
$db_pass = 'testpw';
$db_data = 'testdb';
$pidfile = '/var/run/friendica.pid';
// Set the database connection charset to UTF8.
// Changing this value will likely corrupt the special characters.
// You have been warned.
$a->config['system']['db_charset'] = "anotherCharset";
// Choose a legal default timezone. If you are unsure, use "America/Los_Angeles".
// It can be changed later and only applies to timestamps for anonymous viewers.
$default_timezone = 'Europe/Berlin';
$lang = 'fr';
// What is your site name?
$a->config['sitename'] = "Friendica My Network";
// Your choices are REGISTER_OPEN, REGISTER_APPROVE, or REGISTER_CLOSED.
// Be certain to create your own personal account before setting
// REGISTER_CLOSED. 'register_text' (if set) will be displayed prominently on
// the registration page. REGISTER_APPROVE requires you set 'admin_email'
// to the email address of an already registered person who can authorise
// and/or approve/deny the request.
// In order to perform system administration via the admin panel, admin_email
// must precisely match the email address of the person logged in.
$a->config['register_policy'] = REGISTER_OPEN;
$a->config['register_text'] = 'A register text';
$a->config['admin_email'] = 'admin@friendica.local';
$a->config['admin_nickname'] = 'Friendly admin';
// Maximum size of an imported message, 0 is unlimited
$a->config['max_import_size'] = 999;
// maximum size of uploaded photos
$a->config['system']['maximagesize'] = 666;
// Location of PHP command line processor
$a->config['php_path'] = '/another/php';
// PuSH - aka pubsubhubbub URL. This makes delivery of public posts as fast as private posts
$a->config['system']['huburl'] = '[internal]';
// allowed themes (change this from admin panel after installation)
$a->config['system']['allowed_themes'] = 'quattro,vier,duepuntozero';
// default system theme
$a->config['system']['theme'] = 'duepuntozero';
// By default allow pseudonyms
$a->config['system']['no_regfullname'] = true;
//Deny public access to the local directory
//$a->config['system']['block_local_dir'] = false;
// Location of the global directory
$a->config['system']['directory'] = 'http://another.url';

View file

@ -23,5 +23,6 @@ return [
'system' => [
'default_timezone' => 'UTC',
'language' => 'en',
'theme' => 'frio',
],
];

View file

@ -11,6 +11,9 @@ username = testuser
password = testpw
database = testdb
[system]
theme = frio
[config]
admin_email = admin@test.it
INI;

View file

@ -13,7 +13,7 @@ use Friendica\Core\System;
use Friendica\Factory;
use Friendica\Network\HTTPException;
use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigCacheLoader;
use Friendica\Util\Config\ConfigFileLoader;
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 ConfigCacheLoader($basePath, $mode);
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);

View file

@ -7,7 +7,7 @@ use Friendica\Database\DBA;
use Friendica\Factory;
use Friendica\Test\DatabaseTest;
use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigCacheLoader;
use Friendica\Util\Config\ConfigFileLoader;
class DBATest extends DatabaseTest
{
@ -15,7 +15,7 @@ class DBATest extends DatabaseTest
{
$basePath = BasePath::create(dirname(__DIR__) . '/../../');
$mode = new App\Mode($basePath);
$configLoader = new ConfigCacheLoader($basePath, $mode);
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);

View file

@ -7,7 +7,7 @@ use Friendica\Database\DBStructure;
use Friendica\Factory;
use Friendica\Test\DatabaseTest;
use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigCacheLoader;
use Friendica\Util\Config\ConfigFileLoader;
class DBStructureTest extends DatabaseTest
{
@ -15,7 +15,7 @@ class DBStructureTest extends DatabaseTest
{
$basePath = BasePath::create(dirname(__DIR__) . '/../../');
$mode = new App\Mode($basePath);
$configLoader = new ConfigCacheLoader($basePath, $mode);
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);

View file

@ -1,133 +0,0 @@
<?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 .
'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->assertNull($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 .
'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->assertNull($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()));
}
}

View file

@ -6,11 +6,11 @@ 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\ConfigFileLoader;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStream;
class ConfigCacheLoaderTest extends MockedTest
class ConfigFileLoaderTest extends MockedTest
{
use VFSTrait;
@ -34,10 +34,10 @@ class ConfigCacheLoaderTest extends MockedTest
*/
public function testLoadConfigFiles()
{
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configCacheLoader->loadConfigFiles($configCache);
$configFileLoader->setupCache($configCache);
$this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
}
@ -55,10 +55,10 @@ class ConfigCacheLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent('<?php return true;');
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configCacheLoader->loadConfigFiles($configCache);
$configFileLoader->setupCache($configCache);
}
/**
@ -79,10 +79,10 @@ class ConfigCacheLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configCacheLoader->loadConfigFiles($configCache);
$configFileLoader->setupCache($configCache);
$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
$this->assertEquals('testuser', $configCache->get('database', 'username'));
@ -111,10 +111,10 @@ class ConfigCacheLoaderTest extends MockedTest
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configCacheLoader->loadConfigFiles($configCache);
$configFileLoader->setupCache($configCache);
$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
$this->assertEquals('testuser', $configCache->get('database', 'username'));
@ -136,16 +136,16 @@ class ConfigCacheLoaderTest extends MockedTest
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR .
'.htconfig.test.php';
'.htconfig.php';
vfsStream::newFile('.htconfig.php')
->at($this->root)
->setContent(file_get_contents($file));
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configCacheLoader->loadConfigFiles($configCache);
$configFileLoader->setupCache($configCache);
$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
$this->assertEquals('testuser', $configCache->get('database', 'username'));
@ -157,7 +157,7 @@ class ConfigCacheLoaderTest extends MockedTest
$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('admin@test.it', $configCache->get('config', 'admin_email'));
$this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
$this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
@ -191,9 +191,9 @@ class ConfigCacheLoaderTest extends MockedTest
->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
->setContent(file_get_contents($file));
$configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$conf = $configCacheLoader->loadAddonConfig('test');
$conf = $configFileLoader->loadAddonConfig('test');
$this->assertEquals('testhost', $conf['database']['hostname']);
$this->assertEquals('testuser', $conf['database']['username']);

View file

@ -0,0 +1,139 @@
<?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\ConfigFileLoader;
use Friendica\Util\Config\ConfigFileSaver;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStream;
class ConfigFileSaverTest 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);
}
public function dataConfigFiles()
{
return [
'config' => [
'fileName' => 'local.config.php',
'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config',
'relativePath' => 'config',
],
'ini' => [
'fileName' => 'local.ini.php',
'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config',
'relativePath' => 'config',
],
'htconfig' => [
'fileName' => '.htconfig.php',
'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config',
'relativePath' => '',
],
];
}
/**
* Test the saveToConfigFile() method
* @dataProvider dataConfigFiles
*
* @todo 20190324 [nupplaphil] for ini-configs, it isn't possible to use $ or ! inside values
*/
public function testSaveToConfig($fileName, $filePath, $relativePath)
{
$this->delConfigFile('local.config.php');
if (empty($relativePath)) {
$root = $this->root;
$relativeFullName = $fileName;
} else {
$root = $this->root->getChild($relativePath);
$relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
}
vfsStream::newFile($fileName)
->at($root)
->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
$configFileSaver = new ConfigFileSaver($this->root->url());
$configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
$configCache = new ConfigCache();
$configFileLoader->setupCache($configCache);
$this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
$this->assertNull($configCache->get('config', 'test_val'));
$this->assertNull($configCache->get('system', 'test_val2'));
$configFileSaver->addConfigValue('system', 'theme', 'frio');
$configFileSaver->addConfigValue('config', 'admin_email', 'new@mail.it');
$configFileSaver->addConfigValue('config', 'test_val', 'Testingwith@all.we can');
$configFileSaver->addConfigValue('system', 'theme', 'vier');
$configFileSaver->addConfigValue('system', 'test_val2', 'TestIt Now');
$this->assertTrue($configFileSaver->saveToConfigFile());
$newConfigCache = new ConfigCache();
$configFileLoader->setupCache($newConfigCache);
$this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
$this->assertEquals('Testingwith@all.we can', $newConfigCache->get('config', 'test_val'));
$this->assertEquals('vier', $newConfigCache->get('system', 'theme'));
$this->assertEquals('TestIt Now', $newConfigCache->get('system', 'test_val2'));
$this->assertTrue($this->root->hasChild($relativeFullName));
$this->assertTrue($this->root->hasChild($relativeFullName . '.old'));
$this->assertFalse($this->root->hasChild($relativeFullName . '.tmp'));
$this->assertEquals(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName), file_get_contents($this->root->getChild($relativeFullName . '.old')->url()));
}
/**
* Test the saveToConfigFile() method without permissions
* @dataProvider dataConfigFiles
*/
public function testNoPermission($fileName, $filePath, $relativePath)
{
$this->delConfigFile('local.config.php');
if (empty($relativePath)) {
$root = $this->root;
$relativeFullName = $fileName;
} else {
$root = $this->root->getChild($relativePath);
$relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
}
$root->chmod(000);
vfsStream::newFile($fileName)
->at($root)
->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
}
}