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

@ -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));
}
}