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 protected function setUp(): void
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
86 public function testWriteEmptyUpdatesFile()
88 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
90 UpdaterUtils
::write_updates_file('', array('test'));
94 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
96 * @expectedException Exception
98 public function testWriteUpdatesFileNotWritable()
100 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
102 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
104 chmod($updatesFile, 0444);
106 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
107 } catch (Exception
$e) {
108 unlink($updatesFile);
114 * Test the update() method, with no update to run.
115 * 1. Everything already run.
116 * 2. User is logged out.
118 public function testNoUpdates()
121 'updateMethodDummy1',
122 'updateMethodDummy2',
123 'updateMethodDummy3',
124 'updateMethodException',
126 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
127 $this->assertEquals(array(), $updater->update());
129 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
130 $this->assertEquals(array(), $updater->update());
134 * Test the update() method, with all updates to run (except the failing one).
136 public function testUpdatesFirstTime()
138 $updates = array('updateMethodException',);
139 $expectedUpdates = array(
140 'updateMethodDummy1',
141 'updateMethodDummy2',
142 'updateMethodDummy3',
144 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
145 $this->assertEquals($expectedUpdates, $updater->update());
149 * Test the update() method, only one update to run.
151 public function testOneUpdate()
154 'updateMethodDummy1',
155 'updateMethodDummy3',
156 'updateMethodException',
158 $expectedUpdate = array('updateMethodDummy2');
160 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
161 $this->assertEquals($expectedUpdate, $updater->update());
165 * Test Update failed.
167 public function testUpdateFailed()
169 $this->expectException(\Exception
::class);
172 'updateMethodDummy1',
173 'updateMethodDummy2',
174 'updateMethodDummy3',
177 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
182 * Test update mergeDeprecatedConfig:
183 * 1. init a config file.
184 * 2. init a options.php file with update value.
186 * 4. check updated value in config file.
188 public function testUpdateMergeDeprecatedConfig()
190 $this->conf
->setConfigFile('tests/utils/config/configPhp');
191 $this->conf
->reset();
193 $optionsFile = 'tests/updater/options.php';
195 $GLOBALS[\'privateLinkByDefault\'] = true;';
196 file_put_contents($optionsFile, $options);
199 $this->conf
->setConfigFile('tests/updater/config');
202 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
203 // This writes a new config file in tests/updater/config.php
204 $updater->updateMethodMergeDeprecatedConfigFile();
206 // make sure updated field is changed
207 $this->conf
->reload();
208 $this->assertTrue($this->conf
->get('privacy.default_private_links'));
209 $this->assertFalse(is_file($optionsFile));
210 // Delete the generated file.
211 unlink($this->conf
->getConfigFileExt());
215 * Test mergeDeprecatedConfig in without options file.
217 public function testMergeDeprecatedConfigNoFile()
219 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
220 $updater->updateMethodMergeDeprecatedConfigFile();
222 $this->assertEquals('root', $this->conf
->get('credentials.login'));
226 * Test renameDashTags update method.
228 public function testRenameDashTags()
230 $refDB = new \
ReferenceLinkDB(true);
231 $refDB->write(self
::$testDatastore);
232 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
234 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
235 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
236 $updater->updateMethodRenameDashTags();
237 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
241 * Convert old PHP config file to JSON config.
243 public function testConfigToJson()
245 $configFile = 'tests/utils/config/configPhp';
246 $this->conf
->setConfigFile($configFile);
247 $this->conf
->reset();
249 // The ConfigIO is initialized with ConfigPhp.
250 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigPhp
);
252 $updater = new LegacyUpdater(array(), array(), $this->conf
, false);
253 $done = $updater->updateMethodConfigToJson();
254 $this->assertTrue($done);
256 // The ConfigIO has been updated to ConfigJson.
257 $this->assertTrue($this->conf
->getConfigIO() instanceof ConfigJson
);
258 $this->assertTrue(file_exists($this->conf
->getConfigFileExt()));
260 // Check JSON config data.
261 $this->conf
->reload();
262 $this->assertEquals('root', $this->conf
->get('credentials.login'));
263 $this->assertEquals('lala', $this->conf
->get('redirector.url'));
264 $this->assertEquals('data/datastore.php', $this->conf
->get('resource.datastore'));
265 $this->assertEquals('1', $this->conf
->get('plugins.WALLABAG_VERSION'));
267 rename($configFile . '.save.php', $configFile . '.php');
268 unlink($this->conf
->getConfigFileExt());
272 * Launch config conversion update with an existing JSON file => nothing to do.
274 public function testConfigToJsonNothingToDo()
276 $filetime = filemtime($this->conf
->getConfigFileExt());
277 $updater = new LegacyUpdater(array(), array(), $this->conf
, false);
278 $done = $updater->updateMethodConfigToJson();
279 $this->assertTrue($done);
280 $expected = filemtime($this->conf
->getConfigFileExt());
281 $this->assertEquals($expected, $filetime);
285 * Test escapeUnescapedConfig with valid data.
287 public function testEscapeConfig()
289 $sandbox = 'sandbox/config';
290 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
291 $this->conf
= new ConfigManager($sandbox);
292 $title = '<script>alert("title");</script>';
293 $headerLink = '<script>alert("header_link");</script>';
294 $this->conf
->set('general.title', $title);
295 $this->conf
->set('general.header_link', $headerLink);
296 $updater = new LegacyUpdater(array(), array(), $this->conf
, true);
297 $done = $updater->updateMethodEscapeUnescapedConfig();
298 $this->assertTrue($done);
299 $this->conf
->reload();
300 $this->assertEquals(escape($title), $this->conf
->get('general.title'));
301 $this->assertEquals(escape($headerLink), $this->conf
->get('general.header_link'));
302 unlink($sandbox . '.json.php');
306 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
308 public function testUpdateApiSettings()
310 $confFile = 'sandbox/config';
311 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
312 $conf = new ConfigManager($confFile);
313 $updater = new LegacyUpdater(array(), array(), $conf, true);
315 $this->assertFalse($conf->exists('api.enabled'));
316 $this->assertFalse($conf->exists('api.secret'));
317 $updater->updateMethodApiSettings();
319 $this->assertTrue($conf->get('api.enabled'));
320 $this->assertTrue($conf->exists('api.secret'));
321 unlink($confFile .'.json.php');
325 * Test updateMethodApiSettings(): already set, do nothing.
327 public function testUpdateApiSettingsNothingToDo()
329 $confFile = 'sandbox/config';
330 copy(self
::$configFile .'.json.php', $confFile .'.json.php');
331 $conf = new ConfigManager($confFile);
332 $conf->set('api.enabled', false);
333 $conf->set('api.secret', '');
334 $updater = new LegacyUpdater(array(), array(), $conf, true);
335 $updater->updateMethodApiSettings();
336 $this->assertFalse($conf->get('api.enabled'));
337 $this->assertEmpty($conf->get('api.secret'));
338 unlink($confFile .'.json.php');
342 * Test updateMethodDatastoreIds().
344 public function testDatastoreIds()
347 '20121206_182539' => array(
348 'linkdate' => '20121206_182539',
349 'title' => 'Geek and Poke',
350 'url' => 'http://geek-and-poke.com/',
351 'description' => 'desc',
352 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
353 'updated' => '20121206_190301',
356 '20121206_172539' => array(
357 'linkdate' => '20121206_172539',
358 'title' => 'UserFriendly - Samba',
359 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
361 'tags' => 'samba cartoon web',
364 '20121206_142300' => array(
365 'linkdate' => '20121206_142300',
366 'title' => 'UserFriendly - Web Designer',
367 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
368 'description' => 'Naming conventions... #private',
369 'tags' => 'samba cartoon web',
373 $refDB = new \
ReferenceLinkDB(true);
374 $refDB->setLinks($links);
375 $refDB->write(self
::$testDatastore);
376 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
378 $checksum = hash_file('sha1', self
::$testDatastore);
380 $this->conf
->set('resource.data_dir', 'sandbox');
381 $this->conf
->set('resource.datastore', self
::$testDatastore);
383 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
384 $this->assertTrue($updater->updateMethodDatastoreIds());
386 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
388 $backupFiles = glob($this->conf
->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
390 foreach ($backupFiles as $backupFile) {
391 if (strpos($backupFile, '_1') === false) {
392 $backup = $backupFile;
395 $this->assertNotNull($backup);
396 $this->assertFileExists($backup);
397 $this->assertEquals($checksum, hash_file('sha1', $backup));
400 $this->assertEquals(3, count($linkDB));
401 $this->assertTrue(isset($linkDB[0]));
402 $this->assertFalse(isset($linkDB[0]['linkdate']));
403 $this->assertEquals(0, $linkDB[0]['id']);
404 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
405 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
406 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
407 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
408 $this->assertTrue($linkDB[0]['private']);
410 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_142300'),
411 $linkDB[0]['created']
414 $this->assertTrue(isset($linkDB[1]));
415 $this->assertFalse(isset($linkDB[1]['linkdate']));
416 $this->assertEquals(1, $linkDB[1]['id']);
417 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
419 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_172539'),
420 $linkDB[1]['created']
423 $this->assertTrue(isset($linkDB[2]));
424 $this->assertFalse(isset($linkDB[2]['linkdate']));
425 $this->assertEquals(2, $linkDB[2]['id']);
426 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
428 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_182539'),
429 $linkDB[2]['created']
432 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20121206_190301'),
433 $linkDB[2]['updated']
438 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
440 public function testDatastoreIdsNothingToDo()
442 $refDB = new \
ReferenceLinkDB(true);
443 $refDB->write(self
::$testDatastore);
444 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
446 $this->conf
->set('resource.data_dir', 'sandbox');
447 $this->conf
->set('resource.datastore', self
::$testDatastore);
449 $checksum = hash_file('sha1', self
::$testDatastore);
450 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
451 $this->assertTrue($updater->updateMethodDatastoreIds());
452 $this->assertEquals($checksum, hash_file('sha1', self
::$testDatastore));
456 * Test defaultTheme update with default settings: nothing to do.
458 public function testDefaultThemeWithDefaultSettings()
460 $sandbox = 'sandbox/config';
461 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
462 $this->conf
= new ConfigManager($sandbox);
463 $updater = new LegacyUpdater([], [], $this->conf
, true);
464 $this->assertTrue($updater->updateMethodDefaultTheme());
466 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
467 $this->assertEquals('default', $this->conf
->get('resource.theme'));
468 $this->conf
= new ConfigManager($sandbox);
469 $this->assertEquals('tpl/', $this->conf
->get('resource.raintpl_tpl'));
470 $this->assertEquals('default', $this->conf
->get('resource.theme'));
471 unlink($sandbox . '.json.php');
475 * Test defaultTheme update with a custom theme in a subfolder
477 public function testDefaultThemeWithCustomTheme()
479 $theme = 'iamanartist';
480 $sandbox = 'sandbox/config';
481 copy(self
::$configFile . '.json.php', $sandbox . '.json.php');
482 $this->conf
= new ConfigManager($sandbox);
483 mkdir('sandbox/'. $theme);
484 touch('sandbox/'. $theme .'/linklist.html');
485 $this->conf
->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
486 $updater = new LegacyUpdater([], [], $this->conf
, true);
487 $this->assertTrue($updater->updateMethodDefaultTheme());
489 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
490 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
491 $this->conf
= new ConfigManager($sandbox);
492 $this->assertEquals('sandbox', $this->conf
->get('resource.raintpl_tpl'));
493 $this->assertEquals($theme, $this->conf
->get('resource.theme'));
494 unlink($sandbox . '.json.php');
495 unlink('sandbox/'. $theme .'/linklist.html');
496 rmdir('sandbox/'. $theme);
500 * Test updateMethodEscapeMarkdown with markdown plugin enabled
501 * => setting markdown_escape set to false.
503 public function testEscapeMarkdownSettingToFalse()
505 $sandboxConf = 'sandbox/config';
506 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
507 $this->conf
= new ConfigManager($sandboxConf);
509 $this->conf
->set('general.enabled_plugins', ['markdown']);
510 $updater = new LegacyUpdater([], [], $this->conf
, true);
511 $this->assertTrue($updater->updateMethodEscapeMarkdown());
512 $this->assertFalse($this->conf
->get('security.markdown_escape'));
515 $this->conf
= new ConfigManager($sandboxConf);
516 $this->assertFalse($this->conf
->get('security.markdown_escape'));
521 * Test updateMethodEscapeMarkdown with markdown plugin disabled
522 * => setting markdown_escape set to true.
524 public function testEscapeMarkdownSettingToTrue()
526 $sandboxConf = 'sandbox/config';
527 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
528 $this->conf
= new ConfigManager($sandboxConf);
530 $this->conf
->set('general.enabled_plugins', []);
531 $updater = new LegacyUpdater([], [], $this->conf
, true);
532 $this->assertTrue($updater->updateMethodEscapeMarkdown());
533 $this->assertTrue($this->conf
->get('security.markdown_escape'));
536 $this->conf
= new ConfigManager($sandboxConf);
537 $this->assertTrue($this->conf
->get('security.markdown_escape'));
541 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
543 public function testEscapeMarkdownSettingNothingToDoEnabled()
545 $sandboxConf = 'sandbox/config';
546 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
547 $this->conf
= new ConfigManager($sandboxConf);
548 $this->conf
->set('security.markdown_escape', true);
549 $updater = new LegacyUpdater([], [], $this->conf
, true);
550 $this->assertTrue($updater->updateMethodEscapeMarkdown());
551 $this->assertTrue($this->conf
->get('security.markdown_escape'));
555 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
557 public function testEscapeMarkdownSettingNothingToDoDisabled()
559 $this->conf
->set('security.markdown_escape', false);
560 $updater = new LegacyUpdater([], [], $this->conf
, true);
561 $this->assertTrue($updater->updateMethodEscapeMarkdown());
562 $this->assertFalse($this->conf
->get('security.markdown_escape'));
566 * Test updateMethodPiwikUrl with valid data
568 public function testUpdatePiwikUrlValid()
570 $sandboxConf = 'sandbox/config';
571 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
572 $this->conf
= new ConfigManager($sandboxConf);
573 $url = 'mypiwik.tld';
574 $this->conf
->set('plugins.PIWIK_URL', $url);
575 $updater = new LegacyUpdater([], [], $this->conf
, true);
576 $this->assertTrue($updater->updateMethodPiwikUrl());
577 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
580 $this->conf
= new ConfigManager($sandboxConf);
581 $this->assertEquals('http://'. $url, $this->conf
->get('plugins.PIWIK_URL'));
585 * Test updateMethodPiwikUrl without setting
587 public function testUpdatePiwikUrlEmpty()
589 $updater = new LegacyUpdater([], [], $this->conf
, true);
590 $this->assertTrue($updater->updateMethodPiwikUrl());
591 $this->assertEmpty($this->conf
->get('plugins.PIWIK_URL'));
595 * Test updateMethodPiwikUrl: valid URL, nothing to do
597 public function testUpdatePiwikUrlNothingToDo()
599 $url = 'https://mypiwik.tld';
600 $this->conf
->set('plugins.PIWIK_URL', $url);
601 $updater = new LegacyUpdater([], [], $this->conf
, true);
602 $this->assertTrue($updater->updateMethodPiwikUrl());
603 $this->assertEquals($url, $this->conf
->get('plugins.PIWIK_URL'));
607 * Test updateMethodAtomDefault with show_atom set to false
610 public function testUpdateMethodAtomDefault()
612 $sandboxConf = 'sandbox/config';
613 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
614 $this->conf
= new ConfigManager($sandboxConf);
615 $this->conf
->set('feed.show_atom', false);
616 $updater = new LegacyUpdater([], [], $this->conf
, true);
617 $this->assertTrue($updater->updateMethodAtomDefault());
618 $this->assertTrue($this->conf
->get('feed.show_atom'));
620 $this->conf
= new ConfigManager($sandboxConf);
621 $this->assertTrue($this->conf
->get('feed.show_atom'));
624 * Test updateMethodAtomDefault with show_atom not set.
627 public function testUpdateMethodAtomDefaultNoExist()
629 $sandboxConf = 'sandbox/config';
630 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
631 $this->conf
= new ConfigManager($sandboxConf);
632 $updater = new LegacyUpdater([], [], $this->conf
, true);
633 $this->assertTrue($updater->updateMethodAtomDefault());
634 $this->assertTrue($this->conf
->get('feed.show_atom'));
637 * Test updateMethodAtomDefault with show_atom set to true.
640 public function testUpdateMethodAtomDefaultAlreadyTrue()
642 $sandboxConf = 'sandbox/config';
643 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
644 $this->conf
= new ConfigManager($sandboxConf);
645 $this->conf
->set('feed.show_atom', true);
646 $updater = new LegacyUpdater([], [], $this->conf
, true);
647 $this->assertTrue($updater->updateMethodAtomDefault());
648 $this->assertTrue($this->conf
->get('feed.show_atom'));
652 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
654 public function testUpdateMethodDownloadSizeAndTimeoutConf()
656 $sandboxConf = 'sandbox/config';
657 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
658 $this->conf
= new ConfigManager($sandboxConf);
659 $updater = new LegacyUpdater([], [], $this->conf
, true);
660 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
661 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
662 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
664 $this->conf
= new ConfigManager($sandboxConf);
665 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
666 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
670 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
672 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
674 $sandboxConf = 'sandbox/config';
675 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
676 $this->conf
= new ConfigManager($sandboxConf);
677 $this->conf
->set('general.download_max_size', 38);
678 $this->conf
->set('general.download_timeout', 70);
679 $updater = new LegacyUpdater([], [], $this->conf
, true);
680 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
681 $this->assertEquals(38, $this->conf
->get('general.download_max_size'));
682 $this->assertEquals(70, $this->conf
->get('general.download_timeout'));
686 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
688 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
690 $sandboxConf = 'sandbox/config';
691 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
692 $this->conf
= new ConfigManager($sandboxConf);
693 $this->conf
->set('general.download_max_size', 38);
694 $updater = new LegacyUpdater([], [], $this->conf
, true);
695 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
696 $this->assertEquals(38, $this->conf
->get('general.download_max_size'));
697 $this->assertEquals(30, $this->conf
->get('general.download_timeout'));
701 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
703 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
705 $sandboxConf = 'sandbox/config';
706 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
707 $this->conf
= new ConfigManager($sandboxConf);
708 $this->conf
->set('general.download_timeout', 3);
709 $updater = new LegacyUpdater([], [], $this->conf
, true);
710 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
711 $this->assertEquals(4194304, $this->conf
->get('general.download_max_size'));
712 $this->assertEquals(3, $this->conf
->get('general.download_timeout'));
716 * Test updateMethodWebThumbnailer with thumbnails enabled.
718 public function testUpdateMethodWebThumbnailerEnabled()
720 $this->conf
->remove('thumbnails');
721 $this->conf
->set('thumbnail.enable_thumbnails', true);
722 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
723 $this->assertTrue($updater->updateMethodWebThumbnailer());
724 $this->assertFalse($this->conf
->exists('thumbnail'));
725 $this->assertEquals(\Shaarli\Thumbnailer
::MODE_ALL
, $this->conf
->get('thumbnails.mode'));
726 $this->assertEquals(125, $this->conf
->get('thumbnails.width'));
727 $this->assertEquals(90, $this->conf
->get('thumbnails.height'));
728 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
732 * Test updateMethodWebThumbnailer with thumbnails disabled.
734 public function testUpdateMethodWebThumbnailerDisabled()
736 if (isset($_SESSION['warnings'])) {
737 unset($_SESSION['warnings']);
740 $this->conf
->remove('thumbnails');
741 $this->conf
->set('thumbnail.enable_thumbnails', false);
742 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
743 $this->assertTrue($updater->updateMethodWebThumbnailer());
744 $this->assertFalse($this->conf
->exists('thumbnail'));
745 $this->assertEquals(Thumbnailer
::MODE_NONE
, $this->conf
->get('thumbnails.mode'));
746 $this->assertEquals(125, $this->conf
->get('thumbnails.width'));
747 $this->assertEquals(90, $this->conf
->get('thumbnails.height'));
748 $this->assertTrue(empty($_SESSION['warnings']));
752 * Test updateMethodWebThumbnailer with thumbnails disabled.
754 public function testUpdateMethodWebThumbnailerNothingToDo()
756 if (isset($_SESSION['warnings'])) {
757 unset($_SESSION['warnings']);
760 $updater = new LegacyUpdater([], [], $this->conf
, true, $_SESSION);
761 $this->assertTrue($updater->updateMethodWebThumbnailer());
762 $this->assertFalse($this->conf
->exists('thumbnail'));
763 $this->assertEquals(Thumbnailer
::MODE_COMMON
, $this->conf
->get('thumbnails.mode'));
764 $this->assertEquals(90, $this->conf
->get('thumbnails.width'));
765 $this->assertEquals(53, $this->conf
->get('thumbnails.height'));
766 $this->assertTrue(empty($_SESSION['warnings']));
770 * Test updateMethodSetSticky().
772 public function testUpdateStickyValid()
780 'created' => new DateTime(),
783 1 => ['id' => 1] +
$blank,
784 2 => ['id' => 2] +
$blank,
786 $refDB = new \
ReferenceLinkDB(true);
787 $refDB->setLinks($links);
788 $refDB->write(self
::$testDatastore);
789 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
791 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
792 $this->assertTrue($updater->updateMethodSetSticky());
794 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
795 foreach ($linkDB as $link) {
796 $this->assertFalse($link['sticky']);
801 * Test updateMethodSetSticky().
803 public function testUpdateStickyNothingToDo()
811 'created' => new DateTime(),
814 1 => ['id' => 1, 'sticky' => true] +
$blank,
815 2 => ['id' => 2] +
$blank,
817 $refDB = new \
ReferenceLinkDB(true);
818 $refDB->setLinks($links);
819 $refDB->write(self
::$testDatastore);
820 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
822 $updater = new LegacyUpdater(array(), $linkDB, $this->conf
, true);
823 $this->assertTrue($updater->updateMethodSetSticky());
825 $linkDB = new LegacyLinkDB(self
::$testDatastore, true, false);
826 $this->assertTrue($linkDB[1]['sticky']);
830 * Test updateMethodRemoveRedirector().
832 public function testUpdateRemoveRedirector()
834 $sandboxConf = 'sandbox/config';
835 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
836 $this->conf
= new ConfigManager($sandboxConf);
837 $updater = new LegacyUpdater([], null, $this->conf
, true);
838 $this->assertTrue($updater->updateMethodRemoveRedirector());
839 $this->assertFalse($this->conf
->exists('redirector'));
840 $this->conf
= new ConfigManager($sandboxConf);
841 $this->assertFalse($this->conf
->exists('redirector'));
845 * Test updateMethodFormatterSetting()
847 public function testUpdateMethodFormatterSettingDefault()
849 $sandboxConf = 'sandbox/config';
850 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
851 $this->conf
= new ConfigManager($sandboxConf);
852 $this->conf
->set('formatter', 'default');
853 $updater = new LegacyUpdater([], null, $this->conf
, true);
854 $enabledPlugins = $this->conf
->get('general.enabled_plugins');
855 $this->assertFalse(in_array('markdown', $enabledPlugins));
856 $this->assertTrue($updater->updateMethodFormatterSetting());
857 $this->assertEquals('default', $this->conf
->get('formatter'));
858 $this->assertEquals($enabledPlugins, $this->conf
->get('general.enabled_plugins'));
860 $this->conf
= new ConfigManager($sandboxConf);
861 $this->assertEquals('default', $this->conf
->get('formatter'));
862 $this->assertEquals($enabledPlugins, $this->conf
->get('general.enabled_plugins'));
866 * Test updateMethodFormatterSetting()
868 public function testUpdateMethodFormatterSettingMarkdown()
870 $sandboxConf = 'sandbox/config';
871 copy(self
::$configFile . '.json.php', $sandboxConf . '.json.php');
872 $this->conf
= new ConfigManager($sandboxConf);
873 $this->conf
->set('formatter', 'default');
874 $updater = new LegacyUpdater([], null, $this->conf
, true);
875 $enabledPlugins = $this->conf
->get('general.enabled_plugins');
876 $enabledPlugins[] = 'markdown';
877 $this->conf
->set('general.enabled_plugins', $enabledPlugins);
879 $this->assertTrue(in_array('markdown', $this->conf
->get('general.enabled_plugins')));
880 $this->assertTrue($updater->updateMethodFormatterSetting());
881 $this->assertEquals('markdown', $this->conf
->get('formatter'));
882 $this->assertFalse(in_array('markdown', $this->conf
->get('general.enabled_plugins')));
884 $this->conf
= new ConfigManager($sandboxConf);
885 $this->assertEquals('markdown', $this->conf
->get('formatter'));
886 $this->assertFalse(in_array('markdown', $this->conf
->get('general.enabled_plugins')));