From 59404d7909b21682ec0782778452a8a70e38b25e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 18 May 2016 21:43:59 +0200 Subject: Introduce a configuration manager (not plugged yet) --- tests/config/ConfigManagerTest.php | 48 +++++++++++++++ tests/config/ConfigPhpTest.php | 82 +++++++++++++++++++++++++ tests/config/ConfigPluginTest.php | 121 +++++++++++++++++++++++++++++++++++++ tests/config/php/configOK.php | 14 +++++ 4 files changed, 265 insertions(+) create mode 100644 tests/config/ConfigManagerTest.php create mode 100644 tests/config/ConfigPhpTest.php create mode 100644 tests/config/ConfigPluginTest.php create mode 100644 tests/config/php/configOK.php (limited to 'tests') diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php new file mode 100644 index 00000000..1b6358f3 --- /dev/null +++ b/tests/config/ConfigManagerTest.php @@ -0,0 +1,48 @@ +conf = ConfigManager::getInstance(); + } + + public function tearDown() + { + @unlink($this->conf->getConfigFile()); + } + + public function testSetWriteGet() + { + // This won't work with ConfigPhp. + $this->markTestIncomplete(); + + $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->write(true); + $this->conf->reload(); + + $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')); + } + +} \ No newline at end of file diff --git a/tests/config/ConfigPhpTest.php b/tests/config/ConfigPhpTest.php new file mode 100644 index 00000000..0f849bd5 --- /dev/null +++ b/tests/config/ConfigPhpTest.php @@ -0,0 +1,82 @@ +configIO = new ConfigPhp(); + } + + /** + * Read a simple existing config file. + */ + public function testRead() + { + $conf = $this->configIO->read('tests/config/php/configOK'); + $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/config/php/configWrite'; + $data = array( + 'login' => 'root', + 'redirector' => 'lala', + 'config' => array( + 'DATASTORE' => 'data/datastore.php', + ), + 'plugins' => array( + 'WALLABAG_VERSION' => '1', + ) + ); + $this->configIO->write($dataFile, $data); + $expected = 'assertEquals($expected, file_get_contents($dataFile .'.php')); + unlink($dataFile .'.php'); + } + + /** + * Overwrite an existing setting. + */ + public function testOverwrite() + { + $source = 'tests/config/php/configOK.php'; + $dest = 'tests/config/php/configOverwrite'; + copy($source, $dest . '.php'); + $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 .'.php'); + } +} diff --git a/tests/config/ConfigPluginTest.php b/tests/config/ConfigPluginTest.php new file mode 100644 index 00000000..716631b0 --- /dev/null +++ b/tests/config/ConfigPluginTest.php @@ -0,0 +1,121 @@ + 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']); + } +} diff --git a/tests/config/php/configOK.php b/tests/config/php/configOK.php new file mode 100644 index 00000000..b91ad293 --- /dev/null +++ b/tests/config/php/configOK.php @@ -0,0 +1,14 @@ + Date: Wed, 18 May 2016 21:48:24 +0200 Subject: Replace $GLOBALS configuration with the configuration manager in the whole code base --- tests/ApplicationUtilsTest.php | 54 ++++---- tests/ConfigTest.php | 244 ----------------------------------- tests/FeedBuilderTest.php | 6 +- tests/LinkDBTest.php | 2 +- tests/Updater/DummyUpdater.php | 5 +- tests/Updater/UpdaterTest.php | 80 +++++++----- tests/config/ConfigPhpTest.php | 16 +-- tests/config/php/configOK.php | 14 -- tests/utils/config/configPhp.php | 14 ++ tests/utils/config/configUpdater.php | 15 +++ 10 files changed, 118 insertions(+), 332 deletions(-) delete mode 100644 tests/ConfigTest.php delete mode 100644 tests/config/php/configOK.php create mode 100644 tests/utils/config/configPhp.php create mode 100644 tests/utils/config/configUpdater.php (limited to 'tests') diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index 6064357d..cf82b655 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -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 = ConfigManager::getInstance(); + $conf->set('config.CACHEDIR', 'cache'); + $conf->set('config.CONFIG_FILE', 'data/config.php'); + $conf->set('config.DATADIR', 'data'); + $conf->set('config.DATASTORE', 'data/datastore.php'); + $conf->set('config.IPBANS_FILENAME', 'data/ipbans.php'); + $conf->set('config.LOG_FILE', 'data/log.txt'); + $conf->set('config.PAGECACHE', 'pagecache'); + $conf->set('config.RAINTPL_TMP', 'tmp'); + $conf->set('config.RAINTPL_TPL', 'tpl'); + $conf->set('config.UPDATECHECK_FILENAME', 'data/lastupdatecheck.txt'); + $this->assertEquals( array(), - ApplicationUtils::checkResourcePermissions($config) + ApplicationUtils::checkResourcePermissions() ); } @@ -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 = ConfigManager::getInstance(); + $conf->set('config.CACHEDIR', 'null/cache'); + $conf->set('config.CONFIG_FILE', 'null/data/config.php'); + $conf->set('config.DATADIR', 'null/data'); + $conf->set('config.DATASTORE', 'null/data/store.php'); + $conf->set('config.IPBANS_FILENAME', 'null/data/ipbans.php'); + $conf->set('config.LOG_FILE', 'null/data/log.txt'); + $conf->set('config.PAGECACHE', 'null/pagecache'); + $conf->set('config.RAINTPL_TMP', 'null/tmp'); + $conf->set('config.RAINTPL_TPL', 'null/tpl'); + $conf->set('config.UPDATECHECK_FILENAME', '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() ); } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php deleted file mode 100644 index 7200aae6..00000000 --- a/tests/ConfigTest.php +++ /dev/null @@ -1,244 +0,0 @@ - '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']); - } -} diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index 069b1581..a4d6960c 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -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']); } /** diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index b055fe91..956be3b4 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -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() { diff --git a/tests/Updater/DummyUpdater.php b/tests/Updater/DummyUpdater.php index e9ef2aaa..6724b203 100644 --- a/tests/Updater/DummyUpdater.php +++ b/tests/Updater/DummyUpdater.php @@ -12,13 +12,12 @@ 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. */ - public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn) + public function __construct($doneUpdates, $linkDB, $isLoggedIn) { - parent::__construct($doneUpdates, $config, $linkDB, $isLoggedIn); + parent::__construct($doneUpdates, $linkDB, $isLoggedIn); // Retrieve all update methods. // For unit test, only retrieve final methods, diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index a29d9067..8bfb4ba3 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -1,5 +1,6 @@ false, 'privateLinkByDefault' => false, 'config' => array( - 'CONFIG_FILE' => 'tests/Updater/config.php', 'DATADIR' => 'tests/Updater', 'PAGECACHE' => 'sandbox/pagecache', 'config1' => 'config1data', 'config2' => 'config2data', ) ); + + ConfigManager::$CONFIG_FILE = 'tests/Updater/config'; + $this->conf = ConfigManager::getInstance(); + foreach (self::$configFields as $key => $value) { + $this->conf->set($key, $value); + } + $this->conf->write(true); } /** @@ -50,16 +67,16 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function tearDown() { - if (is_file(self::$configFields['config']['CONFIG_FILE'])) { - unlink(self::$configFields['config']['CONFIG_FILE']); + if (is_file(self::$configFile)) { + unlink(self::$configFile); } 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'); + if (is_file(self::$configFields['config']['DATADIR'] . '/updates.txt')) { + unlink(self::$configFields['config']['DATADIR'] . '/updates.txt'); } } @@ -69,7 +86,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase public function testReadEmptyUpdatesFile() { $this->assertEquals(array(), read_updates_file('')); - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; + $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; touch($updatesFile); $this->assertEquals(array(), read_updates_file($updatesFile)); } @@ -79,7 +96,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testReadWriteUpdatesFile() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; + $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; $updatesMethods = array('m1', 'm2', 'm3'); write_updates_file($updatesFile, $updatesMethods); @@ -112,7 +129,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testWriteUpdatesFileNotWritable() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; + $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; touch($updatesFile); chmod($updatesFile, 0444); @write_updates_file($updatesFile, array('test')); @@ -131,10 +148,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy3', 'updateMethodException', ); - $updater = new DummyUpdater($updates, array(), array(), true); + $updater = new DummyUpdater($updates, array(), true); $this->assertEquals(array(), $updater->update()); - $updater = new DummyUpdater(array(), array(), array(), false); + $updater = new DummyUpdater(array(), array(), false); $this->assertEquals(array(), $updater->update()); } @@ -149,7 +166,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy2', 'updateMethodDummy3', ); - $updater = new DummyUpdater($updates, array(), array(), true); + $updater = new DummyUpdater($updates, array(), true); $this->assertEquals($expectedUpdates, $updater->update()); } @@ -165,7 +182,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase ); $expectedUpdate = array('updateMethodDummy2'); - $updater = new DummyUpdater($updates, array(), array(), true); + $updater = new DummyUpdater($updates, array(), true); $this->assertEquals($expectedUpdate, $updater->update()); } @@ -182,7 +199,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy3', ); - $updater = new DummyUpdater($updates, array(), array(), true); + $updater = new DummyUpdater($updates, array(), true); $updater->update(); } @@ -195,26 +212,25 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testUpdateMergeDeprecatedConfig() { - // init - writeConfig(self::$configFields, true); - $configCopy = self::$configFields; - $invert = !$configCopy['privateLinkByDefault']; - $configCopy['privateLinkByDefault'] = $invert; - // Use writeConfig to create a options.php - $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php'; - writeConfig($configCopy, true); + ConfigManager::$CONFIG_FILE = 'tests/Updater/options'; + $invert = !$this->conf->get('privateLinkByDefault'); + $this->conf->set('privateLinkByDefault', $invert); + $this->conf->write(true); + + $optionsFile = 'tests/Updater/options.php'; + $this->assertTrue(is_file($optionsFile)); - $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); + ConfigManager::$CONFIG_FILE = 'tests/Updater/config'; // merge configs - $updater = new Updater(array(), self::$configFields, array(), true); + $updater = new Updater(array(), array(), true); $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->assertEquals($invert, $this->conf->get('privateLinkByDefault')); + $this->assertFalse(is_file($optionsFile)); } /** @@ -222,22 +238,22 @@ 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(), true); $updater->updateMethodMergeDeprecatedConfigFile(); - include self::$configFields['config']['CONFIG_FILE']; - $this->assertEquals(self::$configFields['login'], $GLOBALS['login']); + $this->assertEquals(self::$configFields['login'], $this->conf->get('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, true); $updater->updateMethodRenameDashTags(); $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); } diff --git a/tests/config/ConfigPhpTest.php b/tests/config/ConfigPhpTest.php index 0f849bd5..58cd8d2a 100644 --- a/tests/config/ConfigPhpTest.php +++ b/tests/config/ConfigPhpTest.php @@ -22,7 +22,7 @@ class ConfigPhpTest extends PHPUnit_Framework_TestCase */ public function testRead() { - $conf = $this->configIO->read('tests/config/php/configOK'); + $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']); @@ -42,7 +42,7 @@ class ConfigPhpTest extends PHPUnit_Framework_TestCase */ public function testWriteNew() { - $dataFile = 'tests/config/php/configWrite'; + $dataFile = 'tests/utils/config/configWrite.php'; $data = array( 'login' => 'root', 'redirector' => 'lala', @@ -60,8 +60,8 @@ $GLOBALS[\'redirector\'] = \'lala\'; $GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\'; $GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\'; '; - $this->assertEquals($expected, file_get_contents($dataFile .'.php')); - unlink($dataFile .'.php'); + $this->assertEquals($expected, file_get_contents($dataFile)); + unlink($dataFile); } /** @@ -69,14 +69,14 @@ $GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\'; */ public function testOverwrite() { - $source = 'tests/config/php/configOK.php'; - $dest = 'tests/config/php/configOverwrite'; - copy($source, $dest . '.php'); + $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 .'.php'); + unlink($dest); } } diff --git a/tests/config/php/configOK.php b/tests/config/php/configOK.php deleted file mode 100644 index b91ad293..00000000 --- a/tests/config/php/configOK.php +++ /dev/null @@ -1,14 +0,0 @@ - Date: Sun, 29 May 2016 12:32:14 +0200 Subject: Adds ConfigJson which handle the configuration in JSON format. Also use the Updater to make the transition --- tests/Updater/UpdaterTest.php | 66 ++++++++++-- tests/config/ConfigJsonTest.php | 125 +++++++++++++++++++++++ tests/config/ConfigManagerTest.php | 146 +++++++++++++++++++++++++-- tests/utils/config/configInvalid.json.php | 4 + tests/utils/config/configJson.json.php | 19 ++++ tests/utils/config/configUpdateDone.json.php | 4 + 6 files changed, 348 insertions(+), 16 deletions(-) create mode 100644 tests/config/ConfigJsonTest.php create mode 100644 tests/utils/config/configInvalid.json.php create mode 100644 tests/utils/config/configJson.json.php create mode 100644 tests/utils/config/configUpdateDone.json.php (limited to 'tests') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 8bfb4ba3..f8de2f70 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -20,9 +20,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase protected static $testDatastore = 'sandbox/datastore.php'; /** - * @var string Config file path. + * @var string Config file path (without extension). */ - protected static $configFile = 'tests/Updater/config.php'; + protected static $configFile = 'tests/utils/config/configUpdater'; /** * @var ConfigManager @@ -52,8 +52,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase ) ); - ConfigManager::$CONFIG_FILE = 'tests/Updater/config'; - $this->conf = ConfigManager::getInstance(); + ConfigManager::$CONFIG_FILE = self::$configFile; + $this->conf = ConfigManager::reset(); + $this->conf->reload(); foreach (self::$configFields as $key => $value) { $this->conf->set($key, $value); } @@ -67,8 +68,8 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function tearDown() { - if (is_file(self::$configFile)) { - unlink(self::$configFile); + if (is_file('tests/Updater/config.json')) { + unlink('tests/Updater/config.json'); } if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) { @@ -214,6 +215,8 @@ class UpdaterTest extends PHPUnit_Framework_TestCase { // Use writeConfig to create a options.php ConfigManager::$CONFIG_FILE = 'tests/Updater/options'; + $this->conf->setConfigIO(new ConfigPhp()); + $invert = !$this->conf->get('privateLinkByDefault'); $this->conf->set('privateLinkByDefault', $invert); $this->conf->write(true); @@ -225,12 +228,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase // merge configs $updater = new Updater(array(), array(), true); + // This writes a new config file in tests/Updater/config.php $updater->updateMethodMergeDeprecatedConfigFile(); // make sure updated field is changed $this->conf->reload(); $this->assertEquals($invert, $this->conf->get('privateLinkByDefault')); $this->assertFalse(is_file($optionsFile)); + // Delete the generated file. + unlink($this->conf->getConfigFile()); } /** @@ -257,4 +263,52 @@ class UpdaterTest extends PHPUnit_Framework_TestCase $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'; + ConfigManager::$CONFIG_FILE = $configFile; + $conf = ConfigManager::reset(); + + // The ConfigIO is initialized with ConfigPhp. + $this->assertTrue($conf->getConfigIO() instanceof ConfigPhp); + + $updater = new Updater(array(), array(), false); + $done = $updater->updateMethodConfigToJson(); + $this->assertTrue($done); + + // The ConfigIO has been updated to ConfigJson. + $this->assertTrue($conf->getConfigIO() instanceof ConfigJson); + $this->assertTrue(file_exists($conf->getConfigFile())); + + // Check JSON config data. + $conf->reload(); + $this->assertEquals('root', $conf->get('login')); + $this->assertEquals('lala', $conf->get('redirector')); + $this->assertEquals('data/datastore.php', $conf->get('config.DATASTORE')); + $this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION')); + + rename($configFile . '.save.php', $configFile . '.php'); + unlink($conf->getConfigFile()); + } + + /** + * Launch config conversion update with an existing JSON file => nothing to do. + */ + public function testConfigToJsonNothingToDo() + { + $configFile = 'tests/utils/config/configUpdateDone'; + ConfigManager::$CONFIG_FILE = $configFile; + $conf = ConfigManager::reset(); + $conf->reload(); + $filetime = filemtime($conf->getConfigFile()); + $updater = new Updater(array(), array(), false); + $done = $updater->updateMethodConfigToJson(); + $this->assertTrue($done); + $expected = filemtime($conf->getConfigFile()); + $this->assertEquals($expected, $filetime); + } } diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php new file mode 100644 index 00000000..5b3bce46 --- /dev/null +++ b/tests/config/ConfigJsonTest.php @@ -0,0 +1,125 @@ +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['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')); + } + + /** + * 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( + 'login' => 'root', + 'redirector' => 'lala', + 'config' => array( + 'DATASTORE' => 'data/datastore.php', + ), + '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 = '{ + "login": "root", + "redirector": "lala", + "config": { + "DATASTORE": "data\/datastore.php" + }, + "plugins": { + "WALLABAG_VERSION": "1" + } +}'; + } else { + $expected = '{"login":"root","redirector":"lala","config":{"DATASTORE":"data\/datastore.php"},"plugins":{"WALLABAG_VERSION":"1"}}'; + } + $expected = ConfigJson::$PHP_HEADER . $expected; + $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'] = 'blabla'; + $this->configIO->write($dest, $conf); + $conf = $this->configIO->read($dest); + $this->assertEquals('blabla', $conf['redirector']); + 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); + } +} diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 1b6358f3..7390699c 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -6,7 +6,7 @@ * 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 +class ConfigManagerTest extends PHPUnit_Framework_TestCase { /** * @var ConfigManager @@ -15,28 +15,49 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase public function setUp() { - ConfigManager::$CONFIG_FILE = 'tests/config/config'; - $this->conf = ConfigManager::getInstance(); + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson'; + $this->conf = ConfigManager::reset(); } - public function tearDown() + /** + * Simple config test: + * 1. Set settings. + * 2. Check settings value. + */ + public function testSetGet() { - @unlink($this->conf->getConfigFile()); + $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 won't work with ConfigPhp. - $this->markTestIncomplete(); - $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); + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; $this->conf->write(true); $this->conf->reload(); + unlink($this->conf->getConfigFile()); $this->assertEquals(42, $this->conf->get('paramInt')); $this->assertEquals('value1', $this->conf->get('paramString')); @@ -44,5 +65,110 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray')); $this->assertEquals(null, $this->conf->get('paramNull')); } - -} \ No newline at end of file + + /** + * Test set/write/get with nested keys. + */ + public function testSetWriteGetNested() + { + $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested'); + + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->write(true); + $this->conf->reload(); + unlink($this->conf->getConfigFile()); + + $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() + { + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->assertFalse(file_exists($this->conf->getConfigFile())); + $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('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() + { + $conf = $this->conf; + $this->assertTrue($conf === ConfigManager::getInstance()); + $this->assertFalse($conf === $this->conf->reset()); + $this->assertFalse($conf === ConfigManager::getInstance()); + } + + /** + * Reload the config from file. + */ + public function testReload() + { + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $newConf = ConfigJson::$PHP_HEADER . '{ "key": "value" }'; + file_put_contents($this->conf->getConfigFile(), $newConf); + $this->conf->reload(); + unlink($this->conf->getConfigFile()); + // Previous conf no longer exists, and new values have been loaded. + $this->assertFalse($this->conf->exists('login')); + $this->assertEquals('value', $this->conf->get('key')); + } +} diff --git a/tests/utils/config/configInvalid.json.php b/tests/utils/config/configInvalid.json.php new file mode 100644 index 00000000..c53e471d --- /dev/null +++ b/tests/utils/config/configInvalid.json.php @@ -0,0 +1,4 @@ + Date: Sun, 29 May 2016 14:41:30 +0200 Subject: Use the configuration manager for wallabag and readityourself plugin --- tests/plugins/PluginReadityourselfTest.php | 6 ++++-- tests/plugins/PluginWallabagTest.php | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php index 8bf17bf1..bc5da042 100644 --- a/tests/plugins/PluginReadityourselfTest.php +++ b/tests/plugins/PluginReadityourselfTest.php @@ -25,7 +25,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase */ function testReadityourselfLinklist() { - $GLOBALS['plugins']['READITYOUSELF_URL'] = 'value'; + $conf = ConfigManager::getInstance(); + $conf->set('plugins.READITYOUSELF_URL', 'value'); $str = 'http://randomstr.com/test'; $data = array( 'title' => $str, @@ -52,7 +53,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase */ function testReadityourselfLinklistWithoutConfig() { - unset($GLOBALS['plugins']['READITYOUSELF_URL']); + $conf = ConfigManager::getInstance(); + $conf->set('plugins.READITYOUSELF_URL', null); $str = 'http://randomstr.com/test'; $data = array( 'title' => $str, diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index 5d3a60e0..e6f8a8b6 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php @@ -25,7 +25,8 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase */ function testWallabagLinklist() { - $GLOBALS['plugins']['WALLABAG_URL'] = 'value'; + $conf = ConfigManager::getInstance(); + $conf->set('plugins.WALLABAG_URL', 'value'); $str = 'http://randomstr.com/test'; $data = array( 'title' => $str, @@ -45,7 +46,7 @@ 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'))); } } -- cgit v1.2.3 From da10377b3c263d96a46cf9101c202554343d2cd0 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 29 May 2016 16:10:32 +0200 Subject: Rename configuration keys and fix GLOBALS in templates --- tests/ApplicationUtilsTest.php | 40 ++++++------ tests/Updater/UpdaterTest.php | 97 +++++++--------------------- tests/config/ConfigJsonTest.php | 38 ++++++----- tests/config/ConfigManagerTest.php | 6 +- tests/utils/config/configJson.json.php | 33 ++++++---- tests/utils/config/configUpdateDone.json.php | 4 -- tests/utils/config/configUpdater.php | 15 ----- 7 files changed, 93 insertions(+), 140 deletions(-) delete mode 100644 tests/utils/config/configUpdateDone.json.php delete mode 100644 tests/utils/config/configUpdater.php (limited to 'tests') diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index cf82b655..f92412ba 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -277,16 +277,16 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase public function testCheckCurrentResourcePermissions() { $conf = ConfigManager::getInstance(); - $conf->set('config.CACHEDIR', 'cache'); - $conf->set('config.CONFIG_FILE', 'data/config.php'); - $conf->set('config.DATADIR', 'data'); - $conf->set('config.DATASTORE', 'data/datastore.php'); - $conf->set('config.IPBANS_FILENAME', 'data/ipbans.php'); - $conf->set('config.LOG_FILE', 'data/log.txt'); - $conf->set('config.PAGECACHE', 'pagecache'); - $conf->set('config.RAINTPL_TMP', 'tmp'); - $conf->set('config.RAINTPL_TPL', 'tpl'); - $conf->set('config.UPDATECHECK_FILENAME', 'data/lastupdatecheck.txt'); + $conf->set('path.thumbnails_cache', 'cache'); + $conf->set('path.config', 'data/config.php'); + $conf->set('path.data_dir', 'data'); + $conf->set('path.datastore', 'data/datastore.php'); + $conf->set('path.ban_file', 'data/ipbans.php'); + $conf->set('path.log', 'data/log.txt'); + $conf->set('path.page_cache', 'pagecache'); + $conf->set('path.raintpl_tmp', 'tmp'); + $conf->set('path.raintpl_tpl', 'tpl'); + $conf->set('path.update_check', 'data/lastupdatecheck.txt'); $this->assertEquals( array(), @@ -300,16 +300,16 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase public function testCheckCurrentResourcePermissionsErrors() { $conf = ConfigManager::getInstance(); - $conf->set('config.CACHEDIR', 'null/cache'); - $conf->set('config.CONFIG_FILE', 'null/data/config.php'); - $conf->set('config.DATADIR', 'null/data'); - $conf->set('config.DATASTORE', 'null/data/store.php'); - $conf->set('config.IPBANS_FILENAME', 'null/data/ipbans.php'); - $conf->set('config.LOG_FILE', 'null/data/log.txt'); - $conf->set('config.PAGECACHE', 'null/pagecache'); - $conf->set('config.RAINTPL_TMP', 'null/tmp'); - $conf->set('config.RAINTPL_TPL', 'null/tpl'); - $conf->set('config.UPDATECHECK_FILENAME', 'null/data/lastupdatecheck.txt'); + $conf->set('path.thumbnails_cache', 'null/cache'); + $conf->set('path.config', 'null/data/config.php'); + $conf->set('path.data_dir', 'null/data'); + $conf->set('path.datastore', 'null/data/store.php'); + $conf->set('path.ban_file', 'null/data/ipbans.php'); + $conf->set('path.log', 'null/data/log.txt'); + $conf->set('path.page_cache', 'null/pagecache'); + $conf->set('path.raintpl_tmp', 'null/tmp'); + $conf->set('path.raintpl_tpl', 'null/tpl'); + $conf->set('path.update_check', 'null/data/lastupdatecheck.txt'); $this->assertEquals( array( '"null/tpl" directory is not readable', diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index f8de2f70..04883a46 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -9,11 +9,6 @@ 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. */ @@ -22,7 +17,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase /** * @var string Config file path (without extension). */ - protected static $configFile = 'tests/utils/config/configUpdater'; + protected static $configFile = 'tests/utils/config/configJson'; /** * @var ConfigManager @@ -34,51 +29,8 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ 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( - 'DATADIR' => 'tests/Updater', - 'PAGECACHE' => 'sandbox/pagecache', - 'config1' => 'config1data', - 'config2' => 'config2data', - ) - ); - ConfigManager::$CONFIG_FILE = self::$configFile; $this->conf = ConfigManager::reset(); - $this->conf->reload(); - foreach (self::$configFields as $key => $value) { - $this->conf->set($key, $value); - } - $this->conf->write(true); - } - - /** - * Executed after each test. - * - * @return void - */ - public function tearDown() - { - if (is_file('tests/Updater/config.json')) { - unlink('tests/Updater/config.json'); - } - - if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) { - unlink(self::$configFields['config']['DATADIR'] . '/options.php'); - } - - if (is_file(self::$configFields['config']['DATADIR'] . '/updates.txt')) { - unlink(self::$configFields['config']['DATADIR'] . '/updates.txt'); - } } /** @@ -87,9 +39,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase public function testReadEmptyUpdatesFile() { $this->assertEquals(array(), read_updates_file('')); - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; + $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt'; touch($updatesFile); $this->assertEquals(array(), read_updates_file($updatesFile)); + unlink($updatesFile); } /** @@ -97,7 +50,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testReadWriteUpdatesFile() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; + $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt'; $updatesMethods = array('m1', 'm2', 'm3'); write_updates_file($updatesFile, $updatesMethods); @@ -109,6 +62,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase write_updates_file($updatesFile, $updatesMethods); $readMethods = read_updates_file($updatesFile); $this->assertEquals($readMethods, $updatesMethods); + unlink($updatesFile); } /** @@ -130,10 +84,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testWriteUpdatesFileNotWritable() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; + $updatesFile = $this->conf->get('path.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; + } } /** @@ -213,17 +172,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testUpdateMergeDeprecatedConfig() { - // Use writeConfig to create a options.php - ConfigManager::$CONFIG_FILE = 'tests/Updater/options'; - $this->conf->setConfigIO(new ConfigPhp()); - - $invert = !$this->conf->get('privateLinkByDefault'); - $this->conf->set('privateLinkByDefault', $invert); - $this->conf->write(true); + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configPhp'; + $this->conf = $this->conf->reset(); $optionsFile = 'tests/Updater/options.php'; - $this->assertTrue(is_file($optionsFile)); + $options = 'conf->reload(); - $this->assertEquals($invert, $this->conf->get('privateLinkByDefault')); + $this->assertTrue($this->conf->get('general.default_private_links')); $this->assertFalse(is_file($optionsFile)); // Delete the generated file. unlink($this->conf->getConfigFile()); @@ -247,7 +204,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase $updater = new Updater(array(), array(), true); $updater->updateMethodMergeDeprecatedConfigFile(); - $this->assertEquals(self::$configFields['login'], $this->conf->get('login')); + $this->assertEquals('root', $this->conf->get('credentials.login')); } /** @@ -286,9 +243,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase // Check JSON config data. $conf->reload(); - $this->assertEquals('root', $conf->get('login')); - $this->assertEquals('lala', $conf->get('redirector')); - $this->assertEquals('data/datastore.php', $conf->get('config.DATASTORE')); + $this->assertEquals('root', $conf->get('credentials.login')); + $this->assertEquals('lala', $conf->get('extras.redirector')); + $this->assertEquals('data/datastore.php', $conf->get('path.datastore')); $this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION')); rename($configFile . '.save.php', $configFile . '.php'); @@ -300,15 +257,11 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testConfigToJsonNothingToDo() { - $configFile = 'tests/utils/config/configUpdateDone'; - ConfigManager::$CONFIG_FILE = $configFile; - $conf = ConfigManager::reset(); - $conf->reload(); - $filetime = filemtime($conf->getConfigFile()); + $filetime = filemtime($this->conf->getConfigFile()); $updater = new Updater(array(), array(), false); $done = $updater->updateMethodConfigToJson(); $this->assertTrue($done); - $expected = filemtime($conf->getConfigFile()); + $expected = filemtime($this->conf->getConfigFile()); $this->assertEquals($expected, $filetime); } } diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 5b3bce46..0960c729 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php @@ -23,9 +23,9 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase public function testRead() { $conf = $this->configIO->read('tests/utils/config/configJson.json.php'); - $this->assertEquals('root', $conf['login']); - $this->assertEquals('lala', $conf['redirector']); - $this->assertEquals('data/datastore.php', $conf['config']['DATASTORE']); + $this->assertEquals('root', $conf['credentials']['login']); + $this->assertEquals('lala', $conf['extras']['redirector']); + $this->assertEquals('tests/utils/config/datastore.php', $conf['path']['datastore']); $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']); } @@ -55,10 +55,14 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase { $dataFile = 'tests/utils/config/configWrite.json.php'; $data = array( - 'login' => 'root', - 'redirector' => 'lala', - 'config' => array( - 'DATASTORE' => 'data/datastore.php', + 'credentials' => array( + 'login' => 'root', + ), + 'path' => array( + 'datastore' => 'data/datastore.php', + ), + 'extras' => array( + 'redirector' => 'lala', ), 'plugins' => array( 'WALLABAG_VERSION' => '1', @@ -68,19 +72,23 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase // PHP 5.3 doesn't support json pretty print. if (defined('JSON_PRETTY_PRINT')) { $expected = '{ - "login": "root", - "redirector": "lala", - "config": { - "DATASTORE": "data\/datastore.php" + "credentials": { + "login": "root" + }, + "path": { + "datastore": "data\/datastore.php" + }, + "extras": { + "redirector": "lala" }, "plugins": { "WALLABAG_VERSION": "1" } }'; } else { - $expected = '{"login":"root","redirector":"lala","config":{"DATASTORE":"data\/datastore.php"},"plugins":{"WALLABAG_VERSION":"1"}}'; + $expected = '{"credentials":{"login":"root"},"path":{"datastore":"data\/datastore.php"},"extras":{"redirector":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; } - $expected = ConfigJson::$PHP_HEADER . $expected; + $expected = ConfigJson::getPhpHeaders() . $expected; $this->assertEquals($expected, file_get_contents($dataFile)); unlink($dataFile); } @@ -94,10 +102,10 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase $dest = 'tests/utils/config/configOverwrite.json.php'; copy($source, $dest); $conf = $this->configIO->read($dest); - $conf['redirector'] = 'blabla'; + $conf['extras']['redirector'] = 'blabla'; $this->configIO->write($dest, $conf); $conf = $this->configIO->read($dest); - $this->assertEquals('blabla', $conf['redirector']); + $this->assertEquals('blabla', $conf['extras']['redirector']); unlink($dest); } diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 7390699c..9ff0f473 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -131,7 +131,7 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testExistsOk() { - $this->assertTrue($this->conf->exists('login')); + $this->assertTrue($this->conf->exists('credentials.login')); $this->assertTrue($this->conf->exists('config.foo')); } @@ -163,12 +163,12 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase public function testReload() { ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; - $newConf = ConfigJson::$PHP_HEADER . '{ "key": "value" }'; + $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }'; file_put_contents($this->conf->getConfigFile(), $newConf); $this->conf->reload(); unlink($this->conf->getConfigFile()); // Previous conf no longer exists, and new values have been loaded. - $this->assertFalse($this->conf->exists('login')); + $this->assertFalse($this->conf->exists('credentials.login')); $this->assertEquals('value', $this->conf->get('key')); } } diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php index 71b59edd..6a841f8a 100644 --- a/tests/utils/config/configJson.json.php +++ b/tests/utils/config/configJson.json.php @@ -1,17 +1,28 @@ Date: Thu, 9 Jun 2016 20:04:02 +0200 Subject: ConfigManager no longer uses singleton pattern --- tests/ApplicationUtilsTest.php | 8 +++--- tests/Updater/DummyUpdater.php | 11 ++++---- tests/Updater/UpdaterTest.php | 57 +++++++++++++++++++------------------- tests/config/ConfigManagerTest.php | 28 +++++++++---------- 4 files changed, 51 insertions(+), 53 deletions(-) (limited to 'tests') diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index f92412ba..3da72639 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -276,7 +276,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase */ public function testCheckCurrentResourcePermissions() { - $conf = ConfigManager::getInstance(); + $conf = new ConfigManager(''); $conf->set('path.thumbnails_cache', 'cache'); $conf->set('path.config', 'data/config.php'); $conf->set('path.data_dir', 'data'); @@ -290,7 +290,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase $this->assertEquals( array(), - ApplicationUtils::checkResourcePermissions() + ApplicationUtils::checkResourcePermissions($conf) ); } @@ -299,7 +299,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase */ public function testCheckCurrentResourcePermissionsErrors() { - $conf = ConfigManager::getInstance(); + $conf = new ConfigManager(''); $conf->set('path.thumbnails_cache', 'null/cache'); $conf->set('path.config', 'null/data/config.php'); $conf->set('path.data_dir', 'null/data'); @@ -322,7 +322,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase '"null/tmp" directory is not readable', '"null/tmp" directory is not writable' ), - ApplicationUtils::checkResourcePermissions() + ApplicationUtils::checkResourcePermissions($conf) ); } } diff --git a/tests/Updater/DummyUpdater.php b/tests/Updater/DummyUpdater.php index 6724b203..a0be4413 100644 --- a/tests/Updater/DummyUpdater.php +++ b/tests/Updater/DummyUpdater.php @@ -11,13 +11,14 @@ class DummyUpdater extends Updater /** * Object constructor. * - * @param array $doneUpdates Updates which are already done. - * @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, $linkDB, $isLoggedIn) + public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) { - parent::__construct($doneUpdates, $linkDB, $isLoggedIn); + parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn); // Retrieve all update methods. // For unit test, only retrieve final methods, diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 04883a46..5ed2df6c 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -29,8 +29,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function setUp() { - ConfigManager::$CONFIG_FILE = self::$configFile; - $this->conf = ConfigManager::reset(); + $this->conf = new ConfigManager(self::$configFile); } /** @@ -108,10 +107,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy3', 'updateMethodException', ); - $updater = new DummyUpdater($updates, array(), true); + $updater = new DummyUpdater($updates, array(), $this->conf, true); $this->assertEquals(array(), $updater->update()); - $updater = new DummyUpdater(array(), array(), false); + $updater = new DummyUpdater(array(), array(), $this->conf, false); $this->assertEquals(array(), $updater->update()); } @@ -126,7 +125,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy2', 'updateMethodDummy3', ); - $updater = new DummyUpdater($updates, array(), true); + $updater = new DummyUpdater($updates, array(), $this->conf, true); $this->assertEquals($expectedUpdates, $updater->update()); } @@ -142,7 +141,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase ); $expectedUpdate = array('updateMethodDummy2'); - $updater = new DummyUpdater($updates, array(), true); + $updater = new DummyUpdater($updates, array(), $this->conf, true); $this->assertEquals($expectedUpdate, $updater->update()); } @@ -159,7 +158,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 'updateMethodDummy3', ); - $updater = new DummyUpdater($updates, array(), true); + $updater = new DummyUpdater($updates, array(), $this->conf, true); $updater->update(); } @@ -172,8 +171,8 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testUpdateMergeDeprecatedConfig() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configPhp'; - $this->conf = $this->conf->reset(); + $this->conf->setConfigFile('tests/utils/config/configPhp'); + $this->conf->reset(); $optionsFile = 'tests/Updater/options.php'; $options = 'conf->setConfigFile('tests/Updater/config'); // merge configs - $updater = new Updater(array(), array(), true); + $updater = new Updater(array(), array(), $this->conf, true); // This writes a new config file in tests/Updater/config.php $updater->updateMethodMergeDeprecatedConfigFile(); @@ -193,7 +192,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertTrue($this->conf->get('general.default_private_links')); $this->assertFalse(is_file($optionsFile)); // Delete the generated file. - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); } /** @@ -201,7 +200,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; */ public function testMergeDeprecatedConfigNoFile() { - $updater = new Updater(array(), array(), true); + $updater = new Updater(array(), array(), $this->conf, true); $updater->updateMethodMergeDeprecatedConfigFile(); $this->assertEquals('root', $this->conf->get('credentials.login')); @@ -216,7 +215,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); - $updater = new Updater(array(), $linkDB, true); + $updater = new Updater(array(), $linkDB, $this->conf, true); $updater->updateMethodRenameDashTags(); $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); } @@ -227,29 +226,29 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; public function testConfigToJson() { $configFile = 'tests/utils/config/configPhp'; - ConfigManager::$CONFIG_FILE = $configFile; - $conf = ConfigManager::reset(); + $this->conf->setConfigFile($configFile); + $this->conf->reset(); // The ConfigIO is initialized with ConfigPhp. - $this->assertTrue($conf->getConfigIO() instanceof ConfigPhp); + $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); - $updater = new Updater(array(), array(), false); + $updater = new Updater(array(), array(), $this->conf, false); $done = $updater->updateMethodConfigToJson(); $this->assertTrue($done); // The ConfigIO has been updated to ConfigJson. - $this->assertTrue($conf->getConfigIO() instanceof ConfigJson); - $this->assertTrue(file_exists($conf->getConfigFile())); + $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); + $this->assertTrue(file_exists($this->conf->getConfigFileExt())); // Check JSON config data. - $conf->reload(); - $this->assertEquals('root', $conf->get('credentials.login')); - $this->assertEquals('lala', $conf->get('extras.redirector')); - $this->assertEquals('data/datastore.php', $conf->get('path.datastore')); - $this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION')); + $this->conf->reload(); + $this->assertEquals('root', $this->conf->get('credentials.login')); + $this->assertEquals('lala', $this->conf->get('extras.redirector')); + $this->assertEquals('data/datastore.php', $this->conf->get('path.datastore')); + $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); rename($configFile . '.save.php', $configFile . '.php'); - unlink($conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); } /** @@ -257,11 +256,11 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; */ public function testConfigToJsonNothingToDo() { - $filetime = filemtime($this->conf->getConfigFile()); - $updater = new Updater(array(), array(), false); + $filetime = filemtime($this->conf->getConfigFileExt()); + $updater = new Updater(array(), array(), $this->conf, false); $done = $updater->updateMethodConfigToJson(); $this->assertTrue($done); - $expected = filemtime($this->conf->getConfigFile()); + $expected = filemtime($this->conf->getConfigFileExt()); $this->assertEquals($expected, $filetime); } } diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 9ff0f473..436e3d67 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -15,8 +15,7 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase public function setUp() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson'; - $this->conf = ConfigManager::reset(); + $this->conf = new ConfigManager('tests/utils/config/configJson'); } /** @@ -54,10 +53,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase $this->conf->set('paramArray', array('foo' => 'bar')); $this->conf->set('paramNull', null); - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $this->conf->write(true); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); $this->assertEquals(42, $this->conf->get('paramInt')); $this->assertEquals('value1', $this->conf->get('paramString')); @@ -73,10 +72,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase { $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested'); - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $this->conf->write(true); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff')); } @@ -110,8 +109,8 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testWriteMissingParameter() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; - $this->assertFalse(file_exists($this->conf->getConfigFile())); + $this->conf->setConfigFile('tests/utils/config/configTmp'); + $this->assertFalse(file_exists($this->conf->getConfigFileExt())); $this->conf->reload(); $this->conf->write(true); @@ -151,10 +150,9 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testReset() { - $conf = $this->conf; - $this->assertTrue($conf === ConfigManager::getInstance()); - $this->assertFalse($conf === $this->conf->reset()); - $this->assertFalse($conf === ConfigManager::getInstance()); + $confIO = $this->conf->getConfigIO(); + $this->conf->reset(); + $this->assertFalse($confIO === $this->conf->getConfigIO()); } /** @@ -162,11 +160,11 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testReload() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }'; - file_put_contents($this->conf->getConfigFile(), $newConf); + file_put_contents($this->conf->getConfigFileExt(), $newConf); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + 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')); -- cgit v1.2.3 From 51def0d84955c7a951bd091eb5eeb3fce9deabd4 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 9 Jun 2016 20:04:32 +0200 Subject: PluginManager no longer uses singleton pattern --- tests/PluginManagerTest.php | 34 +++++++++++++++++------------- tests/plugins/PluginReadityourselfTest.php | 10 +++++---- tests/plugins/PluginWallabagTest.php | 7 +++--- 3 files changed, 29 insertions(+), 22 deletions(-) (limited to 'tests') diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 348082c7..61efce68 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php @@ -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']); } diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php index bc5da042..d73e666a 100644 --- a/tests/plugins/PluginReadityourselfTest.php +++ b/tests/plugins/PluginReadityourselfTest.php @@ -4,6 +4,8 @@ * PluginReadityourselfTest.php.php */ +// FIXME! add an init method. +$conf = new ConfigManager(''); require_once 'plugins/readityourself/readityourself.php'; /** @@ -25,7 +27,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase */ function testReadityourselfLinklist() { - $conf = ConfigManager::getInstance(); + $conf = new ConfigManager(''); $conf->set('plugins.READITYOUSELF_URL', 'value'); $str = 'http://randomstr.com/test'; $data = array( @@ -37,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']); @@ -53,7 +55,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase */ function testReadityourselfLinklistWithoutConfig() { - $conf = ConfigManager::getInstance(); + $conf = new ConfigManager(''); $conf->set('plugins.READITYOUSELF_URL', null); $str = 'http://randomstr.com/test'; $data = array( @@ -65,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']); diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index e6f8a8b6..302ee296 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php @@ -4,6 +4,8 @@ * PluginWallabagTest.php.php */ +// FIXME! add an init method. +$conf = new ConfigManager(''); require_once 'plugins/wallabag/wallabag.php'; /** @@ -25,7 +27,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase */ function testWallabagLinklist() { - $conf = ConfigManager::getInstance(); + $conf = new ConfigManager(''); $conf->set('plugins.WALLABAG_URL', 'value'); $str = 'http://randomstr.com/test'; $data = array( @@ -37,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']); @@ -49,4 +51,3 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase $this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL'))); } } - -- cgit v1.2.3 From 894a3c4bf38d8dcadb6941049b9167e5101805bd Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 11 Jun 2016 09:08:02 +0200 Subject: Rename configuration key for better sections --- tests/ApplicationUtilsTest.php | 40 +++++++++++++++++----------------- tests/Updater/UpdaterTest.php | 12 +++++----- tests/config/ConfigJsonTest.php | 22 +++++++++---------- tests/utils/config/configJson.json.php | 10 +++++---- 4 files changed, 43 insertions(+), 41 deletions(-) (limited to 'tests') diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index 3da72639..c37a94f0 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -277,16 +277,16 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase public function testCheckCurrentResourcePermissions() { $conf = new ConfigManager(''); - $conf->set('path.thumbnails_cache', 'cache'); - $conf->set('path.config', 'data/config.php'); - $conf->set('path.data_dir', 'data'); - $conf->set('path.datastore', 'data/datastore.php'); - $conf->set('path.ban_file', 'data/ipbans.php'); - $conf->set('path.log', 'data/log.txt'); - $conf->set('path.page_cache', 'pagecache'); - $conf->set('path.raintpl_tmp', 'tmp'); - $conf->set('path.raintpl_tpl', 'tpl'); - $conf->set('path.update_check', 'data/lastupdatecheck.txt'); + $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(), @@ -300,16 +300,16 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase public function testCheckCurrentResourcePermissionsErrors() { $conf = new ConfigManager(''); - $conf->set('path.thumbnails_cache', 'null/cache'); - $conf->set('path.config', 'null/data/config.php'); - $conf->set('path.data_dir', 'null/data'); - $conf->set('path.datastore', 'null/data/store.php'); - $conf->set('path.ban_file', 'null/data/ipbans.php'); - $conf->set('path.log', 'null/data/log.txt'); - $conf->set('path.page_cache', 'null/pagecache'); - $conf->set('path.raintpl_tmp', 'null/tmp'); - $conf->set('path.raintpl_tpl', 'null/tpl'); - $conf->set('path.update_check', 'null/data/lastupdatecheck.txt'); + $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', diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 5ed2df6c..6bdce08b 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -38,7 +38,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase public function testReadEmptyUpdatesFile() { $this->assertEquals(array(), read_updates_file('')); - $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt'; + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; touch($updatesFile); $this->assertEquals(array(), read_updates_file($updatesFile)); unlink($updatesFile); @@ -49,7 +49,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testReadWriteUpdatesFile() { - $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt'; + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; $updatesMethods = array('m1', 'm2', 'm3'); write_updates_file($updatesFile, $updatesMethods); @@ -83,7 +83,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testWriteUpdatesFileNotWritable() { - $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt'; + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; touch($updatesFile); chmod($updatesFile, 0444); try { @@ -189,7 +189,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; // make sure updated field is changed $this->conf->reload(); - $this->assertTrue($this->conf->get('general.default_private_links')); + $this->assertTrue($this->conf->get('privacy.default_private_links')); $this->assertFalse(is_file($optionsFile)); // Delete the generated file. unlink($this->conf->getConfigFileExt()); @@ -243,8 +243,8 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; // Check JSON config data. $this->conf->reload(); $this->assertEquals('root', $this->conf->get('credentials.login')); - $this->assertEquals('lala', $this->conf->get('extras.redirector')); - $this->assertEquals('data/datastore.php', $this->conf->get('path.datastore')); + $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'); diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 0960c729..359e9112 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php @@ -24,8 +24,8 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase { $conf = $this->configIO->read('tests/utils/config/configJson.json.php'); $this->assertEquals('root', $conf['credentials']['login']); - $this->assertEquals('lala', $conf['extras']['redirector']); - $this->assertEquals('tests/utils/config/datastore.php', $conf['path']['datastore']); + $this->assertEquals('lala', $conf['redirector']['url']); + $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']); $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']); } @@ -58,11 +58,11 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase 'credentials' => array( 'login' => 'root', ), - 'path' => array( + 'resource' => array( 'datastore' => 'data/datastore.php', ), - 'extras' => array( - 'redirector' => 'lala', + 'redirector' => array( + 'url' => 'lala', ), 'plugins' => array( 'WALLABAG_VERSION' => '1', @@ -75,18 +75,18 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase "credentials": { "login": "root" }, - "path": { + "resource": { "datastore": "data\/datastore.php" }, - "extras": { - "redirector": "lala" + "redirector": { + "url": "lala" }, "plugins": { "WALLABAG_VERSION": "1" } }'; } else { - $expected = '{"credentials":{"login":"root"},"path":{"datastore":"data\/datastore.php"},"extras":{"redirector":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; + $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; } $expected = ConfigJson::getPhpHeaders() . $expected; $this->assertEquals($expected, file_get_contents($dataFile)); @@ -102,10 +102,10 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase $dest = 'tests/utils/config/configOverwrite.json.php'; copy($source, $dest); $conf = $this->configIO->read($dest); - $conf['extras']['redirector'] = 'blabla'; + $conf['redirector']['url'] = 'blabla'; $this->configIO->write($dest, $conf); $conf = $this->configIO->read($dest); - $this->assertEquals('blabla', $conf['extras']['redirector']); + $this->assertEquals('blabla', $conf['redirector']['url']); unlink($dest); } diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php index 6a841f8a..c54882c3 100644 --- a/tests/utils/config/configJson.json.php +++ b/tests/utils/config/configJson.json.php @@ -10,17 +10,19 @@ }, "general": { "timezone":"Europe\/Paris", - "default_private_linksheader_link":true, "title": "Shaarli", "header_link": "?" }, - "extras": { - "redirector":"lala" + "privacy": { + "default_private_links":true + }, + "redirector": { + "url":"lala" }, "config": { "foo": "bar" }, - "path": { + "resource": { "datastore": "tests\/utils\/config\/datastore.php", "data_dir": "tests\/utils\/config" }, -- cgit v1.2.3 From 5ff23f02b80ec6ddee28dee869171ee8e3656b7c Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 20 Jun 2016 18:30:37 +0200 Subject: Add closing PHP tags to JSON config files --- tests/config/ConfigJsonTest.php | 2 +- tests/utils/config/configInvalid.json.php | 1 + tests/utils/config/configJson.json.php | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 359e9112..99c88820 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php @@ -88,7 +88,7 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase } else { $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; } - $expected = ConfigJson::getPhpHeaders() . $expected; + $expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix(); $this->assertEquals($expected, file_get_contents($dataFile)); unlink($dataFile); } diff --git a/tests/utils/config/configInvalid.json.php b/tests/utils/config/configInvalid.json.php index c53e471d..167f2168 100644 --- a/tests/utils/config/configInvalid.json.php +++ b/tests/utils/config/configInvalid.json.php @@ -2,3 +2,4 @@ { bad: bad, } +*/ ?> \ No newline at end of file diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php index c54882c3..06a302e8 100644 --- a/tests/utils/config/configJson.json.php +++ b/tests/utils/config/configJson.json.php @@ -30,3 +30,5 @@ "WALLABAG_VERSION": 1 } } +*/ ?> + -- cgit v1.2.3