2 use Shaarli\Config\ConfigJson
;
3 use Shaarli\Config\ConfigManager
;
4 use Shaarli\Config\ConfigPhp
;
6 require_once 'tests/Updater/DummyUpdater.php';
7 require_once 'inc/rain.tpl.class.php';
11 * Runs unit tests against the Updater class.
13 class UpdaterTest
extends PHPUnit_Framework_TestCase
16 * @var string Path to test datastore.
18 protected static $testDatastore = 'sandbox/datastore.php';
21 * @var string Config file path (without extension).
23 protected static $configFile = 'tests/utils/config/configJson';
31 * Executed before each test.
33 public function setUp()
35 $this->conf
= new ConfigManager(self
::$configFile);
39 * Test read_updates_file with an empty/missing file.
41 public function testReadEmptyUpdatesFile()
43 $this->assertEquals(array(), read_updates_file(''));
44 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
46 $this->assertEquals(array(), read_updates_file($updatesFile));
51 * Test read/write updates file.
53 public function testReadWriteUpdatesFile()
55 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
56 $updatesMethods = array('m1', 'm2', 'm3');
58 write_updates_file($updatesFile, $updatesMethods);
59 $readMethods = read_updates_file($updatesFile);
60 $this->assertEquals($readMethods, $updatesMethods);
63 $updatesMethods[] = 'm4';
64 write_updates_file($updatesFile, $updatesMethods);
65 $readMethods = read_updates_file($updatesFile);
66 $this->assertEquals($readMethods, $updatesMethods);
71 * Test errors in write_updates_file(): empty updates file.
73 * @expectedException Exception
74 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
76 public function testWriteEmptyUpdatesFile()
78 write_updates_file('', array('test'));
82 * Test errors in write_updates_file(): not writable updates file.
84 * @expectedException Exception
85 * @expectedExceptionMessageRegExp /Unable to write(.*)/
87 public function testWriteUpdatesFileNotWritable()
89 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
91 chmod($updatesFile, 0444);
93 @write_updates_file($updatesFile, array('test'));
94 } catch (Exception
$e) {
101 * Test the update() method, with no update to run.
102 * 1. Everything already run.
103 * 2. User is logged out.
105 public function testNoUpdates()
108 'updateMethodDummy1',
109 'updateMethodDummy2',
110 'updateMethodDummy3',
111 'updateMethodException',
113 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
114 $this->assertEquals(array(), $updater->update());
116 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
117 $this->assertEquals(array(), $updater->update());
121 * Test the update() method, with all updates to run (except the failing one).
123 public function testUpdatesFirstTime()
125 $updates = array('updateMethodException',);
126 $expectedUpdates = array(
127 'updateMethodDummy1',
128 'updateMethodDummy2',
129 'updateMethodDummy3',
131 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
132 $this->assertEquals($expectedUpdates, $updater->update());
136 * Test the update() method, only one update to run.
138 public function testOneUpdate()
141 'updateMethodDummy1',
142 'updateMethodDummy3',
143 'updateMethodException',
145 $expectedUpdate = array('updateMethodDummy2');
147 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
148 $this->assertEquals($expectedUpdate, $updater->update());
152 * Test Update failed.
154 * @expectedException UpdaterException
156 public function testUpdateFailed()
159 'updateMethodDummy1',
160 'updateMethodDummy2',
161 'updateMethodDummy3',
164 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
169 * Test update mergeDeprecatedConfig:
170 * 1. init a config file.
171 * 2. init a options.php file with update value.
173 * 4. check updated value in config file.
175 public function testUpdateMergeDeprecatedConfig()
177 $this->conf
->setConfigFile('tests/utils/config/configPhp');
178 $this->conf
->reset();
180 $optionsFile = 'tests/Updater/options.php';
182 $GLOBALS[\'privateLinkByDefault\'] = true;';
183 file_put_contents($optionsFile, $options);
186 $this->conf
->setConfigFile('tests/Updater/config');
189 $updater = new Updater(array(), array(), $this->conf
, true);
190 // This writes a new config file in tests/Updater/config.php
191 $updater->updateMethodMergeDeprecatedConfigFile();
193 // make sure updated field is changed
194 $this->conf
->reload();
195 $this->assertTrue($this->conf
->get('privacy.default_private_links'));
196 $this->assertFalse(is_file($optionsFile));
197 // Delete the generated file.
198 unlink($this->conf
->getConfigFileExt());
202 * Test mergeDeprecatedConfig in without options file.
204 public function testMergeDeprecatedConfigNoFile()
206 $updater = new Updater(array(), array(), $this->conf
, true);
207 $updater->updateMethodMergeDeprecatedConfigFile();
209 $this->assertEquals('root', $this->conf
->get('credentials.login'));
213 * Test renameDashTags update method.
215 public function testRenameDashTags()
217 $refDB = new ReferenceLinkDB();
218 $refDB->write(self
::$testDatastore);
219 $linkDB = new LinkDB(self
::$testDatastore, true, false);
221 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
222 $updater = new Updater(array(), $linkDB, $this->conf
, true);
223 $updater->updateMethodRenameDashTags();
224 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
228 * Convert old PHP config file to JSON config.
230 public function testConfigToJson()
232 $configFile = 'tests/utils/config/configPhp';
233 $this->conf
->setConfigFile($configFile);
234 $this->conf
->reset();
236 // The ConfigIO is initialized with ConfigPhp.
237 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
239 $updater = new Updater(array(), array(), $this->conf
, false);
240 $done = $updater->updateMethodConfigToJson();
241 $this->assertTrue($done);
243 // The ConfigIO has been updated to ConfigJson.
244 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
245 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
247 // Check JSON config data.
248 $this->conf
->reload();
249 $this->assertEquals('root', $this->conf
->get('credentials.login'));
250 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
251 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
252 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
254 rename($configFile . '.save.php', $configFile . '.php');
255 unlink($this->conf
->getConfigFileExt());
259 * Launch config conversion update with an existing JSON file => nothing to do.
261 public function testConfigToJsonNothingToDo()
263 $filetime = filemtime($this->conf
->getConfigFileExt());
264 $updater = new Updater(array(), array(), $this->conf
, false);
265 $done = $updater->updateMethodConfigToJson();
266 $this->assertTrue($done);
267 $expected = filemtime($this->conf
->getConfigFileExt());
268 $this->assertEquals($expected, $filetime);
272 * Test escapeUnescapedConfig with valid data.
274 public function testEscapeConfig()
276 $sandbox = 'sandbox/config';
277 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
278 $this->conf
= new ConfigManager($sandbox);
279 $title = '<script>alert("title");</script>';
280 $headerLink = '<script>alert("header_link");</script>';
281 $redirectorUrl = '<script>alert("redirector");</script>';
282 $this->conf
->set('general.title', $title);
283 $this->conf
->set('general.header_link', $headerLink);
284 $this->conf
->set('redirector.url', $redirectorUrl);
285 $updater = new Updater(array(), array(), $this->conf
, true);
286 $done = $updater->updateMethodEscapeUnescapedConfig();
287 $this->assertTrue($done);
288 $this->conf
->reload();
289 $this->assertEquals(escape($title), $this->conf
->get('general.title'));
290 $this->assertEquals(escape($headerLink), $this->conf
->get('general.header_link'));
291 $this->assertEquals(escape($redirectorUrl), $this->conf
->get('redirector.url'));
292 unlink($sandbox . '.json.php');
296 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
298 public function testUpdateApiSettings()
300 $confFile = 'sandbox/config';
301 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
302 $conf = new ConfigManager($confFile);
303 $updater = new Updater(array(), array(), $conf, true);
305 $this->assertFalse($conf->exists('api.enabled'));
306 $this->assertFalse($conf->exists('api.secret'));
307 $updater->updateMethodApiSettings();
309 $this->assertTrue($conf->get('api.enabled'));
310 $this->assertTrue($conf->exists('api.secret'));
311 unlink($confFile .'.json.php');
315 * Test updateMethodApiSettings(): already set, do nothing.
317 public function testUpdateApiSettingsNothingToDo()
319 $confFile = 'sandbox/config';
320 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
321 $conf = new ConfigManager($confFile);
322 $conf->set('api.enabled', false);
323 $conf->set('api.secret', '');
324 $updater = new Updater(array(), array(), $conf, true);
325 $updater->updateMethodApiSettings();
326 $this->assertFalse($conf->get('api.enabled'));
327 $this->assertEmpty($conf->get('api.secret'));
328 unlink($confFile .'.json.php');
332 * Test updateMethodDatastoreIds().
334 public function testDatastoreIds()
337 '20121206_182539' => array(
338 'linkdate' => '20121206_182539',
339 'title' => 'Geek and Poke',
340 'url' => 'http://geek-and-poke.com/',
341 'description' => 'desc',
342 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
343 'updated' => '20121206_190301',
346 '20121206_172539' => array(
347 'linkdate' => '20121206_172539',
348 'title' => 'UserFriendly - Samba',
349 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
351 'tags' => 'samba cartoon web',
354 '20121206_142300' => array(
355 'linkdate' => '20121206_142300',
356 'title' => 'UserFriendly - Web Designer',
357 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
358 'description' => 'Naming conventions... #private',
359 'tags' => 'samba cartoon web',
363 $refDB = new ReferenceLinkDB();
364 $refDB->setLinks($links);
365 $refDB->write(self
::$testDatastore);
366 $linkDB = new LinkDB(self
::$testDatastore, true, false);
368 $checksum = hash_file('sha1', self
::$testDatastore);
370 $this->conf
->set('resource.data_dir', 'sandbox');
371 $this->conf
->set('resource.datastore', self
::$testDatastore);
373 $updater = new Updater(array(), $linkDB, $this->conf
, true);
374 $this->assertTrue($updater->updateMethodDatastoreIds());
376 $linkDB = new LinkDB(self
::$testDatastore, true, false);
378 $backup = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
379 $backup = $backup[0];
381 $this->assertFileExists($backup);
382 $this->assertEquals($checksum, hash_file('sha1', $backup));
385 $this->assertEquals(3, count($linkDB));
386 $this->assertTrue(isset($linkDB[0]));
387 $this->assertFalse(isset($linkDB[0]['linkdate']));
388 $this->assertEquals(0, $linkDB[0]['id']);
389 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
390 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
391 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
392 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
393 $this->assertTrue($linkDB[0]['private']);
394 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_142300'), $linkDB[0]['created']);
396 $this->assertTrue(isset($linkDB[1]));
397 $this->assertFalse(isset($linkDB[1]['linkdate']));
398 $this->assertEquals(1, $linkDB[1]['id']);
399 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
400 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_172539'), $linkDB[1]['created']);
402 $this->assertTrue(isset($linkDB[2]));
403 $this->assertFalse(isset($linkDB[2]['linkdate']));
404 $this->assertEquals(2, $linkDB[2]['id']);
405 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
406 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_182539'), $linkDB[2]['created']);
407 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20121206_190301'), $linkDB[2]['updated']);
411 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
413 public function testDatastoreIdsNothingToDo()
415 $refDB = new ReferenceLinkDB();
416 $refDB->write(self
::$testDatastore);
417 $linkDB = new LinkDB(self
::$testDatastore, true, false);
419 $this->conf
->set('resource.data_dir', 'sandbox');
420 $this->conf
->set('resource.datastore', self
::$testDatastore);
422 $checksum = hash_file('sha1', self
::$testDatastore);
423 $updater = new Updater(array(), $linkDB, $this->conf
, true);
424 $this->assertTrue($updater->updateMethodDatastoreIds());
425 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));
429 * Test defaultTheme update with default settings: nothing to do.
431 public function testDefaultThemeWithDefaultSettings()
433 $sandbox = 'sandbox/config';
434 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
435 $this->conf
= new ConfigManager($sandbox);
436 $updater = new Updater([], [], $this->conf
, true);
437 $this->assertTrue($updater->updateMethodDefaultTheme());
439 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
440 $this->assertEquals('default', $this->conf
->get('resource.theme'));
441 $this->conf
= new ConfigManager($sandbox);
442 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
443 $this->assertEquals('default', $this->conf
->get('resource.theme'));
444 unlink($sandbox . '.json.php');
448 * Test defaultTheme update with a custom theme in a subfolder
450 public function testDefaultThemeWithCustomTheme()
452 $theme = 'iamanartist';
453 $sandbox = 'sandbox/config';
454 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
455 $this->conf
= new ConfigManager($sandbox);
456 mkdir('sandbox/'. $theme);
457 touch('sandbox/'. $theme .'/linklist.html');
458 $this->conf
->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
459 $updater = new Updater([], [], $this->conf
, true);
460 $this->assertTrue($updater->updateMethodDefaultTheme());
462 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
463 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
464 $this->conf
= new ConfigManager($sandbox);
465 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
466 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
467 unlink($sandbox . '.json.php');
468 unlink('sandbox/'. $theme .'/linklist.html');
469 rmdir('sandbox/'. $theme);
473 * Test updateMethodEscapeMarkdown with markdown plugin enabled
474 * => setting markdown_escape set to false.
476 public function testEscapeMarkdownSettingToFalse()
478 $sandboxConf = 'sandbox/config';
479 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
480 $this->conf
= new ConfigManager($sandboxConf);
482 $this->conf
->set('general.enabled_plugins', ['markdown']);
483 $updater = new Updater([], [], $this->conf
, true);
484 $this->assertTrue($updater->updateMethodEscapeMarkdown());
485 $this->assertFalse($this->conf
->get('security.markdown_escape'));
488 $this->conf
= new ConfigManager($sandboxConf);
489 $this->assertFalse($this->conf
->get('security.markdown_escape'));
494 * Test updateMethodEscapeMarkdown with markdown plugin disabled
495 * => setting markdown_escape set to true.
497 public function testEscapeMarkdownSettingToTrue()
499 $sandboxConf = 'sandbox/config';
500 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
501 $this->conf
= new ConfigManager($sandboxConf);
503 $this->conf
->set('general.enabled_plugins', []);
504 $updater = new Updater([], [], $this->conf
, true);
505 $this->assertTrue($updater->updateMethodEscapeMarkdown());
506 $this->assertTrue($this->conf
->get('security.markdown_escape'));
509 $this->conf
= new ConfigManager($sandboxConf);
510 $this->assertTrue($this->conf
->get('security.markdown_escape'));
514 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
516 public function testEscapeMarkdownSettingNothingToDoEnabled()
518 $sandboxConf = 'sandbox/config';
519 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
520 $this->conf
= new ConfigManager($sandboxConf);
521 $this->conf
->set('security.markdown_escape', true);
522 $updater = new Updater([], [], $this->conf
, true);
523 $this->assertTrue($updater->updateMethodEscapeMarkdown());
524 $this->assertTrue($this->conf
->get('security.markdown_escape'));
528 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
530 public function testEscapeMarkdownSettingNothingToDoDisabled()
532 $this->conf
->set('security.markdown_escape', false);
533 $updater = new Updater([], [], $this->conf
, true);
534 $this->assertTrue($updater->updateMethodEscapeMarkdown());
535 $this->assertFalse($this->conf
->get('security.markdown_escape'));
539 * Test updateMethodPiwikUrl with valid data
541 public function testUpdatePiwikUrlValid()
543 $sandboxConf = 'sandbox/config';
544 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
545 $this->conf
= new ConfigManager($sandboxConf);
546 $url = 'mypiwik.tld';
547 $this->conf
->set('plugins.PIWIK_URL', $url);
548 $updater = new Updater([], [], $this->conf
, true);
549 $this->assertTrue($updater->updateMethodPiwikUrl());
550 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
553 $this->conf
= new ConfigManager($sandboxConf);
554 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
558 * Test updateMethodPiwikUrl without setting
560 public function testUpdatePiwikUrlEmpty()
562 $updater = new Updater([], [], $this->conf
, true);
563 $this->assertTrue($updater->updateMethodPiwikUrl());
564 $this->assertEmpty($this->conf
->get('plugins.PIWIK_URL'));
568 * Test updateMethodPiwikUrl: valid URL, nothing to do
570 public function testUpdatePiwikUrlNothingToDo()
572 $url = 'https://mypiwik.tld';
573 $this->conf
->set('plugins.PIWIK_URL', $url);
574 $updater = new Updater([], [], $this->conf
, true);
575 $this->assertTrue($updater->updateMethodPiwikUrl());
576 $this->assertEquals($url, $this->conf
->get('plugins.PIWIK_URL'));
580 * Test updateMethodAtomDefault with show_atom set to false
583 public function testUpdateMethodAtomDefault()
585 $sandboxConf = 'sandbox/config';
586 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
587 $this->conf
= new ConfigManager($sandboxConf);
588 $this->conf
->set('feed.show_atom', false);
589 $updater = new Updater([], [], $this->conf
, true);
590 $this->assertTrue($updater->updateMethodAtomDefault());
591 $this->assertTrue($this->conf
->get('feed.show_atom'));
593 $this->conf
= new ConfigManager($sandboxConf);
594 $this->assertTrue($this->conf
->get('feed.show_atom'));
597 * Test updateMethodAtomDefault with show_atom not set.
600 public function testUpdateMethodAtomDefaultNoExist()
602 $sandboxConf = 'sandbox/config';
603 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
604 $this->conf
= new ConfigManager($sandboxConf);
605 $updater = new Updater([], [], $this->conf
, true);
606 $this->assertTrue($updater->updateMethodAtomDefault());
607 $this->assertTrue($this->conf
->get('feed.show_atom'));
610 * Test updateMethodAtomDefault with show_atom set to true.
613 public function testUpdateMethodAtomDefaultAlreadyTrue()
615 $sandboxConf = 'sandbox/config';
616 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
617 $this->conf
= new ConfigManager($sandboxConf);
618 $this->conf
->set('feed.show_atom', true);
619 $updater = new Updater([], [], $this->conf
, true);
620 $this->assertTrue($updater->updateMethodAtomDefault());
621 $this->assertTrue($this->conf
->get('feed.show_atom'));