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 @@