diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-17 21:34:12 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 09:56:32 +0100 |
commit | e26e2060f5470ce8bf4c5973284bae07b8af170a (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/updater | |
parent | cf92b4dd1521241eefc58eaf6dcd202cd83969d8 (diff) | |
download | Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.gz Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.zst Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.zip |
Add and update unit test for the new system (Bookmark + Service)
See #1307
Diffstat (limited to 'tests/updater')
-rw-r--r-- | tests/updater/DummyUpdater.php | 13 | ||||
-rw-r--r-- | tests/updater/UpdaterTest.php | 684 |
2 files changed, 18 insertions, 679 deletions
diff --git a/tests/updater/DummyUpdater.php b/tests/updater/DummyUpdater.php index 9e866f1f..07c7f5c4 100644 --- a/tests/updater/DummyUpdater.php +++ b/tests/updater/DummyUpdater.php | |||
@@ -4,6 +4,7 @@ namespace Shaarli\Updater; | |||
4 | use Exception; | 4 | use Exception; |
5 | use ReflectionClass; | 5 | use ReflectionClass; |
6 | use ReflectionMethod; | 6 | use ReflectionMethod; |
7 | use Shaarli\Bookmark\BookmarkFileService; | ||
7 | use Shaarli\Bookmark\LinkDB; | 8 | use Shaarli\Bookmark\LinkDB; |
8 | use Shaarli\Config\ConfigManager; | 9 | use Shaarli\Config\ConfigManager; |
9 | 10 | ||
@@ -16,14 +17,14 @@ class DummyUpdater extends Updater | |||
16 | /** | 17 | /** |
17 | * Object constructor. | 18 | * Object constructor. |
18 | * | 19 | * |
19 | * @param array $doneUpdates Updates which are already done. | 20 | * @param array $doneUpdates Updates which are already done. |
20 | * @param LinkDB $linkDB LinkDB instance. | 21 | * @param BookmarkFileService $bookmarkService LinkDB instance. |
21 | * @param ConfigManager $conf Configuration Manager instance. | 22 | * @param ConfigManager $conf Configuration Manager instance. |
22 | * @param boolean $isLoggedIn True if the user is logged in. | 23 | * @param boolean $isLoggedIn True if the user is logged in. |
23 | */ | 24 | */ |
24 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) | 25 | public function __construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn) |
25 | { | 26 | { |
26 | parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn); | 27 | parent::__construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn); |
27 | 28 | ||
28 | // Retrieve all update methods. | 29 | // Retrieve all update methods. |
29 | // For unit test, only retrieve final methods, | 30 | // For unit test, only retrieve final methods, |
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php index ac87e33c..c689982b 100644 --- a/tests/updater/UpdaterTest.php +++ b/tests/updater/UpdaterTest.php | |||
@@ -1,15 +1,9 @@ | |||
1 | <?php | 1 | <?php |
2 | namespace Shaarli\Updater; | 2 | namespace Shaarli\Updater; |
3 | 3 | ||
4 | use DateTime; | ||
5 | use Exception; | 4 | use Exception; |
6 | use Shaarli\Bookmark\LinkDB; | ||
7 | use Shaarli\Config\ConfigJson; | ||
8 | use Shaarli\Config\ConfigManager; | 5 | use Shaarli\Config\ConfigManager; |
9 | use Shaarli\Config\ConfigPhp; | ||
10 | use Shaarli\Thumbnailer; | ||
11 | 6 | ||
12 | require_once 'application/updater/UpdaterUtils.php'; | ||
13 | require_once 'tests/updater/DummyUpdater.php'; | 7 | require_once 'tests/updater/DummyUpdater.php'; |
14 | require_once 'tests/utils/ReferenceLinkDB.php'; | 8 | require_once 'tests/utils/ReferenceLinkDB.php'; |
15 | require_once 'inc/rain.tpl.class.php'; | 9 | require_once 'inc/rain.tpl.class.php'; |
@@ -45,14 +39,14 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
45 | } | 39 | } |
46 | 40 | ||
47 | /** | 41 | /** |
48 | * Test read_updates_file with an empty/missing file. | 42 | * Test UpdaterUtils::read_updates_file with an empty/missing file. |
49 | */ | 43 | */ |
50 | public function testReadEmptyUpdatesFile() | 44 | public function testReadEmptyUpdatesFile() |
51 | { | 45 | { |
52 | $this->assertEquals(array(), read_updates_file('')); | 46 | $this->assertEquals(array(), UpdaterUtils::read_updates_file('')); |
53 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | 47 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
54 | touch($updatesFile); | 48 | touch($updatesFile); |
55 | $this->assertEquals(array(), read_updates_file($updatesFile)); | 49 | $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile)); |
56 | unlink($updatesFile); | 50 | unlink($updatesFile); |
57 | } | 51 | } |
58 | 52 | ||
@@ -64,31 +58,31 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
64 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | 58 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
65 | $updatesMethods = array('m1', 'm2', 'm3'); | 59 | $updatesMethods = array('m1', 'm2', 'm3'); |
66 | 60 | ||
67 | write_updates_file($updatesFile, $updatesMethods); | 61 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); |
68 | $readMethods = read_updates_file($updatesFile); | 62 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); |
69 | $this->assertEquals($readMethods, $updatesMethods); | 63 | $this->assertEquals($readMethods, $updatesMethods); |
70 | 64 | ||
71 | // Update | 65 | // Update |
72 | $updatesMethods[] = 'm4'; | 66 | $updatesMethods[] = 'm4'; |
73 | write_updates_file($updatesFile, $updatesMethods); | 67 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); |
74 | $readMethods = read_updates_file($updatesFile); | 68 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); |
75 | $this->assertEquals($readMethods, $updatesMethods); | 69 | $this->assertEquals($readMethods, $updatesMethods); |
76 | unlink($updatesFile); | 70 | unlink($updatesFile); |
77 | } | 71 | } |
78 | 72 | ||
79 | /** | 73 | /** |
80 | * Test errors in write_updates_file(): empty updates file. | 74 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. |
81 | * | 75 | * |
82 | * @expectedException Exception | 76 | * @expectedException Exception |
83 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | 77 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ |
84 | */ | 78 | */ |
85 | public function testWriteEmptyUpdatesFile() | 79 | public function testWriteEmptyUpdatesFile() |
86 | { | 80 | { |
87 | write_updates_file('', array('test')); | 81 | UpdaterUtils::write_updates_file('', array('test')); |
88 | } | 82 | } |
89 | 83 | ||
90 | /** | 84 | /** |
91 | * Test errors in write_updates_file(): not writable updates file. | 85 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. |
92 | * | 86 | * |
93 | * @expectedException Exception | 87 | * @expectedException Exception |
94 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | 88 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ |
@@ -99,7 +93,7 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
99 | touch($updatesFile); | 93 | touch($updatesFile); |
100 | chmod($updatesFile, 0444); | 94 | chmod($updatesFile, 0444); |
101 | try { | 95 | try { |
102 | @write_updates_file($updatesFile, array('test')); | 96 | @UpdaterUtils::write_updates_file($updatesFile, array('test')); |
103 | } catch (Exception $e) { | 97 | } catch (Exception $e) { |
104 | unlink($updatesFile); | 98 | unlink($updatesFile); |
105 | throw $e; | 99 | throw $e; |
@@ -173,660 +167,4 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
173 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | 167 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
174 | $updater->update(); | 168 | $updater->update(); |
175 | } | 169 | } |
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 | if (isset($_SESSION['warnings'])) { | ||
728 | unset($_SESSION['warnings']); | ||
729 | } | ||
730 | $this->conf->remove('thumbnails'); | ||
731 | $this->conf->set('thumbnail.enable_thumbnails', false); | ||
732 | $updater = new Updater([], [], $this->conf, true, $_SESSION); | ||
733 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | ||
734 | $this->assertFalse($this->conf->exists('thumbnail')); | ||
735 | $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode')); | ||
736 | $this->assertEquals(125, $this->conf->get('thumbnails.width')); | ||
737 | $this->assertEquals(90, $this->conf->get('thumbnails.height')); | ||
738 | $this->assertTrue(empty($_SESSION['warnings'])); | ||
739 | } | ||
740 | |||
741 | /** | ||
742 | * Test updateMethodWebThumbnailer with thumbnails disabled. | ||
743 | */ | ||
744 | public function testUpdateMethodWebThumbnailerNothingToDo() | ||
745 | { | ||
746 | if (isset($_SESSION['warnings'])) { | ||
747 | unset($_SESSION['warnings']); | ||
748 | } | ||
749 | $updater = new Updater([], [], $this->conf, true, $_SESSION); | ||
750 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | ||
751 | $this->assertFalse($this->conf->exists('thumbnail')); | ||
752 | $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode')); | ||
753 | $this->assertEquals(90, $this->conf->get('thumbnails.width')); | ||
754 | $this->assertEquals(53, $this->conf->get('thumbnails.height')); | ||
755 | $this->assertTrue(empty($_SESSION['warnings'])); | ||
756 | } | ||
757 | |||
758 | /** | ||
759 | * Test updateMethodSetSticky(). | ||
760 | */ | ||
761 | public function testUpdateStickyValid() | ||
762 | { | ||
763 | $blank = [ | ||
764 | 'id' => 1, | ||
765 | 'url' => 'z', | ||
766 | 'title' => '', | ||
767 | 'description' => '', | ||
768 | 'tags' => '', | ||
769 | 'created' => new DateTime(), | ||
770 | ]; | ||
771 | $links = [ | ||
772 | 1 => ['id' => 1] + $blank, | ||
773 | 2 => ['id' => 2] + $blank, | ||
774 | ]; | ||
775 | $refDB = new \ReferenceLinkDB(); | ||
776 | $refDB->setLinks($links); | ||
777 | $refDB->write(self::$testDatastore); | ||
778 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
779 | |||
780 | $updater = new Updater(array(), $linkDB, $this->conf, true); | ||
781 | $this->assertTrue($updater->updateMethodSetSticky()); | ||
782 | |||
783 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
784 | foreach ($linkDB as $link) { | ||
785 | $this->assertFalse($link['sticky']); | ||
786 | } | ||
787 | } | ||
788 | |||
789 | /** | ||
790 | * Test updateMethodSetSticky(). | ||
791 | */ | ||
792 | public function testUpdateStickyNothingToDo() | ||
793 | { | ||
794 | $blank = [ | ||
795 | 'id' => 1, | ||
796 | 'url' => 'z', | ||
797 | 'title' => '', | ||
798 | 'description' => '', | ||
799 | 'tags' => '', | ||
800 | 'created' => new DateTime(), | ||
801 | ]; | ||
802 | $links = [ | ||
803 | 1 => ['id' => 1, 'sticky' => true] + $blank, | ||
804 | 2 => ['id' => 2] + $blank, | ||
805 | ]; | ||
806 | $refDB = new \ReferenceLinkDB(); | ||
807 | $refDB->setLinks($links); | ||
808 | $refDB->write(self::$testDatastore); | ||
809 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
810 | |||
811 | $updater = new Updater(array(), $linkDB, $this->conf, true); | ||
812 | $this->assertTrue($updater->updateMethodSetSticky()); | ||
813 | |||
814 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
815 | $this->assertTrue($linkDB[1]['sticky']); | ||
816 | } | ||
817 | |||
818 | /** | ||
819 | * Test updateMethodRemoveRedirector(). | ||
820 | */ | ||
821 | public function testUpdateRemoveRedirector() | ||
822 | { | ||
823 | $sandboxConf = 'sandbox/config'; | ||
824 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
825 | $this->conf = new ConfigManager($sandboxConf); | ||
826 | $updater = new Updater([], null, $this->conf, true); | ||
827 | $this->assertTrue($updater->updateMethodRemoveRedirector()); | ||
828 | $this->assertFalse($this->conf->exists('redirector')); | ||
829 | $this->conf = new ConfigManager($sandboxConf); | ||
830 | $this->assertFalse($this->conf->exists('redirector')); | ||
831 | } | ||
832 | } | 170 | } |