]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/Updater/UpdaterTest.php
Rename configuration keys and fix GLOBALS in templates
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
index f8de2f706efb572d8c843c52ed8a8a060f1ec3f7..04883a463dad01c28ad376ec853b170b654df868 100644 (file)
@@ -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 = '<?php
+$GLOBALS[\'privateLinkByDefault\'] = true;';
+        file_put_contents($optionsFile, $options);
 
+        // tmp config file.
         ConfigManager::$CONFIG_FILE = 'tests/Updater/config';
 
         // merge configs
@@ -233,7 +190,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
 
         // make sure updated field is changed
         $this->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);
     }
 }