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 updateMethodDatastoreIds().
295 public function testDatastoreIds()
298 '20121206_182539' => array(
299 'linkdate' => '20121206_182539',
300 'title' => 'Geek and Poke',
301 'url' => 'http://geek-and-poke.com/',
302 'description' => 'desc',
303 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
304 'updated' => '20121206_190301',
307 '20121206_172539' => array(
308 'linkdate' => '20121206_172539',
309 'title' => 'UserFriendly - Samba',
310 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
312 'tags' => 'samba cartoon web',
315 '20121206_142300' => array(
316 'linkdate' => '20121206_142300',
317 'title' => 'UserFriendly - Web Designer',
318 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
319 'description' => 'Naming conventions... #private',
320 'tags' => 'samba cartoon web',
324 $refDB = new ReferenceLinkDB();
325 $refDB->setLinks($links);
326 $refDB->write(self
::$testDatastore);
327 $linkDB = new LinkDB(self
::$testDatastore, true, false);
329 $checksum = hash_file('sha1', self
::$testDatastore);
331 $this->conf
->set('resource.data_dir', 'sandbox');
332 $this->conf
->set('resource.datastore', self
::$testDatastore);
334 $updater = new Updater(array(), $linkDB, $this->conf
, true);
335 $this->assertTrue($updater->updateMethodDatastoreIds());
337 $linkDB = new LinkDB(self
::$testDatastore, true, false);
339 $backup = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
340 $backup = $backup[0];
342 $this->assertFileExists($backup);
343 $this->assertEquals($checksum, hash_file('sha1', $backup));
346 $this->assertEquals(3, count($linkDB));
347 $this->assertTrue(isset($linkDB[0]));
348 $this->assertFalse(isset($linkDB[0]['linkdate']));
349 $this->assertEquals(0, $linkDB[0]['id']);
350 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
351 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
352 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
353 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
354 $this->assertTrue($linkDB[0]['private']);
355 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'), $linkDB[0]['created']);
357 $this->assertTrue(isset($linkDB[1]));
358 $this->assertFalse(isset($linkDB[1]['linkdate']));
359 $this->assertEquals(1, $linkDB[1]['id']);
360 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
361 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'), $linkDB[1]['created']);
363 $this->assertTrue(isset($linkDB[2]));
364 $this->assertFalse(isset($linkDB[2]['linkdate']));
365 $this->assertEquals(2, $linkDB[2]['id']);
366 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
367 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'), $linkDB[2]['created']);
368 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_190301'), $linkDB[2]['updated']);
372 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
374 public function testDatastoreIdsNothingToDo()
376 $refDB = new ReferenceLinkDB();
377 $refDB->write(self
::$testDatastore);
378 $linkDB = new LinkDB(self
::$testDatastore, true, false);
380 $this->conf
->set('resource.data_dir', 'sandbox');
381 $this->conf
->set('resource.datastore', self
::$testDatastore);
383 $checksum = hash_file('sha1', self
::$testDatastore);
384 $updater = new Updater(array(), $linkDB, $this->conf
, true);
385 $this->assertTrue($updater->updateMethodDatastoreIds());
386 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));
390 * Test updateMethodEscapeMarkdown with markdown plugin enabled
391 * => setting markdown_escape set to false.
393 public function testEscapeMarkdownSettingToFalse()
395 $sandboxConf = 'sandbox/config';
396 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
397 $this->conf
= new ConfigManager($sandboxConf);
399 $this->conf
->set('general.enabled_plugins', ['markdown']);
400 $updater = new Updater([], [], $this->conf
, true);
401 $this->assertTrue($updater->updateMethodEscapeMarkdown());
402 $this->assertFalse($this->conf
->get('security.markdown_escape'));
405 $this->conf
= new ConfigManager($sandboxConf);
406 $this->assertFalse($this->conf
->get('security.markdown_escape'));
410 * Test updateMethodEscapeMarkdown with markdown plugin disabled
411 * => setting markdown_escape set to true.
413 public function testEscapeMarkdownSettingToTrue()
415 $sandboxConf = 'sandbox/config';
416 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
417 $this->conf
= new ConfigManager($sandboxConf);
419 $this->conf
->set('general.enabled_plugins', []);
420 $updater = new Updater([], [], $this->conf
, true);
421 $this->assertTrue($updater->updateMethodEscapeMarkdown());
422 $this->assertTrue($this->conf
->get('security.markdown_escape'));
425 $this->conf
= new ConfigManager($sandboxConf);
426 $this->assertTrue($this->conf
->get('security.markdown_escape'));
430 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
432 public function testEscapeMarkdownSettingNothingToDoEnabled()
434 $sandboxConf = 'sandbox/config';
435 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
436 $this->conf
= new ConfigManager($sandboxConf);
437 $this->conf
->set('security.markdown_escape', true);
438 $updater = new Updater([], [], $this->conf
, true);
439 $this->assertTrue($updater->updateMethodEscapeMarkdown());
440 $this->assertTrue($this->conf
->get('security.markdown_escape'));
444 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
446 public function testEscapeMarkdownSettingNothingToDoDisabled()
448 $this->conf
->set('security.markdown_escape', false);
449 $updater = new Updater([], [], $this->conf
, true);
450 $this->assertTrue($updater->updateMethodEscapeMarkdown());
451 $this->assertFalse($this->conf
->get('security.markdown_escape'));