]>
Commit | Line | Data |
---|---|---|
510377d2 | 1 | <?php |
3c66e564 V |
2 | use Shaarli\Config\ConfigJson; |
3 | use Shaarli\Config\ConfigManager; | |
4 | use Shaarli\Config\ConfigPhp; | |
510377d2 A |
5 | |
6 | require_once 'tests/Updater/DummyUpdater.php'; | |
a0df0651 | 7 | require_once 'inc/rain.tpl.class.php'; |
510377d2 A |
8 | |
9 | /** | |
10 | * Class UpdaterTest. | |
11 | * Runs unit tests against the Updater class. | |
12 | */ | |
13 | class UpdaterTest extends PHPUnit_Framework_TestCase | |
14 | { | |
21979ff1 A |
15 | /** |
16 | * @var string Path to test datastore. | |
17 | */ | |
18 | protected static $testDatastore = 'sandbox/datastore.php'; | |
19 | ||
684e662a | 20 | /** |
b74b96bf | 21 | * @var string Config file path (without extension). |
684e662a | 22 | */ |
28f26524 | 23 | protected static $configFile = 'sandbox/config'; |
684e662a A |
24 | |
25 | /** | |
26 | * @var ConfigManager | |
27 | */ | |
28 | protected $conf; | |
29 | ||
510377d2 A |
30 | /** |
31 | * Executed before each test. | |
32 | */ | |
33 | public function setUp() | |
34 | { | |
28f26524 | 35 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); |
278d9ee2 | 36 | $this->conf = new ConfigManager(self::$configFile); |
510377d2 A |
37 | } |
38 | ||
39 | /** | |
40 | * Test read_updates_file with an empty/missing file. | |
41 | */ | |
42 | public function testReadEmptyUpdatesFile() | |
43 | { | |
44 | $this->assertEquals(array(), read_updates_file('')); | |
894a3c4b | 45 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
46 | touch($updatesFile); |
47 | $this->assertEquals(array(), read_updates_file($updatesFile)); | |
da10377b | 48 | unlink($updatesFile); |
510377d2 A |
49 | } |
50 | ||
51 | /** | |
52 | * Test read/write updates file. | |
53 | */ | |
54 | public function testReadWriteUpdatesFile() | |
55 | { | |
894a3c4b | 56 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
57 | $updatesMethods = array('m1', 'm2', 'm3'); |
58 | ||
59 | write_updates_file($updatesFile, $updatesMethods); | |
60 | $readMethods = read_updates_file($updatesFile); | |
61 | $this->assertEquals($readMethods, $updatesMethods); | |
62 | ||
63 | // Update | |
64 | $updatesMethods[] = 'm4'; | |
65 | write_updates_file($updatesFile, $updatesMethods); | |
66 | $readMethods = read_updates_file($updatesFile); | |
67 | $this->assertEquals($readMethods, $updatesMethods); | |
da10377b | 68 | unlink($updatesFile); |
510377d2 A |
69 | } |
70 | ||
71 | /** | |
72 | * Test errors in write_updates_file(): empty updates file. | |
73 | * | |
74 | * @expectedException Exception | |
75 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | |
76 | */ | |
77 | public function testWriteEmptyUpdatesFile() | |
78 | { | |
79 | write_updates_file('', array('test')); | |
80 | } | |
81 | ||
82 | /** | |
83 | * Test errors in write_updates_file(): not writable updates file. | |
84 | * | |
85 | * @expectedException Exception | |
86 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | |
87 | */ | |
88 | public function testWriteUpdatesFileNotWritable() | |
89 | { | |
894a3c4b | 90 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
91 | touch($updatesFile); |
92 | chmod($updatesFile, 0444); | |
da10377b A |
93 | try { |
94 | @write_updates_file($updatesFile, array('test')); | |
95 | } catch (Exception $e) { | |
96 | unlink($updatesFile); | |
97 | throw $e; | |
98 | } | |
510377d2 A |
99 | } |
100 | ||
101 | /** | |
102 | * Test the update() method, with no update to run. | |
103 | * 1. Everything already run. | |
104 | * 2. User is logged out. | |
105 | */ | |
106 | public function testNoUpdates() | |
107 | { | |
108 | $updates = array( | |
109 | 'updateMethodDummy1', | |
110 | 'updateMethodDummy2', | |
111 | 'updateMethodDummy3', | |
112 | 'updateMethodException', | |
113 | ); | |
278d9ee2 | 114 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
115 | $this->assertEquals(array(), $updater->update()); |
116 | ||
278d9ee2 | 117 | $updater = new DummyUpdater(array(), array(), $this->conf, false); |
510377d2 A |
118 | $this->assertEquals(array(), $updater->update()); |
119 | } | |
120 | ||
121 | /** | |
122 | * Test the update() method, with all updates to run (except the failing one). | |
123 | */ | |
124 | public function testUpdatesFirstTime() | |
125 | { | |
126 | $updates = array('updateMethodException',); | |
127 | $expectedUpdates = array( | |
128 | 'updateMethodDummy1', | |
129 | 'updateMethodDummy2', | |
130 | 'updateMethodDummy3', | |
131 | ); | |
278d9ee2 | 132 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
133 | $this->assertEquals($expectedUpdates, $updater->update()); |
134 | } | |
135 | ||
136 | /** | |
137 | * Test the update() method, only one update to run. | |
138 | */ | |
139 | public function testOneUpdate() | |
140 | { | |
141 | $updates = array( | |
142 | 'updateMethodDummy1', | |
143 | 'updateMethodDummy3', | |
144 | 'updateMethodException', | |
145 | ); | |
146 | $expectedUpdate = array('updateMethodDummy2'); | |
147 | ||
278d9ee2 | 148 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
149 | $this->assertEquals($expectedUpdate, $updater->update()); |
150 | } | |
151 | ||
152 | /** | |
153 | * Test Update failed. | |
154 | * | |
155 | * @expectedException UpdaterException | |
156 | */ | |
157 | public function testUpdateFailed() | |
158 | { | |
159 | $updates = array( | |
160 | 'updateMethodDummy1', | |
161 | 'updateMethodDummy2', | |
162 | 'updateMethodDummy3', | |
163 | ); | |
164 | ||
278d9ee2 | 165 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
166 | $updater->update(); |
167 | } | |
168 | ||
169 | /** | |
170 | * Test update mergeDeprecatedConfig: | |
171 | * 1. init a config file. | |
172 | * 2. init a options.php file with update value. | |
173 | * 3. merge. | |
174 | * 4. check updated value in config file. | |
175 | */ | |
176 | public function testUpdateMergeDeprecatedConfig() | |
177 | { | |
278d9ee2 A |
178 | $this->conf->setConfigFile('tests/utils/config/configPhp'); |
179 | $this->conf->reset(); | |
684e662a A |
180 | |
181 | $optionsFile = 'tests/Updater/options.php'; | |
da10377b A |
182 | $options = '<?php |
183 | $GLOBALS[\'privateLinkByDefault\'] = true;'; | |
184 | file_put_contents($optionsFile, $options); | |
510377d2 | 185 | |
da10377b | 186 | // tmp config file. |
278d9ee2 | 187 | $this->conf->setConfigFile('tests/Updater/config'); |
510377d2 A |
188 | |
189 | // merge configs | |
278d9ee2 | 190 | $updater = new Updater(array(), array(), $this->conf, true); |
b74b96bf | 191 | // This writes a new config file in tests/Updater/config.php |
510377d2 A |
192 | $updater->updateMethodMergeDeprecatedConfigFile(); |
193 | ||
194 | // make sure updated field is changed | |
684e662a | 195 | $this->conf->reload(); |
894a3c4b | 196 | $this->assertTrue($this->conf->get('privacy.default_private_links')); |
684e662a | 197 | $this->assertFalse(is_file($optionsFile)); |
b74b96bf | 198 | // Delete the generated file. |
278d9ee2 | 199 | unlink($this->conf->getConfigFileExt()); |
510377d2 A |
200 | } |
201 | ||
202 | /** | |
203 | * Test mergeDeprecatedConfig in without options file. | |
204 | */ | |
205 | public function testMergeDeprecatedConfigNoFile() | |
206 | { | |
278d9ee2 | 207 | $updater = new Updater(array(), array(), $this->conf, true); |
510377d2 A |
208 | $updater->updateMethodMergeDeprecatedConfigFile(); |
209 | ||
da10377b | 210 | $this->assertEquals('root', $this->conf->get('credentials.login')); |
510377d2 | 211 | } |
21979ff1 | 212 | |
684e662a A |
213 | /** |
214 | * Test renameDashTags update method. | |
215 | */ | |
21979ff1 A |
216 | public function testRenameDashTags() |
217 | { | |
218 | $refDB = new ReferenceLinkDB(); | |
219 | $refDB->write(self::$testDatastore); | |
220 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
c3dfd899 | 221 | |
528a6f8a | 222 | $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
278d9ee2 | 223 | $updater = new Updater(array(), $linkDB, $this->conf, true); |
21979ff1 | 224 | $updater->updateMethodRenameDashTags(); |
528a6f8a | 225 | $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
21979ff1 | 226 | } |
b74b96bf A |
227 | |
228 | /** | |
229 | * Convert old PHP config file to JSON config. | |
230 | */ | |
231 | public function testConfigToJson() | |
232 | { | |
233 | $configFile = 'tests/utils/config/configPhp'; | |
278d9ee2 A |
234 | $this->conf->setConfigFile($configFile); |
235 | $this->conf->reset(); | |
b74b96bf A |
236 | |
237 | // The ConfigIO is initialized with ConfigPhp. | |
278d9ee2 | 238 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); |
b74b96bf | 239 | |
278d9ee2 | 240 | $updater = new Updater(array(), array(), $this->conf, false); |
b74b96bf A |
241 | $done = $updater->updateMethodConfigToJson(); |
242 | $this->assertTrue($done); | |
243 | ||
244 | // The ConfigIO has been updated to ConfigJson. | |
278d9ee2 A |
245 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); |
246 | $this->assertTrue(file_exists($this->conf->getConfigFileExt())); | |
b74b96bf A |
247 | |
248 | // Check JSON config data. | |
278d9ee2 A |
249 | $this->conf->reload(); |
250 | $this->assertEquals('root', $this->conf->get('credentials.login')); | |
894a3c4b A |
251 | $this->assertEquals('lala', $this->conf->get('redirector.url')); |
252 | $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); | |
278d9ee2 | 253 | $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); |
b74b96bf A |
254 | |
255 | rename($configFile . '.save.php', $configFile . '.php'); | |
278d9ee2 | 256 | unlink($this->conf->getConfigFileExt()); |
b74b96bf A |
257 | } |
258 | ||
259 | /** | |
260 | * Launch config conversion update with an existing JSON file => nothing to do. | |
261 | */ | |
262 | public function testConfigToJsonNothingToDo() | |
263 | { | |
278d9ee2 A |
264 | $filetime = filemtime($this->conf->getConfigFileExt()); |
265 | $updater = new Updater(array(), array(), $this->conf, false); | |
b74b96bf A |
266 | $done = $updater->updateMethodConfigToJson(); |
267 | $this->assertTrue($done); | |
278d9ee2 | 268 | $expected = filemtime($this->conf->getConfigFileExt()); |
b74b96bf A |
269 | $this->assertEquals($expected, $filetime); |
270 | } | |
b9f8b837 A |
271 | |
272 | /** | |
273 | * Test escapeUnescapedConfig with valid data. | |
274 | */ | |
275 | public function testEscapeConfig() | |
276 | { | |
277 | $sandbox = 'sandbox/config'; | |
cbfdcff2 | 278 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); |
b9f8b837 A |
279 | $this->conf = new ConfigManager($sandbox); |
280 | $title = '<script>alert("title");</script>'; | |
281 | $headerLink = '<script>alert("header_link");</script>'; | |
282 | $redirectorUrl = '<script>alert("redirector");</script>'; | |
283 | $this->conf->set('general.title', $title); | |
284 | $this->conf->set('general.header_link', $headerLink); | |
285 | $this->conf->set('redirector.url', $redirectorUrl); | |
286 | $updater = new Updater(array(), array(), $this->conf, true); | |
287 | $done = $updater->updateMethodEscapeUnescapedConfig(); | |
288 | $this->assertTrue($done); | |
289 | $this->conf->reload(); | |
290 | $this->assertEquals(escape($title), $this->conf->get('general.title')); | |
291 | $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); | |
292 | $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); | |
cbfdcff2 A |
293 | unlink($sandbox . '.json.php'); |
294 | } | |
295 | ||
296 | /** | |
297 | * Test updateMethodApiSettings(): create default settings for the API (enabled + secret). | |
298 | */ | |
299 | public function testUpdateApiSettings() | |
300 | { | |
301 | $confFile = 'sandbox/config'; | |
302 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | |
303 | $conf = new ConfigManager($confFile); | |
304 | $updater = new Updater(array(), array(), $conf, true); | |
305 | ||
306 | $this->assertFalse($conf->exists('api.enabled')); | |
307 | $this->assertFalse($conf->exists('api.secret')); | |
308 | $updater->updateMethodApiSettings(); | |
309 | $conf->reload(); | |
310 | $this->assertTrue($conf->get('api.enabled')); | |
311 | $this->assertTrue($conf->exists('api.secret')); | |
312 | unlink($confFile .'.json.php'); | |
313 | } | |
314 | ||
315 | /** | |
316 | * Test updateMethodApiSettings(): already set, do nothing. | |
317 | */ | |
318 | public function testUpdateApiSettingsNothingToDo() | |
319 | { | |
320 | $confFile = 'sandbox/config'; | |
321 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | |
322 | $conf = new ConfigManager($confFile); | |
323 | $conf->set('api.enabled', false); | |
324 | $conf->set('api.secret', ''); | |
325 | $updater = new Updater(array(), array(), $conf, true); | |
326 | $updater->updateMethodApiSettings(); | |
327 | $this->assertFalse($conf->get('api.enabled')); | |
328 | $this->assertEmpty($conf->get('api.secret')); | |
329 | unlink($confFile .'.json.php'); | |
b9f8b837 | 330 | } |
c3dfd899 A |
331 | |
332 | /** | |
333 | * Test updateMethodDatastoreIds(). | |
334 | */ | |
335 | public function testDatastoreIds() | |
336 | { | |
337 | $links = array( | |
338 | '20121206_182539' => array( | |
339 | 'linkdate' => '20121206_182539', | |
340 | 'title' => 'Geek and Poke', | |
341 | 'url' => 'http://geek-and-poke.com/', | |
342 | 'description' => 'desc', | |
343 | 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', | |
344 | 'updated' => '20121206_190301', | |
345 | 'private' => false, | |
346 | ), | |
347 | '20121206_172539' => array( | |
348 | 'linkdate' => '20121206_172539', | |
349 | 'title' => 'UserFriendly - Samba', | |
350 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', | |
351 | 'description' => '', | |
352 | 'tags' => 'samba cartoon web', | |
353 | 'private' => false, | |
354 | ), | |
355 | '20121206_142300' => array( | |
356 | 'linkdate' => '20121206_142300', | |
357 | 'title' => 'UserFriendly - Web Designer', | |
358 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', | |
359 | 'description' => 'Naming conventions... #private', | |
360 | 'tags' => 'samba cartoon web', | |
361 | 'private' => true, | |
362 | ), | |
363 | ); | |
364 | $refDB = new ReferenceLinkDB(); | |
365 | $refDB->setLinks($links); | |
366 | $refDB->write(self::$testDatastore); | |
367 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
368 | ||
369 | $checksum = hash_file('sha1', self::$testDatastore); | |
370 | ||
371 | $this->conf->set('resource.data_dir', 'sandbox'); | |
372 | $this->conf->set('resource.datastore', self::$testDatastore); | |
373 | ||
374 | $updater = new Updater(array(), $linkDB, $this->conf, true); | |
375 | $this->assertTrue($updater->updateMethodDatastoreIds()); | |
376 | ||
377 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
378 | ||
379 | $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); | |
380 | $backup = $backup[0]; | |
381 | ||
382 | $this->assertFileExists($backup); | |
383 | $this->assertEquals($checksum, hash_file('sha1', $backup)); | |
384 | unlink($backup); | |
385 | ||
386 | $this->assertEquals(3, count($linkDB)); | |
387 | $this->assertTrue(isset($linkDB[0])); | |
388 | $this->assertFalse(isset($linkDB[0]['linkdate'])); | |
389 | $this->assertEquals(0, $linkDB[0]['id']); | |
390 | $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); | |
391 | $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); | |
392 | $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); | |
393 | $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); | |
394 | $this->assertTrue($linkDB[0]['private']); | |
d592daea | 395 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); |
c3dfd899 A |
396 | |
397 | $this->assertTrue(isset($linkDB[1])); | |
398 | $this->assertFalse(isset($linkDB[1]['linkdate'])); | |
399 | $this->assertEquals(1, $linkDB[1]['id']); | |
400 | $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); | |
d592daea | 401 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); |
c3dfd899 A |
402 | |
403 | $this->assertTrue(isset($linkDB[2])); | |
404 | $this->assertFalse(isset($linkDB[2]['linkdate'])); | |
405 | $this->assertEquals(2, $linkDB[2]['id']); | |
406 | $this->assertEquals('Geek and Poke', $linkDB[2]['title']); | |
d592daea A |
407 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']); |
408 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']); | |
c3dfd899 A |
409 | } |
410 | ||
411 | /** | |
412 | * Test updateMethodDatastoreIds() with the update already applied: nothing to do. | |
413 | */ | |
414 | public function testDatastoreIdsNothingToDo() | |
415 | { | |
416 | $refDB = new ReferenceLinkDB(); | |
417 | $refDB->write(self::$testDatastore); | |
418 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
419 | ||
420 | $this->conf->set('resource.data_dir', 'sandbox'); | |
421 | $this->conf->set('resource.datastore', self::$testDatastore); | |
422 | ||
423 | $checksum = hash_file('sha1', self::$testDatastore); | |
424 | $updater = new Updater(array(), $linkDB, $this->conf, true); | |
425 | $this->assertTrue($updater->updateMethodDatastoreIds()); | |
426 | $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); | |
427 | } | |
04a0e8ea A |
428 | |
429 | /** | |
430 | * Test defaultTheme update with default settings: nothing to do. | |
431 | */ | |
432 | public function testDefaultThemeWithDefaultSettings() | |
433 | { | |
434 | $sandbox = 'sandbox/config'; | |
435 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); | |
436 | $this->conf = new ConfigManager($sandbox); | |
437 | $updater = new Updater([], [], $this->conf, true); | |
438 | $this->assertTrue($updater->updateMethodDefaultTheme()); | |
439 | ||
440 | $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); | |
441 | $this->assertEquals('default', $this->conf->get('resource.theme')); | |
442 | $this->conf = new ConfigManager($sandbox); | |
443 | $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); | |
444 | $this->assertEquals('default', $this->conf->get('resource.theme')); | |
445 | unlink($sandbox . '.json.php'); | |
446 | } | |
447 | ||
448 | /** | |
449 | * Test defaultTheme update with a custom theme in a subfolder | |
450 | */ | |
451 | public function testDefaultThemeWithCustomTheme() | |
452 | { | |
453 | $theme = 'iamanartist'; | |
454 | $sandbox = 'sandbox/config'; | |
455 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); | |
456 | $this->conf = new ConfigManager($sandbox); | |
457 | mkdir('sandbox/'. $theme); | |
458 | touch('sandbox/'. $theme .'/linklist.html'); | |
459 | $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/'); | |
460 | $updater = new Updater([], [], $this->conf, true); | |
461 | $this->assertTrue($updater->updateMethodDefaultTheme()); | |
462 | ||
463 | $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); | |
464 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | |
465 | $this->conf = new ConfigManager($sandbox); | |
466 | $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); | |
467 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | |
468 | unlink($sandbox . '.json.php'); | |
469 | unlink('sandbox/'. $theme .'/linklist.html'); | |
470 | rmdir('sandbox/'. $theme); | |
471 | } | |
7dcbfde5 | 472 | |
e0376101 A |
473 | /** |
474 | * Test updateMethodEscapeMarkdown with markdown plugin enabled | |
475 | * => setting markdown_escape set to false. | |
476 | */ | |
477 | public function testEscapeMarkdownSettingToFalse() | |
478 | { | |
479 | $sandboxConf = 'sandbox/config'; | |
480 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
481 | $this->conf = new ConfigManager($sandboxConf); | |
482 | ||
483 | $this->conf->set('general.enabled_plugins', ['markdown']); | |
484 | $updater = new Updater([], [], $this->conf, true); | |
485 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
486 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
487 | ||
488 | // reload from file | |
489 | $this->conf = new ConfigManager($sandboxConf); | |
490 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
491 | } | |
492 | ||
493 | ||
494 | /** | |
495 | * Test updateMethodEscapeMarkdown with markdown plugin disabled | |
496 | * => setting markdown_escape set to true. | |
497 | */ | |
498 | public function testEscapeMarkdownSettingToTrue() | |
499 | { | |
500 | $sandboxConf = 'sandbox/config'; | |
501 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
502 | $this->conf = new ConfigManager($sandboxConf); | |
503 | ||
504 | $this->conf->set('general.enabled_plugins', []); | |
505 | $updater = new Updater([], [], $this->conf, true); | |
506 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
507 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
508 | ||
509 | // reload from file | |
510 | $this->conf = new ConfigManager($sandboxConf); | |
511 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
512 | } | |
513 | ||
514 | /** | |
515 | * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled) | |
516 | */ | |
517 | public function testEscapeMarkdownSettingNothingToDoEnabled() | |
518 | { | |
519 | $sandboxConf = 'sandbox/config'; | |
520 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
521 | $this->conf = new ConfigManager($sandboxConf); | |
522 | $this->conf->set('security.markdown_escape', true); | |
523 | $updater = new Updater([], [], $this->conf, true); | |
524 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
525 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
526 | } | |
527 | ||
528 | /** | |
529 | * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled) | |
530 | */ | |
531 | public function testEscapeMarkdownSettingNothingToDoDisabled() | |
532 | { | |
533 | $this->conf->set('security.markdown_escape', false); | |
534 | $updater = new Updater([], [], $this->conf, true); | |
535 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
536 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
537 | } | |
fe83d45c A |
538 | |
539 | /** | |
540 | * Test updateMethodPiwikUrl with valid data | |
541 | */ | |
542 | public function testUpdatePiwikUrlValid() | |
543 | { | |
544 | $sandboxConf = 'sandbox/config'; | |
545 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
546 | $this->conf = new ConfigManager($sandboxConf); | |
547 | $url = 'mypiwik.tld'; | |
548 | $this->conf->set('plugins.PIWIK_URL', $url); | |
549 | $updater = new Updater([], [], $this->conf, true); | |
550 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
551 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | |
552 | ||
553 | // reload from file | |
554 | $this->conf = new ConfigManager($sandboxConf); | |
555 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | |
556 | } | |
557 | ||
558 | /** | |
559 | * Test updateMethodPiwikUrl without setting | |
560 | */ | |
561 | public function testUpdatePiwikUrlEmpty() | |
562 | { | |
563 | $updater = new Updater([], [], $this->conf, true); | |
564 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
565 | $this->assertEmpty($this->conf->get('plugins.PIWIK_URL')); | |
566 | } | |
567 | ||
568 | /** | |
569 | * Test updateMethodPiwikUrl: valid URL, nothing to do | |
570 | */ | |
571 | public function testUpdatePiwikUrlNothingToDo() | |
572 | { | |
573 | $url = 'https://mypiwik.tld'; | |
574 | $this->conf->set('plugins.PIWIK_URL', $url); | |
575 | $updater = new Updater([], [], $this->conf, true); | |
576 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
577 | $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL')); | |
578 | } | |
2ea89aba A |
579 | |
580 | /** | |
581 | * Test updateMethodAtomDefault with show_atom set to false | |
582 | * => update to true. | |
583 | */ | |
584 | public function testUpdateMethodAtomDefault() | |
585 | { | |
586 | $sandboxConf = 'sandbox/config'; | |
587 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
588 | $this->conf = new ConfigManager($sandboxConf); | |
589 | $this->conf->set('feed.show_atom', false); | |
590 | $updater = new Updater([], [], $this->conf, true); | |
591 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
592 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
593 | // reload from file | |
594 | $this->conf = new ConfigManager($sandboxConf); | |
595 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
596 | } | |
597 | /** | |
598 | * Test updateMethodAtomDefault with show_atom not set. | |
599 | * => nothing to do | |
600 | */ | |
601 | public function testUpdateMethodAtomDefaultNoExist() | |
602 | { | |
603 | $sandboxConf = 'sandbox/config'; | |
604 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
605 | $this->conf = new ConfigManager($sandboxConf); | |
606 | $updater = new Updater([], [], $this->conf, true); | |
607 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
608 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
609 | } | |
610 | /** | |
611 | * Test updateMethodAtomDefault with show_atom set to true. | |
612 | * => nothing to do | |
613 | */ | |
614 | public function testUpdateMethodAtomDefaultAlreadyTrue() | |
615 | { | |
616 | $sandboxConf = 'sandbox/config'; | |
617 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
618 | $this->conf = new ConfigManager($sandboxConf); | |
619 | $this->conf->set('feed.show_atom', true); | |
620 | $updater = new Updater([], [], $this->conf, true); | |
621 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
622 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
623 | } | |
4ff3ed1c A |
624 | |
625 | /** | |
626 | * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined. | |
627 | */ | |
628 | public function testUpdateMethodDownloadSizeAndTimeoutConf() | |
629 | { | |
630 | $sandboxConf = 'sandbox/config'; | |
631 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
632 | $this->conf = new ConfigManager($sandboxConf); | |
633 | $updater = new Updater([], [], $this->conf, true); | |
634 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | |
635 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | |
636 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | |
637 | ||
638 | $this->conf = new ConfigManager($sandboxConf); | |
639 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | |
640 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | |
641 | } | |
642 | ||
643 | /** | |
644 | * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined. | |
645 | */ | |
646 | public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore() | |
647 | { | |
648 | $sandboxConf = 'sandbox/config'; | |
649 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
650 | $this->conf = new ConfigManager($sandboxConf); | |
651 | $this->conf->set('general.download_max_size', 38); | |
652 | $this->conf->set('general.download_timeout', 70); | |
653 | $updater = new Updater([], [], $this->conf, true); | |
654 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | |
655 | $this->assertEquals(38, $this->conf->get('general.download_max_size')); | |
656 | $this->assertEquals(70, $this->conf->get('general.download_timeout')); | |
657 | } | |
658 | ||
659 | /** | |
660 | * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here. | |
661 | */ | |
662 | public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize() | |
663 | { | |
664 | $sandboxConf = 'sandbox/config'; | |
665 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
666 | $this->conf = new ConfigManager($sandboxConf); | |
667 | $this->conf->set('general.download_max_size', 38); | |
668 | $updater = new Updater([], [], $this->conf, true); | |
669 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | |
670 | $this->assertEquals(38, $this->conf->get('general.download_max_size')); | |
671 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | |
672 | } | |
673 | ||
674 | /** | |
675 | * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here. | |
676 | */ | |
677 | public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout() | |
678 | { | |
679 | $sandboxConf = 'sandbox/config'; | |
680 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
681 | $this->conf = new ConfigManager($sandboxConf); | |
682 | $this->conf->set('general.download_timeout', 3); | |
683 | $updater = new Updater([], [], $this->conf, true); | |
684 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | |
685 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | |
686 | $this->assertEquals(3, $this->conf->get('general.download_timeout')); | |
687 | } | |
e85b7a05 A |
688 | |
689 | /** | |
28f26524 | 690 | * Test updateMethodWebThumbnailer with thumbnails enabled. |
e85b7a05 A |
691 | */ |
692 | public function testUpdateMethodWebThumbnailerEnabled() | |
693 | { | |
28f26524 | 694 | $this->conf->remove('thumbnails'); |
e85b7a05 | 695 | $this->conf->set('thumbnail.enable_thumbnails', true); |
28f26524 | 696 | $updater = new Updater([], [], $this->conf, true, $_SESSION); |
e85b7a05 A |
697 | $this->assertTrue($updater->updateMethodWebThumbnailer()); |
698 | $this->assertFalse($this->conf->exists('thumbnail')); | |
699 | $this->assertTrue($this->conf->get('thumbnails.enabled')); | |
700 | $this->assertEquals(125, $this->conf->get('thumbnails.width')); | |
701 | $this->assertEquals(90, $this->conf->get('thumbnails.height')); | |
28f26524 A |
702 | $this->assertContains('You have enabled thumbnails', $_SESSION['warnings'][0]); |
703 | } | |
704 | ||
705 | /** | |
706 | * Test updateMethodWebThumbnailer with thumbnails disabled. | |
707 | */ | |
708 | public function testUpdateMethodWebThumbnailerDisabled() | |
709 | { | |
710 | $this->conf->remove('thumbnails'); | |
711 | $this->conf->set('thumbnail.enable_thumbnails', false); | |
712 | $updater = new Updater([], [], $this->conf, true, $_SESSION); | |
713 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | |
714 | $this->assertFalse($this->conf->exists('thumbnail')); | |
715 | $this->assertFalse($this->conf->get('thumbnails.enabled')); | |
716 | $this->assertEquals(125, $this->conf->get('thumbnails.width')); | |
717 | $this->assertEquals(90, $this->conf->get('thumbnails.height')); | |
718 | $this->assertTrue(empty($_SESSION['warnings'])); | |
719 | } | |
720 | ||
721 | /** | |
722 | * Test updateMethodWebThumbnailer with thumbnails disabled. | |
723 | */ | |
724 | public function testUpdateMethodWebThumbnailerNothingToDo() | |
725 | { | |
726 | $updater = new Updater([], [], $this->conf, true, $_SESSION); | |
727 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | |
728 | $this->assertFalse($this->conf->exists('thumbnail')); | |
729 | $this->assertTrue($this->conf->get('thumbnails.enabled')); | |
730 | $this->assertEquals(90, $this->conf->get('thumbnails.width')); | |
731 | $this->assertEquals(53, $this->conf->get('thumbnails.height')); | |
732 | $this->assertTrue(empty($_SESSION['warnings'])); | |
e85b7a05 | 733 | } |
510377d2 | 734 | } |