]>
Commit | Line | Data |
---|---|---|
d06265fb A |
1 | <?php |
2 | /** | |
3 | * Config' tests | |
4 | */ | |
5 | ||
6 | require_once 'application/Config.php'; | |
7 | ||
8 | /** | |
9 | * Unitary tests for Shaarli config related functions | |
10 | */ | |
11 | class 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 mergeDeprecatedConfig while being logged in: | |
138 | * 1. init a config file. | |
139 | * 2. init a options.php file with update value. | |
140 | * 3. merge. | |
141 | * 4. check updated value in config file. | |
142 | */ | |
143 | public function testMergeDeprecatedConfig() | |
144 | { | |
145 | // init | |
146 | writeConfig(self::$configFields, true); | |
147 | $configCopy = self::$configFields; | |
148 | $invert = !$configCopy['privateLinkByDefault']; | |
149 | $configCopy['privateLinkByDefault'] = $invert; | |
150 | ||
151 | // Use writeConfig to create a options.php | |
152 | $configCopy['config']['CONFIG_FILE'] = 'tests/options.php'; | |
153 | writeConfig($configCopy, true); | |
154 | ||
155 | $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); | |
156 | ||
157 | // merge configs | |
158 | mergeDeprecatedConfig(self::$configFields, true); | |
159 | ||
160 | // make sure updated field is changed | |
161 | include self::$configFields['config']['CONFIG_FILE']; | |
162 | $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']); | |
163 | $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE'])); | |
164 | } | |
165 | ||
166 | /** | |
167 | * Test mergeDeprecatedConfig while being logged in without options file. | |
168 | */ | |
169 | public function testMergeDeprecatedConfigNoFile() | |
170 | { | |
171 | writeConfig(self::$configFields, true); | |
172 | mergeDeprecatedConfig(self::$configFields, true); | |
173 | ||
174 | include self::$configFields['config']['CONFIG_FILE']; | |
175 | $this->assertEquals(self::$configFields['login'], $GLOBALS['login']); | |
176 | } | |
dea0ba28 A |
177 | |
178 | /** | |
179 | * Test save_plugin_config with valid data. | |
180 | * | |
181 | * @throws PluginConfigOrderException | |
182 | */ | |
183 | public function testSavePluginConfigValid() | |
184 | { | |
185 | $data = array( | |
186 | 'order_plugin1' => 2, // no plugin related | |
187 | 'plugin2' => 0, // new - at the end | |
188 | 'plugin3' => 0, // 2nd | |
189 | 'order_plugin3' => 8, | |
190 | 'plugin4' => 0, // 1st | |
191 | 'order_plugin4' => 5, | |
192 | ); | |
193 | ||
194 | $expected = array( | |
195 | 'plugin3', | |
196 | 'plugin4', | |
197 | 'plugin2', | |
198 | ); | |
199 | ||
200 | $out = save_plugin_config($data); | |
201 | $this->assertEquals($expected, $out); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Test save_plugin_config with invalid data. | |
206 | * | |
207 | * @expectedException PluginConfigOrderException | |
208 | */ | |
209 | public function testSavePluginConfigInvalid() | |
210 | { | |
211 | $data = array( | |
212 | 'plugin2' => 0, | |
213 | 'plugin3' => 0, | |
214 | 'order_plugin3' => 0, | |
215 | 'plugin4' => 0, | |
216 | 'order_plugin4' => 0, | |
217 | ); | |
218 | ||
219 | save_plugin_config($data); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Test save_plugin_config without data. | |
224 | */ | |
225 | public function testSavePluginConfigEmpty() | |
226 | { | |
227 | $this->assertEquals(array(), save_plugin_config(array())); | |
228 | } | |
229 | ||
230 | /** | |
231 | * Test validate_plugin_order with valid data. | |
232 | */ | |
233 | public function testValidatePluginOrderValid() | |
234 | { | |
235 | $data = array( | |
236 | 'order_plugin1' => 2, | |
237 | 'plugin2' => 0, | |
238 | 'plugin3' => 0, | |
239 | 'order_plugin3' => 1, | |
240 | 'plugin4' => 0, | |
241 | 'order_plugin4' => 5, | |
242 | ); | |
243 | ||
244 | $this->assertTrue(validate_plugin_order($data)); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Test validate_plugin_order with invalid data. | |
249 | */ | |
250 | public function testValidatePluginOrderInvalid() | |
251 | { | |
252 | $data = array( | |
253 | 'order_plugin1' => 2, | |
254 | 'order_plugin3' => 1, | |
255 | 'order_plugin4' => 1, | |
256 | ); | |
257 | ||
258 | $this->assertFalse(validate_plugin_order($data)); | |
259 | } | |
260 | ||
261 | /** | |
262 | * Test load_plugin_parameter_values. | |
263 | */ | |
264 | public function testLoadPluginParameterValues() | |
265 | { | |
266 | $plugins = array( | |
267 | 'plugin_name' => array( | |
268 | 'parameters' => array( | |
269 | 'param1' => true, | |
270 | 'param2' => false, | |
271 | 'param3' => '', | |
272 | ) | |
273 | ) | |
274 | ); | |
275 | ||
276 | $parameters = array( | |
277 | 'param1' => 'value1', | |
278 | 'param2' => 'value2', | |
279 | ); | |
280 | ||
281 | $result = load_plugin_parameter_values($plugins, $parameters); | |
282 | $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']); | |
283 | $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']); | |
284 | $this->assertEquals('', $result['plugin_name']['parameters']['param3']); | |
285 | } | |
d06265fb | 286 | } |