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