Merge pull request from ArthurHoaro/config-manager

Introduce a configuration manager
This commit is contained in:
Arthur 2016-07-09 07:19:48 +02:00 committed by GitHub
commit 649af5b501
41 changed files with 2099 additions and 1133 deletions

View file

@ -3,6 +3,7 @@
* ApplicationUtils' tests
*/
require_once 'application/config/ConfigManager.php';
require_once 'application/ApplicationUtils.php';
/**
@ -59,7 +60,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
$testTimeout
)
);
$this->assertRegexp(
$this->assertRegExp(
self::$versionPattern,
ApplicationUtils::getLatestGitVersionCode(
'https://raw.githubusercontent.com/shaarli/Shaarli/'
@ -275,21 +276,21 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
*/
public function testCheckCurrentResourcePermissions()
{
$config = array(
'CACHEDIR' => 'cache',
'CONFIG_FILE' => 'data/config.php',
'DATADIR' => 'data',
'DATASTORE' => 'data/datastore.php',
'IPBANS_FILENAME' => 'data/ipbans.php',
'LOG_FILE' => 'data/log.txt',
'PAGECACHE' => 'pagecache',
'RAINTPL_TMP' => 'tmp',
'RAINTPL_TPL' => 'tpl',
'UPDATECHECK_FILENAME' => 'data/lastupdatecheck.txt'
);
$conf = new ConfigManager('');
$conf->set('resource.thumbnails_cache', 'cache');
$conf->set('resource.config', 'data/config.php');
$conf->set('resource.data_dir', 'data');
$conf->set('resource.datastore', 'data/datastore.php');
$conf->set('resource.ban_file', 'data/ipbans.php');
$conf->set('resource.log', 'data/log.txt');
$conf->set('resource.page_cache', 'pagecache');
$conf->set('resource.raintpl_tmp', 'tmp');
$conf->set('resource.raintpl_tpl', 'tpl');
$conf->set('resource.update_check', 'data/lastupdatecheck.txt');
$this->assertEquals(
array(),
ApplicationUtils::checkResourcePermissions($config)
ApplicationUtils::checkResourcePermissions($conf)
);
}
@ -298,18 +299,17 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
*/
public function testCheckCurrentResourcePermissionsErrors()
{
$config = array(
'CACHEDIR' => 'null/cache',
'CONFIG_FILE' => 'null/data/config.php',
'DATADIR' => 'null/data',
'DATASTORE' => 'null/data/store.php',
'IPBANS_FILENAME' => 'null/data/ipbans.php',
'LOG_FILE' => 'null/data/log.txt',
'PAGECACHE' => 'null/pagecache',
'RAINTPL_TMP' => 'null/tmp',
'RAINTPL_TPL' => 'null/tpl',
'UPDATECHECK_FILENAME' => 'null/data/lastupdatecheck.txt'
);
$conf = new ConfigManager('');
$conf->set('resource.thumbnails_cache', 'null/cache');
$conf->set('resource.config', 'null/data/config.php');
$conf->set('resource.data_dir', 'null/data');
$conf->set('resource.datastore', 'null/data/store.php');
$conf->set('resource.ban_file', 'null/data/ipbans.php');
$conf->set('resource.log', 'null/data/log.txt');
$conf->set('resource.page_cache', 'null/pagecache');
$conf->set('resource.raintpl_tmp', 'null/tmp');
$conf->set('resource.raintpl_tpl', 'null/tpl');
$conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
$this->assertEquals(
array(
'"null/tpl" directory is not readable',
@ -322,7 +322,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
'"null/tmp" directory is not readable',
'"null/tmp" directory is not writable'
),
ApplicationUtils::checkResourcePermissions($config)
ApplicationUtils::checkResourcePermissions($conf)
);
}
}

View file

@ -1,244 +0,0 @@
<?php
/**
* Config' tests
*/
require_once 'application/Config.php';
/**
* Unitary tests for Shaarli config related functions
*/
class ConfigTest extends PHPUnit_Framework_TestCase
{
// Configuration input set.
private static $configFields;
/**
* Executed before each test.
*/
public function setUp()
{
self::$configFields = array(
'login' => 'login',
'hash' => 'hash',
'salt' => 'salt',
'timezone' => 'Europe/Paris',
'title' => 'title',
'titleLink' => 'titleLink',
'redirector' => '',
'disablesessionprotection' => false,
'privateLinkByDefault' => false,
'config' => array(
'CONFIG_FILE' => 'tests/config.php',
'DATADIR' => 'tests',
'config1' => 'config1data',
'config2' => 'config2data',
)
);
}
/**
* Executed after each test.
*
* @return void
*/
public function tearDown()
{
if (is_file(self::$configFields['config']['CONFIG_FILE'])) {
unlink(self::$configFields['config']['CONFIG_FILE']);
}
}
/**
* Test writeConfig function, valid use case, while being logged in.
*/
public function testWriteConfig()
{
writeConfig(self::$configFields, true);
include self::$configFields['config']['CONFIG_FILE'];
$this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
$this->assertEquals(self::$configFields['hash'], $GLOBALS['hash']);
$this->assertEquals(self::$configFields['salt'], $GLOBALS['salt']);
$this->assertEquals(self::$configFields['timezone'], $GLOBALS['timezone']);
$this->assertEquals(self::$configFields['title'], $GLOBALS['title']);
$this->assertEquals(self::$configFields['titleLink'], $GLOBALS['titleLink']);
$this->assertEquals(self::$configFields['redirector'], $GLOBALS['redirector']);
$this->assertEquals(self::$configFields['disablesessionprotection'], $GLOBALS['disablesessionprotection']);
$this->assertEquals(self::$configFields['privateLinkByDefault'], $GLOBALS['privateLinkByDefault']);
$this->assertEquals(self::$configFields['config']['config1'], $GLOBALS['config']['config1']);
$this->assertEquals(self::$configFields['config']['config2'], $GLOBALS['config']['config2']);
}
/**
* Test writeConfig option while logged in:
* 1. init fields.
* 2. update fields, add new sub config, add new root config.
* 3. rewrite config.
* 4. check result.
*/
public function testWriteConfigFieldUpdate()
{
writeConfig(self::$configFields, true);
self::$configFields['title'] = 'ok';
self::$configFields['config']['config1'] = 'ok';
self::$configFields['config']['config_new'] = 'ok';
self::$configFields['new'] = 'should not be saved';
writeConfig(self::$configFields, true);
include self::$configFields['config']['CONFIG_FILE'];
$this->assertEquals('ok', $GLOBALS['title']);
$this->assertEquals('ok', $GLOBALS['config']['config1']);
$this->assertEquals('ok', $GLOBALS['config']['config_new']);
$this->assertFalse(isset($GLOBALS['new']));
}
/**
* Test writeConfig function with an empty array.
*
* @expectedException MissingFieldConfigException
*/
public function testWriteConfigEmpty()
{
writeConfig(array(), true);
}
/**
* Test writeConfig function with a missing mandatory field.
*
* @expectedException MissingFieldConfigException
*/
public function testWriteConfigMissingField()
{
unset(self::$configFields['login']);
writeConfig(self::$configFields, true);
}
/**
* Test writeConfig function while being logged out, and there is no config file existing.
*/
public function testWriteConfigLoggedOutNoFile()
{
writeConfig(self::$configFields, false);
}
/**
* Test writeConfig function while being logged out, and a config file already exists.
*
* @expectedException UnauthorizedConfigException
*/
public function testWriteConfigLoggedOutWithFile()
{
file_put_contents(self::$configFields['config']['CONFIG_FILE'], '');
writeConfig(self::$configFields, false);
}
/**
* Test save_plugin_config with valid data.
*
* @throws PluginConfigOrderException
*/
public function testSavePluginConfigValid()
{
$data = array(
'order_plugin1' => 2, // no plugin related
'plugin2' => 0, // new - at the end
'plugin3' => 0, // 2nd
'order_plugin3' => 8,
'plugin4' => 0, // 1st
'order_plugin4' => 5,
);
$expected = array(
'plugin3',
'plugin4',
'plugin2',
);
$out = save_plugin_config($data);
$this->assertEquals($expected, $out);
}
/**
* Test save_plugin_config with invalid data.
*
* @expectedException PluginConfigOrderException
*/
public function testSavePluginConfigInvalid()
{
$data = array(
'plugin2' => 0,
'plugin3' => 0,
'order_plugin3' => 0,
'plugin4' => 0,
'order_plugin4' => 0,
);
save_plugin_config($data);
}
/**
* Test save_plugin_config without data.
*/
public function testSavePluginConfigEmpty()
{
$this->assertEquals(array(), save_plugin_config(array()));
}
/**
* Test validate_plugin_order with valid data.
*/
public function testValidatePluginOrderValid()
{
$data = array(
'order_plugin1' => 2,
'plugin2' => 0,
'plugin3' => 0,
'order_plugin3' => 1,
'plugin4' => 0,
'order_plugin4' => 5,
);
$this->assertTrue(validate_plugin_order($data));
}
/**
* Test validate_plugin_order with invalid data.
*/
public function testValidatePluginOrderInvalid()
{
$data = array(
'order_plugin1' => 2,
'order_plugin3' => 1,
'order_plugin4' => 1,
);
$this->assertFalse(validate_plugin_order($data));
}
/**
* Test load_plugin_parameter_values.
*/
public function testLoadPluginParameterValues()
{
$plugins = array(
'plugin_name' => array(
'parameters' => array(
'param1' => true,
'param2' => false,
'param3' => '',
)
)
);
$parameters = array(
'param1' => 'value1',
'param2' => 'value2',
);
$result = load_plugin_parameter_values($plugins, $parameters);
$this->assertEquals('value1', $result['plugin_name']['parameters']['param1']);
$this->assertEquals('value2', $result['plugin_name']['parameters']['param2']);
$this->assertEquals('', $result['plugin_name']['parameters']['param3']);
}
}

View file

@ -76,7 +76,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
// Test headers (RSS)
$this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
$this->assertEmpty($data['pubsubhub_url']);
$this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $data['last_update']);
$this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $data['last_update']);
$this->assertEquals(true, $data['show_dates']);
$this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
$this->assertEquals('http://host.tld/', $data['index_url']);
@ -88,7 +88,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('20150310_114651', $link['linkdate']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']);
$this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $link['iso_date']);
$this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['iso_date']);
$this->assertContains('Stallman has a beard', $link['description']);
$this->assertContains('Permalink', $link['description']);
$this->assertContains('http://host.tld/?WDWyig', $link['description']);
@ -113,7 +113,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$link = array_shift($data['links']);
$this->assertEquals('2015-03-10T11:46:51+01:00', $link['iso_date']);
$this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:+\d{2}/', $link['iso_date']);
}
/**

View file

@ -101,7 +101,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
* Attempt to instantiate a LinkDB whereas the datastore is not writable
*
* @expectedException IOException
* @expectedExceptionMessageRegExp /Error accessing null/
* @expectedExceptionMessageRegExp /Error accessing\nnull/
*/
public function testConstructDatastoreNotWriteable()
{

View file

@ -23,6 +23,17 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
*/
private static $pluginName = 'test';
/**
* @var PluginManager $pluginManager Plugin Mananger instance.
*/
protected $pluginManager;
public function setUp()
{
$conf = new ConfigManager('');
$this->pluginManager = new PluginManager($conf);
}
/**
* Test plugin loading and hook execution.
*
@ -30,23 +41,21 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
*/
public function testPlugin()
{
$pluginManager = PluginManager::getInstance();
PluginManager::$PLUGINS_PATH = self::$pluginPath;
$pluginManager->load(array(self::$pluginName));
$this->pluginManager->load(array(self::$pluginName));
$this->assertTrue(function_exists('hook_test_random'));
$data = array(0 => 'woot');
$pluginManager->executeHooks('random', $data);
$this->pluginManager->executeHooks('random', $data);
$this->assertEquals('woot', $data[1]);
$data = array(0 => 'woot');
$pluginManager->executeHooks('random', $data, array('target' => 'test'));
$this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
$this->assertEquals('page test', $data[1]);
$data = array(0 => 'woot');
$pluginManager->executeHooks('random', $data, array('loggedin' => true));
$this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
$this->assertEquals('loggedin', $data[1]);
}
@ -57,11 +66,8 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
*/
public function testPluginNotFound()
{
$pluginManager = PluginManager::getInstance();
$pluginManager->load(array());
$pluginManager->load(array('nope', 'renope'));
$this->pluginManager->load(array());
$this->pluginManager->load(array('nope', 'renope'));
}
/**
@ -69,16 +75,14 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
*/
public function testGetPluginsMeta()
{
$pluginManager = PluginManager::getInstance();
PluginManager::$PLUGINS_PATH = self::$pluginPath;
$pluginManager->load(array(self::$pluginName));
$this->pluginManager->load(array(self::$pluginName));
$expectedParameters = array(
'pop' => '',
'hip' => '',
);
$meta = $pluginManager->getPluginsMeta();
$meta = $this->pluginManager->getPluginsMeta();
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
}

View file

@ -11,14 +11,14 @@ class DummyUpdater extends Updater
/**
* Object constructor.
*
* @param array $doneUpdates Updates which are already done.
* @param array $config Shaarli's configuration array.
* @param LinkDB $linkDB LinkDB instance.
* @param boolean $isLoggedIn True if the user is logged in.
* @param array $doneUpdates Updates which are already done.
* @param LinkDB $linkDB LinkDB instance.
* @param ConfigManager $conf Configuration Manager instance.
* @param boolean $isLoggedIn True if the user is logged in.
*/
public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn)
public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
{
parent::__construct($doneUpdates, $config, $linkDB, $isLoggedIn);
parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn);
// Retrieve all update methods.
// For unit test, only retrieve final methods,

View file

@ -1,5 +1,6 @@
<?php
require_once 'application/config/ConfigManager.php';
require_once 'tests/Updater/DummyUpdater.php';
/**
@ -8,59 +9,27 @@ require_once 'tests/Updater/DummyUpdater.php';
*/
class UpdaterTest extends PHPUnit_Framework_TestCase
{
/**
* @var array Configuration input set.
*/
private static $configFields;
/**
* @var string Path to test datastore.
*/
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var string Config file path (without extension).
*/
protected static $configFile = 'tests/utils/config/configJson';
/**
* @var ConfigManager
*/
protected $conf;
/**
* Executed before each test.
*/
public function setUp()
{
self::$configFields = array(
'login' => 'login',
'hash' => 'hash',
'salt' => 'salt',
'timezone' => 'Europe/Paris',
'title' => 'title',
'titleLink' => 'titleLink',
'redirector' => '',
'disablesessionprotection' => false,
'privateLinkByDefault' => false,
'config' => array(
'CONFIG_FILE' => 'tests/Updater/config.php',
'DATADIR' => 'tests/Updater',
'PAGECACHE' => 'sandbox/pagecache',
'config1' => 'config1data',
'config2' => 'config2data',
)
);
}
/**
* Executed after each test.
*
* @return void
*/
public function tearDown()
{
if (is_file(self::$configFields['config']['CONFIG_FILE'])) {
unlink(self::$configFields['config']['CONFIG_FILE']);
}
if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) {
unlink(self::$configFields['config']['DATADIR'] . '/options.php');
}
if (is_file(self::$configFields['config']['DATADIR'] . '/updates.json')) {
unlink(self::$configFields['config']['DATADIR'] . '/updates.json');
}
$this->conf = new ConfigManager(self::$configFile);
}
/**
@ -69,9 +38,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
public function testReadEmptyUpdatesFile()
{
$this->assertEquals(array(), read_updates_file(''));
$updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json';
$updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
touch($updatesFile);
$this->assertEquals(array(), read_updates_file($updatesFile));
unlink($updatesFile);
}
/**
@ -79,7 +49,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function testReadWriteUpdatesFile()
{
$updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json';
$updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
$updatesMethods = array('m1', 'm2', 'm3');
write_updates_file($updatesFile, $updatesMethods);
@ -91,6 +61,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
write_updates_file($updatesFile, $updatesMethods);
$readMethods = read_updates_file($updatesFile);
$this->assertEquals($readMethods, $updatesMethods);
unlink($updatesFile);
}
/**
@ -112,10 +83,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function testWriteUpdatesFileNotWritable()
{
$updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json';
$updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
touch($updatesFile);
chmod($updatesFile, 0444);
@write_updates_file($updatesFile, array('test'));
try {
@write_updates_file($updatesFile, array('test'));
} catch (Exception $e) {
unlink($updatesFile);
throw $e;
}
}
/**
@ -131,10 +107,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy3',
'updateMethodException',
);
$updater = new DummyUpdater($updates, array(), array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals(array(), $updater->update());
$updater = new DummyUpdater(array(), array(), array(), false);
$updater = new DummyUpdater(array(), array(), $this->conf, false);
$this->assertEquals(array(), $updater->update());
}
@ -149,7 +125,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy2',
'updateMethodDummy3',
);
$updater = new DummyUpdater($updates, array(), array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals($expectedUpdates, $updater->update());
}
@ -165,7 +141,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
);
$expectedUpdate = array('updateMethodDummy2');
$updater = new DummyUpdater($updates, array(), array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$this->assertEquals($expectedUpdate, $updater->update());
}
@ -182,7 +158,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
'updateMethodDummy3',
);
$updater = new DummyUpdater($updates, array(), array(), true);
$updater = new DummyUpdater($updates, array(), $this->conf, true);
$updater->update();
}
@ -195,26 +171,28 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function testUpdateMergeDeprecatedConfig()
{
// init
writeConfig(self::$configFields, true);
$configCopy = self::$configFields;
$invert = !$configCopy['privateLinkByDefault'];
$configCopy['privateLinkByDefault'] = $invert;
$this->conf->setConfigFile('tests/utils/config/configPhp');
$this->conf->reset();
// Use writeConfig to create a options.php
$configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php';
writeConfig($configCopy, true);
$optionsFile = 'tests/Updater/options.php';
$options = '<?php
$GLOBALS[\'privateLinkByDefault\'] = true;';
file_put_contents($optionsFile, $options);
$this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));
// tmp config file.
$this->conf->setConfigFile('tests/Updater/config');
// merge configs
$updater = new Updater(array(), self::$configFields, array(), true);
$updater = new Updater(array(), array(), $this->conf, true);
// This writes a new config file in tests/Updater/config.php
$updater->updateMethodMergeDeprecatedConfigFile();
// make sure updated field is changed
include self::$configFields['config']['CONFIG_FILE'];
$this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);
$this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));
$this->conf->reload();
$this->assertTrue($this->conf->get('privacy.default_private_links'));
$this->assertFalse(is_file($optionsFile));
// Delete the generated file.
unlink($this->conf->getConfigFileExt());
}
/**
@ -222,23 +200,67 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function testMergeDeprecatedConfigNoFile()
{
writeConfig(self::$configFields, true);
$updater = new Updater(array(), self::$configFields, array(), true);
$updater = new Updater(array(), array(), $this->conf, true);
$updater->updateMethodMergeDeprecatedConfigFile();
include self::$configFields['config']['CONFIG_FILE'];
$this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
$this->assertEquals('root', $this->conf->get('credentials.login'));
}
/**
* Test renameDashTags update method.
*/
public function testRenameDashTags()
{
$refDB = new ReferenceLinkDB();
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
$updater = new Updater(array(), self::$configFields, $linkDB, true);
$updater = new Updater(array(), $linkDB, $this->conf, true);
$updater->updateMethodRenameDashTags();
$this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
}
/**
* Convert old PHP config file to JSON config.
*/
public function testConfigToJson()
{
$configFile = 'tests/utils/config/configPhp';
$this->conf->setConfigFile($configFile);
$this->conf->reset();
// The ConfigIO is initialized with ConfigPhp.
$this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
$updater = new Updater(array(), array(), $this->conf, false);
$done = $updater->updateMethodConfigToJson();
$this->assertTrue($done);
// The ConfigIO has been updated to ConfigJson.
$this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
$this->assertTrue(file_exists($this->conf->getConfigFileExt()));
// Check JSON config data.
$this->conf->reload();
$this->assertEquals('root', $this->conf->get('credentials.login'));
$this->assertEquals('lala', $this->conf->get('redirector.url'));
$this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
$this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
rename($configFile . '.save.php', $configFile . '.php');
unlink($this->conf->getConfigFileExt());
}
/**
* Launch config conversion update with an existing JSON file => nothing to do.
*/
public function testConfigToJsonNothingToDo()
{
$filetime = filemtime($this->conf->getConfigFileExt());
$updater = new Updater(array(), array(), $this->conf, false);
$done = $updater->updateMethodConfigToJson();
$this->assertTrue($done);
$expected = filemtime($this->conf->getConfigFileExt());
$this->assertEquals($expected, $filetime);
}
}

View file

@ -0,0 +1,133 @@
<?php
require_once 'application/config/ConfigJson.php';
/**
* Class ConfigJsonTest
*/
class ConfigJsonTest extends PHPUnit_Framework_TestCase
{
/**
* @var ConfigJson
*/
protected $configIO;
public function setUp()
{
$this->configIO = new ConfigJson();
}
/**
* Read a simple existing config file.
*/
public function testRead()
{
$conf = $this->configIO->read('tests/utils/config/configJson.json.php');
$this->assertEquals('root', $conf['credentials']['login']);
$this->assertEquals('lala', $conf['redirector']['url']);
$this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
$this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
}
/**
* Read a non existent config file -> empty array.
*/
public function testReadNonExistent()
{
$this->assertEquals(array(), $this->configIO->read('nope'));
}
/**
* Read a non existent config file -> empty array.
*
* @expectedException Exception
* @expectedExceptionMessage An error occured while parsing JSON file: error code #4
*/
public function testReadInvalidJson()
{
$this->configIO->read('tests/utils/config/configInvalid.json.php');
}
/**
* Write a new config file.
*/
public function testWriteNew()
{
$dataFile = 'tests/utils/config/configWrite.json.php';
$data = array(
'credentials' => array(
'login' => 'root',
),
'resource' => array(
'datastore' => 'data/datastore.php',
),
'redirector' => array(
'url' => 'lala',
),
'plugins' => array(
'WALLABAG_VERSION' => '1',
)
);
$this->configIO->write($dataFile, $data);
// PHP 5.3 doesn't support json pretty print.
if (defined('JSON_PRETTY_PRINT')) {
$expected = '{
"credentials": {
"login": "root"
},
"resource": {
"datastore": "data\/datastore.php"
},
"redirector": {
"url": "lala"
},
"plugins": {
"WALLABAG_VERSION": "1"
}
}';
} else {
$expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
}
$expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix();
$this->assertEquals($expected, file_get_contents($dataFile));
unlink($dataFile);
}
/**
* Overwrite an existing setting.
*/
public function testOverwrite()
{
$source = 'tests/utils/config/configJson.json.php';
$dest = 'tests/utils/config/configOverwrite.json.php';
copy($source, $dest);
$conf = $this->configIO->read($dest);
$conf['redirector']['url'] = 'blabla';
$this->configIO->write($dest, $conf);
$conf = $this->configIO->read($dest);
$this->assertEquals('blabla', $conf['redirector']['url']);
unlink($dest);
}
/**
* Write to invalid path.
*
* @expectedException IOException
*/
public function testWriteInvalidArray()
{
$conf = array('conf' => 'value');
@$this->configIO->write(array(), $conf);
}
/**
* Write to invalid path.
*
* @expectedException IOException
*/
public function testWriteInvalidBlank()
{
$conf = array('conf' => 'value');
@$this->configIO->write('', $conf);
}
}

View file

@ -0,0 +1,172 @@
<?php
/**
* Unit tests for Class ConfigManagerTest
*
* Note: it only test the manager with ConfigJson,
* ConfigPhp is only a workaround to handle the transition to JSON type.
*/
class ConfigManagerTest extends PHPUnit_Framework_TestCase
{
/**
* @var ConfigManager
*/
protected $conf;
public function setUp()
{
$this->conf = new ConfigManager('tests/utils/config/configJson');
}
/**
* Simple config test:
* 1. Set settings.
* 2. Check settings value.
*/
public function testSetGet()
{
$this->conf->set('paramInt', 42);
$this->conf->set('paramString', 'value1');
$this->conf->set('paramBool', false);
$this->conf->set('paramArray', array('foo' => 'bar'));
$this->conf->set('paramNull', null);
$this->assertEquals(42, $this->conf->get('paramInt'));
$this->assertEquals('value1', $this->conf->get('paramString'));
$this->assertFalse($this->conf->get('paramBool'));
$this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
$this->assertEquals(null, $this->conf->get('paramNull'));
}
/**
* Set/write/get config test:
* 1. Set settings.
* 2. Write it to the config file.
* 3. Read the file.
* 4. Check settings value.
*/
public function testSetWriteGet()
{
$this->conf->set('paramInt', 42);
$this->conf->set('paramString', 'value1');
$this->conf->set('paramBool', false);
$this->conf->set('paramArray', array('foo' => 'bar'));
$this->conf->set('paramNull', null);
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->conf->write(true);
$this->conf->reload();
unlink($this->conf->getConfigFileExt());
$this->assertEquals(42, $this->conf->get('paramInt'));
$this->assertEquals('value1', $this->conf->get('paramString'));
$this->assertFalse($this->conf->get('paramBool'));
$this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
$this->assertEquals(null, $this->conf->get('paramNull'));
}
/**
* Test set/write/get with nested keys.
*/
public function testSetWriteGetNested()
{
$this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->conf->write(true);
$this->conf->reload();
unlink($this->conf->getConfigFileExt());
$this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
}
/**
* Set with an empty key.
*
* @expectedException Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/
public function testSetEmptyKey()
{
$this->conf->set('', 'stuff');
}
/**
* Set with an array key.
*
* @expectedException Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/
public function testSetArrayKey()
{
$this->conf->set(array('foo' => 'bar'), 'stuff');
}
/**
* Try to write the config without mandatory parameter (e.g. 'login').
*
* @expectedException MissingFieldConfigException
*/
public function testWriteMissingParameter()
{
$this->conf->setConfigFile('tests/utils/config/configTmp');
$this->assertFalse(file_exists($this->conf->getConfigFileExt()));
$this->conf->reload();
$this->conf->write(true);
}
/**
* Try to get non existent config keys.
*/
public function testGetNonExistent()
{
$this->assertEquals('', $this->conf->get('nope.test'));
$this->assertEquals('default', $this->conf->get('nope.test', 'default'));
}
/**
* Test the 'exists' method with existent values.
*/
public function testExistsOk()
{
$this->assertTrue($this->conf->exists('credentials.login'));
$this->assertTrue($this->conf->exists('config.foo'));
}
/**
* Test the 'exists' method with non existent or invalid values.
*/
public function testExistsKo()
{
$this->assertFalse($this->conf->exists('nope'));
$this->assertFalse($this->conf->exists('nope.nope'));
$this->assertFalse($this->conf->exists(''));
$this->assertFalse($this->conf->exists(false));
}
/**
* Reset the ConfigManager instance.
*/
public function testReset()
{
$confIO = $this->conf->getConfigIO();
$this->conf->reset();
$this->assertFalse($confIO === $this->conf->getConfigIO());
}
/**
* Reload the config from file.
*/
public function testReload()
{
$this->conf->setConfigFile('tests/utils/config/configTmp');
$newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
file_put_contents($this->conf->getConfigFileExt(), $newConf);
$this->conf->reload();
unlink($this->conf->getConfigFileExt());
// Previous conf no longer exists, and new values have been loaded.
$this->assertFalse($this->conf->exists('credentials.login'));
$this->assertEquals('value', $this->conf->get('key'));
}
}

View file

@ -0,0 +1,82 @@
<?php
require_once 'application/config/ConfigPhp.php';
/**
* Class ConfigPhpTest
*/
class ConfigPhpTest extends PHPUnit_Framework_TestCase
{
/**
* @var ConfigPhp
*/
protected $configIO;
public function setUp()
{
$this->configIO = new ConfigPhp();
}
/**
* Read a simple existing config file.
*/
public function testRead()
{
$conf = $this->configIO->read('tests/utils/config/configPhp.php');
$this->assertEquals('root', $conf['login']);
$this->assertEquals('lala', $conf['redirector']);
$this->assertEquals('data/datastore.php', $conf['config']['DATASTORE']);
$this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
}
/**
* Read a non existent config file -> empty array.
*/
public function testReadNonExistent()
{
$this->assertEquals(array(), $this->configIO->read('nope'));
}
/**
* Write a new config file.
*/
public function testWriteNew()
{
$dataFile = 'tests/utils/config/configWrite.php';
$data = array(
'login' => 'root',
'redirector' => 'lala',
'config' => array(
'DATASTORE' => 'data/datastore.php',
),
'plugins' => array(
'WALLABAG_VERSION' => '1',
)
);
$this->configIO->write($dataFile, $data);
$expected = '<?php
$GLOBALS[\'login\'] = \'root\';
$GLOBALS[\'redirector\'] = \'lala\';
$GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
$GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
';
$this->assertEquals($expected, file_get_contents($dataFile));
unlink($dataFile);
}
/**
* Overwrite an existing setting.
*/
public function testOverwrite()
{
$source = 'tests/utils/config/configPhp.php';
$dest = 'tests/utils/config/configOverwrite.php';
copy($source, $dest);
$conf = $this->configIO->read($dest);
$conf['redirector'] = 'blabla';
$this->configIO->write($dest, $conf);
$conf = $this->configIO->read($dest);
$this->assertEquals('blabla', $conf['redirector']);
unlink($dest);
}
}

View file

@ -0,0 +1,121 @@
<?php
/**
* Config' tests
*/
require_once 'application/config/ConfigPlugin.php';
/**
* Unitary tests for Shaarli config related functions
*/
class ConfigPluginTest extends PHPUnit_Framework_TestCase
{
/**
* Test save_plugin_config with valid data.
*
* @throws PluginConfigOrderException
*/
public function testSavePluginConfigValid()
{
$data = array(
'order_plugin1' => 2, // no plugin related
'plugin2' => 0, // new - at the end
'plugin3' => 0, // 2nd
'order_plugin3' => 8,
'plugin4' => 0, // 1st
'order_plugin4' => 5,
);
$expected = array(
'plugin3',
'plugin4',
'plugin2',
);
$out = save_plugin_config($data);
$this->assertEquals($expected, $out);
}
/**
* Test save_plugin_config with invalid data.
*
* @expectedException PluginConfigOrderException
*/
public function testSavePluginConfigInvalid()
{
$data = array(
'plugin2' => 0,
'plugin3' => 0,
'order_plugin3' => 0,
'plugin4' => 0,
'order_plugin4' => 0,
);
save_plugin_config($data);
}
/**
* Test save_plugin_config without data.
*/
public function testSavePluginConfigEmpty()
{
$this->assertEquals(array(), save_plugin_config(array()));
}
/**
* Test validate_plugin_order with valid data.
*/
public function testValidatePluginOrderValid()
{
$data = array(
'order_plugin1' => 2,
'plugin2' => 0,
'plugin3' => 0,
'order_plugin3' => 1,
'plugin4' => 0,
'order_plugin4' => 5,
);
$this->assertTrue(validate_plugin_order($data));
}
/**
* Test validate_plugin_order with invalid data.
*/
public function testValidatePluginOrderInvalid()
{
$data = array(
'order_plugin1' => 2,
'order_plugin3' => 1,
'order_plugin4' => 1,
);
$this->assertFalse(validate_plugin_order($data));
}
/**
* Test load_plugin_parameter_values.
*/
public function testLoadPluginParameterValues()
{
$plugins = array(
'plugin_name' => array(
'parameters' => array(
'param1' => true,
'param2' => false,
'param3' => '',
)
)
);
$parameters = array(
'param1' => 'value1',
'param2' => 'value2',
);
$result = load_plugin_parameter_values($plugins, $parameters);
$this->assertEquals('value1', $result['plugin_name']['parameters']['param1']);
$this->assertEquals('value2', $result['plugin_name']['parameters']['param2']);
$this->assertEquals('', $result['plugin_name']['parameters']['param3']);
}
}

View file

@ -4,6 +4,8 @@
* PluginReadityourselfTest.php.php
*/
// FIXME! add an init method.
$conf = new ConfigManager('');
require_once 'plugins/readityourself/readityourself.php';
/**
@ -25,7 +27,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
*/
function testReadityourselfLinklist()
{
$GLOBALS['plugins']['READITYOUSELF_URL'] = 'value';
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
'title' => $str,
@ -36,7 +39,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
)
);
$data = hook_readityourself_render_linklist($data);
$data = hook_readityourself_render_linklist($data, $conf);
$link = $data['links'][0];
// data shouldn't be altered
$this->assertEquals($str, $data['title']);
@ -52,7 +55,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
*/
function testReadityourselfLinklistWithoutConfig()
{
unset($GLOBALS['plugins']['READITYOUSELF_URL']);
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', null);
$str = 'http://randomstr.com/test';
$data = array(
'title' => $str,
@ -63,7 +67,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
)
);
$data = hook_readityourself_render_linklist($data);
$data = hook_readityourself_render_linklist($data, $conf);
$link = $data['links'][0];
// data shouldn't be altered
$this->assertEquals($str, $data['title']);

View file

@ -4,6 +4,8 @@
* PluginWallabagTest.php.php
*/
// FIXME! add an init method.
$conf = new ConfigManager('');
require_once 'plugins/wallabag/wallabag.php';
/**
@ -25,7 +27,8 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
*/
function testWallabagLinklist()
{
$GLOBALS['plugins']['WALLABAG_URL'] = 'value';
$conf = new ConfigManager('');
$conf->set('plugins.WALLABAG_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
'title' => $str,
@ -36,7 +39,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
)
);
$data = hook_wallabag_render_linklist($data);
$data = hook_wallabag_render_linklist($data, $conf);
$link = $data['links'][0];
// data shouldn't be altered
$this->assertEquals($str, $data['title']);
@ -45,7 +48,6 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
// plugin data
$this->assertEquals(1, count($link['link_plugin']));
$this->assertNotFalse(strpos($link['link_plugin'][0], urlencode($str)));
$this->assertNotFalse(strpos($link['link_plugin'][0], $GLOBALS['plugins']['WALLABAG_URL']));
$this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
}
}

View file

@ -0,0 +1,5 @@
<?php /*
{
bad: bad,
}
*/ ?>

View file

@ -0,0 +1,34 @@
<?php /*
{
"credentials": {
"login":"root",
"hash":"hash",
"salt":"salt"
},
"security": {
"session_protection_disabled":false
},
"general": {
"timezone":"Europe\/Paris",
"title": "Shaarli",
"header_link": "?"
},
"privacy": {
"default_private_links":true
},
"redirector": {
"url":"lala"
},
"config": {
"foo": "bar"
},
"resource": {
"datastore": "tests\/utils\/config\/datastore.php",
"data_dir": "tests\/utils\/config"
},
"plugins": {
"WALLABAG_VERSION": 1
}
}
*/ ?>

View file

@ -0,0 +1,14 @@
<?php
$GLOBALS['login'] = 'root';
$GLOBALS['hash'] = 'hash';
$GLOBALS['salt'] = 'salt';
$GLOBALS['timezone'] = 'Europe/Paris';
$GLOBALS['title'] = 'title';
$GLOBALS['titleLink'] = 'titleLink';
$GLOBALS['redirector'] = 'lala';
$GLOBALS['disablesessionprotection'] = false;
$GLOBALS['privateLinkByDefault'] = false;
$GLOBALS['config']['DATADIR'] = 'tests/Updater';
$GLOBALS['config']['PAGECACHE'] = 'sandbox/pagecache';
$GLOBALS['config']['DATASTORE'] = 'data/datastore.php';
$GLOBALS['plugins']['WALLABAG_VERSION'] = '1';