From 684e662a58b02bde225e44d3677987b6fc3adf0b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 18 May 2016 21:48:24 +0200 Subject: Replace $GLOBALS configuration with the configuration manager in the whole code base --- tests/Updater/DummyUpdater.php | 5 ++- tests/Updater/UpdaterTest.php | 80 +++++++++++++++++++++++++----------------- 2 files changed, 50 insertions(+), 35 deletions(-) (limited to 'tests/Updater') 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'))); } -- cgit v1.2.3 From b74b96bfbd0b778ac50fd17f5e107c51435b1678 Mon Sep 17 00:00:00 2001 From: ArthurHoaro 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 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 6 deletions(-) (limited to 'tests/Updater') 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); + } } -- 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/Updater/UpdaterTest.php | 97 +++++++++++-------------------------------- 1 file changed, 25 insertions(+), 72 deletions(-) (limited to 'tests/Updater') 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); } } -- cgit v1.2.3 From 278d9ee2836df7d805845077f26f8cecd16f0f4f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 9 Jun 2016 20:04:02 +0200 Subject: ConfigManager no longer uses singleton pattern --- tests/Updater/DummyUpdater.php | 11 ++++---- tests/Updater/UpdaterTest.php | 57 +++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 34 deletions(-) (limited to 'tests/Updater') 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); } } -- 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/Updater/UpdaterTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests/Updater') 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'); -- cgit v1.2.3 From b9f8b83790a57b55f7d12471460537a268a24642 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 2 Aug 2016 12:54:55 +0200 Subject: Fix update method escapeUnescapedConfig * Actually run it * unit tests Fixes #611 --- tests/Updater/UpdaterTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests/Updater') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 6bdce08b..0d0ad922 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -263,4 +263,28 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $expected = filemtime($this->conf->getConfigFileExt()); $this->assertEquals($expected, $filetime); } + + /** + * Test escapeUnescapedConfig with valid data. + */ + public function testEscapeConfig() + { + $sandbox = 'sandbox/config'; + copy(self::$configFile .'.json.php', $sandbox .'.json.php'); + $this->conf = new ConfigManager($sandbox); + $title = ''; + $headerLink = ''; + $redirectorUrl = ''; + $this->conf->set('general.title', $title); + $this->conf->set('general.header_link', $headerLink); + $this->conf->set('redirector.url', $redirectorUrl); + $updater = new Updater(array(), array(), $this->conf, true); + $done = $updater->updateMethodEscapeUnescapedConfig(); + $this->assertTrue($done); + $this->conf->reload(); + $this->assertEquals(escape($title), $this->conf->get('general.title')); + $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); + $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); + unlink($sandbox .'.json.php'); + } } -- cgit v1.2.3 From c3dfd8995921083ff7250c25d0b6ab1184b91aff Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 16:17:25 +0100 Subject: Unit Test for the new ID system --- tests/Updater/UpdaterTest.php | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) (limited to 'tests/Updater') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 0d0ad922..b8a050b0 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -214,6 +214,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $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(), $linkDB, $this->conf, true); $updater->updateMethodRenameDashTags(); @@ -287,4 +288,101 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); unlink($sandbox .'.json.php'); } + + /** + * Test updateMethodDatastoreIds(). + */ + public function testDatastoreIds() + { + $links = array( + '20121206_182539' => array( + 'linkdate' => '20121206_182539', + 'title' => 'Geek and Poke', + 'url' => 'http://geek-and-poke.com/', + 'description' => 'desc', + 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', + 'updated' => '20121206_190301', + 'private' => false, + ), + '20121206_172539' => array( + 'linkdate' => '20121206_172539', + 'title' => 'UserFriendly - Samba', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', + 'description' => '', + 'tags' => 'samba cartoon web', + 'private' => false, + ), + '20121206_142300' => array( + 'linkdate' => '20121206_142300', + 'title' => 'UserFriendly - Web Designer', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', + 'description' => 'Naming conventions... #private', + 'tags' => 'samba cartoon web', + 'private' => true, + ), + ); + $refDB = new ReferenceLinkDB(); + $refDB->setLinks($links); + $refDB->write(self::$testDatastore); + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $checksum = hash_file('sha1', self::$testDatastore); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $updater = new Updater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); + $backup = $backup[0]; + + $this->assertFileExists($backup); + $this->assertEquals($checksum, hash_file('sha1', $backup)); + unlink($backup); + + $this->assertEquals(3, count($linkDB)); + $this->assertTrue(isset($linkDB[0])); + $this->assertFalse(isset($linkDB[0]['linkdate'])); + $this->assertEquals(0, $linkDB[0]['id']); + $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); + $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); + $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); + $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); + $this->assertTrue($linkDB[0]['private']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); + + $this->assertTrue(isset($linkDB[1])); + $this->assertFalse(isset($linkDB[1]['linkdate'])); + $this->assertEquals(1, $linkDB[1]['id']); + $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); + + $this->assertTrue(isset($linkDB[2])); + $this->assertFalse(isset($linkDB[2]['linkdate'])); + $this->assertEquals(2, $linkDB[2]['id']); + $this->assertEquals('Geek and Poke', $linkDB[2]['title']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); + } + + /** + * Test updateMethodDatastoreIds() with the update already applied: nothing to do. + */ + public function testDatastoreIdsNothingToDo() + { + $refDB = new ReferenceLinkDB(); + $refDB->write(self::$testDatastore); + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $checksum = hash_file('sha1', self::$testDatastore); + $updater = new Updater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); + } } -- cgit v1.2.3 From d592daea8343bb4dfecff5d97e93699581ccc58c Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 18:24:15 +0100 Subject: Add a persistent 'shorturl' key to all links All existing link will keep their permalinks. New links will have smallhash generated with date+id. The purpose of this is to avoid collision between links due to their creation date. --- tests/Updater/UpdaterTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/Updater') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index b8a050b0..4948fe52 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -352,20 +352,20 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); $this->assertTrue($linkDB[0]['private']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); $this->assertTrue(isset($linkDB[1])); $this->assertFalse(isset($linkDB[1]['linkdate'])); $this->assertEquals(1, $linkDB[1]['id']); $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); $this->assertTrue(isset($linkDB[2])); $this->assertFalse(isset($linkDB[2]['linkdate'])); $this->assertEquals(2, $linkDB[2]['id']); $this->assertEquals('Geek and Poke', $linkDB[2]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']); } /** -- cgit v1.2.3 From 9ff17ae20effa5d54fd8481c19518123590e3bd0 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 27 Feb 2017 19:45:55 +0100 Subject: Add markdown_escape setting This setting allows to escape HTML in markdown rendering or not. The goal behind it is to avoid XSS issue in shared instances. More info: * the setting is set to true by default * it is set to false for anyone who already have the plugin enabled (avoid breaking existing entries) * improve the HTML sanitization when the setting is set to false - but don't consider it XSS proof * mention the setting in the plugin README --- tests/Updater/UpdaterTest.php | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'tests/Updater') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 4948fe52..17d1ba81 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -385,4 +385,69 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertTrue($updater->updateMethodDatastoreIds()); $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); } + + /** + * Test updateMethodEscapeMarkdown with markdown plugin enabled + * => setting markdown_escape set to false. + */ + public function testEscapeMarkdownSettingToFalse() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + + $this->conf->set('general.enabled_plugins', ['markdown']); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertFalse($this->conf->get('security.markdown_escape')); + + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertFalse($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodEscapeMarkdown with markdown plugin disabled + * => setting markdown_escape set to true. + */ + public function testEscapeMarkdownSettingToTrue() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + + $this->conf->set('general.enabled_plugins', []); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertTrue($this->conf->get('security.markdown_escape')); + + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertTrue($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled) + */ + public function testEscapeMarkdownSettingNothingToDoEnabled() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('security.markdown_escape', true); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertTrue($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled) + */ + public function testEscapeMarkdownSettingNothingToDoDisabled() + { + $this->conf->set('security.markdown_escape', false); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertFalse($this->conf->get('security.markdown_escape')); + } } -- cgit v1.2.3 From 8868f3ca461011a8fb6dd9f90b60ed697ab52fc5 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 4 Mar 2017 09:52:48 +0100 Subject: UpdaterTest: ensure PHP 5.3 compatibility Signed-off-by: VirtualTam --- tests/Updater/UpdaterTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests/Updater') diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 17d1ba81..a3e8a4d2 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -396,8 +396,8 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); $this->conf = new ConfigManager($sandboxConf); - $this->conf->set('general.enabled_plugins', ['markdown']); - $updater = new Updater([], [], $this->conf, true); + $this->conf->set('general.enabled_plugins', array('markdown')); + $updater = new Updater(array(), array(), $this->conf, true); $this->assertTrue($updater->updateMethodEscapeMarkdown()); $this->assertFalse($this->conf->get('security.markdown_escape')); @@ -416,8 +416,8 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); $this->conf = new ConfigManager($sandboxConf); - $this->conf->set('general.enabled_plugins', []); - $updater = new Updater([], [], $this->conf, true); + $this->conf->set('general.enabled_plugins', array()); + $updater = new Updater(array(), array(), $this->conf, true); $this->assertTrue($updater->updateMethodEscapeMarkdown()); $this->assertTrue($this->conf->get('security.markdown_escape')); @@ -435,7 +435,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); $this->conf = new ConfigManager($sandboxConf); $this->conf->set('security.markdown_escape', true); - $updater = new Updater([], [], $this->conf, true); + $updater = new Updater(array(), array(), $this->conf, true); $this->assertTrue($updater->updateMethodEscapeMarkdown()); $this->assertTrue($this->conf->get('security.markdown_escape')); } @@ -446,7 +446,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; public function testEscapeMarkdownSettingNothingToDoDisabled() { $this->conf->set('security.markdown_escape', false); - $updater = new Updater([], [], $this->conf, true); + $updater = new Updater(array(), array(), $this->conf, true); $this->assertTrue($updater->updateMethodEscapeMarkdown()); $this->assertFalse($this->conf->get('security.markdown_escape')); } -- cgit v1.2.3