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 array Configuration input set.
15 private static $configFields;
18 * @var string Path to test datastore.
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string Config file path (without extension).
25 protected static $configFile = 'tests/utils/config/configUpdater';
33 * Executed before each test.
35 public function setUp()
37 self
::$configFields = array(
41 'timezone' => 'Europe/Paris',
43 'titleLink' => 'titleLink',
45 'disablesessionprotection' => false,
46 'privateLinkByDefault' => false,
48 'DATADIR' => 'tests/Updater',
49 'PAGECACHE' => 'sandbox/pagecache',
50 'config1' => 'config1data',
51 'config2' => 'config2data',
55 ConfigManager
::$CONFIG_FILE = self
::$configFile;
56 $this->conf
= ConfigManager
::reset();
57 $this->conf
->reload();
58 foreach (self
::$configFields as $key => $value) {
59 $this->conf
->set($key, $value);
61 $this->conf
->write(true);
65 * Executed after each test.
69 public function tearDown()
71 if (is_file('tests/Updater/config.json')) {
72 unlink('tests/Updater/config.json');
75 if (is_file(self
::$configFields['config']['DATADIR'] . '/options.php')) {
76 unlink(self
::$configFields['config']['DATADIR'] . '/options.php');
79 if (is_file(self
::$configFields['config']['DATADIR'] . '/updates.txt')) {
80 unlink(self
::$configFields['config']['DATADIR'] . '/updates.txt');
85 * Test read_updates_file with an empty/missing file.
87 public function testReadEmptyUpdatesFile()
89 $this->assertEquals(array(), read_updates_file(''));
90 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
92 $this->assertEquals(array(), read_updates_file($updatesFile));
96 * Test read/write updates file.
98 public function testReadWriteUpdatesFile()
100 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
101 $updatesMethods = array('m1', 'm2', 'm3');
103 write_updates_file($updatesFile, $updatesMethods);
104 $readMethods = read_updates_file($updatesFile);
105 $this->assertEquals($readMethods, $updatesMethods);
108 $updatesMethods[] = 'm4';
109 write_updates_file($updatesFile, $updatesMethods);
110 $readMethods = read_updates_file($updatesFile);
111 $this->assertEquals($readMethods, $updatesMethods);
115 * Test errors in write_updates_file(): empty updates file.
117 * @expectedException Exception
118 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
120 public function testWriteEmptyUpdatesFile()
122 write_updates_file('', array('test'));
126 * Test errors in write_updates_file(): not writable updates file.
128 * @expectedException Exception
129 * @expectedExceptionMessageRegExp /Unable to write(.*)/
131 public function testWriteUpdatesFileNotWritable()
133 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
135 chmod($updatesFile, 0444);
136 @write_updates_file($updatesFile, array('test'));
140 * Test the update() method, with no update to run.
141 * 1. Everything already run.
142 * 2. User is logged out.
144 public function testNoUpdates()
147 'updateMethodDummy1',
148 'updateMethodDummy2',
149 'updateMethodDummy3',
150 'updateMethodException',
152 $updater = new DummyUpdater($updates, array(), true);
153 $this->assertEquals(array(), $updater->update());
155 $updater = new DummyUpdater(array(), array(), false);
156 $this->assertEquals(array(), $updater->update());
160 * Test the update() method, with all updates to run (except the failing one).
162 public function testUpdatesFirstTime()
164 $updates = array('updateMethodException',);
165 $expectedUpdates = array(
166 'updateMethodDummy1',
167 'updateMethodDummy2',
168 'updateMethodDummy3',
170 $updater = new DummyUpdater($updates, array(), true);
171 $this->assertEquals($expectedUpdates, $updater->update());
175 * Test the update() method, only one update to run.
177 public function testOneUpdate()
180 'updateMethodDummy1',
181 'updateMethodDummy3',
182 'updateMethodException',
184 $expectedUpdate = array('updateMethodDummy2');
186 $updater = new DummyUpdater($updates, array(), true);
187 $this->assertEquals($expectedUpdate, $updater->update());
191 * Test Update failed.
193 * @expectedException UpdaterException
195 public function testUpdateFailed()
198 'updateMethodDummy1',
199 'updateMethodDummy2',
200 'updateMethodDummy3',
203 $updater = new DummyUpdater($updates, array(), true);
208 * Test update mergeDeprecatedConfig:
209 * 1. init a config file.
210 * 2. init a options.php file with update value.
212 * 4. check updated value in config file.
214 public function testUpdateMergeDeprecatedConfig()
216 // Use writeConfig to create a options.php
217 ConfigManager
::$CONFIG_FILE = 'tests/Updater/options';
218 $this->conf
->setConfigIO(new ConfigPhp());
220 $invert = !$this->conf
->get('privateLinkByDefault');
221 $this->conf
->set('privateLinkByDefault', $invert);
222 $this->conf
->write(true);
224 $optionsFile = 'tests/Updater/options.php';
225 $this->assertTrue(is_file($optionsFile));
227 ConfigManager
::$CONFIG_FILE = 'tests/Updater/config';
230 $updater = new Updater(array(), array(), true);
231 // This writes a new config file in tests/Updater/config.php
232 $updater->updateMethodMergeDeprecatedConfigFile();
234 // make sure updated field is changed
235 $this->conf
->reload();
236 $this->assertEquals($invert, $this->conf
->get('privateLinkByDefault'));
237 $this->assertFalse(is_file($optionsFile));
238 // Delete the generated file.
239 unlink($this->conf
->getConfigFile());
243 * Test mergeDeprecatedConfig in without options file.
245 public function testMergeDeprecatedConfigNoFile()
247 $updater = new Updater(array(), array(), true);
248 $updater->updateMethodMergeDeprecatedConfigFile();
250 $this->assertEquals(self
::$configFields['login'], $this->conf
->get('login'));
254 * Test renameDashTags update method.
256 public function testRenameDashTags()
258 $refDB = new ReferenceLinkDB();
259 $refDB->write(self
::$testDatastore);
260 $linkDB = new LinkDB(self
::$testDatastore, true, false);
261 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
262 $updater = new Updater(array(), $linkDB, true);
263 $updater->updateMethodRenameDashTags();
264 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
268 * Convert old PHP config file to JSON config.
270 public function testConfigToJson()
272 $configFile = 'tests/utils/config/configPhp';
273 ConfigManager
::$CONFIG_FILE = $configFile;
274 $conf = ConfigManager
::reset();
276 // The ConfigIO is initialized with ConfigPhp.
277 $this->assertTrue($conf->getConfigIO() instanceof ConfigPhp
);
279 $updater = new Updater(array(), array(), false);
280 $done = $updater->updateMethodConfigToJson();
281 $this->assertTrue($done);
283 // The ConfigIO has been updated to ConfigJson.
284 $this->assertTrue($conf->getConfigIO() instanceof ConfigJson
);
285 $this->assertTrue(file_exists($conf->getConfigFile()));
287 // Check JSON config data.
289 $this->assertEquals('root', $conf->get('login'));
290 $this->assertEquals('lala', $conf->get('redirector'));
291 $this->assertEquals('data/datastore.php', $conf->get('config.DATASTORE'));
292 $this->assertEquals('1', $conf->get('plugins.WALLABAG_VERSION'));
294 rename($configFile . '.save.php', $configFile . '.php');
295 unlink($conf->getConfigFile());
299 * Launch config conversion update with an existing JSON file => nothing to do.
301 public function testConfigToJsonNothingToDo()
303 $configFile = 'tests/utils/config/configUpdateDone';
304 ConfigManager
::$CONFIG_FILE = $configFile;
305 $conf = ConfigManager
::reset();
307 $filetime = filemtime($conf->getConfigFile());
308 $updater = new Updater(array(), array(), false);
309 $done = $updater->updateMethodConfigToJson();
310 $this->assertTrue($done);
311 $expected = filemtime($conf->getConfigFile());
312 $this->assertEquals($expected, $filetime);