X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FUpdater%2FUpdaterTest.php;h=6bdce08b2c77c385f2f5c385c1eb3869c43ae86d;hb=876533e86801246bef893e7124ce044ebf33239f;hp=f8de2f706efb572d8c843c52ed8a8a060f1ec3f7;hpb=b74b96bfbd0b778ac50fd17f5e107c51435b1678;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index f8de2f70..6bdce08b 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,7 @@ 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'); - } + $this->conf = new ConfigManager(self::$configFile); } /** @@ -87,9 +38,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('resource.data_dir') . '/updates.txt'; touch($updatesFile); $this->assertEquals(array(), read_updates_file($updatesFile)); + unlink($updatesFile); } /** @@ -97,7 +49,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testReadWriteUpdatesFile() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; $updatesMethods = array('m1', 'm2', 'm3'); write_updates_file($updatesFile, $updatesMethods); @@ -109,6 +61,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase write_updates_file($updatesFile, $updatesMethods); $readMethods = read_updates_file($updatesFile); $this->assertEquals($readMethods, $updatesMethods); + unlink($updatesFile); } /** @@ -130,10 +83,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testWriteUpdatesFileNotWritable() { - $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; touch($updatesFile); chmod($updatesFile, 0444); - @write_updates_file($updatesFile, array('test')); + try { + @write_updates_file($updatesFile, array('test')); + } catch (Exception $e) { + unlink($updatesFile); + throw $e; + } } /** @@ -149,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()); } @@ -167,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()); } @@ -183,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()); } @@ -200,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(); } @@ -213,30 +171,28 @@ 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); + $this->conf->setConfigFile('tests/utils/config/configPhp'); + $this->conf->reset(); $optionsFile = 'tests/Updater/options.php'; - $this->assertTrue(is_file($optionsFile)); + $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(); // make sure updated field is changed $this->conf->reload(); - $this->assertEquals($invert, $this->conf->get('privateLinkByDefault')); + $this->assertTrue($this->conf->get('privacy.default_private_links')); $this->assertFalse(is_file($optionsFile)); // Delete the generated file. - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); } /** @@ -244,10 +200,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase */ public function testMergeDeprecatedConfigNoFile() { - $updater = new Updater(array(), array(), true); + $updater = new Updater(array(), array(), $this->conf, true); $updater->updateMethodMergeDeprecatedConfigFile(); - $this->assertEquals(self::$configFields['login'], $this->conf->get('login')); + $this->assertEquals('root', $this->conf->get('credentials.login')); } /** @@ -259,7 +215,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase $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'))); } @@ -270,29 +226,29 @@ class UpdaterTest extends PHPUnit_Framework_TestCase 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('login')); - $this->assertEquals('lala', $conf->get('redirector')); - $this->assertEquals('data/datastore.php', $conf->get('config.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('redirector.url')); + $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); + $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); rename($configFile . '.save.php', $configFile . '.php'); - unlink($conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); } /** @@ -300,15 +256,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()); - $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($conf->getConfigFile()); + $expected = filemtime($this->conf->getConfigFileExt()); $this->assertEquals($expected, $filetime); } }