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 $this->conf
= new ConfigManager(self
::$configFile);
36 * Test read_updates_file with an empty/missing file.
38 public function testReadEmptyUpdatesFile()
40 $this->assertEquals(array(), read_updates_file(''));
41 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
43 $this->assertEquals(array(), read_updates_file($updatesFile));
48 * Test read/write updates file.
50 public function testReadWriteUpdatesFile()
52 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
53 $updatesMethods = array('m1', 'm2', 'm3');
55 write_updates_file($updatesFile, $updatesMethods);
56 $readMethods = read_updates_file($updatesFile);
57 $this->assertEquals($readMethods, $updatesMethods);
60 $updatesMethods[] = 'm4';
61 write_updates_file($updatesFile, $updatesMethods);
62 $readMethods = read_updates_file($updatesFile);
63 $this->assertEquals($readMethods, $updatesMethods);
68 * Test errors in write_updates_file(): empty updates file.
70 * @expectedException Exception
71 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
73 public function testWriteEmptyUpdatesFile()
75 write_updates_file('', array('test'));
79 * Test errors in write_updates_file(): not writable updates file.
81 * @expectedException Exception
82 * @expectedExceptionMessageRegExp /Unable to write(.*)/
84 public function testWriteUpdatesFileNotWritable()
86 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
88 chmod($updatesFile, 0444);
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception
$e) {
98 * Test the update() method, with no update to run.
99 * 1. Everything already run.
100 * 2. User is logged out.
102 public function testNoUpdates()
105 'updateMethodDummy1',
106 'updateMethodDummy2',
107 'updateMethodDummy3',
108 'updateMethodException',
110 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
111 $this->assertEquals(array(), $updater->update());
113 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
114 $this->assertEquals(array(), $updater->update());
118 * Test the update() method, with all updates to run (except the failing one).
120 public function testUpdatesFirstTime()
122 $updates = array('updateMethodException',);
123 $expectedUpdates = array(
124 'updateMethodDummy1',
125 'updateMethodDummy2',
126 'updateMethodDummy3',
128 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
129 $this->assertEquals($expectedUpdates, $updater->update());
133 * Test the update() method, only one update to run.
135 public function testOneUpdate()
138 'updateMethodDummy1',
139 'updateMethodDummy3',
140 'updateMethodException',
142 $expectedUpdate = array('updateMethodDummy2');
144 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
145 $this->assertEquals($expectedUpdate, $updater->update());
149 * Test Update failed.
151 * @expectedException UpdaterException
153 public function testUpdateFailed()
156 'updateMethodDummy1',
157 'updateMethodDummy2',
158 'updateMethodDummy3',
161 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
166 * Test update mergeDeprecatedConfig:
167 * 1. init a config file.
168 * 2. init a options.php file with update value.
170 * 4. check updated value in config file.
172 public function testUpdateMergeDeprecatedConfig()
174 $this->conf
->setConfigFile('tests/utils/config/configPhp');
175 $this->conf
->reset();
177 $optionsFile = 'tests/Updater/options.php';
179 $GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
183 $this->conf
->setConfigFile('tests/Updater/config');
186 $updater = new Updater(array(), array(), $this->conf
, true);
187 // This writes a new config file in tests/Updater/config.php
188 $updater->updateMethodMergeDeprecatedConfigFile();
190 // make sure updated field is changed
191 $this->conf
->reload();
192 $this->assertTrue($this->conf
->get('privacy.default_private_links'));
193 $this->assertFalse(is_file($optionsFile));
194 // Delete the generated file.
195 unlink($this->conf
->getConfigFileExt());
199 * Test mergeDeprecatedConfig in without options file.
201 public function testMergeDeprecatedConfigNoFile()
203 $updater = new Updater(array(), array(), $this->conf
, true);
204 $updater->updateMethodMergeDeprecatedConfigFile();
206 $this->assertEquals('root', $this->conf
->get('credentials.login'));
210 * Test renameDashTags update method.
212 public function testRenameDashTags()
214 $refDB = new ReferenceLinkDB();
215 $refDB->write(self
::$testDatastore);
216 $linkDB = new LinkDB(self
::$testDatastore, true, false);
217 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
218 $updater = new Updater(array(), $linkDB, $this->conf
, true);
219 $updater->updateMethodRenameDashTags();
220 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
224 * Convert old PHP config file to JSON config.
226 public function testConfigToJson()
228 $configFile = 'tests/utils/config/configPhp';
229 $this->conf
->setConfigFile($configFile);
230 $this->conf
->reset();
232 // The ConfigIO is initialized with ConfigPhp.
233 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
235 $updater = new Updater(array(), array(), $this->conf
, false);
236 $done = $updater->updateMethodConfigToJson();
237 $this->assertTrue($done);
239 // The ConfigIO has been updated to ConfigJson.
240 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
241 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
243 // Check JSON config data.
244 $this->conf
->reload();
245 $this->assertEquals('root', $this->conf
->get('credentials.login'));
246 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
247 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
248 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
250 rename($configFile . '.save.php', $configFile . '.php');
251 unlink($this->conf
->getConfigFileExt());
255 * Launch config conversion update with an existing JSON file => nothing to do.
257 public function testConfigToJsonNothingToDo()
259 $filetime = filemtime($this->conf
->getConfigFileExt());
260 $updater = new Updater(array(), array(), $this->conf
, false);
261 $done = $updater->updateMethodConfigToJson();
262 $this->assertTrue($done);
263 $expected = filemtime($this->conf
->getConfigFileExt());
264 $this->assertEquals($expected, $filetime);