]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/legacy/LegacyUpdaterTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / legacy / LegacyUpdaterTest.php
1 <?php
2 namespace Shaarli\Updater;
3
4 use DateTime;
5 use Exception;
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;
13
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';
18
19 /**
20 * Class UpdaterTest.
21 * Runs unit tests against the updater class.
22 */
23 class LegacyUpdaterTest extends \Shaarli\TestCase
24 {
25 /**
26 * @var string Path to test datastore.
27 */
28 protected static $testDatastore = 'sandbox/datastore.php';
29
30 /**
31 * @var string Config file path (without extension).
32 */
33 protected static $configFile = 'sandbox/config';
34
35 /**
36 * @var ConfigManager
37 */
38 protected $conf;
39
40 /**
41 * Executed before each test.
42 */
43 protected function setUp(): void
44 {
45 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
46 $this->conf = new ConfigManager(self::$configFile);
47 }
48
49 /**
50 * Test UpdaterUtils::read_updates_file with an empty/missing file.
51 */
52 public function testReadEmptyUpdatesFile()
53 {
54 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
55 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
56 touch($updatesFile);
57 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
58 unlink($updatesFile);
59 }
60
61 /**
62 * Test read/write updates file.
63 */
64 public function testReadWriteUpdatesFile()
65 {
66 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
67 $updatesMethods = array('m1', 'm2', 'm3');
68
69 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
70 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
71 $this->assertEquals($readMethods, $updatesMethods);
72
73 // Update
74 $updatesMethods[] = 'm4';
75 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
76 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
77 $this->assertEquals($readMethods, $updatesMethods);
78 unlink($updatesFile);
79 }
80
81 /**
82 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
83 *
84 * @expectedException Exception
85 */
86 public function testWriteEmptyUpdatesFile()
87 {
88 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
89
90 UpdaterUtils::write_updates_file('', array('test'));
91 }
92
93 /**
94 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
95 *
96 * @expectedException Exception
97 */
98 public function testWriteUpdatesFileNotWritable()
99 {
100 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
101
102 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
103 touch($updatesFile);
104 chmod($updatesFile, 0444);
105 try {
106 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
107 } catch (Exception $e) {
108 unlink($updatesFile);
109 throw $e;
110 }
111 }
112
113 /**
114 * Test the update() method, with no update to run.
115 * 1. Everything already run.
116 * 2. User is logged out.
117 */
118 public function testNoUpdates()
119 {
120 $updates = array(
121 'updateMethodDummy1',
122 'updateMethodDummy2',
123 'updateMethodDummy3',
124 'updateMethodException',
125 );
126 $updater = new DummyUpdater($updates, array(), $this->conf, true);
127 $this->assertEquals(array(), $updater->update());
128
129 $updater = new DummyUpdater(array(), array(), $this->conf, false);
130 $this->assertEquals(array(), $updater->update());
131 }
132
133 /**
134 * Test the update() method, with all updates to run (except the failing one).
135 */
136 public function testUpdatesFirstTime()
137 {
138 $updates = array('updateMethodException',);
139 $expectedUpdates = array(
140 'updateMethodDummy1',
141 'updateMethodDummy2',
142 'updateMethodDummy3',
143 );
144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
145 $this->assertEquals($expectedUpdates, $updater->update());
146 }
147
148 /**
149 * Test the update() method, only one update to run.
150 */
151 public function testOneUpdate()
152 {
153 $updates = array(
154 'updateMethodDummy1',
155 'updateMethodDummy3',
156 'updateMethodException',
157 );
158 $expectedUpdate = array('updateMethodDummy2');
159
160 $updater = new DummyUpdater($updates, array(), $this->conf, true);
161 $this->assertEquals($expectedUpdate, $updater->update());
162 }
163
164 /**
165 * Test Update failed.
166 */
167 public function testUpdateFailed()
168 {
169 $this->expectException(\Exception::class);
170
171 $updates = array(
172 'updateMethodDummy1',
173 'updateMethodDummy2',
174 'updateMethodDummy3',
175 );
176
177 $updater = new DummyUpdater($updates, array(), $this->conf, true);
178 $updater->update();
179 }
180
181 /**
182 * Test update mergeDeprecatedConfig:
183 * 1. init a config file.
184 * 2. init a options.php file with update value.
185 * 3. merge.
186 * 4. check updated value in config file.
187 */
188 public function testUpdateMergeDeprecatedConfig()
189 {
190 $this->conf->setConfigFile('tests/utils/config/configPhp');
191 $this->conf->reset();
192
193 $optionsFile = 'tests/updater/options.php';
194 $options = '<?php
195 $GLOBALS[\'privateLinkByDefault\'] = true;';
196 file_put_contents($optionsFile, $options);
197
198 // tmp config file.
199 $this->conf->setConfigFile('tests/updater/config');
200
201 // merge configs
202 $updater = new LegacyUpdater(array(), array(), $this->conf, true);
203 // This writes a new config file in tests/updater/config.php
204 $updater->updateMethodMergeDeprecatedConfigFile();
205
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());
212 }
213
214 /**
215 * Test mergeDeprecatedConfig in without options file.
216 */
217 public function testMergeDeprecatedConfigNoFile()
218 {
219 $updater = new LegacyUpdater(array(), array(), $this->conf, true);
220 $updater->updateMethodMergeDeprecatedConfigFile();
221
222 $this->assertEquals('root', $this->conf->get('credentials.login'));
223 }
224
225 /**
226 * Test renameDashTags update method.
227 */
228 public function testRenameDashTags()
229 {
230 $refDB = new \ReferenceLinkDB(true);
231 $refDB->write(self::$testDatastore);
232 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
233
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')));
238 }
239
240 /**
241 * Convert old PHP config file to JSON config.
242 */
243 public function testConfigToJson()
244 {
245 $configFile = 'tests/utils/config/configPhp';
246 $this->conf->setConfigFile($configFile);
247 $this->conf->reset();
248
249 // The ConfigIO is initialized with ConfigPhp.
250 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
251
252 $updater = new LegacyUpdater(array(), array(), $this->conf, false);
253 $done = $updater->updateMethodConfigToJson();
254 $this->assertTrue($done);
255
256 // The ConfigIO has been updated to ConfigJson.
257 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
258 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
259
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'));
266
267 rename($configFile . '.save.php', $configFile . '.php');
268 unlink($this->conf->getConfigFileExt());
269 }
270
271 /**
272 * Launch config conversion update with an existing JSON file => nothing to do.
273 */
274 public function testConfigToJsonNothingToDo()
275 {
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);
282 }
283
284 /**
285 * Test escapeUnescapedConfig with valid data.
286 */
287 public function testEscapeConfig()
288 {
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');
303 }
304
305 /**
306 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
307 */
308 public function testUpdateApiSettings()
309 {
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);
314
315 $this->assertFalse($conf->exists('api.enabled'));
316 $this->assertFalse($conf->exists('api.secret'));
317 $updater->updateMethodApiSettings();
318 $conf->reload();
319 $this->assertTrue($conf->get('api.enabled'));
320 $this->assertTrue($conf->exists('api.secret'));
321 unlink($confFile .'.json.php');
322 }
323
324 /**
325 * Test updateMethodApiSettings(): already set, do nothing.
326 */
327 public function testUpdateApiSettingsNothingToDo()
328 {
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');
339 }
340
341 /**
342 * Test updateMethodDatastoreIds().
343 */
344 public function testDatastoreIds()
345 {
346 $links = array(
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',
354 'private' => false,
355 ),
356 '20121206_172539' => array(
357 'linkdate' => '20121206_172539',
358 'title' => 'UserFriendly - Samba',
359 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
360 'description' => '',
361 'tags' => 'samba cartoon web',
362 'private' => false,
363 ),
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',
370 'private' => true,
371 ),
372 );
373 $refDB = new \ReferenceLinkDB(true);
374 $refDB->setLinks($links);
375 $refDB->write(self::$testDatastore);
376 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
377
378 $checksum = hash_file('sha1', self::$testDatastore);
379
380 $this->conf->set('resource.data_dir', 'sandbox');
381 $this->conf->set('resource.datastore', self::$testDatastore);
382
383 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
384 $this->assertTrue($updater->updateMethodDatastoreIds());
385
386 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
387
388 $backupFiles = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
389 $backup = null;
390 foreach ($backupFiles as $backupFile) {
391 if (strpos($backupFile, '_1') === false) {
392 $backup = $backupFile;
393 }
394 }
395 $this->assertNotNull($backup);
396 $this->assertFileExists($backup);
397 $this->assertEquals($checksum, hash_file('sha1', $backup));
398 unlink($backup);
399
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']);
409 $this->assertEquals(
410 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_142300'),
411 $linkDB[0]['created']
412 );
413
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']);
418 $this->assertEquals(
419 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_172539'),
420 $linkDB[1]['created']
421 );
422
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']);
427 $this->assertEquals(
428 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_182539'),
429 $linkDB[2]['created']
430 );
431 $this->assertEquals(
432 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_190301'),
433 $linkDB[2]['updated']
434 );
435 }
436
437 /**
438 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
439 */
440 public function testDatastoreIdsNothingToDo()
441 {
442 $refDB = new \ReferenceLinkDB(true);
443 $refDB->write(self::$testDatastore);
444 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
445
446 $this->conf->set('resource.data_dir', 'sandbox');
447 $this->conf->set('resource.datastore', self::$testDatastore);
448
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));
453 }
454
455 /**
456 * Test defaultTheme update with default settings: nothing to do.
457 */
458 public function testDefaultThemeWithDefaultSettings()
459 {
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());
465
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');
472 }
473
474 /**
475 * Test defaultTheme update with a custom theme in a subfolder
476 */
477 public function testDefaultThemeWithCustomTheme()
478 {
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());
488
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);
497 }
498
499 /**
500 * Test updateMethodEscapeMarkdown with markdown plugin enabled
501 * => setting markdown_escape set to false.
502 */
503 public function testEscapeMarkdownSettingToFalse()
504 {
505 $sandboxConf = 'sandbox/config';
506 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
507 $this->conf = new ConfigManager($sandboxConf);
508
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'));
513
514 // reload from file
515 $this->conf = new ConfigManager($sandboxConf);
516 $this->assertFalse($this->conf->get('security.markdown_escape'));
517 }
518
519
520 /**
521 * Test updateMethodEscapeMarkdown with markdown plugin disabled
522 * => setting markdown_escape set to true.
523 */
524 public function testEscapeMarkdownSettingToTrue()
525 {
526 $sandboxConf = 'sandbox/config';
527 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
528 $this->conf = new ConfigManager($sandboxConf);
529
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'));
534
535 // reload from file
536 $this->conf = new ConfigManager($sandboxConf);
537 $this->assertTrue($this->conf->get('security.markdown_escape'));
538 }
539
540 /**
541 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
542 */
543 public function testEscapeMarkdownSettingNothingToDoEnabled()
544 {
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'));
552 }
553
554 /**
555 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
556 */
557 public function testEscapeMarkdownSettingNothingToDoDisabled()
558 {
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'));
563 }
564
565 /**
566 * Test updateMethodPiwikUrl with valid data
567 */
568 public function testUpdatePiwikUrlValid()
569 {
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'));
578
579 // reload from file
580 $this->conf = new ConfigManager($sandboxConf);
581 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
582 }
583
584 /**
585 * Test updateMethodPiwikUrl without setting
586 */
587 public function testUpdatePiwikUrlEmpty()
588 {
589 $updater = new LegacyUpdater([], [], $this->conf, true);
590 $this->assertTrue($updater->updateMethodPiwikUrl());
591 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
592 }
593
594 /**
595 * Test updateMethodPiwikUrl: valid URL, nothing to do
596 */
597 public function testUpdatePiwikUrlNothingToDo()
598 {
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'));
604 }
605
606 /**
607 * Test updateMethodAtomDefault with show_atom set to false
608 * => update to true.
609 */
610 public function testUpdateMethodAtomDefault()
611 {
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'));
619 // reload from file
620 $this->conf = new ConfigManager($sandboxConf);
621 $this->assertTrue($this->conf->get('feed.show_atom'));
622 }
623 /**
624 * Test updateMethodAtomDefault with show_atom not set.
625 * => nothing to do
626 */
627 public function testUpdateMethodAtomDefaultNoExist()
628 {
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'));
635 }
636 /**
637 * Test updateMethodAtomDefault with show_atom set to true.
638 * => nothing to do
639 */
640 public function testUpdateMethodAtomDefaultAlreadyTrue()
641 {
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'));
649 }
650
651 /**
652 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
653 */
654 public function testUpdateMethodDownloadSizeAndTimeoutConf()
655 {
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'));
663
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'));
667 }
668
669 /**
670 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
671 */
672 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
673 {
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'));
683 }
684
685 /**
686 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
687 */
688 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
689 {
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'));
698 }
699
700 /**
701 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
702 */
703 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
704 {
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'));
713 }
714
715 /**
716 * Test updateMethodWebThumbnailer with thumbnails enabled.
717 */
718 public function testUpdateMethodWebThumbnailerEnabled()
719 {
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->assertContainsPolyfill('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
729 }
730
731 /**
732 * Test updateMethodWebThumbnailer with thumbnails disabled.
733 */
734 public function testUpdateMethodWebThumbnailerDisabled()
735 {
736 if (isset($_SESSION['warnings'])) {
737 unset($_SESSION['warnings']);
738 }
739
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']));
749 }
750
751 /**
752 * Test updateMethodWebThumbnailer with thumbnails disabled.
753 */
754 public function testUpdateMethodWebThumbnailerNothingToDo()
755 {
756 if (isset($_SESSION['warnings'])) {
757 unset($_SESSION['warnings']);
758 }
759
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']));
767 }
768
769 /**
770 * Test updateMethodSetSticky().
771 */
772 public function testUpdateStickyValid()
773 {
774 $blank = [
775 'id' => 1,
776 'url' => 'z',
777 'title' => '',
778 'description' => '',
779 'tags' => '',
780 'created' => new DateTime(),
781 ];
782 $links = [
783 1 => ['id' => 1] + $blank,
784 2 => ['id' => 2] + $blank,
785 ];
786 $refDB = new \ReferenceLinkDB(true);
787 $refDB->setLinks($links);
788 $refDB->write(self::$testDatastore);
789 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
790
791 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
792 $this->assertTrue($updater->updateMethodSetSticky());
793
794 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
795 foreach ($linkDB as $link) {
796 $this->assertFalse($link['sticky']);
797 }
798 }
799
800 /**
801 * Test updateMethodSetSticky().
802 */
803 public function testUpdateStickyNothingToDo()
804 {
805 $blank = [
806 'id' => 1,
807 'url' => 'z',
808 'title' => '',
809 'description' => '',
810 'tags' => '',
811 'created' => new DateTime(),
812 ];
813 $links = [
814 1 => ['id' => 1, 'sticky' => true] + $blank,
815 2 => ['id' => 2] + $blank,
816 ];
817 $refDB = new \ReferenceLinkDB(true);
818 $refDB->setLinks($links);
819 $refDB->write(self::$testDatastore);
820 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
821
822 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
823 $this->assertTrue($updater->updateMethodSetSticky());
824
825 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
826 $this->assertTrue($linkDB[1]['sticky']);
827 }
828
829 /**
830 * Test updateMethodRemoveRedirector().
831 */
832 public function testUpdateRemoveRedirector()
833 {
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'));
842 }
843
844 /**
845 * Test updateMethodFormatterSetting()
846 */
847 public function testUpdateMethodFormatterSettingDefault()
848 {
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'));
859
860 $this->conf = new ConfigManager($sandboxConf);
861 $this->assertEquals('default', $this->conf->get('formatter'));
862 $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins'));
863 }
864
865 /**
866 * Test updateMethodFormatterSetting()
867 */
868 public function testUpdateMethodFormatterSettingMarkdown()
869 {
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);
878
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')));
883
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')));
887 }
888 }