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