aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Updater/UpdaterTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Updater/UpdaterTest.php')
-rw-r--r--tests/Updater/UpdaterTest.php187
1 files changed, 178 insertions, 9 deletions
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index a3e8a4d2..77578528 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -1,7 +1,10 @@
1<?php 1<?php
2use Shaarli\Config\ConfigJson;
3use Shaarli\Config\ConfigManager;
4use Shaarli\Config\ConfigPhp;
2 5
3require_once 'application/config/ConfigManager.php';
4require_once 'tests/Updater/DummyUpdater.php'; 6require_once 'tests/Updater/DummyUpdater.php';
7require_once 'inc/rain.tpl.class.php';
5 8
6/** 9/**
7 * Class UpdaterTest. 10 * Class UpdaterTest.
@@ -271,7 +274,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
271 public function testEscapeConfig() 274 public function testEscapeConfig()
272 { 275 {
273 $sandbox = 'sandbox/config'; 276 $sandbox = 'sandbox/config';
274 copy(self::$configFile .'.json.php', $sandbox .'.json.php'); 277 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
275 $this->conf = new ConfigManager($sandbox); 278 $this->conf = new ConfigManager($sandbox);
276 $title = '<script>alert("title");</script>'; 279 $title = '<script>alert("title");</script>';
277 $headerLink = '<script>alert("header_link");</script>'; 280 $headerLink = '<script>alert("header_link");</script>';
@@ -286,7 +289,43 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
286 $this->assertEquals(escape($title), $this->conf->get('general.title')); 289 $this->assertEquals(escape($title), $this->conf->get('general.title'));
287 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); 290 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
288 $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); 291 $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url'));
289 unlink($sandbox .'.json.php'); 292 unlink($sandbox . '.json.php');
293 }
294
295 /**
296 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
297 */
298 public function testUpdateApiSettings()
299 {
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);
304
305 $this->assertFalse($conf->exists('api.enabled'));
306 $this->assertFalse($conf->exists('api.secret'));
307 $updater->updateMethodApiSettings();
308 $conf->reload();
309 $this->assertTrue($conf->get('api.enabled'));
310 $this->assertTrue($conf->exists('api.secret'));
311 unlink($confFile .'.json.php');
312 }
313
314 /**
315 * Test updateMethodApiSettings(): already set, do nothing.
316 */
317 public function testUpdateApiSettingsNothingToDo()
318 {
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');
290 } 329 }
291 330
292 /** 331 /**
@@ -387,6 +426,50 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
387 } 426 }
388 427
389 /** 428 /**
429 * Test defaultTheme update with default settings: nothing to do.
430 */
431 public function testDefaultThemeWithDefaultSettings()
432 {
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());
438
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');
445 }
446
447 /**
448 * Test defaultTheme update with a custom theme in a subfolder
449 */
450 public function testDefaultThemeWithCustomTheme()
451 {
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());
461
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);
470 }
471
472 /**
390 * Test updateMethodEscapeMarkdown with markdown plugin enabled 473 * Test updateMethodEscapeMarkdown with markdown plugin enabled
391 * => setting markdown_escape set to false. 474 * => setting markdown_escape set to false.
392 */ 475 */
@@ -396,8 +479,8 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
396 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); 479 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
397 $this->conf = new ConfigManager($sandboxConf); 480 $this->conf = new ConfigManager($sandboxConf);
398 481
399 $this->conf->set('general.enabled_plugins', array('markdown')); 482 $this->conf->set('general.enabled_plugins', ['markdown']);
400 $updater = new Updater(array(), array(), $this->conf, true); 483 $updater = new Updater([], [], $this->conf, true);
401 $this->assertTrue($updater->updateMethodEscapeMarkdown()); 484 $this->assertTrue($updater->updateMethodEscapeMarkdown());
402 $this->assertFalse($this->conf->get('security.markdown_escape')); 485 $this->assertFalse($this->conf->get('security.markdown_escape'));
403 486
@@ -416,8 +499,8 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
416 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); 499 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
417 $this->conf = new ConfigManager($sandboxConf); 500 $this->conf = new ConfigManager($sandboxConf);
418 501
419 $this->conf->set('general.enabled_plugins', array()); 502 $this->conf->set('general.enabled_plugins', []);
420 $updater = new Updater(array(), array(), $this->conf, true); 503 $updater = new Updater([], [], $this->conf, true);
421 $this->assertTrue($updater->updateMethodEscapeMarkdown()); 504 $this->assertTrue($updater->updateMethodEscapeMarkdown());
422 $this->assertTrue($this->conf->get('security.markdown_escape')); 505 $this->assertTrue($this->conf->get('security.markdown_escape'));
423 506
@@ -435,7 +518,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
435 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); 518 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
436 $this->conf = new ConfigManager($sandboxConf); 519 $this->conf = new ConfigManager($sandboxConf);
437 $this->conf->set('security.markdown_escape', true); 520 $this->conf->set('security.markdown_escape', true);
438 $updater = new Updater(array(), array(), $this->conf, true); 521 $updater = new Updater([], [], $this->conf, true);
439 $this->assertTrue($updater->updateMethodEscapeMarkdown()); 522 $this->assertTrue($updater->updateMethodEscapeMarkdown());
440 $this->assertTrue($this->conf->get('security.markdown_escape')); 523 $this->assertTrue($this->conf->get('security.markdown_escape'));
441 } 524 }
@@ -446,8 +529,94 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
446 public function testEscapeMarkdownSettingNothingToDoDisabled() 529 public function testEscapeMarkdownSettingNothingToDoDisabled()
447 { 530 {
448 $this->conf->set('security.markdown_escape', false); 531 $this->conf->set('security.markdown_escape', false);
449 $updater = new Updater(array(), array(), $this->conf, true); 532 $updater = new Updater([], [], $this->conf, true);
450 $this->assertTrue($updater->updateMethodEscapeMarkdown()); 533 $this->assertTrue($updater->updateMethodEscapeMarkdown());
451 $this->assertFalse($this->conf->get('security.markdown_escape')); 534 $this->assertFalse($this->conf->get('security.markdown_escape'));
452 } 535 }
536
537 /**
538 * Test updateMethodPiwikUrl with valid data
539 */
540 public function testUpdatePiwikUrlValid()
541 {
542 $sandboxConf = 'sandbox/config';
543 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
544 $this->conf = new ConfigManager($sandboxConf);
545 $url = 'mypiwik.tld';
546 $this->conf->set('plugins.PIWIK_URL', $url);
547 $updater = new Updater([], [], $this->conf, true);
548 $this->assertTrue($updater->updateMethodPiwikUrl());
549 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
550
551 // reload from file
552 $this->conf = new ConfigManager($sandboxConf);
553 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
554 }
555
556 /**
557 * Test updateMethodPiwikUrl without setting
558 */
559 public function testUpdatePiwikUrlEmpty()
560 {
561 $updater = new Updater([], [], $this->conf, true);
562 $this->assertTrue($updater->updateMethodPiwikUrl());
563 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
564 }
565
566 /**
567 * Test updateMethodPiwikUrl: valid URL, nothing to do
568 */
569 public function testUpdatePiwikUrlNothingToDo()
570 {
571 $url = 'https://mypiwik.tld';
572 $this->conf->set('plugins.PIWIK_URL', $url);
573 $updater = new Updater([], [], $this->conf, true);
574 $this->assertTrue($updater->updateMethodPiwikUrl());
575 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
576 }
577
578 /**
579 * Test updateMethodAtomDefault with show_atom set to false
580 * => update to true.
581 */
582 public function testUpdateMethodAtomDefault()
583 {
584 $sandboxConf = 'sandbox/config';
585 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
586 $this->conf = new ConfigManager($sandboxConf);
587 $this->conf->set('feed.show_atom', false);
588 $updater = new Updater([], [], $this->conf, true);
589 $this->assertTrue($updater->updateMethodAtomDefault());
590 $this->assertTrue($this->conf->get('feed.show_atom'));
591 // reload from file
592 $this->conf = new ConfigManager($sandboxConf);
593 $this->assertTrue($this->conf->get('feed.show_atom'));
594 }
595 /**
596 * Test updateMethodAtomDefault with show_atom not set.
597 * => nothing to do
598 */
599 public function testUpdateMethodAtomDefaultNoExist()
600 {
601 $sandboxConf = 'sandbox/config';
602 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
603 $this->conf = new ConfigManager($sandboxConf);
604 $updater = new Updater([], [], $this->conf, true);
605 $this->assertTrue($updater->updateMethodAtomDefault());
606 $this->assertTrue($this->conf->get('feed.show_atom'));
607 }
608 /**
609 * Test updateMethodAtomDefault with show_atom set to true.
610 * => nothing to do
611 */
612 public function testUpdateMethodAtomDefaultAlreadyTrue()
613 {
614 $sandboxConf = 'sandbox/config';
615 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
616 $this->conf = new ConfigManager($sandboxConf);
617 $this->conf->set('feed.show_atom', true);
618 $updater = new Updater([], [], $this->conf, true);
619 $this->assertTrue($updater->updateMethodAtomDefault());
620 $this->assertTrue($this->conf->get('feed.show_atom'));
621 }
453} 622}