2 namespace Shaarli\Updater
;
6 use Shaarli\Bookmark\Bookmark
;
7 use Shaarli\Config\ConfigJson
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Config\ConfigPhp
;
10 use Shaarli\Legacy\LegacyLinkDB
;
11 use Shaarli\Legacy\LegacyUpdater
;
12 use Shaarli\Thumbnailer
;
14 require_once 'application/updater/UpdaterUtils.php';
15 require_once 'tests/updater/DummyUpdater.php';
16 require_once 'tests/utils/ReferenceLinkDB.php';
17 require_once 'inc/rain.tpl.class.php';
21 * Runs unit tests against the updater class.
23 class LegacyUpdaterTest
extends \PHPUnit\Framework\TestCase
26 * @var string Path to test datastore.
28 protected static $testDatastore = 'sandbox/datastore.php';
31 * @var string Config file path (without extension).
33 protected static $configFile = 'sandbox/config';
41 * Executed before each test.
43 public function setUp()
45 copy('tests/utils/config/configJson.json.php', self
::$configFile .'.json.php');
46 $this->conf
= new ConfigManager(self
::$configFile);
50 * Test UpdaterUtils::read_updates_file with an empty/missing file.
52 public function testReadEmptyUpdatesFile()
54 $this->assertEquals(array(), UpdaterUtils
::read_updates_file(''));
55 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
57 $this->assertEquals(array(), UpdaterUtils
::read_updates_file($updatesFile));
62 * Test read/write updates file.
64 public function testReadWriteUpdatesFile()
66 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
67 $updatesMethods = array('m1', 'm2', 'm3');
69 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
70 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
71 $this->assertEquals($readMethods, $updatesMethods);
74 $updatesMethods[] = 'm4';
75 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
76 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
77 $this->assertEquals($readMethods, $updatesMethods);
82 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
84 * @expectedException Exception
85 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
87 public function testWriteEmptyUpdatesFile()
89 UpdaterUtils
::write_updates_file('', array('test'));
93 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
95 * @expectedException Exception
96 * @expectedExceptionMessageRegExp /Unable to write(.*)/
98 public function testWriteUpdatesFileNotWritable()
100 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
102 chmod($updatesFile, 0444);
104 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
105 } catch (Exception
$e) {
106 unlink($updatesFile);
112 * Test the update() method, with no update to run.
113 * 1. Everything already run.
114 * 2. User is logged out.
116 public function testNoUpdates()
119 'updateMethodDummy1',
120 'updateMethodDummy2',
121 'updateMethodDummy3',
122 'updateMethodException',
124 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
125 $this->assertEquals(array(), $updater->update());
127 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
128 $this->assertEquals(array(), $updater->update());
132 * Test the update() method, with all updates to run (except the failing one).
134 public function testUpdatesFirstTime()
136 $updates = array('updateMethodException',);
137 $expectedUpdates = array(
138 'updateMethodDummy1',
139 'updateMethodDummy2',
140 'updateMethodDummy3',
142 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
143 $this->assertEquals($expectedUpdates, $updater->update());
147 * Test the update() method, only one update to run.
149 public function testOneUpdate()
152 'updateMethodDummy1',
153 'updateMethodDummy3',
154 'updateMethodException',
156 $expectedUpdate = array('updateMethodDummy2');
158 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
159 $this->assertEquals($expectedUpdate, $updater->update());
163 * Test Update failed.
165 * @expectedException \Exception
167 public function testUpdateFailed()
170 'updateMethodDummy1',
171 'updateMethodDummy2',
172 'updateMethodDummy3',
175 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
180 * Test update mergeDeprecatedConfig:
181 * 1. init a config file.
182 * 2. init a options.php file with update value.
184 * 4. check updated value in config file.
186 public function testUpdateMergeDeprecatedConfig()
188 $this->conf
->setConfigFile('tests/utils/config/configPhp');
189 $this->conf
->reset();
191 $optionsFile = 'tests/updater/options.php';
193 $GLOBALS[\'privateLinkByDefault\'] = true;';
194 file_put_contents($optionsFile, $options);
197 $this->conf
->setConfigFile('tests/updater/config');
200 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
201 // This writes a new config file in tests/updater/config.php
202 $updater->updateMethodMergeDeprecatedConfigFile();
204 // make sure updated field is changed
205 $this->conf
->reload();
206 $this->assertTrue($this->conf
->get('privacy.default_private_links'));
207 $this->assertFalse(is_file($optionsFile));
208 // Delete the generated file.
209 unlink($this->conf
->getConfigFileExt());
213 * Test mergeDeprecatedConfig in without options file.
215 public function testMergeDeprecatedConfigNoFile()
217 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
218 $updater->updateMethodMergeDeprecatedConfigFile();
220 $this->assertEquals('root', $this->conf
->get('credentials.login'));
224 * Test renameDashTags update method.
226 public function testRenameDashTags()
228 $refDB = new \
ReferenceLinkDB(true);
229 $refDB->write(self
::$testDatastore);
230 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
232 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
233 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
234 $updater->updateMethodRenameDashTags();
235 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
239 * Convert old PHP config file to JSON config.
241 public function testConfigToJson()
243 $configFile = 'tests/utils/config/configPhp';
244 $this->conf
->setConfigFile($configFile);
245 $this->conf
->reset();
247 // The ConfigIO is initialized with ConfigPhp.
248 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
250 $updater = new LegacyUpdater(array(), array(), $this->conf
, false);
251 $done = $updater->updateMethodConfigToJson();
252 $this->assertTrue($done);
254 // The ConfigIO has been updated to ConfigJson.
255 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
256 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
258 // Check JSON config data.
259 $this->conf
->reload();
260 $this->assertEquals('root', $this->conf
->get('credentials.login'));
261 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
262 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
263 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
265 rename($configFile . '.save.php', $configFile . '.php');
266 unlink($this->conf
->getConfigFileExt());
270 * Launch config conversion update with an existing JSON file => nothing to do.
272 public function testConfigToJsonNothingToDo()
274 $filetime = filemtime($this->conf
->getConfigFileExt());
275 $updater = new LegacyUpdater(array(), array(), $this->conf
, false);
276 $done = $updater->updateMethodConfigToJson();
277 $this->assertTrue($done);
278 $expected = filemtime($this->conf
->getConfigFileExt());
279 $this->assertEquals($expected, $filetime);
283 * Test escapeUnescapedConfig with valid data.
285 public function testEscapeConfig()
287 $sandbox = 'sandbox/config';
288 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
289 $this->conf
= new ConfigManager($sandbox);
290 $title = '<script>alert("title");</script>';
291 $headerLink = '<script>alert("header_link");</script>';
292 $this->conf
->set('general.title', $title);
293 $this->conf
->set('general.header_link', $headerLink);
294 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
295 $done = $updater->updateMethodEscapeUnescapedConfig();
296 $this->assertTrue($done);
297 $this->conf
->reload();
298 $this->assertEquals(escape($title), $this->conf
->get('general.title'));
299 $this->assertEquals(escape($headerLink), $this->conf
->get('general.header_link'));
300 unlink($sandbox . '.json.php');
304 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
306 public function testUpdateApiSettings()
308 $confFile = 'sandbox/config';
309 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
310 $conf = new ConfigManager($confFile);
311 $updater = new LegacyUpdater(array(), array(), $conf, true);
313 $this->assertFalse($conf->exists('api.enabled'));
314 $this->assertFalse($conf->exists('api.secret'));
315 $updater->updateMethodApiSettings();
317 $this->assertTrue($conf->get('api.enabled'));
318 $this->assertTrue($conf->exists('api.secret'));
319 unlink($confFile .'.json.php');
323 * Test updateMethodApiSettings(): already set, do nothing.
325 public function testUpdateApiSettingsNothingToDo()
327 $confFile = 'sandbox/config';
328 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
329 $conf = new ConfigManager($confFile);
330 $conf->set('api.enabled', false);
331 $conf->set('api.secret', '');
332 $updater = new LegacyUpdater(array(), array(), $conf, true);
333 $updater->updateMethodApiSettings();
334 $this->assertFalse($conf->get('api.enabled'));
335 $this->assertEmpty($conf->get('api.secret'));
336 unlink($confFile .'.json.php');
340 * Test updateMethodDatastoreIds().
342 public function testDatastoreIds()
345 '20121206_182539' => array(
346 'linkdate' => '20121206_182539',
347 'title' => 'Geek and Poke',
348 'url' => 'http://geek-and-poke.com/',
349 'description' => 'desc',
350 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
351 'updated' => '20121206_190301',
354 '20121206_172539' => array(
355 'linkdate' => '20121206_172539',
356 'title' => 'UserFriendly - Samba',
357 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
359 'tags' => 'samba cartoon web',
362 '20121206_142300' => array(
363 'linkdate' => '20121206_142300',
364 'title' => 'UserFriendly - Web Designer',
365 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
366 'description' => 'Naming conventions... #private',
367 'tags' => 'samba cartoon web',
371 $refDB = new \
ReferenceLinkDB(true);
372 $refDB->setLinks($links);
373 $refDB->write(self
::$testDatastore);
374 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
376 $checksum = hash_file('sha1', self
::$testDatastore);
378 $this->conf
->set('resource.data_dir', 'sandbox');
379 $this->conf
->set('resource.datastore', self
::$testDatastore);
381 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
382 $this->assertTrue($updater->updateMethodDatastoreIds());
384 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
386 $backupFiles = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
388 foreach ($backupFiles as $backupFile) {
389 if (strpos($backupFile, '_1') === false) {
390 $backup = $backupFile;
393 $this->assertNotNull($backup);
394 $this->assertFileExists($backup);
395 $this->assertEquals($checksum, hash_file('sha1', $backup));
398 $this->assertEquals(3, count($linkDB));
399 $this->assertTrue(isset($linkDB[0]));
400 $this->assertFalse(isset($linkDB[0]['linkdate']));
401 $this->assertEquals(0, $linkDB[0]['id']);
402 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
403 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
404 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
405 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
406 $this->assertTrue($linkDB[0]['private']);
408 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_142300'),
409 $linkDB[0]['created']
412 $this->assertTrue(isset($linkDB[1]));
413 $this->assertFalse(isset($linkDB[1]['linkdate']));
414 $this->assertEquals(1, $linkDB[1]['id']);
415 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
417 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_172539'),
418 $linkDB[1]['created']
421 $this->assertTrue(isset($linkDB[2]));
422 $this->assertFalse(isset($linkDB[2]['linkdate']));
423 $this->assertEquals(2, $linkDB[2]['id']);
424 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
426 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_182539'),
427 $linkDB[2]['created']
430 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_190301'),
431 $linkDB[2]['updated']
436 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
438 public function testDatastoreIdsNothingToDo()
440 $refDB = new \
ReferenceLinkDB(true);
441 $refDB->write(self
::$testDatastore);
442 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
444 $this->conf
->set('resource.data_dir', 'sandbox');
445 $this->conf
->set('resource.datastore', self
::$testDatastore);
447 $checksum = hash_file('sha1', self
::$testDatastore);
448 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
449 $this->assertTrue($updater->updateMethodDatastoreIds());
450 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));
454 * Test defaultTheme update with default settings: nothing to do.
456 public function testDefaultThemeWithDefaultSettings()
458 $sandbox = 'sandbox/config';
459 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
460 $this->conf
= new ConfigManager($sandbox);
461 $updater = new LegacyUpdater([], [], $this->conf
, true);
462 $this->assertTrue($updater->updateMethodDefaultTheme());
464 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
465 $this->assertEquals('default', $this->conf
->get('resource.theme'));
466 $this->conf
= new ConfigManager($sandbox);
467 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
468 $this->assertEquals('default', $this->conf
->get('resource.theme'));
469 unlink($sandbox . '.json.php');
473 * Test defaultTheme update with a custom theme in a subfolder
475 public function testDefaultThemeWithCustomTheme()
477 $theme = 'iamanartist';
478 $sandbox = 'sandbox/config';
479 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
480 $this->conf
= new ConfigManager($sandbox);
481 mkdir('sandbox/'. $theme);
482 touch('sandbox/'. $theme .'/linklist.html');
483 $this->conf
->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
484 $updater = new LegacyUpdater([], [], $this->conf
, true);
485 $this->assertTrue($updater->updateMethodDefaultTheme());
487 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
488 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
489 $this->conf
= new ConfigManager($sandbox);
490 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
491 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
492 unlink($sandbox . '.json.php');
493 unlink('sandbox/'. $theme .'/linklist.html');
494 rmdir('sandbox/'. $theme);
498 * Test updateMethodEscapeMarkdown with markdown plugin enabled
499 * => setting markdown_escape set to false.
501 public function testEscapeMarkdownSettingToFalse()
503 $sandboxConf = 'sandbox/config';
504 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
505 $this->conf
= new ConfigManager($sandboxConf);
507 $this->conf
->set('general.enabled_plugins', ['markdown']);
508 $updater = new LegacyUpdater([], [], $this->conf
, true);
509 $this->assertTrue($updater->updateMethodEscapeMarkdown());
510 $this->assertFalse($this->conf
->get('security.markdown_escape'));
513 $this->conf
= new ConfigManager($sandboxConf);
514 $this->assertFalse($this->conf
->get('security.markdown_escape'));
519 * Test updateMethodEscapeMarkdown with markdown plugin disabled
520 * => setting markdown_escape set to true.
522 public function testEscapeMarkdownSettingToTrue()
524 $sandboxConf = 'sandbox/config';
525 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
526 $this->conf
= new ConfigManager($sandboxConf);
528 $this->conf
->set('general.enabled_plugins', []);
529 $updater = new LegacyUpdater([], [], $this->conf
, true);
530 $this->assertTrue($updater->updateMethodEscapeMarkdown());
531 $this->assertTrue($this->conf
->get('security.markdown_escape'));
534 $this->conf
= new ConfigManager($sandboxConf);
535 $this->assertTrue($this->conf
->get('security.markdown_escape'));
539 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
541 public function testEscapeMarkdownSettingNothingToDoEnabled()
543 $sandboxConf = 'sandbox/config';
544 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
545 $this->conf
= new ConfigManager($sandboxConf);
546 $this->conf
->set('security.markdown_escape', true);
547 $updater = new LegacyUpdater([], [], $this->conf
, true);
548 $this->assertTrue($updater->updateMethodEscapeMarkdown());
549 $this->assertTrue($this->conf
->get('security.markdown_escape'));
553 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
555 public function testEscapeMarkdownSettingNothingToDoDisabled()
557 $this->conf
->set('security.markdown_escape', false);
558 $updater = new LegacyUpdater([], [], $this->conf
, true);
559 $this->assertTrue($updater->updateMethodEscapeMarkdown());
560 $this->assertFalse($this->conf
->get('security.markdown_escape'));
564 * Test updateMethodPiwikUrl with valid data
566 public function testUpdatePiwikUrlValid()
568 $sandboxConf = 'sandbox/config';
569 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
570 $this->conf
= new ConfigManager($sandboxConf);
571 $url = 'mypiwik.tld';
572 $this->conf
->set('plugins.PIWIK_URL', $url);
573 $updater = new LegacyUpdater([], [], $this->conf
, true);
574 $this->assertTrue($updater->updateMethodPiwikUrl());
575 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
578 $this->conf
= new ConfigManager($sandboxConf);
579 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
583 * Test updateMethodPiwikUrl without setting
585 public function testUpdatePiwikUrlEmpty()
587 $updater = new LegacyUpdater([], [], $this->conf
, true);
588 $this->assertTrue($updater->updateMethodPiwikUrl());
589 $this->assertEmpty($this->conf
->get('plugins.PIWIK_URL'));
593 * Test updateMethodPiwikUrl: valid URL, nothing to do
595 public function testUpdatePiwikUrlNothingToDo()
597 $url = 'https://mypiwik.tld';
598 $this->conf
->set('plugins.PIWIK_URL', $url);
599 $updater = new LegacyUpdater([], [], $this->conf
, true);
600 $this->assertTrue($updater->updateMethodPiwikUrl());
601 $this->assertEquals($url, $this->conf
->get('plugins.PIWIK_URL'));
605 * Test updateMethodAtomDefault with show_atom set to false
608 public function testUpdateMethodAtomDefault()
610 $sandboxConf = 'sandbox/config';
611 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
612 $this->conf
= new ConfigManager($sandboxConf);
613 $this->conf
->set('feed.show_atom', false);
614 $updater = new LegacyUpdater([], [], $this->conf
, true);
615 $this->assertTrue($updater->updateMethodAtomDefault());
616 $this->assertTrue($this->conf
->get('feed.show_atom'));
618 $this->conf
= new ConfigManager($sandboxConf);
619 $this->assertTrue($this->conf
->get('feed.show_atom'));
622 * Test updateMethodAtomDefault with show_atom not set.
625 public function testUpdateMethodAtomDefaultNoExist()
627 $sandboxConf = 'sandbox/config';
628 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
629 $this->conf
= new ConfigManager($sandboxConf);
630 $updater = new LegacyUpdater([], [], $this->conf
, true);
631 $this->assertTrue($updater->updateMethodAtomDefault());
632 $this->assertTrue($this->conf
->get('feed.show_atom'));
635 * Test updateMethodAtomDefault with show_atom set to true.
638 public function testUpdateMethodAtomDefaultAlreadyTrue()
640 $sandboxConf = 'sandbox/config';
641 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
642 $this->conf
= new ConfigManager($sandboxConf);
643 $this->conf
->set('feed.show_atom', true);
644 $updater = new LegacyUpdater([], [], $this->conf
, true);
645 $this->assertTrue($updater->updateMethodAtomDefault());
646 $this->assertTrue($this->conf
->get('feed.show_atom'));
650 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
652 public function testUpdateMethodDownloadSizeAndTimeoutConf()
654 $sandboxConf = 'sandbox/config';
655 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
656 $this->conf
= new ConfigManager($sandboxConf);
657 $updater = new LegacyUpdater([], [], $this->conf
, true);
658 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
659 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
660 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
662 $this->conf
= new ConfigManager($sandboxConf);
663 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
664 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
668 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
670 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
672 $sandboxConf = 'sandbox/config';
673 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
674 $this->conf
= new ConfigManager($sandboxConf);
675 $this->conf
->set('general.download_max_size', 38);
676 $this->conf
->set('general.download_timeout', 70);
677 $updater = new LegacyUpdater([], [], $this->conf
, true);
678 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
679 $this->assertEquals(38, $this->conf
->get('general.download_max_size'));
680 $this->assertEquals(70, $this->conf
->get('general.download_timeout'));
684 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
686 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
688 $sandboxConf = 'sandbox/config';
689 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
690 $this->conf
= new ConfigManager($sandboxConf);
691 $this->conf
->set('general.download_max_size', 38);
692 $updater = new LegacyUpdater([], [], $this->conf
, true);
693 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
694 $this->assertEquals(38, $this->conf
->get('general.download_max_size'));
695 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
699 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
701 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
703 $sandboxConf = 'sandbox/config';
704 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
705 $this->conf
= new ConfigManager($sandboxConf);
706 $this->conf
->set('general.download_timeout', 3);
707 $updater = new LegacyUpdater([], [], $this->conf
, true);
708 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
709 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
710 $this->assertEquals(3, $this->conf
->get('general.download_timeout'));
714 * Test updateMethodWebThumbnailer with thumbnails enabled.
716 public function testUpdateMethodWebThumbnailerEnabled()
718 $this->conf
->remove('thumbnails');
719 $this->conf
->set('thumbnail.enable_thumbnails', true);
720 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
721 $this->assertTrue($updater->updateMethodWebThumbnailer());
722 $this->assertFalse($this->conf
->exists('thumbnail'));
723 $this->assertEquals(\Shaarli\Thumbnailer
::MODE_ALL
, $this->conf
->get('thumbnails.mode'));
724 $this->assertEquals(125, $this->conf
->get('thumbnails.width'));
725 $this->assertEquals(90, $this->conf
->get('thumbnails.height'));
726 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
730 * Test updateMethodWebThumbnailer with thumbnails disabled.
732 public function testUpdateMethodWebThumbnailerDisabled()
734 if (isset($_SESSION['warnings'])) {
735 unset($_SESSION['warnings']);
738 $this->conf
->remove('thumbnails');
739 $this->conf
->set('thumbnail.enable_thumbnails', false);
740 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
741 $this->assertTrue($updater->updateMethodWebThumbnailer());
742 $this->assertFalse($this->conf
->exists('thumbnail'));
743 $this->assertEquals(Thumbnailer
::MODE_NONE
, $this->conf
->get('thumbnails.mode'));
744 $this->assertEquals(125, $this->conf
->get('thumbnails.width'));
745 $this->assertEquals(90, $this->conf
->get('thumbnails.height'));
746 $this->assertTrue(empty($_SESSION['warnings']));
750 * Test updateMethodWebThumbnailer with thumbnails disabled.
752 public function testUpdateMethodWebThumbnailerNothingToDo()
754 if (isset($_SESSION['warnings'])) {
755 unset($_SESSION['warnings']);
758 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
759 $this->assertTrue($updater->updateMethodWebThumbnailer());
760 $this->assertFalse($this->conf
->exists('thumbnail'));
761 $this->assertEquals(Thumbnailer
::MODE_COMMON
, $this->conf
->get('thumbnails.mode'));
762 $this->assertEquals(90, $this->conf
->get('thumbnails.width'));
763 $this->assertEquals(53, $this->conf
->get('thumbnails.height'));
764 $this->assertTrue(empty($_SESSION['warnings']));
768 * Test updateMethodSetSticky().
770 public function testUpdateStickyValid()
778 'created' => new DateTime(),
781 1 => ['id' => 1] +
$blank,
782 2 => ['id' => 2] +
$blank,
784 $refDB = new \
ReferenceLinkDB(true);
785 $refDB->setLinks($links);
786 $refDB->write(self
::$testDatastore);
787 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
789 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
790 $this->assertTrue($updater->updateMethodSetSticky());
792 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
793 foreach ($linkDB as $link) {
794 $this->assertFalse($link['sticky']);
799 * Test updateMethodSetSticky().
801 public function testUpdateStickyNothingToDo()
809 'created' => new DateTime(),
812 1 => ['id' => 1, 'sticky' => true] +
$blank,
813 2 => ['id' => 2] +
$blank,
815 $refDB = new \
ReferenceLinkDB(true);
816 $refDB->setLinks($links);
817 $refDB->write(self
::$testDatastore);
818 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
820 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
821 $this->assertTrue($updater->updateMethodSetSticky());
823 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
824 $this->assertTrue($linkDB[1]['sticky']);
828 * Test updateMethodRemoveRedirector().
830 public function testUpdateRemoveRedirector()
832 $sandboxConf = 'sandbox/config';
833 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
834 $this->conf
= new ConfigManager($sandboxConf);
835 $updater = new LegacyUpdater([], null, $this->conf
, true);
836 $this->assertTrue($updater->updateMethodRemoveRedirector());
837 $this->assertFalse($this->conf
->exists('redirector'));
838 $this->conf
= new ConfigManager($sandboxConf);
839 $this->assertFalse($this->conf
->exists('redirector'));
843 * Test updateMethodFormatterSetting()
845 public function testUpdateMethodFormatterSettingDefault()
847 $sandboxConf = 'sandbox/config';
848 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
849 $this->conf
= new ConfigManager($sandboxConf);
850 $this->conf
->set('formatter', 'default');
851 $updater = new LegacyUpdater([], null, $this->conf
, true);
852 $enabledPlugins = $this->conf
->get('general.enabled_plugins');
853 $this->assertFalse(in_array('markdown', $enabledPlugins));
854 $this->assertTrue($updater->updateMethodFormatterSetting());
855 $this->assertEquals('default', $this->conf
->get('formatter'));
856 $this->assertEquals($enabledPlugins, $this->conf
->get('general.enabled_plugins'));
858 $this->conf
= new ConfigManager($sandboxConf);
859 $this->assertEquals('default', $this->conf
->get('formatter'));
860 $this->assertEquals($enabledPlugins, $this->conf
->get('general.enabled_plugins'));
864 * Test updateMethodFormatterSetting()
866 public function testUpdateMethodFormatterSettingMarkdown()
868 $sandboxConf = 'sandbox/config';
869 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
870 $this->conf
= new ConfigManager($sandboxConf);
871 $this->conf
->set('formatter', 'default');
872 $updater = new LegacyUpdater([], null, $this->conf
, true);
873 $enabledPlugins = $this->conf
->get('general.enabled_plugins');
874 $enabledPlugins[] = 'markdown';
875 $this->conf
->set('general.enabled_plugins', $enabledPlugins);
877 $this->assertTrue(in_array('markdown', $this->conf
->get('general.enabled_plugins')));
878 $this->assertTrue($updater->updateMethodFormatterSetting());
879 $this->assertEquals('markdown', $this->conf
->get('formatter'));
880 $this->assertFalse(in_array('markdown', $this->conf
->get('general.enabled_plugins')));
882 $this->conf
= new ConfigManager($sandboxConf);
883 $this->assertEquals('markdown', $this->conf
->get('formatter'));
884 $this->assertFalse(in_array('markdown', $this->conf
->get('general.enabled_plugins')));