6 require_once 'application/Config.php';
9 * Unitary tests for Shaarli config related functions
11 class ConfigTest
extends PHPUnit_Framework_TestCase
13 // Configuration input set.
14 private static $configFields;
17 * Executed before each test.
19 public function setUp()
21 self
::$configFields = array(
25 'timezone' => 'Europe/Paris',
27 'titleLink' => 'titleLink',
29 'disablesessionprotection' => false,
30 'privateLinkByDefault' => false,
32 'CONFIG_FILE' => 'tests/config.php',
34 'config1' => 'config1data',
35 'config2' => 'config2data',
41 * Executed after each test.
45 public function tearDown()
47 if (is_file(self
::$configFields['config']['CONFIG_FILE'])) {
48 unlink(self
::$configFields['config']['CONFIG_FILE']);
53 * Test writeConfig function, valid use case, while being logged in.
55 public function testWriteConfig()
57 writeConfig(self
::$configFields, true);
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']);
74 * Test writeConfig option while logged in:
76 * 2. update fields, add new sub config, add new root config.
80 public function testWriteConfigFieldUpdate()
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);
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']));
97 * Test writeConfig function with an empty array.
99 * @expectedException MissingFieldConfigException
101 public function testWriteConfigEmpty()
103 writeConfig(array(), true);
107 * Test writeConfig function with a missing mandatory field.
109 * @expectedException MissingFieldConfigException
111 public function testWriteConfigMissingField()
113 unset(self
::$configFields['login']);
114 writeConfig(self
::$configFields, true);
118 * Test writeConfig function while being logged out, and there is no config file existing.
120 public function testWriteConfigLoggedOutNoFile()
122 writeConfig(self
::$configFields, false);
126 * Test writeConfig function while being logged out, and a config file already exists.
128 * @expectedException UnauthorizedConfigException
130 public function testWriteConfigLoggedOutWithFile()
132 file_put_contents(self
::$configFields['config']['CONFIG_FILE'], '');
133 writeConfig(self
::$configFields, false);
137 * Test save_plugin_config with valid data.
139 * @throws PluginConfigOrderException
141 public function testSavePluginConfigValid()
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,
158 $out = save_plugin_config($data);
159 $this->assertEquals($expected, $out);
163 * Test save_plugin_config with invalid data.
165 * @expectedException PluginConfigOrderException
167 public function testSavePluginConfigInvalid()
172 'order_plugin3' => 0,
174 'order_plugin4' => 0,
177 save_plugin_config($data);
181 * Test save_plugin_config without data.
183 public function testSavePluginConfigEmpty()
185 $this->assertEquals(array(), save_plugin_config(array()));
189 * Test validate_plugin_order with valid data.
191 public function testValidatePluginOrderValid()
194 'order_plugin1' => 2,
197 'order_plugin3' => 1,
199 'order_plugin4' => 5,
202 $this->assertTrue(validate_plugin_order($data));
206 * Test validate_plugin_order with invalid data.
208 public function testValidatePluginOrderInvalid()
211 'order_plugin1' => 2,
212 'order_plugin3' => 1,
213 'order_plugin4' => 1,
216 $this->assertFalse(validate_plugin_order($data));
220 * Test load_plugin_parameter_values.
222 public function testLoadPluginParameterValues()
225 'plugin_name' => array(
226 'parameters' => array(
235 'param1' => 'value1',
236 'param2' => 'value2',
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']);