aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ThumbnailerTest.php114
-rw-r--r--tests/Updater/UpdaterTest.php50
-rw-r--r--tests/config/ConfigManagerTest.php23
-rw-r--r--tests/utils/config/configJson.json.php71
-rw-r--r--tests/utils/config/wt.json12
5 files changed, 258 insertions, 12 deletions
diff --git a/tests/ThumbnailerTest.php b/tests/ThumbnailerTest.php
new file mode 100644
index 00000000..08311545
--- /dev/null
+++ b/tests/ThumbnailerTest.php
@@ -0,0 +1,114 @@
1<?php
2
3namespace Shaarli;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Config\ConfigManager;
7use WebThumbnailer\Application\ConfigManager as WTConfigManager;
8
9/**
10 * Class ThumbnailerTest
11 *
12 * We only make 1 thumb test because:
13 *
14 * 1. the thumbnailer library is itself tested
15 * 2. we don't want to make too many external requests during the tests
16 */
17class ThumbnailerTest extends TestCase
18{
19 const WIDTH = 190;
20
21 const HEIGHT = 210;
22
23 /**
24 * @var Thumbnailer;
25 */
26 protected $thumbnailer;
27
28 /**
29 * @var ConfigManager
30 */
31 protected $conf;
32
33 public function setUp()
34 {
35 $this->conf = new ConfigManager('tests/utils/config/configJson');
36 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
37 $this->conf->set('thumbnails.width', self::WIDTH);
38 $this->conf->set('thumbnails.height', self::HEIGHT);
39 $this->conf->set('dev.debug', true);
40
41 $this->thumbnailer = new Thumbnailer($this->conf);
42 // cache files in the sandbox
43 WTConfigManager::addFile('tests/utils/config/wt.json');
44 }
45
46 public function tearDown()
47 {
48 $this->rrmdirContent('sandbox/');
49 }
50
51 /**
52 * Test a thumbnail with a custom size in 'all' mode.
53 */
54 public function testThumbnailAllValid()
55 {
56 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
57 $this->assertNotFalse($thumb);
58 $image = imagecreatefromstring(file_get_contents($thumb));
59 $this->assertEquals(self::WIDTH, imagesx($image));
60 $this->assertEquals(self::HEIGHT, imagesy($image));
61 }
62
63 /**
64 * Test a thumbnail with a custom size in 'common' mode.
65 */
66 public function testThumbnailCommonValid()
67 {
68 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
69 $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
70 $this->assertNotFalse($thumb);
71 $image = imagecreatefromstring(file_get_contents($thumb));
72 $this->assertEquals(self::WIDTH, imagesx($image));
73 $this->assertEquals(self::HEIGHT, imagesy($image));
74 }
75
76 /**
77 * Test a thumbnail in 'common' mode which isn't include in common websites.
78 */
79 public function testThumbnailCommonInvalid()
80 {
81 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
82 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
83 $this->assertFalse($thumb);
84 }
85
86 /**
87 * Test a thumbnail that can't be retrieved.
88 */
89 public function testThumbnailNotValid()
90 {
91 $oldlog = ini_get('error_log');
92 ini_set('error_log', '/dev/null');
93
94 $thumbnailer = new Thumbnailer(new ConfigManager());
95 $thumb = $thumbnailer->get('nope');
96 $this->assertFalse($thumb);
97
98 ini_set('error_log', $oldlog);
99 }
100
101 protected function rrmdirContent($dir) {
102 if (is_dir($dir)) {
103 $objects = scandir($dir);
104 foreach ($objects as $object) {
105 if ($object != "." && $object != "..") {
106 if (is_dir($dir."/".$object))
107 $this->rrmdirContent($dir."/".$object);
108 else
109 unlink($dir."/".$object);
110 }
111 }
112 }
113 }
114}
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index 94e3c7d3..cacee2d2 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -2,6 +2,7 @@
2use Shaarli\Config\ConfigJson; 2use Shaarli\Config\ConfigJson;
3use Shaarli\Config\ConfigManager; 3use Shaarli\Config\ConfigManager;
4use Shaarli\Config\ConfigPhp; 4use Shaarli\Config\ConfigPhp;
5use Shaarli\Thumbnailer;
5 6
6require_once 'tests/Updater/DummyUpdater.php'; 7require_once 'tests/Updater/DummyUpdater.php';
7require_once 'inc/rain.tpl.class.php'; 8require_once 'inc/rain.tpl.class.php';
@@ -20,7 +21,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
20 /** 21 /**
21 * @var string Config file path (without extension). 22 * @var string Config file path (without extension).
22 */ 23 */
23 protected static $configFile = 'tests/utils/config/configJson'; 24 protected static $configFile = 'sandbox/config';
24 25
25 /** 26 /**
26 * @var ConfigManager 27 * @var ConfigManager
@@ -32,6 +33,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
32 */ 33 */
33 public function setUp() 34 public function setUp()
34 { 35 {
36 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
35 $this->conf = new ConfigManager(self::$configFile); 37 $this->conf = new ConfigManager(self::$configFile);
36 } 38 }
37 39
@@ -684,4 +686,50 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
684 $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); 686 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
685 $this->assertEquals(3, $this->conf->get('general.download_timeout')); 687 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
686 } 688 }
689
690 /**
691 * Test updateMethodWebThumbnailer with thumbnails enabled.
692 */
693 public function testUpdateMethodWebThumbnailerEnabled()
694 {
695 $this->conf->remove('thumbnails');
696 $this->conf->set('thumbnail.enable_thumbnails', true);
697 $updater = new Updater([], [], $this->conf, true, $_SESSION);
698 $this->assertTrue($updater->updateMethodWebThumbnailer());
699 $this->assertFalse($this->conf->exists('thumbnail'));
700 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
701 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
702 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
703 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
704 }
705
706 /**
707 * Test updateMethodWebThumbnailer with thumbnails disabled.
708 */
709 public function testUpdateMethodWebThumbnailerDisabled()
710 {
711 $this->conf->remove('thumbnails');
712 $this->conf->set('thumbnail.enable_thumbnails', false);
713 $updater = new Updater([], [], $this->conf, true, $_SESSION);
714 $this->assertTrue($updater->updateMethodWebThumbnailer());
715 $this->assertFalse($this->conf->exists('thumbnail'));
716 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
717 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
718 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
719 $this->assertTrue(empty($_SESSION['warnings']));
720 }
721
722 /**
723 * Test updateMethodWebThumbnailer with thumbnails disabled.
724 */
725 public function testUpdateMethodWebThumbnailerNothingToDo()
726 {
727 $updater = new Updater([], [], $this->conf, true, $_SESSION);
728 $this->assertTrue($updater->updateMethodWebThumbnailer());
729 $this->assertFalse($this->conf->exists('thumbnail'));
730 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
731 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
732 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
733 $this->assertTrue(empty($_SESSION['warnings']));
734 }
687} 735}
diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php
index 1ec447b2..4a4e94ac 100644
--- a/tests/config/ConfigManagerTest.php
+++ b/tests/config/ConfigManagerTest.php
@@ -81,6 +81,18 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase
81 $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff')); 81 $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
82 } 82 }
83 83
84 public function testSetDeleteNested()
85 {
86 $this->conf->set('foo.bar.key.stuff', 'testSetDeleteNested');
87 $this->assertTrue($this->conf->exists('foo.bar'));
88 $this->assertTrue($this->conf->exists('foo.bar.key.stuff'));
89 $this->assertEquals('testSetDeleteNested', $this->conf->get('foo.bar.key.stuff'));
90
91 $this->conf->remove('foo.bar');
92 $this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
93 $this->assertFalse($this->conf->exists('foo.bar'));
94 }
95
84 /** 96 /**
85 * Set with an empty key. 97 * Set with an empty key.
86 * 98 *
@@ -104,6 +116,17 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase
104 } 116 }
105 117
106 /** 118 /**
119 * Remove with an empty key.
120 *
121 * @expectedException \Exception
122 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
123 */
124 public function testRmoveEmptyKey()
125 {
126 $this->conf->remove('');
127 }
128
129 /**
107 * Try to write the config without mandatory parameter (e.g. 'login'). 130 * Try to write the config without mandatory parameter (e.g. 'login').
108 * 131 *
109 * @expectedException Shaarli\Config\Exception\MissingFieldConfigException 132 * @expectedException Shaarli\Config\Exception\MissingFieldConfigException
diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php
index 9c9288f3..1549ddfc 100644
--- a/tests/utils/config/configJson.json.php
+++ b/tests/utils/config/configJson.json.php
@@ -1,35 +1,84 @@
1<?php /* 1<?php /*
2{ 2{
3 "credentials": { 3 "credentials": {
4 "login":"root", 4 "login": "root",
5 "hash":"hash", 5 "hash": "hash",
6 "salt":"salt" 6 "salt": "salt"
7 }, 7 },
8 "security": { 8 "security": {
9 "session_protection_disabled":false 9 "session_protection_disabled": false,
10 "ban_after": 4,
11 "ban_duration": 1800,
12 "open_shaarli": false,
13 "allowed_protocols": [
14 "ftp",
15 "ftps",
16 "magnet"
17 ]
10 }, 18 },
11 "general": { 19 "general": {
12 "timezone":"Europe\/Paris", 20 "timezone": "Europe\/Paris",
13 "title": "Shaarli", 21 "title": "Shaarli",
14 "header_link": "?" 22 "header_link": "?",
23 "links_per_page": 20,
24 "enabled_plugins": [
25 "qrcode"
26 ],
27 "default_note_title": "Note: "
15 }, 28 },
16 "privacy": { 29 "privacy": {
17 "default_private_links":true 30 "default_private_links": true,
31 "hide_public_links": false,
32 "force_login": false,
33 "hide_timestamps": false,
34 "remember_user_default": true
18 }, 35 },
19 "redirector": { 36 "redirector": {
20 "url":"lala" 37 "url": "lala",
38 "encode_url": true
21 }, 39 },
22 "config": { 40 "config": {
23 "foo": "bar" 41 "foo": "bar"
24 }, 42 },
25 "resource": { 43 "resource": {
26 "datastore": "tests\/utils\/config\/datastore.php", 44 "datastore": "tests\/utils\/config\/datastore.php",
27 "data_dir": "sandbox/", 45 "data_dir": "sandbox\/",
28 "raintpl_tpl": "tpl/" 46 "raintpl_tpl": "tpl\/",
47 "config": "data\/config.php",
48 "ban_file": "data\/ipbans.php",
49 "updates": "data\/updates.txt",
50 "log": "data\/log.txt",
51 "update_check": "data\/lastupdatecheck.txt",
52 "history": "data\/history.php",
53 "theme": "default",
54 "raintpl_tmp": "tmp\/",
55 "thumbnails_cache": "cache",
56 "page_cache": "pagecache"
29 }, 57 },
30 "plugins": { 58 "plugins": {
31 "WALLABAG_VERSION": 1 59 "WALLABAG_VERSION": 1
60 },
61 "dev": {
62 "debug": true
63 },
64 "updates": {
65 "check_updates": false,
66 "check_updates_branch": "stable",
67 "check_updates_interval": 86400
68 },
69 "feed": {
70 "rss_permalinks": true,
71 "show_atom": true
72 },
73 "translation": {
74 "language": "auto",
75 "mode": "php",
76 "extensions": []
77 },
78 "thumbnails": {
79 "mode": "common",
80 "width": 90,
81 "height": 53
32 } 82 }
33} 83}
34*/ ?> 84*/ ?>
35
diff --git a/tests/utils/config/wt.json b/tests/utils/config/wt.json
new file mode 100644
index 00000000..69ce49a6
--- /dev/null
+++ b/tests/utils/config/wt.json
@@ -0,0 +1,12 @@
1{
2 "settings": {
3 "default": {
4 "_comment": "infinite cache",
5 "cache_duration": -1,
6 "timeout": 10
7 },
8 "path": {
9 "cache": "sandbox/"
10 }
11 }
12} \ No newline at end of file