aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ApplicationUtilsTest.php54
-rw-r--r--tests/ConfigTest.php244
-rw-r--r--tests/FeedBuilderTest.php6
-rw-r--r--tests/LinkDBTest.php2
-rw-r--r--tests/PluginManagerTest.php34
-rw-r--r--tests/Updater/DummyUpdater.php12
-rw-r--r--tests/Updater/UpdaterTest.php160
-rw-r--r--tests/config/ConfigJsonTest.php133
-rw-r--r--tests/config/ConfigManagerTest.php172
-rw-r--r--tests/config/ConfigPhpTest.php82
-rw-r--r--tests/config/ConfigPluginTest.php121
-rw-r--r--tests/plugins/PluginReadityourselfTest.php12
-rw-r--r--tests/plugins/PluginWallabagTest.php10
-rw-r--r--tests/utils/config/configInvalid.json.php5
-rw-r--r--tests/utils/config/configJson.json.php34
-rw-r--r--tests/utils/config/configPhp.php14
16 files changed, 722 insertions, 373 deletions
diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php
index 6064357d..c37a94f0 100644
--- a/tests/ApplicationUtilsTest.php
+++ b/tests/ApplicationUtilsTest.php
@@ -3,6 +3,7 @@
3 * ApplicationUtils' tests 3 * ApplicationUtils' tests
4 */ 4 */
5 5
6require_once 'application/config/ConfigManager.php';
6require_once 'application/ApplicationUtils.php'; 7require_once 'application/ApplicationUtils.php';
7 8
8/** 9/**
@@ -59,7 +60,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
59 $testTimeout 60 $testTimeout
60 ) 61 )
61 ); 62 );
62 $this->assertRegexp( 63 $this->assertRegExp(
63 self::$versionPattern, 64 self::$versionPattern,
64 ApplicationUtils::getLatestGitVersionCode( 65 ApplicationUtils::getLatestGitVersionCode(
65 'https://raw.githubusercontent.com/shaarli/Shaarli/' 66 'https://raw.githubusercontent.com/shaarli/Shaarli/'
@@ -275,21 +276,21 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
275 */ 276 */
276 public function testCheckCurrentResourcePermissions() 277 public function testCheckCurrentResourcePermissions()
277 { 278 {
278 $config = array( 279 $conf = new ConfigManager('');
279 'CACHEDIR' => 'cache', 280 $conf->set('resource.thumbnails_cache', 'cache');
280 'CONFIG_FILE' => 'data/config.php', 281 $conf->set('resource.config', 'data/config.php');
281 'DATADIR' => 'data', 282 $conf->set('resource.data_dir', 'data');
282 'DATASTORE' => 'data/datastore.php', 283 $conf->set('resource.datastore', 'data/datastore.php');
283 'IPBANS_FILENAME' => 'data/ipbans.php', 284 $conf->set('resource.ban_file', 'data/ipbans.php');
284 'LOG_FILE' => 'data/log.txt', 285 $conf->set('resource.log', 'data/log.txt');
285 'PAGECACHE' => 'pagecache', 286 $conf->set('resource.page_cache', 'pagecache');
286 'RAINTPL_TMP' => 'tmp', 287 $conf->set('resource.raintpl_tmp', 'tmp');
287 'RAINTPL_TPL' => 'tpl', 288 $conf->set('resource.raintpl_tpl', 'tpl');
288 'UPDATECHECK_FILENAME' => 'data/lastupdatecheck.txt' 289 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
289 ); 290
290 $this->assertEquals( 291 $this->assertEquals(
291 array(), 292 array(),
292 ApplicationUtils::checkResourcePermissions($config) 293 ApplicationUtils::checkResourcePermissions($conf)
293 ); 294 );
294 } 295 }
295 296
@@ -298,18 +299,17 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
298 */ 299 */
299 public function testCheckCurrentResourcePermissionsErrors() 300 public function testCheckCurrentResourcePermissionsErrors()
300 { 301 {
301 $config = array( 302 $conf = new ConfigManager('');
302 'CACHEDIR' => 'null/cache', 303 $conf->set('resource.thumbnails_cache', 'null/cache');
303 'CONFIG_FILE' => 'null/data/config.php', 304 $conf->set('resource.config', 'null/data/config.php');
304 'DATADIR' => 'null/data', 305 $conf->set('resource.data_dir', 'null/data');
305 'DATASTORE' => 'null/data/store.php', 306 $conf->set('resource.datastore', 'null/data/store.php');
306 'IPBANS_FILENAME' => 'null/data/ipbans.php', 307 $conf->set('resource.ban_file', 'null/data/ipbans.php');
307 'LOG_FILE' => 'null/data/log.txt', 308 $conf->set('resource.log', 'null/data/log.txt');
308 'PAGECACHE' => 'null/pagecache', 309 $conf->set('resource.page_cache', 'null/pagecache');
309 'RAINTPL_TMP' => 'null/tmp', 310 $conf->set('resource.raintpl_tmp', 'null/tmp');
310 'RAINTPL_TPL' => 'null/tpl', 311 $conf->set('resource.raintpl_tpl', 'null/tpl');
311 'UPDATECHECK_FILENAME' => 'null/data/lastupdatecheck.txt' 312 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
312 );
313 $this->assertEquals( 313 $this->assertEquals(
314 array( 314 array(
315 '"null/tpl" directory is not readable', 315 '"null/tpl" directory is not readable',
@@ -322,7 +322,7 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
322 '"null/tmp" directory is not readable', 322 '"null/tmp" directory is not readable',
323 '"null/tmp" directory is not writable' 323 '"null/tmp" directory is not writable'
324 ), 324 ),
325 ApplicationUtils::checkResourcePermissions($config) 325 ApplicationUtils::checkResourcePermissions($conf)
326 ); 326 );
327 } 327 }
328} 328}
diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php
deleted file mode 100644
index 7200aae6..00000000
--- a/tests/ConfigTest.php
+++ /dev/null
@@ -1,244 +0,0 @@
1<?php
2/**
3 * Config' tests
4 */
5
6require_once 'application/Config.php';
7
8/**
9 * Unitary tests for Shaarli config related functions
10 */
11class ConfigTest extends PHPUnit_Framework_TestCase
12{
13 // Configuration input set.
14 private static $configFields;
15
16 /**
17 * Executed before each test.
18 */
19 public function setUp()
20 {
21 self::$configFields = array(
22 'login' => 'login',
23 'hash' => 'hash',
24 'salt' => 'salt',
25 'timezone' => 'Europe/Paris',
26 'title' => 'title',
27 'titleLink' => 'titleLink',
28 'redirector' => '',
29 'disablesessionprotection' => false,
30 'privateLinkByDefault' => false,
31 'config' => array(
32 'CONFIG_FILE' => 'tests/config.php',
33 'DATADIR' => 'tests',
34 'config1' => 'config1data',
35 'config2' => 'config2data',
36 )
37 );
38 }
39
40 /**
41 * Executed after each test.
42 *
43 * @return void
44 */
45 public function tearDown()
46 {
47 if (is_file(self::$configFields['config']['CONFIG_FILE'])) {
48 unlink(self::$configFields['config']['CONFIG_FILE']);
49 }
50 }
51
52 /**
53 * Test writeConfig function, valid use case, while being logged in.
54 */
55 public function testWriteConfig()
56 {
57 writeConfig(self::$configFields, true);
58
59 include self::$configFields['config']['CONFIG_FILE'];
60 $this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
61 $this->assertEquals(self::$configFields['hash'], $GLOBALS['hash']);
62 $this->assertEquals(self::$configFields['salt'], $GLOBALS['salt']);
63 $this->assertEquals(self::$configFields['timezone'], $GLOBALS['timezone']);
64 $this->assertEquals(self::$configFields['title'], $GLOBALS['title']);
65 $this->assertEquals(self::$configFields['titleLink'], $GLOBALS['titleLink']);
66 $this->assertEquals(self::$configFields['redirector'], $GLOBALS['redirector']);
67 $this->assertEquals(self::$configFields['disablesessionprotection'], $GLOBALS['disablesessionprotection']);
68 $this->assertEquals(self::$configFields['privateLinkByDefault'], $GLOBALS['privateLinkByDefault']);
69 $this->assertEquals(self::$configFields['config']['config1'], $GLOBALS['config']['config1']);
70 $this->assertEquals(self::$configFields['config']['config2'], $GLOBALS['config']['config2']);
71 }
72
73 /**
74 * Test writeConfig option while logged in:
75 * 1. init fields.
76 * 2. update fields, add new sub config, add new root config.
77 * 3. rewrite config.
78 * 4. check result.
79 */
80 public function testWriteConfigFieldUpdate()
81 {
82 writeConfig(self::$configFields, true);
83 self::$configFields['title'] = 'ok';
84 self::$configFields['config']['config1'] = 'ok';
85 self::$configFields['config']['config_new'] = 'ok';
86 self::$configFields['new'] = 'should not be saved';
87 writeConfig(self::$configFields, true);
88
89 include self::$configFields['config']['CONFIG_FILE'];
90 $this->assertEquals('ok', $GLOBALS['title']);
91 $this->assertEquals('ok', $GLOBALS['config']['config1']);
92 $this->assertEquals('ok', $GLOBALS['config']['config_new']);
93 $this->assertFalse(isset($GLOBALS['new']));
94 }
95
96 /**
97 * Test writeConfig function with an empty array.
98 *
99 * @expectedException MissingFieldConfigException
100 */
101 public function testWriteConfigEmpty()
102 {
103 writeConfig(array(), true);
104 }
105
106 /**
107 * Test writeConfig function with a missing mandatory field.
108 *
109 * @expectedException MissingFieldConfigException
110 */
111 public function testWriteConfigMissingField()
112 {
113 unset(self::$configFields['login']);
114 writeConfig(self::$configFields, true);
115 }
116
117 /**
118 * Test writeConfig function while being logged out, and there is no config file existing.
119 */
120 public function testWriteConfigLoggedOutNoFile()
121 {
122 writeConfig(self::$configFields, false);
123 }
124
125 /**
126 * Test writeConfig function while being logged out, and a config file already exists.
127 *
128 * @expectedException UnauthorizedConfigException
129 */
130 public function testWriteConfigLoggedOutWithFile()
131 {
132 file_put_contents(self::$configFields['config']['CONFIG_FILE'], '');
133 writeConfig(self::$configFields, false);
134 }
135
136 /**
137 * Test save_plugin_config with valid data.
138 *
139 * @throws PluginConfigOrderException
140 */
141 public function testSavePluginConfigValid()
142 {
143 $data = array(
144 'order_plugin1' => 2, // no plugin related
145 'plugin2' => 0, // new - at the end
146 'plugin3' => 0, // 2nd
147 'order_plugin3' => 8,
148 'plugin4' => 0, // 1st
149 'order_plugin4' => 5,
150 );
151
152 $expected = array(
153 'plugin3',
154 'plugin4',
155 'plugin2',
156 );
157
158 $out = save_plugin_config($data);
159 $this->assertEquals($expected, $out);
160 }
161
162 /**
163 * Test save_plugin_config with invalid data.
164 *
165 * @expectedException PluginConfigOrderException
166 */
167 public function testSavePluginConfigInvalid()
168 {
169 $data = array(
170 'plugin2' => 0,
171 'plugin3' => 0,
172 'order_plugin3' => 0,
173 'plugin4' => 0,
174 'order_plugin4' => 0,
175 );
176
177 save_plugin_config($data);
178 }
179
180 /**
181 * Test save_plugin_config without data.
182 */
183 public function testSavePluginConfigEmpty()
184 {
185 $this->assertEquals(array(), save_plugin_config(array()));
186 }
187
188 /**
189 * Test validate_plugin_order with valid data.
190 */
191 public function testValidatePluginOrderValid()
192 {
193 $data = array(
194 'order_plugin1' => 2,
195 'plugin2' => 0,
196 'plugin3' => 0,
197 'order_plugin3' => 1,
198 'plugin4' => 0,
199 'order_plugin4' => 5,
200 );
201
202 $this->assertTrue(validate_plugin_order($data));
203 }
204
205 /**
206 * Test validate_plugin_order with invalid data.
207 */
208 public function testValidatePluginOrderInvalid()
209 {
210 $data = array(
211 'order_plugin1' => 2,
212 'order_plugin3' => 1,
213 'order_plugin4' => 1,
214 );
215
216 $this->assertFalse(validate_plugin_order($data));
217 }
218
219 /**
220 * Test load_plugin_parameter_values.
221 */
222 public function testLoadPluginParameterValues()
223 {
224 $plugins = array(
225 'plugin_name' => array(
226 'parameters' => array(
227 'param1' => true,
228 'param2' => false,
229 'param3' => '',
230 )
231 )
232 );
233
234 $parameters = array(
235 'param1' => 'value1',
236 'param2' => 'value2',
237 );
238
239 $result = load_plugin_parameter_values($plugins, $parameters);
240 $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']);
241 $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']);
242 $this->assertEquals('', $result['plugin_name']['parameters']['param3']);
243 }
244}
diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php
index 647b2db2..460fb0c5 100644
--- a/tests/FeedBuilderTest.php
+++ b/tests/FeedBuilderTest.php
@@ -76,7 +76,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
76 // Test headers (RSS) 76 // Test headers (RSS)
77 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); 77 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
78 $this->assertEmpty($data['pubsubhub_url']); 78 $this->assertEmpty($data['pubsubhub_url']);
79 $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $data['last_update']); 79 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $data['last_update']);
80 $this->assertEquals(true, $data['show_dates']); 80 $this->assertEquals(true, $data['show_dates']);
81 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); 81 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
82 $this->assertEquals('http://host.tld/', $data['index_url']); 82 $this->assertEquals('http://host.tld/', $data['index_url']);
@@ -88,7 +88,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
88 $this->assertEquals('20150310_114651', $link['linkdate']); 88 $this->assertEquals('20150310_114651', $link['linkdate']);
89 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 89 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
90 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 90 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
91 $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $link['iso_date']); 91 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['iso_date']);
92 $this->assertContains('Stallman has a beard', $link['description']); 92 $this->assertContains('Stallman has a beard', $link['description']);
93 $this->assertContains('Permalink', $link['description']); 93 $this->assertContains('Permalink', $link['description']);
94 $this->assertContains('http://host.tld/?WDWyig', $link['description']); 94 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
@@ -113,7 +113,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
113 $data = $feedBuilder->buildData(); 113 $data = $feedBuilder->buildData();
114 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 114 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
115 $link = array_shift($data['links']); 115 $link = array_shift($data['links']);
116 $this->assertEquals('2015-03-10T11:46:51+01:00', $link['iso_date']); 116 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:+\d{2}/', $link['iso_date']);
117 } 117 }
118 118
119 /** 119 /**
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php
index 0db81fd6..46956f20 100644
--- a/tests/LinkDBTest.php
+++ b/tests/LinkDBTest.php
@@ -101,7 +101,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
101 * Attempt to instantiate a LinkDB whereas the datastore is not writable 101 * Attempt to instantiate a LinkDB whereas the datastore is not writable
102 * 102 *
103 * @expectedException IOException 103 * @expectedException IOException
104 * @expectedExceptionMessageRegExp /Error accessing null/ 104 * @expectedExceptionMessageRegExp /Error accessing\nnull/
105 */ 105 */
106 public function testConstructDatastoreNotWriteable() 106 public function testConstructDatastoreNotWriteable()
107 { 107 {
diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php
index 348082c7..61efce68 100644
--- a/tests/PluginManagerTest.php
+++ b/tests/PluginManagerTest.php
@@ -24,29 +24,38 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
24 private static $pluginName = 'test'; 24 private static $pluginName = 'test';
25 25
26 /** 26 /**
27 * @var PluginManager $pluginManager Plugin Mananger instance.
28 */
29 protected $pluginManager;
30
31 public function setUp()
32 {
33 $conf = new ConfigManager('');
34 $this->pluginManager = new PluginManager($conf);
35 }
36
37 /**
27 * Test plugin loading and hook execution. 38 * Test plugin loading and hook execution.
28 * 39 *
29 * @return void 40 * @return void
30 */ 41 */
31 public function testPlugin() 42 public function testPlugin()
32 { 43 {
33 $pluginManager = PluginManager::getInstance();
34
35 PluginManager::$PLUGINS_PATH = self::$pluginPath; 44 PluginManager::$PLUGINS_PATH = self::$pluginPath;
36 $pluginManager->load(array(self::$pluginName)); 45 $this->pluginManager->load(array(self::$pluginName));
37 46
38 $this->assertTrue(function_exists('hook_test_random')); 47 $this->assertTrue(function_exists('hook_test_random'));
39 48
40 $data = array(0 => 'woot'); 49 $data = array(0 => 'woot');
41 $pluginManager->executeHooks('random', $data); 50 $this->pluginManager->executeHooks('random', $data);
42 $this->assertEquals('woot', $data[1]); 51 $this->assertEquals('woot', $data[1]);
43 52
44 $data = array(0 => 'woot'); 53 $data = array(0 => 'woot');
45 $pluginManager->executeHooks('random', $data, array('target' => 'test')); 54 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
46 $this->assertEquals('page test', $data[1]); 55 $this->assertEquals('page test', $data[1]);
47 56
48 $data = array(0 => 'woot'); 57 $data = array(0 => 'woot');
49 $pluginManager->executeHooks('random', $data, array('loggedin' => true)); 58 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
50 $this->assertEquals('loggedin', $data[1]); 59 $this->assertEquals('loggedin', $data[1]);
51 } 60 }
52 61
@@ -57,11 +66,8 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
57 */ 66 */
58 public function testPluginNotFound() 67 public function testPluginNotFound()
59 { 68 {
60 $pluginManager = PluginManager::getInstance(); 69 $this->pluginManager->load(array());
61 70 $this->pluginManager->load(array('nope', 'renope'));
62 $pluginManager->load(array());
63
64 $pluginManager->load(array('nope', 'renope'));
65 } 71 }
66 72
67 /** 73 /**
@@ -69,16 +75,14 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
69 */ 75 */
70 public function testGetPluginsMeta() 76 public function testGetPluginsMeta()
71 { 77 {
72 $pluginManager = PluginManager::getInstance();
73
74 PluginManager::$PLUGINS_PATH = self::$pluginPath; 78 PluginManager::$PLUGINS_PATH = self::$pluginPath;
75 $pluginManager->load(array(self::$pluginName)); 79 $this->pluginManager->load(array(self::$pluginName));
76 80
77 $expectedParameters = array( 81 $expectedParameters = array(
78 'pop' => '', 82 'pop' => '',
79 'hip' => '', 83 'hip' => '',
80 ); 84 );
81 $meta = $pluginManager->getPluginsMeta(); 85 $meta = $this->pluginManager->getPluginsMeta();
82 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); 86 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
83 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); 87 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
84 } 88 }
diff --git a/tests/Updater/DummyUpdater.php b/tests/Updater/DummyUpdater.php
index e9ef2aaa..a0be4413 100644
--- a/tests/Updater/DummyUpdater.php
+++ b/tests/Updater/DummyUpdater.php
@@ -11,14 +11,14 @@ class DummyUpdater extends Updater
11 /** 11 /**
12 * Object constructor. 12 * Object constructor.
13 * 13 *
14 * @param array $doneUpdates Updates which are already done. 14 * @param array $doneUpdates Updates which are already done.
15 * @param array $config Shaarli's configuration array. 15 * @param LinkDB $linkDB LinkDB instance.
16 * @param LinkDB $linkDB LinkDB instance. 16 * @param ConfigManager $conf Configuration Manager instance.
17 * @param boolean $isLoggedIn True if the user is logged in. 17 * @param boolean $isLoggedIn True if the user is logged in.
18 */ 18 */
19 public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn) 19 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
20 { 20 {
21 parent::__construct($doneUpdates, $config, $linkDB, $isLoggedIn); 21 parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn);
22 22
23 // Retrieve all update methods. 23 // Retrieve all update methods.
24 // For unit test, only retrieve final methods, 24 // For unit test, only retrieve final methods,
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index a29d9067..6bdce08b 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -1,5 +1,6 @@
1<?php 1<?php
2 2
3require_once 'application/config/ConfigManager.php';
3require_once 'tests/Updater/DummyUpdater.php'; 4require_once 'tests/Updater/DummyUpdater.php';
4 5
5/** 6/**
@@ -9,58 +10,26 @@ require_once 'tests/Updater/DummyUpdater.php';
9class UpdaterTest extends PHPUnit_Framework_TestCase 10class UpdaterTest extends PHPUnit_Framework_TestCase
10{ 11{
11 /** 12 /**
12 * @var array Configuration input set. 13 * @var string Path to test datastore.
13 */ 14 */
14 private static $configFields; 15 protected static $testDatastore = 'sandbox/datastore.php';
15 16
16 /** 17 /**
17 * @var string Path to test datastore. 18 * @var string Config file path (without extension).
18 */ 19 */
19 protected static $testDatastore = 'sandbox/datastore.php'; 20 protected static $configFile = 'tests/utils/config/configJson';
20 21
21 /** 22 /**
22 * Executed before each test. 23 * @var ConfigManager
23 */ 24 */
24 public function setUp() 25 protected $conf;
25 {
26 self::$configFields = array(
27 'login' => 'login',
28 'hash' => 'hash',
29 'salt' => 'salt',
30 'timezone' => 'Europe/Paris',
31 'title' => 'title',
32 'titleLink' => 'titleLink',
33 'redirector' => '',
34 'disablesessionprotection' => false,
35 'privateLinkByDefault' => false,
36 'config' => array(
37 'CONFIG_FILE' => 'tests/Updater/config.php',
38 'DATADIR' => 'tests/Updater',
39 'PAGECACHE' => 'sandbox/pagecache',
40 'config1' => 'config1data',
41 'config2' => 'config2data',
42 )
43 );
44 }
45 26
46 /** 27 /**
47 * Executed after each test. 28 * Executed before each test.
48 *
49 * @return void
50 */ 29 */
51 public function tearDown() 30 public function setUp()
52 { 31 {
53 if (is_file(self::$configFields['config']['CONFIG_FILE'])) { 32 $this->conf = new ConfigManager(self::$configFile);
54 unlink(self::$configFields['config']['CONFIG_FILE']);
55 }
56
57 if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) {
58 unlink(self::$configFields['config']['DATADIR'] . '/options.php');
59 }
60
61 if (is_file(self::$configFields['config']['DATADIR'] . '/updates.json')) {
62 unlink(self::$configFields['config']['DATADIR'] . '/updates.json');
63 }
64 } 33 }
65 34
66 /** 35 /**
@@ -69,9 +38,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
69 public function testReadEmptyUpdatesFile() 38 public function testReadEmptyUpdatesFile()
70 { 39 {
71 $this->assertEquals(array(), read_updates_file('')); 40 $this->assertEquals(array(), read_updates_file(''));
72 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 41 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
73 touch($updatesFile); 42 touch($updatesFile);
74 $this->assertEquals(array(), read_updates_file($updatesFile)); 43 $this->assertEquals(array(), read_updates_file($updatesFile));
44 unlink($updatesFile);
75 } 45 }
76 46
77 /** 47 /**
@@ -79,7 +49,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
79 */ 49 */
80 public function testReadWriteUpdatesFile() 50 public function testReadWriteUpdatesFile()
81 { 51 {
82 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 52 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
83 $updatesMethods = array('m1', 'm2', 'm3'); 53 $updatesMethods = array('m1', 'm2', 'm3');
84 54
85 write_updates_file($updatesFile, $updatesMethods); 55 write_updates_file($updatesFile, $updatesMethods);
@@ -91,6 +61,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
91 write_updates_file($updatesFile, $updatesMethods); 61 write_updates_file($updatesFile, $updatesMethods);
92 $readMethods = read_updates_file($updatesFile); 62 $readMethods = read_updates_file($updatesFile);
93 $this->assertEquals($readMethods, $updatesMethods); 63 $this->assertEquals($readMethods, $updatesMethods);
64 unlink($updatesFile);
94 } 65 }
95 66
96 /** 67 /**
@@ -112,10 +83,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
112 */ 83 */
113 public function testWriteUpdatesFileNotWritable() 84 public function testWriteUpdatesFileNotWritable()
114 { 85 {
115 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 86 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
116 touch($updatesFile); 87 touch($updatesFile);
117 chmod($updatesFile, 0444); 88 chmod($updatesFile, 0444);
118 @write_updates_file($updatesFile, array('test')); 89 try {
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception $e) {
92 unlink($updatesFile);
93 throw $e;
94 }
119 } 95 }
120 96
121 /** 97 /**
@@ -131,10 +107,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
131 'updateMethodDummy3', 107 'updateMethodDummy3',
132 'updateMethodException', 108 'updateMethodException',
133 ); 109 );
134 $updater = new DummyUpdater($updates, array(), array(), true); 110 $updater = new DummyUpdater($updates, array(), $this->conf, true);
135 $this->assertEquals(array(), $updater->update()); 111 $this->assertEquals(array(), $updater->update());
136 112
137 $updater = new DummyUpdater(array(), array(), array(), false); 113 $updater = new DummyUpdater(array(), array(), $this->conf, false);
138 $this->assertEquals(array(), $updater->update()); 114 $this->assertEquals(array(), $updater->update());
139 } 115 }
140 116
@@ -149,7 +125,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
149 'updateMethodDummy2', 125 'updateMethodDummy2',
150 'updateMethodDummy3', 126 'updateMethodDummy3',
151 ); 127 );
152 $updater = new DummyUpdater($updates, array(), array(), true); 128 $updater = new DummyUpdater($updates, array(), $this->conf, true);
153 $this->assertEquals($expectedUpdates, $updater->update()); 129 $this->assertEquals($expectedUpdates, $updater->update());
154 } 130 }
155 131
@@ -165,7 +141,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
165 ); 141 );
166 $expectedUpdate = array('updateMethodDummy2'); 142 $expectedUpdate = array('updateMethodDummy2');
167 143
168 $updater = new DummyUpdater($updates, array(), array(), true); 144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
169 $this->assertEquals($expectedUpdate, $updater->update()); 145 $this->assertEquals($expectedUpdate, $updater->update());
170 } 146 }
171 147
@@ -182,7 +158,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
182 'updateMethodDummy3', 158 'updateMethodDummy3',
183 ); 159 );
184 160
185 $updater = new DummyUpdater($updates, array(), array(), true); 161 $updater = new DummyUpdater($updates, array(), $this->conf, true);
186 $updater->update(); 162 $updater->update();
187 } 163 }
188 164
@@ -195,26 +171,28 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
195 */ 171 */
196 public function testUpdateMergeDeprecatedConfig() 172 public function testUpdateMergeDeprecatedConfig()
197 { 173 {
198 // init 174 $this->conf->setConfigFile('tests/utils/config/configPhp');
199 writeConfig(self::$configFields, true); 175 $this->conf->reset();
200 $configCopy = self::$configFields;
201 $invert = !$configCopy['privateLinkByDefault'];
202 $configCopy['privateLinkByDefault'] = $invert;
203 176
204 // Use writeConfig to create a options.php 177 $optionsFile = 'tests/Updater/options.php';
205 $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php'; 178 $options = '<?php
206 writeConfig($configCopy, true); 179$GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
207 181
208 $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); 182 // tmp config file.
183 $this->conf->setConfigFile('tests/Updater/config');
209 184
210 // merge configs 185 // merge configs
211 $updater = new Updater(array(), self::$configFields, array(), true); 186 $updater = new Updater(array(), array(), $this->conf, true);
187 // This writes a new config file in tests/Updater/config.php
212 $updater->updateMethodMergeDeprecatedConfigFile(); 188 $updater->updateMethodMergeDeprecatedConfigFile();
213 189
214 // make sure updated field is changed 190 // make sure updated field is changed
215 include self::$configFields['config']['CONFIG_FILE']; 191 $this->conf->reload();
216 $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']); 192 $this->assertTrue($this->conf->get('privacy.default_private_links'));
217 $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE'])); 193 $this->assertFalse(is_file($optionsFile));
194 // Delete the generated file.
195 unlink($this->conf->getConfigFileExt());
218 } 196 }
219 197
220 /** 198 /**
@@ -222,23 +200,67 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
222 */ 200 */
223 public function testMergeDeprecatedConfigNoFile() 201 public function testMergeDeprecatedConfigNoFile()
224 { 202 {
225 writeConfig(self::$configFields, true); 203 $updater = new Updater(array(), array(), $this->conf, true);
226
227 $updater = new Updater(array(), self::$configFields, array(), true);
228 $updater->updateMethodMergeDeprecatedConfigFile(); 204 $updater->updateMethodMergeDeprecatedConfigFile();
229 205
230 include self::$configFields['config']['CONFIG_FILE']; 206 $this->assertEquals('root', $this->conf->get('credentials.login'));
231 $this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
232 } 207 }
233 208
209 /**
210 * Test renameDashTags update method.
211 */
234 public function testRenameDashTags() 212 public function testRenameDashTags()
235 { 213 {
236 $refDB = new ReferenceLinkDB(); 214 $refDB = new ReferenceLinkDB();
237 $refDB->write(self::$testDatastore); 215 $refDB->write(self::$testDatastore);
238 $linkDB = new LinkDB(self::$testDatastore, true, false); 216 $linkDB = new LinkDB(self::$testDatastore, true, false);
239 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); 217 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
240 $updater = new Updater(array(), self::$configFields, $linkDB, true); 218 $updater = new Updater(array(), $linkDB, $this->conf, true);
241 $updater->updateMethodRenameDashTags(); 219 $updater->updateMethodRenameDashTags();
242 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); 220 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
243 } 221 }
222
223 /**
224 * Convert old PHP config file to JSON config.
225 */
226 public function testConfigToJson()
227 {
228 $configFile = 'tests/utils/config/configPhp';
229 $this->conf->setConfigFile($configFile);
230 $this->conf->reset();
231
232 // The ConfigIO is initialized with ConfigPhp.
233 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
234
235 $updater = new Updater(array(), array(), $this->conf, false);
236 $done = $updater->updateMethodConfigToJson();
237 $this->assertTrue($done);
238
239 // The ConfigIO has been updated to ConfigJson.
240 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
241 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
242
243 // Check JSON config data.
244 $this->conf->reload();
245 $this->assertEquals('root', $this->conf->get('credentials.login'));
246 $this->assertEquals('lala', $this->conf->get('redirector.url'));
247 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
248 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
249
250 rename($configFile . '.save.php', $configFile . '.php');
251 unlink($this->conf->getConfigFileExt());
252 }
253
254 /**
255 * Launch config conversion update with an existing JSON file => nothing to do.
256 */
257 public function testConfigToJsonNothingToDo()
258 {
259 $filetime = filemtime($this->conf->getConfigFileExt());
260 $updater = new Updater(array(), array(), $this->conf, false);
261 $done = $updater->updateMethodConfigToJson();
262 $this->assertTrue($done);
263 $expected = filemtime($this->conf->getConfigFileExt());
264 $this->assertEquals($expected, $filetime);
265 }
244} 266}
diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php
new file mode 100644
index 00000000..99c88820
--- /dev/null
+++ b/tests/config/ConfigJsonTest.php
@@ -0,0 +1,133 @@
1<?php
2
3require_once 'application/config/ConfigJson.php';
4
5/**
6 * Class ConfigJsonTest
7 */
8class ConfigJsonTest extends PHPUnit_Framework_TestCase
9{
10 /**
11 * @var ConfigJson
12 */
13 protected $configIO;
14
15 public function setUp()
16 {
17 $this->configIO = new ConfigJson();
18 }
19
20 /**
21 * Read a simple existing config file.
22 */
23 public function testRead()
24 {
25 $conf = $this->configIO->read('tests/utils/config/configJson.json.php');
26 $this->assertEquals('root', $conf['credentials']['login']);
27 $this->assertEquals('lala', $conf['redirector']['url']);
28 $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
29 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
30 }
31
32 /**
33 * Read a non existent config file -> empty array.
34 */
35 public function testReadNonExistent()
36 {
37 $this->assertEquals(array(), $this->configIO->read('nope'));
38 }
39
40 /**
41 * Read a non existent config file -> empty array.
42 *
43 * @expectedException Exception
44 * @expectedExceptionMessage An error occured while parsing JSON file: error code #4
45 */
46 public function testReadInvalidJson()
47 {
48 $this->configIO->read('tests/utils/config/configInvalid.json.php');
49 }
50
51 /**
52 * Write a new config file.
53 */
54 public function testWriteNew()
55 {
56 $dataFile = 'tests/utils/config/configWrite.json.php';
57 $data = array(
58 'credentials' => array(
59 'login' => 'root',
60 ),
61 'resource' => array(
62 'datastore' => 'data/datastore.php',
63 ),
64 'redirector' => array(
65 'url' => 'lala',
66 ),
67 'plugins' => array(
68 'WALLABAG_VERSION' => '1',
69 )
70 );
71 $this->configIO->write($dataFile, $data);
72 // PHP 5.3 doesn't support json pretty print.
73 if (defined('JSON_PRETTY_PRINT')) {
74 $expected = '{
75 "credentials": {
76 "login": "root"
77 },
78 "resource": {
79 "datastore": "data\/datastore.php"
80 },
81 "redirector": {
82 "url": "lala"
83 },
84 "plugins": {
85 "WALLABAG_VERSION": "1"
86 }
87}';
88 } else {
89 $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
90 }
91 $expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix();
92 $this->assertEquals($expected, file_get_contents($dataFile));
93 unlink($dataFile);
94 }
95
96 /**
97 * Overwrite an existing setting.
98 */
99 public function testOverwrite()
100 {
101 $source = 'tests/utils/config/configJson.json.php';
102 $dest = 'tests/utils/config/configOverwrite.json.php';
103 copy($source, $dest);
104 $conf = $this->configIO->read($dest);
105 $conf['redirector']['url'] = 'blabla';
106 $this->configIO->write($dest, $conf);
107 $conf = $this->configIO->read($dest);
108 $this->assertEquals('blabla', $conf['redirector']['url']);
109 unlink($dest);
110 }
111
112 /**
113 * Write to invalid path.
114 *
115 * @expectedException IOException
116 */
117 public function testWriteInvalidArray()
118 {
119 $conf = array('conf' => 'value');
120 @$this->configIO->write(array(), $conf);
121 }
122
123 /**
124 * Write to invalid path.
125 *
126 * @expectedException IOException
127 */
128 public function testWriteInvalidBlank()
129 {
130 $conf = array('conf' => 'value');
131 @$this->configIO->write('', $conf);
132 }
133}
diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php
new file mode 100644
index 00000000..436e3d67
--- /dev/null
+++ b/tests/config/ConfigManagerTest.php
@@ -0,0 +1,172 @@
1<?php
2
3/**
4 * Unit tests for Class ConfigManagerTest
5 *
6 * Note: it only test the manager with ConfigJson,
7 * ConfigPhp is only a workaround to handle the transition to JSON type.
8 */
9class ConfigManagerTest extends PHPUnit_Framework_TestCase
10{
11 /**
12 * @var ConfigManager
13 */
14 protected $conf;
15
16 public function setUp()
17 {
18 $this->conf = new ConfigManager('tests/utils/config/configJson');
19 }
20
21 /**
22 * Simple config test:
23 * 1. Set settings.
24 * 2. Check settings value.
25 */
26 public function testSetGet()
27 {
28 $this->conf->set('paramInt', 42);
29 $this->conf->set('paramString', 'value1');
30 $this->conf->set('paramBool', false);
31 $this->conf->set('paramArray', array('foo' => 'bar'));
32 $this->conf->set('paramNull', null);
33
34 $this->assertEquals(42, $this->conf->get('paramInt'));
35 $this->assertEquals('value1', $this->conf->get('paramString'));
36 $this->assertFalse($this->conf->get('paramBool'));
37 $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
38 $this->assertEquals(null, $this->conf->get('paramNull'));
39 }
40
41 /**
42 * Set/write/get config test:
43 * 1. Set settings.
44 * 2. Write it to the config file.
45 * 3. Read the file.
46 * 4. Check settings value.
47 */
48 public function testSetWriteGet()
49 {
50 $this->conf->set('paramInt', 42);
51 $this->conf->set('paramString', 'value1');
52 $this->conf->set('paramBool', false);
53 $this->conf->set('paramArray', array('foo' => 'bar'));
54 $this->conf->set('paramNull', null);
55
56 $this->conf->setConfigFile('tests/utils/config/configTmp');
57 $this->conf->write(true);
58 $this->conf->reload();
59 unlink($this->conf->getConfigFileExt());
60
61 $this->assertEquals(42, $this->conf->get('paramInt'));
62 $this->assertEquals('value1', $this->conf->get('paramString'));
63 $this->assertFalse($this->conf->get('paramBool'));
64 $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
65 $this->assertEquals(null, $this->conf->get('paramNull'));
66 }
67
68 /**
69 * Test set/write/get with nested keys.
70 */
71 public function testSetWriteGetNested()
72 {
73 $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
74
75 $this->conf->setConfigFile('tests/utils/config/configTmp');
76 $this->conf->write(true);
77 $this->conf->reload();
78 unlink($this->conf->getConfigFileExt());
79
80 $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
81 }
82
83 /**
84 * Set with an empty key.
85 *
86 * @expectedException Exception
87 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
88 */
89 public function testSetEmptyKey()
90 {
91 $this->conf->set('', 'stuff');
92 }
93
94 /**
95 * Set with an array key.
96 *
97 * @expectedException Exception
98 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
99 */
100 public function testSetArrayKey()
101 {
102 $this->conf->set(array('foo' => 'bar'), 'stuff');
103 }
104
105 /**
106 * Try to write the config without mandatory parameter (e.g. 'login').
107 *
108 * @expectedException MissingFieldConfigException
109 */
110 public function testWriteMissingParameter()
111 {
112 $this->conf->setConfigFile('tests/utils/config/configTmp');
113 $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
114 $this->conf->reload();
115
116 $this->conf->write(true);
117 }
118
119 /**
120 * Try to get non existent config keys.
121 */
122 public function testGetNonExistent()
123 {
124 $this->assertEquals('', $this->conf->get('nope.test'));
125 $this->assertEquals('default', $this->conf->get('nope.test', 'default'));
126 }
127
128 /**
129 * Test the 'exists' method with existent values.
130 */
131 public function testExistsOk()
132 {
133 $this->assertTrue($this->conf->exists('credentials.login'));
134 $this->assertTrue($this->conf->exists('config.foo'));
135 }
136
137 /**
138 * Test the 'exists' method with non existent or invalid values.
139 */
140 public function testExistsKo()
141 {
142 $this->assertFalse($this->conf->exists('nope'));
143 $this->assertFalse($this->conf->exists('nope.nope'));
144 $this->assertFalse($this->conf->exists(''));
145 $this->assertFalse($this->conf->exists(false));
146 }
147
148 /**
149 * Reset the ConfigManager instance.
150 */
151 public function testReset()
152 {
153 $confIO = $this->conf->getConfigIO();
154 $this->conf->reset();
155 $this->assertFalse($confIO === $this->conf->getConfigIO());
156 }
157
158 /**
159 * Reload the config from file.
160 */
161 public function testReload()
162 {
163 $this->conf->setConfigFile('tests/utils/config/configTmp');
164 $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
165 file_put_contents($this->conf->getConfigFileExt(), $newConf);
166 $this->conf->reload();
167 unlink($this->conf->getConfigFileExt());
168 // Previous conf no longer exists, and new values have been loaded.
169 $this->assertFalse($this->conf->exists('credentials.login'));
170 $this->assertEquals('value', $this->conf->get('key'));
171 }
172}
diff --git a/tests/config/ConfigPhpTest.php b/tests/config/ConfigPhpTest.php
new file mode 100644
index 00000000..58cd8d2a
--- /dev/null
+++ b/tests/config/ConfigPhpTest.php
@@ -0,0 +1,82 @@
1<?php
2
3require_once 'application/config/ConfigPhp.php';
4
5/**
6 * Class ConfigPhpTest
7 */
8class ConfigPhpTest extends PHPUnit_Framework_TestCase
9{
10 /**
11 * @var ConfigPhp
12 */
13 protected $configIO;
14
15 public function setUp()
16 {
17 $this->configIO = new ConfigPhp();
18 }
19
20 /**
21 * Read a simple existing config file.
22 */
23 public function testRead()
24 {
25 $conf = $this->configIO->read('tests/utils/config/configPhp.php');
26 $this->assertEquals('root', $conf['login']);
27 $this->assertEquals('lala', $conf['redirector']);
28 $this->assertEquals('data/datastore.php', $conf['config']['DATASTORE']);
29 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
30 }
31
32 /**
33 * Read a non existent config file -> empty array.
34 */
35 public function testReadNonExistent()
36 {
37 $this->assertEquals(array(), $this->configIO->read('nope'));
38 }
39
40 /**
41 * Write a new config file.
42 */
43 public function testWriteNew()
44 {
45 $dataFile = 'tests/utils/config/configWrite.php';
46 $data = array(
47 'login' => 'root',
48 'redirector' => 'lala',
49 'config' => array(
50 'DATASTORE' => 'data/datastore.php',
51 ),
52 'plugins' => array(
53 'WALLABAG_VERSION' => '1',
54 )
55 );
56 $this->configIO->write($dataFile, $data);
57 $expected = '<?php
58$GLOBALS[\'login\'] = \'root\';
59$GLOBALS[\'redirector\'] = \'lala\';
60$GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
61$GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
62';
63 $this->assertEquals($expected, file_get_contents($dataFile));
64 unlink($dataFile);
65 }
66
67 /**
68 * Overwrite an existing setting.
69 */
70 public function testOverwrite()
71 {
72 $source = 'tests/utils/config/configPhp.php';
73 $dest = 'tests/utils/config/configOverwrite.php';
74 copy($source, $dest);
75 $conf = $this->configIO->read($dest);
76 $conf['redirector'] = 'blabla';
77 $this->configIO->write($dest, $conf);
78 $conf = $this->configIO->read($dest);
79 $this->assertEquals('blabla', $conf['redirector']);
80 unlink($dest);
81 }
82}
diff --git a/tests/config/ConfigPluginTest.php b/tests/config/ConfigPluginTest.php
new file mode 100644
index 00000000..716631b0
--- /dev/null
+++ b/tests/config/ConfigPluginTest.php
@@ -0,0 +1,121 @@
1<?php
2/**
3 * Config' tests
4 */
5
6require_once 'application/config/ConfigPlugin.php';
7
8/**
9 * Unitary tests for Shaarli config related functions
10 */
11class ConfigPluginTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * Test save_plugin_config with valid data.
15 *
16 * @throws PluginConfigOrderException
17 */
18 public function testSavePluginConfigValid()
19 {
20 $data = array(
21 'order_plugin1' => 2, // no plugin related
22 'plugin2' => 0, // new - at the end
23 'plugin3' => 0, // 2nd
24 'order_plugin3' => 8,
25 'plugin4' => 0, // 1st
26 'order_plugin4' => 5,
27 );
28
29 $expected = array(
30 'plugin3',
31 'plugin4',
32 'plugin2',
33 );
34
35 $out = save_plugin_config($data);
36 $this->assertEquals($expected, $out);
37 }
38
39 /**
40 * Test save_plugin_config with invalid data.
41 *
42 * @expectedException PluginConfigOrderException
43 */
44 public function testSavePluginConfigInvalid()
45 {
46 $data = array(
47 'plugin2' => 0,
48 'plugin3' => 0,
49 'order_plugin3' => 0,
50 'plugin4' => 0,
51 'order_plugin4' => 0,
52 );
53
54 save_plugin_config($data);
55 }
56
57 /**
58 * Test save_plugin_config without data.
59 */
60 public function testSavePluginConfigEmpty()
61 {
62 $this->assertEquals(array(), save_plugin_config(array()));
63 }
64
65 /**
66 * Test validate_plugin_order with valid data.
67 */
68 public function testValidatePluginOrderValid()
69 {
70 $data = array(
71 'order_plugin1' => 2,
72 'plugin2' => 0,
73 'plugin3' => 0,
74 'order_plugin3' => 1,
75 'plugin4' => 0,
76 'order_plugin4' => 5,
77 );
78
79 $this->assertTrue(validate_plugin_order($data));
80 }
81
82 /**
83 * Test validate_plugin_order with invalid data.
84 */
85 public function testValidatePluginOrderInvalid()
86 {
87 $data = array(
88 'order_plugin1' => 2,
89 'order_plugin3' => 1,
90 'order_plugin4' => 1,
91 );
92
93 $this->assertFalse(validate_plugin_order($data));
94 }
95
96 /**
97 * Test load_plugin_parameter_values.
98 */
99 public function testLoadPluginParameterValues()
100 {
101 $plugins = array(
102 'plugin_name' => array(
103 'parameters' => array(
104 'param1' => true,
105 'param2' => false,
106 'param3' => '',
107 )
108 )
109 );
110
111 $parameters = array(
112 'param1' => 'value1',
113 'param2' => 'value2',
114 );
115
116 $result = load_plugin_parameter_values($plugins, $parameters);
117 $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']);
118 $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']);
119 $this->assertEquals('', $result['plugin_name']['parameters']['param3']);
120 }
121}
diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php
index 8bf17bf1..d73e666a 100644
--- a/tests/plugins/PluginReadityourselfTest.php
+++ b/tests/plugins/PluginReadityourselfTest.php
@@ -4,6 +4,8 @@
4 * PluginReadityourselfTest.php.php 4 * PluginReadityourselfTest.php.php
5 */ 5 */
6 6
7// FIXME! add an init method.
8$conf = new ConfigManager('');
7require_once 'plugins/readityourself/readityourself.php'; 9require_once 'plugins/readityourself/readityourself.php';
8 10
9/** 11/**
@@ -25,7 +27,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
25 */ 27 */
26 function testReadityourselfLinklist() 28 function testReadityourselfLinklist()
27 { 29 {
28 $GLOBALS['plugins']['READITYOUSELF_URL'] = 'value'; 30 $conf = new ConfigManager('');
31 $conf->set('plugins.READITYOUSELF_URL', 'value');
29 $str = 'http://randomstr.com/test'; 32 $str = 'http://randomstr.com/test';
30 $data = array( 33 $data = array(
31 'title' => $str, 34 'title' => $str,
@@ -36,7 +39,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
36 ) 39 )
37 ); 40 );
38 41
39 $data = hook_readityourself_render_linklist($data); 42 $data = hook_readityourself_render_linklist($data, $conf);
40 $link = $data['links'][0]; 43 $link = $data['links'][0];
41 // data shouldn't be altered 44 // data shouldn't be altered
42 $this->assertEquals($str, $data['title']); 45 $this->assertEquals($str, $data['title']);
@@ -52,7 +55,8 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
52 */ 55 */
53 function testReadityourselfLinklistWithoutConfig() 56 function testReadityourselfLinklistWithoutConfig()
54 { 57 {
55 unset($GLOBALS['plugins']['READITYOUSELF_URL']); 58 $conf = new ConfigManager('');
59 $conf->set('plugins.READITYOUSELF_URL', null);
56 $str = 'http://randomstr.com/test'; 60 $str = 'http://randomstr.com/test';
57 $data = array( 61 $data = array(
58 'title' => $str, 62 'title' => $str,
@@ -63,7 +67,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
63 ) 67 )
64 ); 68 );
65 69
66 $data = hook_readityourself_render_linklist($data); 70 $data = hook_readityourself_render_linklist($data, $conf);
67 $link = $data['links'][0]; 71 $link = $data['links'][0];
68 // data shouldn't be altered 72 // data shouldn't be altered
69 $this->assertEquals($str, $data['title']); 73 $this->assertEquals($str, $data['title']);
diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php
index 5d3a60e0..302ee296 100644
--- a/tests/plugins/PluginWallabagTest.php
+++ b/tests/plugins/PluginWallabagTest.php
@@ -4,6 +4,8 @@
4 * PluginWallabagTest.php.php 4 * PluginWallabagTest.php.php
5 */ 5 */
6 6
7// FIXME! add an init method.
8$conf = new ConfigManager('');
7require_once 'plugins/wallabag/wallabag.php'; 9require_once 'plugins/wallabag/wallabag.php';
8 10
9/** 11/**
@@ -25,7 +27,8 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
25 */ 27 */
26 function testWallabagLinklist() 28 function testWallabagLinklist()
27 { 29 {
28 $GLOBALS['plugins']['WALLABAG_URL'] = 'value'; 30 $conf = new ConfigManager('');
31 $conf->set('plugins.WALLABAG_URL', 'value');
29 $str = 'http://randomstr.com/test'; 32 $str = 'http://randomstr.com/test';
30 $data = array( 33 $data = array(
31 'title' => $str, 34 'title' => $str,
@@ -36,7 +39,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
36 ) 39 )
37 ); 40 );
38 41
39 $data = hook_wallabag_render_linklist($data); 42 $data = hook_wallabag_render_linklist($data, $conf);
40 $link = $data['links'][0]; 43 $link = $data['links'][0];
41 // data shouldn't be altered 44 // data shouldn't be altered
42 $this->assertEquals($str, $data['title']); 45 $this->assertEquals($str, $data['title']);
@@ -45,7 +48,6 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
45 // plugin data 48 // plugin data
46 $this->assertEquals(1, count($link['link_plugin'])); 49 $this->assertEquals(1, count($link['link_plugin']));
47 $this->assertNotFalse(strpos($link['link_plugin'][0], urlencode($str))); 50 $this->assertNotFalse(strpos($link['link_plugin'][0], urlencode($str)));
48 $this->assertNotFalse(strpos($link['link_plugin'][0], $GLOBALS['plugins']['WALLABAG_URL'])); 51 $this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
49 } 52 }
50} 53}
51
diff --git a/tests/utils/config/configInvalid.json.php b/tests/utils/config/configInvalid.json.php
new file mode 100644
index 00000000..167f2168
--- /dev/null
+++ b/tests/utils/config/configInvalid.json.php
@@ -0,0 +1,5 @@
1<?php /*
2{
3 bad: bad,
4}
5*/ ?> \ No newline at end of file
diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php
new file mode 100644
index 00000000..06a302e8
--- /dev/null
+++ b/tests/utils/config/configJson.json.php
@@ -0,0 +1,34 @@
1<?php /*
2{
3 "credentials": {
4 "login":"root",
5 "hash":"hash",
6 "salt":"salt"
7 },
8 "security": {
9 "session_protection_disabled":false
10 },
11 "general": {
12 "timezone":"Europe\/Paris",
13 "title": "Shaarli",
14 "header_link": "?"
15 },
16 "privacy": {
17 "default_private_links":true
18 },
19 "redirector": {
20 "url":"lala"
21 },
22 "config": {
23 "foo": "bar"
24 },
25 "resource": {
26 "datastore": "tests\/utils\/config\/datastore.php",
27 "data_dir": "tests\/utils\/config"
28 },
29 "plugins": {
30 "WALLABAG_VERSION": 1
31 }
32}
33*/ ?>
34
diff --git a/tests/utils/config/configPhp.php b/tests/utils/config/configPhp.php
new file mode 100644
index 00000000..0e034175
--- /dev/null
+++ b/tests/utils/config/configPhp.php
@@ -0,0 +1,14 @@
1<?php
2$GLOBALS['login'] = 'root';
3$GLOBALS['hash'] = 'hash';
4$GLOBALS['salt'] = 'salt';
5$GLOBALS['timezone'] = 'Europe/Paris';
6$GLOBALS['title'] = 'title';
7$GLOBALS['titleLink'] = 'titleLink';
8$GLOBALS['redirector'] = 'lala';
9$GLOBALS['disablesessionprotection'] = false;
10$GLOBALS['privateLinkByDefault'] = false;
11$GLOBALS['config']['DATADIR'] = 'tests/Updater';
12$GLOBALS['config']['PAGECACHE'] = 'sandbox/pagecache';
13$GLOBALS['config']['DATASTORE'] = 'data/datastore.php';
14$GLOBALS['plugins']['WALLABAG_VERSION'] = '1';