3 require_once 'application/config/ConfigManager.php';
4 require_once 'tests/Updater/DummyUpdater.php';
8 * Runs unit tests against the Updater class.
10 class UpdaterTest
extends PHPUnit_Framework_TestCase
13 * @var string Path to test datastore.
15 protected static $testDatastore = 'sandbox/datastore.php';
18 * @var string Config file path (without extension).
20 protected static $configFile = 'tests/utils/config/configJson';
28 * Executed before each test.
30 public function setUp()
32 ConfigManager
::$CONFIG_FILE = self
::$configFile;
33 $this->conf
= ConfigManager
::reset();
37 * Test read_updates_file with an empty/missing file.
39 public function testReadEmptyUpdatesFile()
41 $this->assertEquals(array(), read_updates_file(''));
42 $updatesFile = $this->conf
->get('path.data_dir') . '/updates.txt';
44 $this->assertEquals(array(), read_updates_file($updatesFile));
49 * Test read/write updates file.
51 public function testReadWriteUpdatesFile()
53 $updatesFile = $this->conf
->get('path.data_dir') . '/updates.txt';
54 $updatesMethods = array('m1', 'm2', 'm3');
56 write_updates_file($updatesFile, $updatesMethods);
57 $readMethods = read_updates_file($updatesFile);
58 $this->assertEquals($readMethods, $updatesMethods);
61 $updatesMethods[] = 'm4';
62 write_updates_file($updatesFile, $updatesMethods);
63 $readMethods = read_updates_file($updatesFile);
64 $this->assertEquals($readMethods, $updatesMethods);
69 * Test errors in write_updates_file(): empty updates file.
71 * @expectedException Exception
72 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
74 public function testWriteEmptyUpdatesFile()
76 write_updates_file('', array('test'));
80 * Test errors in write_updates_file(): not writable updates file.
82 * @expectedException Exception
83 * @expectedExceptionMessageRegExp /Unable to write(.*)/
85 public function testWriteUpdatesFileNotWritable()
87 $updatesFile = $this->conf
->get('path.data_dir') . '/updates.txt';
89 chmod($updatesFile, 0444);
91 @write_updates_file($updatesFile, array('test'));
92 } catch (Exception
$e) {
99 * Test the update() method, with no update to run.
100 * 1. Everything already run.
101 * 2. User is logged out.
103 public function testNoUpdates()
106 'updateMethodDummy1',
107 'updateMethodDummy2',
108 'updateMethodDummy3',
109 'updateMethodException',
111 $updater = new DummyUpdater($updates, array(), true);
112 $this->assertEquals(array(), $updater->update());
114 $updater = new DummyUpdater(array(), array(), false);
115 $this->assertEquals(array(), $updater->update());
119 * Test the update() method, with all updates to run (except the failing one).
121 public function testUpdatesFirstTime()
123 $updates = array('updateMethodException',);
124 $expectedUpdates = array(
125 'updateMethodDummy1',
126 'updateMethodDummy2',
127 'updateMethodDummy3',
129 $updater = new DummyUpdater($updates, array(), true);
130 $this->assertEquals($expectedUpdates, $updater->update());
134 * Test the update() method, only one update to run.
136 public function testOneUpdate()
139 'updateMethodDummy1',
140 'updateMethodDummy3',
141 'updateMethodException',
143 $expectedUpdate = array('updateMethodDummy2');
145 $updater = new DummyUpdater($updates, array(), true);
146 $this->assertEquals($expectedUpdate, $updater->update());
150 * Test Update failed.
152 * @expectedException UpdaterException
154 public function testUpdateFailed()
157 'updateMethodDummy1',
158 'updateMethodDummy2',
159 'updateMethodDummy3',
162 $updater = new DummyUpdater($updates, array(), true);
167 * Test update mergeDeprecatedConfig:
168 * 1. init a config file.
169 * 2. init a options.php file with update value.
171 * 4. check updated value in config file.
173 public function testUpdateMergeDeprecatedConfig()
175 ConfigManager
::$CONFIG_FILE = 'tests/utils/config/configPhp';
176 $this->conf
= $this->conf
->reset();
178 $optionsFile = 'tests/Updater/options.php';
180 $GLOBALS[\'privateLinkByDefault\'] = true;';
181 file_put_contents($optionsFile, $options);
184 ConfigManager
::$CONFIG_FILE = 'tests/Updater/config';
187 $updater = new Updater(array(), array(), true);
188 // This writes a new config file in tests/Updater/config.php
189 $updater->updateMethodMergeDeprecatedConfigFile();
191 // make sure updated field is changed
192 $this->conf
->reload();
193 $this->assertTrue($this->conf
->get('general.default_private_links'));
194 $this->assertFalse(is_file($optionsFile));
195 // Delete the generated file.
196 unlink($this->conf
->getConfigFile());
200 * Test mergeDeprecatedConfig in without options file.
202 public function testMergeDeprecatedConfigNoFile()
204 $updater = new Updater(array(), array(), true);
205 $updater->updateMethodMergeDeprecatedConfigFile();
207 $this->assertEquals('root', $this->conf
->get('credentials.login'));
211 * Test renameDashTags update method.
213 public function testRenameDashTags()
215 $refDB = new ReferenceLinkDB();
216 $refDB->write(self
::$testDatastore);
217 $linkDB = new LinkDB(self
::$testDatastore, true, false);
218 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
219 $updater = new Updater(array(), $linkDB, true);
220 $updater->updateMethodRenameDashTags();
221 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
225 * Convert old PHP config file to JSON config.
227 public function testConfigToJson()
229 $configFile = 'tests/utils/config/configPhp';
230 ConfigManager
::$CONFIG_FILE = $configFile;
231 $conf = ConfigManager
::reset();
233 // The ConfigIO is initialized with ConfigPhp.
234 $this->assertTrue($conf->getConfigIO() instanceof ConfigPhp
);
236 $updater = new Updater(array(), array(), false);
237 $done = $updater->updateMethodConfigToJson();
238 $this->assertTrue($done);
240 // The ConfigIO has been updated to ConfigJson.
241 $this->assertTrue($conf->getConfigIO() instanceof ConfigJson
);
242 $this->assertTrue(file_exists($conf->getConfigFile()));
244 // Check JSON config data.
246 $this->assertEquals('root', $conf->get('credentials.login'));
247 $this->assertEquals('lala', $conf->get('extras.redirector'));
248 $this->assertEquals('data/datastore.php', $conf->get('path.datastore'));
249 $this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION'));
251 rename($configFile . '.save.php', $configFile . '.php');
252 unlink($conf->getConfigFile());
256 * Launch config conversion update with an existing JSON file => nothing to do.
258 public function testConfigToJsonNothingToDo()
260 $filetime = filemtime($this->conf
->getConfigFile());
261 $updater = new Updater(array(), array(), false);
262 $done = $updater->updateMethodConfigToJson();
263 $this->assertTrue($done);
264 $expected = filemtime($this->conf
->getConfigFile());
265 $this->assertEquals($expected, $filetime);