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