3 require_once 'application/config/ConfigManager.php';
4 require_once 'tests/Updater/DummyUpdater.php';
5 require_once 'inc/rain.tpl.class.php';
9 * Runs unit tests against the Updater class.
11 class UpdaterTest
extends PHPUnit_Framework_TestCase
14 * @var string Path to test datastore.
16 protected static $testDatastore = 'sandbox/datastore.php';
19 * @var string Config file path (without extension).
21 protected static $configFile = 'tests/utils/config/configJson';
29 * Executed before each test.
31 public function setUp()
33 $this->conf
= new ConfigManager(self
::$configFile);
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('resource.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('resource.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('resource.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(), $this->conf
, true);
112 $this->assertEquals(array(), $updater->update());
114 $updater = new DummyUpdater(array(), array(), $this->conf
, 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(), $this->conf
, 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(), $this->conf
, 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(), $this->conf
, 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 $this->conf
->setConfigFile('tests/utils/config/configPhp');
176 $this->conf
->reset();
178 $optionsFile = 'tests/Updater/options.php';
180 $GLOBALS[\'privateLinkByDefault\'] = true;';
181 file_put_contents($optionsFile, $options);
184 $this->conf
->setConfigFile('tests/Updater/config');
187 $updater = new Updater(array(), array(), $this->conf
, 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('privacy.default_private_links'));
194 $this->assertFalse(is_file($optionsFile));
195 // Delete the generated file.
196 unlink($this->conf
->getConfigFileExt());
200 * Test mergeDeprecatedConfig in without options file.
202 public function testMergeDeprecatedConfigNoFile()
204 $updater = new Updater(array(), array(), $this->conf
, 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);
219 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
220 $updater = new Updater(array(), $linkDB, $this->conf
, true);
221 $updater->updateMethodRenameDashTags();
222 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
226 * Convert old PHP config file to JSON config.
228 public function testConfigToJson()
230 $configFile = 'tests/utils/config/configPhp';
231 $this->conf
->setConfigFile($configFile);
232 $this->conf
->reset();
234 // The ConfigIO is initialized with ConfigPhp.
235 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
237 $updater = new Updater(array(), array(), $this->conf
, false);
238 $done = $updater->updateMethodConfigToJson();
239 $this->assertTrue($done);
241 // The ConfigIO has been updated to ConfigJson.
242 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
243 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
245 // Check JSON config data.
246 $this->conf
->reload();
247 $this->assertEquals('root', $this->conf
->get('credentials.login'));
248 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
249 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
250 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
252 rename($configFile . '.save.php', $configFile . '.php');
253 unlink($this->conf
->getConfigFileExt());
257 * Launch config conversion update with an existing JSON file => nothing to do.
259 public function testConfigToJsonNothingToDo()
261 $filetime = filemtime($this->conf
->getConfigFileExt());
262 $updater = new Updater(array(), array(), $this->conf
, false);
263 $done = $updater->updateMethodConfigToJson();
264 $this->assertTrue($done);
265 $expected = filemtime($this->conf
->getConfigFileExt());
266 $this->assertEquals($expected, $filetime);
270 * Test escapeUnescapedConfig with valid data.
272 public function testEscapeConfig()
274 $sandbox = 'sandbox/config';
275 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
276 $this->conf
= new ConfigManager($sandbox);
277 $title = '<script>alert("title");</script>';
278 $headerLink = '<script>alert("header_link");</script>';
279 $redirectorUrl = '<script>alert("redirector");</script>';
280 $this->conf
->set('general.title', $title);
281 $this->conf
->set('general.header_link', $headerLink);
282 $this->conf
->set('redirector.url', $redirectorUrl);
283 $updater = new Updater(array(), array(), $this->conf
, true);
284 $done = $updater->updateMethodEscapeUnescapedConfig();
285 $this->assertTrue($done);
286 $this->conf
->reload();
287 $this->assertEquals(escape($title), $this->conf
->get('general.title'));
288 $this->assertEquals(escape($headerLink), $this->conf
->get('general.header_link'));
289 $this->assertEquals(escape($redirectorUrl), $this->conf
->get('redirector.url'));
290 unlink($sandbox . '.json.php');
294 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
296 public function testUpdateApiSettings()
298 $confFile = 'sandbox/config';
299 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
300 $conf = new ConfigManager($confFile);
301 $updater = new Updater(array(), array(), $conf, true);
303 $this->assertFalse($conf->exists('api.enabled'));
304 $this->assertFalse($conf->exists('api.secret'));
305 $updater->updateMethodApiSettings();
307 $this->assertTrue($conf->get('api.enabled'));
308 $this->assertTrue($conf->exists('api.secret'));
309 unlink($confFile .'.json.php');
313 * Test updateMethodApiSettings(): already set, do nothing.
315 public function testUpdateApiSettingsNothingToDo()
317 $confFile = 'sandbox/config';
318 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
319 $conf = new ConfigManager($confFile);
320 $conf->set('api.enabled', false);
321 $conf->set('api.secret', '');
322 $updater = new Updater(array(), array(), $conf, true);
323 $updater->updateMethodApiSettings();
324 $this->assertFalse($conf->get('api.enabled'));
325 $this->assertEmpty($conf->get('api.secret'));
326 unlink($confFile .'.json.php');
330 * Test updateMethodDatastoreIds().
332 public function testDatastoreIds()
335 '20121206_182539' => array(
336 'linkdate' => '20121206_182539',
337 'title' => 'Geek and Poke',
338 'url' => 'http://geek-and-poke.com/',
339 'description' => 'desc',
340 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
341 'updated' => '20121206_190301',
344 '20121206_172539' => array(
345 'linkdate' => '20121206_172539',
346 'title' => 'UserFriendly - Samba',
347 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
349 'tags' => 'samba cartoon web',
352 '20121206_142300' => array(
353 'linkdate' => '20121206_142300',
354 'title' => 'UserFriendly - Web Designer',
355 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
356 'description' => 'Naming conventions... #private',
357 'tags' => 'samba cartoon web',
361 $refDB = new ReferenceLinkDB();
362 $refDB->setLinks($links);
363 $refDB->write(self
::$testDatastore);
364 $linkDB = new LinkDB(self
::$testDatastore, true, false);
366 $checksum = hash_file('sha1', self
::$testDatastore);
368 $this->conf
->set('resource.data_dir', 'sandbox');
369 $this->conf
->set('resource.datastore', self
::$testDatastore);
371 $updater = new Updater(array(), $linkDB, $this->conf
, true);
372 $this->assertTrue($updater->updateMethodDatastoreIds());
374 $linkDB = new LinkDB(self
::$testDatastore, true, false);
376 $backup = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
377 $backup = $backup[0];
379 $this->assertFileExists($backup);
380 $this->assertEquals($checksum, hash_file('sha1', $backup));
383 $this->assertEquals(3, count($linkDB));
384 $this->assertTrue(isset($linkDB[0]));
385 $this->assertFalse(isset($linkDB[0]['linkdate']));
386 $this->assertEquals(0, $linkDB[0]['id']);
387 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
388 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
389 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
390 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
391 $this->assertTrue($linkDB[0]['private']);
392 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'), $linkDB[0]['created']);
394 $this->assertTrue(isset($linkDB[1]));
395 $this->assertFalse(isset($linkDB[1]['linkdate']));
396 $this->assertEquals(1, $linkDB[1]['id']);
397 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
398 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'), $linkDB[1]['created']);
400 $this->assertTrue(isset($linkDB[2]));
401 $this->assertFalse(isset($linkDB[2]['linkdate']));
402 $this->assertEquals(2, $linkDB[2]['id']);
403 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
404 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'), $linkDB[2]['created']);
405 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_190301'), $linkDB[2]['updated']);
409 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
411 public function testDatastoreIdsNothingToDo()
413 $refDB = new ReferenceLinkDB();
414 $refDB->write(self
::$testDatastore);
415 $linkDB = new LinkDB(self
::$testDatastore, true, false);
417 $this->conf
->set('resource.data_dir', 'sandbox');
418 $this->conf
->set('resource.datastore', self
::$testDatastore);
420 $checksum = hash_file('sha1', self
::$testDatastore);
421 $updater = new Updater(array(), $linkDB, $this->conf
, true);
422 $this->assertTrue($updater->updateMethodDatastoreIds());
423 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));
427 * Test defaultTheme update with default settings: nothing to do.
429 public function testDefaultThemeWithDefaultSettings()
431 $sandbox = 'sandbox/config';
432 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
433 $this->conf
= new ConfigManager($sandbox);
434 $updater = new Updater([], [], $this->conf
, true);
435 $this->assertTrue($updater->updateMethodDefaultTheme());
437 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
438 $this->assertEquals('default', $this->conf
->get('resource.theme'));
439 $this->conf
= new ConfigManager($sandbox);
440 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
441 $this->assertEquals('default', $this->conf
->get('resource.theme'));
442 unlink($sandbox . '.json.php');
446 * Test defaultTheme update with a custom theme in a subfolder
448 public function testDefaultThemeWithCustomTheme()
450 $theme = 'iamanartist';
451 $sandbox = 'sandbox/config';
452 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
453 $this->conf
= new ConfigManager($sandbox);
454 mkdir('sandbox/'. $theme);
455 touch('sandbox/'. $theme .'/linklist.html');
456 $this->conf
->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
457 $updater = new Updater([], [], $this->conf
, true);
458 $this->assertTrue($updater->updateMethodDefaultTheme());
460 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
461 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
462 $this->conf
= new ConfigManager($sandbox);
463 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
464 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
465 unlink($sandbox . '.json.php');
466 unlink('sandbox/'. $theme .'/linklist.html');
467 rmdir('sandbox/'. $theme);
471 * Test updateMethodDefaultThemeVintage with the default theme enabled.
473 public function testSetDefaultThemeToVintage()
475 $sandboxConf = 'sandbox/config';
476 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
477 $this->conf
= new ConfigManager($sandboxConf);
479 $this->conf
->set('resource.theme', 'default');
480 $updater = new Updater([], [], $this->conf
, true);
481 $this->assertTrue($updater->updateMethodDefaultThemeVintage());
482 $this->assertEquals('vintage', $this->conf
->get('resource.theme'));
485 $this->conf
= new ConfigManager($sandboxConf);
486 $this->assertEquals('vintage', $this->conf
->get('resource.theme'));
490 * Test updateMethodDefaultThemeVintage with custom theme enabled => nothing to do.
492 public function testSetDefaultThemeNothingToDo()
494 $sandboxConf = 'sandbox/config';
495 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
496 $this->conf
= new ConfigManager($sandboxConf);
498 $theme = 'myawesometheme';
499 $this->conf
->set('resource.theme', $theme);
500 $this->conf
->write(true);
501 $updater = new Updater([], [], $this->conf
, true);
502 $this->assertTrue($updater->updateMethodDefaultThemeVintage());
503 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
506 $this->conf
= new ConfigManager($sandboxConf);
507 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
511 * Test updateMethodEscapeMarkdown with markdown plugin enabled
512 * => setting markdown_escape set to false.
514 public function testEscapeMarkdownSettingToFalse()
516 $sandboxConf = 'sandbox/config';
517 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
518 $this->conf
= new ConfigManager($sandboxConf);
520 $this->conf
->set('general.enabled_plugins', ['markdown']);
521 $updater = new Updater([], [], $this->conf
, true);
522 $this->assertTrue($updater->updateMethodEscapeMarkdown());
523 $this->assertFalse($this->conf
->get('security.markdown_escape'));
526 $this->conf
= new ConfigManager($sandboxConf);
527 $this->assertFalse($this->conf
->get('security.markdown_escape'));
532 * Test updateMethodEscapeMarkdown with markdown plugin disabled
533 * => setting markdown_escape set to true.
535 public function testEscapeMarkdownSettingToTrue()
537 $sandboxConf = 'sandbox/config';
538 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
539 $this->conf
= new ConfigManager($sandboxConf);
541 $this->conf
->set('general.enabled_plugins', []);
542 $updater = new Updater([], [], $this->conf
, true);
543 $this->assertTrue($updater->updateMethodEscapeMarkdown());
544 $this->assertTrue($this->conf
->get('security.markdown_escape'));
547 $this->conf
= new ConfigManager($sandboxConf);
548 $this->assertTrue($this->conf
->get('security.markdown_escape'));
552 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
554 public function testEscapeMarkdownSettingNothingToDoEnabled()
556 $sandboxConf = 'sandbox/config';
557 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
558 $this->conf
= new ConfigManager($sandboxConf);
559 $this->conf
->set('security.markdown_escape', true);
560 $updater = new Updater([], [], $this->conf
, true);
561 $this->assertTrue($updater->updateMethodEscapeMarkdown());
562 $this->assertTrue($this->conf
->get('security.markdown_escape'));
566 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
568 public function testEscapeMarkdownSettingNothingToDoDisabled()
570 $this->conf
->set('security.markdown_escape', false);
571 $updater = new Updater([], [], $this->conf
, true);
572 $this->assertTrue($updater->updateMethodEscapeMarkdown());
573 $this->assertFalse($this->conf
->get('security.markdown_escape'));