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);
218 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
219 $updater = new Updater(array(), $linkDB, $this->conf
, 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 $this->conf
->setConfigFile($configFile);
231 $this->conf
->reset();
233 // The ConfigIO is initialized with ConfigPhp.
234 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
236 $updater = new Updater(array(), array(), $this->conf
, false);
237 $done = $updater->updateMethodConfigToJson();
238 $this->assertTrue($done);
240 // The ConfigIO has been updated to ConfigJson.
241 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
242 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
244 // Check JSON config data.
245 $this->conf
->reload();
246 $this->assertEquals('root', $this->conf
->get('credentials.login'));
247 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
248 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
249 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
251 rename($configFile . '.save.php', $configFile . '.php');
252 unlink($this->conf
->getConfigFileExt());
256 * Launch config conversion update with an existing JSON file => nothing to do.
258 public function testConfigToJsonNothingToDo()
260 $filetime = filemtime($this->conf
->getConfigFileExt());
261 $updater = new Updater(array(), array(), $this->conf
, false);
262 $done = $updater->updateMethodConfigToJson();
263 $this->assertTrue($done);
264 $expected = filemtime($this->conf
->getConfigFileExt());
265 $this->assertEquals($expected, $filetime);
269 * Test escapeUnescapedConfig with valid data.
271 public function testEscapeConfig()
273 $sandbox = 'sandbox/config';
274 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
275 $this->conf
= new ConfigManager($sandbox);
276 $title = '<script>alert("title");</script>';
277 $headerLink = '<script>alert("header_link");</script>';
278 $redirectorUrl = '<script>alert("redirector");</script>';
279 $this->conf
->set('general.title', $title);
280 $this->conf
->set('general.header_link', $headerLink);
281 $this->conf
->set('redirector.url', $redirectorUrl);
282 $updater = new Updater(array(), array(), $this->conf
, true);
283 $done = $updater->updateMethodEscapeUnescapedConfig();
284 $this->assertTrue($done);
285 $this->conf
->reload();
286 $this->assertEquals(escape($title), $this->conf
->get('general.title'));
287 $this->assertEquals(escape($headerLink), $this->conf
->get('general.header_link'));
288 $this->assertEquals(escape($redirectorUrl), $this->conf
->get('redirector.url'));
289 unlink($sandbox . '.json.php');
293 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
295 public function testUpdateApiSettings()
297 $confFile = 'sandbox/config';
298 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
299 $conf = new ConfigManager($confFile);
300 $updater = new Updater(array(), array(), $conf, true);
302 $this->assertFalse($conf->exists('api.enabled'));
303 $this->assertFalse($conf->exists('api.secret'));
304 $updater->updateMethodApiSettings();
306 $this->assertTrue($conf->get('api.enabled'));
307 $this->assertTrue($conf->exists('api.secret'));
308 unlink($confFile .'.json.php');
312 * Test updateMethodApiSettings(): already set, do nothing.
314 public function testUpdateApiSettingsNothingToDo()
316 $confFile = 'sandbox/config';
317 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
318 $conf = new ConfigManager($confFile);
319 $conf->set('api.enabled', false);
320 $conf->set('api.secret', '');
321 $updater = new Updater(array(), array(), $conf, true);
322 $updater->updateMethodApiSettings();
323 $this->assertFalse($conf->get('api.enabled'));
324 $this->assertEmpty($conf->get('api.secret'));
325 unlink($confFile .'.json.php');
329 * Test updateMethodDatastoreIds().
331 public function testDatastoreIds()
334 '20121206_182539' => array(
335 'linkdate' => '20121206_182539',
336 'title' => 'Geek and Poke',
337 'url' => 'http://geek-and-poke.com/',
338 'description' => 'desc',
339 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
340 'updated' => '20121206_190301',
343 '20121206_172539' => array(
344 'linkdate' => '20121206_172539',
345 'title' => 'UserFriendly - Samba',
346 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
348 'tags' => 'samba cartoon web',
351 '20121206_142300' => array(
352 'linkdate' => '20121206_142300',
353 'title' => 'UserFriendly - Web Designer',
354 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
355 'description' => 'Naming conventions... #private',
356 'tags' => 'samba cartoon web',
360 $refDB = new ReferenceLinkDB();
361 $refDB->setLinks($links);
362 $refDB->write(self
::$testDatastore);
363 $linkDB = new LinkDB(self
::$testDatastore, true, false);
365 $checksum = hash_file('sha1', self
::$testDatastore);
367 $this->conf
->set('resource.data_dir', 'sandbox');
368 $this->conf
->set('resource.datastore', self
::$testDatastore);
370 $updater = new Updater(array(), $linkDB, $this->conf
, true);
371 $this->assertTrue($updater->updateMethodDatastoreIds());
373 $linkDB = new LinkDB(self
::$testDatastore, true, false);
375 $backup = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
376 $backup = $backup[0];
378 $this->assertFileExists($backup);
379 $this->assertEquals($checksum, hash_file('sha1', $backup));
382 $this->assertEquals(3, count($linkDB));
383 $this->assertTrue(isset($linkDB[0]));
384 $this->assertFalse(isset($linkDB[0]['linkdate']));
385 $this->assertEquals(0, $linkDB[0]['id']);
386 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
387 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
388 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
389 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
390 $this->assertTrue($linkDB[0]['private']);
391 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'), $linkDB[0]['created']);
393 $this->assertTrue(isset($linkDB[1]));
394 $this->assertFalse(isset($linkDB[1]['linkdate']));
395 $this->assertEquals(1, $linkDB[1]['id']);
396 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
397 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'), $linkDB[1]['created']);
399 $this->assertTrue(isset($linkDB[2]));
400 $this->assertFalse(isset($linkDB[2]['linkdate']));
401 $this->assertEquals(2, $linkDB[2]['id']);
402 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
403 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'), $linkDB[2]['created']);
404 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_190301'), $linkDB[2]['updated']);
408 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
410 public function testDatastoreIdsNothingToDo()
412 $refDB = new ReferenceLinkDB();
413 $refDB->write(self
::$testDatastore);
414 $linkDB = new LinkDB(self
::$testDatastore, true, false);
416 $this->conf
->set('resource.data_dir', 'sandbox');
417 $this->conf
->set('resource.datastore', self
::$testDatastore);
419 $checksum = hash_file('sha1', self
::$testDatastore);
420 $updater = new Updater(array(), $linkDB, $this->conf
, true);
421 $this->assertTrue($updater->updateMethodDatastoreIds());
422 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));