aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/updater/UpdaterTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-13 12:07:13 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 12:07:13 +0200
commitd9f6275ebca035fec8331652c677981056793ccc (patch)
tree37a64baf4f0eba6b781040605965383d8aded2cc /tests/updater/UpdaterTest.php
parent38672ba0d1c722e5d6d33a58255ceb55e9410e46 (diff)
parentd63ff87a009313141ae684ec447b902562ff6ee7 (diff)
downloadShaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.gz
Shaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.zst
Shaarli-d9f6275ebca035fec8331652c677981056793ccc.zip
Merge branch 'v0.11' into stablestable
Diffstat (limited to 'tests/updater/UpdaterTest.php')
-rw-r--r--tests/updater/UpdaterTest.php825
1 files changed, 825 insertions, 0 deletions
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php
new file mode 100644
index 00000000..738b7361
--- /dev/null
+++ b/tests/updater/UpdaterTest.php
@@ -0,0 +1,825 @@
1<?php
2namespace Shaarli\Updater;
3
4use DateTime;
5use Exception;
6use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigJson;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Config\ConfigPhp;
10use Shaarli\Thumbnailer;
11
12require_once 'application/updater/UpdaterUtils.php';
13require_once 'tests/updater/DummyUpdater.php';
14require_once 'tests/utils/ReferenceLinkDB.php';
15require_once 'inc/rain.tpl.class.php';
16
17/**
18 * Class UpdaterTest.
19 * Runs unit tests against the updater class.
20 */
21class 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 * Test updateMethodEscapeMarkdown with markdown plugin disabled
512 * => setting markdown_escape set to true.
513 */
514 public function testEscapeMarkdownSettingToTrue()
515 {
516 $sandboxConf = 'sandbox/config';
517 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
518 $this->conf = new ConfigManager($sandboxConf);
519
520 $this->conf->set('general.enabled_plugins', []);
521 $updater = new Updater([], [], $this->conf, true);
522 $this->assertTrue($updater->updateMethodEscapeMarkdown());
523 $this->assertTrue($this->conf->get('security.markdown_escape'));
524
525 // reload from file
526 $this->conf = new ConfigManager($sandboxConf);
527 $this->assertTrue($this->conf->get('security.markdown_escape'));
528 }
529
530 /**
531 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
532 */
533 public function testEscapeMarkdownSettingNothingToDoEnabled()
534 {
535 $sandboxConf = 'sandbox/config';
536 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
537 $this->conf = new ConfigManager($sandboxConf);
538 $this->conf->set('security.markdown_escape', true);
539 $updater = new Updater([], [], $this->conf, true);
540 $this->assertTrue($updater->updateMethodEscapeMarkdown());
541 $this->assertTrue($this->conf->get('security.markdown_escape'));
542 }
543
544 /**
545 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
546 */
547 public function testEscapeMarkdownSettingNothingToDoDisabled()
548 {
549 $this->conf->set('security.markdown_escape', false);
550 $updater = new Updater([], [], $this->conf, true);
551 $this->assertTrue($updater->updateMethodEscapeMarkdown());
552 $this->assertFalse($this->conf->get('security.markdown_escape'));
553 }
554
555 /**
556 * Test updateMethodPiwikUrl with valid data
557 */
558 public function testUpdatePiwikUrlValid()
559 {
560 $sandboxConf = 'sandbox/config';
561 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
562 $this->conf = new ConfigManager($sandboxConf);
563 $url = 'mypiwik.tld';
564 $this->conf->set('plugins.PIWIK_URL', $url);
565 $updater = new Updater([], [], $this->conf, true);
566 $this->assertTrue($updater->updateMethodPiwikUrl());
567 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
568
569 // reload from file
570 $this->conf = new ConfigManager($sandboxConf);
571 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
572 }
573
574 /**
575 * Test updateMethodPiwikUrl without setting
576 */
577 public function testUpdatePiwikUrlEmpty()
578 {
579 $updater = new Updater([], [], $this->conf, true);
580 $this->assertTrue($updater->updateMethodPiwikUrl());
581 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
582 }
583
584 /**
585 * Test updateMethodPiwikUrl: valid URL, nothing to do
586 */
587 public function testUpdatePiwikUrlNothingToDo()
588 {
589 $url = 'https://mypiwik.tld';
590 $this->conf->set('plugins.PIWIK_URL', $url);
591 $updater = new Updater([], [], $this->conf, true);
592 $this->assertTrue($updater->updateMethodPiwikUrl());
593 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
594 }
595
596 /**
597 * Test updateMethodAtomDefault with show_atom set to false
598 * => update to true.
599 */
600 public function testUpdateMethodAtomDefault()
601 {
602 $sandboxConf = 'sandbox/config';
603 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
604 $this->conf = new ConfigManager($sandboxConf);
605 $this->conf->set('feed.show_atom', false);
606 $updater = new Updater([], [], $this->conf, true);
607 $this->assertTrue($updater->updateMethodAtomDefault());
608 $this->assertTrue($this->conf->get('feed.show_atom'));
609 // reload from file
610 $this->conf = new ConfigManager($sandboxConf);
611 $this->assertTrue($this->conf->get('feed.show_atom'));
612 }
613 /**
614 * Test updateMethodAtomDefault with show_atom not set.
615 * => nothing to do
616 */
617 public function testUpdateMethodAtomDefaultNoExist()
618 {
619 $sandboxConf = 'sandbox/config';
620 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
621 $this->conf = new ConfigManager($sandboxConf);
622 $updater = new Updater([], [], $this->conf, true);
623 $this->assertTrue($updater->updateMethodAtomDefault());
624 $this->assertTrue($this->conf->get('feed.show_atom'));
625 }
626 /**
627 * Test updateMethodAtomDefault with show_atom set to true.
628 * => nothing to do
629 */
630 public function testUpdateMethodAtomDefaultAlreadyTrue()
631 {
632 $sandboxConf = 'sandbox/config';
633 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
634 $this->conf = new ConfigManager($sandboxConf);
635 $this->conf->set('feed.show_atom', true);
636 $updater = new Updater([], [], $this->conf, true);
637 $this->assertTrue($updater->updateMethodAtomDefault());
638 $this->assertTrue($this->conf->get('feed.show_atom'));
639 }
640
641 /**
642 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
643 */
644 public function testUpdateMethodDownloadSizeAndTimeoutConf()
645 {
646 $sandboxConf = 'sandbox/config';
647 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
648 $this->conf = new ConfigManager($sandboxConf);
649 $updater = new Updater([], [], $this->conf, true);
650 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
651 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
652 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
653
654 $this->conf = new ConfigManager($sandboxConf);
655 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
656 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
657 }
658
659 /**
660 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
661 */
662 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
663 {
664 $sandboxConf = 'sandbox/config';
665 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
666 $this->conf = new ConfigManager($sandboxConf);
667 $this->conf->set('general.download_max_size', 38);
668 $this->conf->set('general.download_timeout', 70);
669 $updater = new Updater([], [], $this->conf, true);
670 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
671 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
672 $this->assertEquals(70, $this->conf->get('general.download_timeout'));
673 }
674
675 /**
676 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
677 */
678 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
679 {
680 $sandboxConf = 'sandbox/config';
681 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
682 $this->conf = new ConfigManager($sandboxConf);
683 $this->conf->set('general.download_max_size', 38);
684 $updater = new Updater([], [], $this->conf, true);
685 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
686 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
687 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
688 }
689
690 /**
691 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
692 */
693 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
694 {
695 $sandboxConf = 'sandbox/config';
696 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
697 $this->conf = new ConfigManager($sandboxConf);
698 $this->conf->set('general.download_timeout', 3);
699 $updater = new Updater([], [], $this->conf, true);
700 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
701 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
702 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
703 }
704
705 /**
706 * Test updateMethodWebThumbnailer with thumbnails enabled.
707 */
708 public function testUpdateMethodWebThumbnailerEnabled()
709 {
710 $this->conf->remove('thumbnails');
711 $this->conf->set('thumbnail.enable_thumbnails', true);
712 $updater = new Updater([], [], $this->conf, true, $_SESSION);
713 $this->assertTrue($updater->updateMethodWebThumbnailer());
714 $this->assertFalse($this->conf->exists('thumbnail'));
715 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
716 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
717 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
718 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
719 }
720
721 /**
722 * Test updateMethodWebThumbnailer with thumbnails disabled.
723 */
724 public function testUpdateMethodWebThumbnailerDisabled()
725 {
726 $this->conf->remove('thumbnails');
727 $this->conf->set('thumbnail.enable_thumbnails', false);
728 $updater = new Updater([], [], $this->conf, true, $_SESSION);
729 $this->assertTrue($updater->updateMethodWebThumbnailer());
730 $this->assertFalse($this->conf->exists('thumbnail'));
731 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
732 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
733 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
734 $this->assertTrue(empty($_SESSION['warnings']));
735 }
736
737 /**
738 * Test updateMethodWebThumbnailer with thumbnails disabled.
739 */
740 public function testUpdateMethodWebThumbnailerNothingToDo()
741 {
742 $updater = new Updater([], [], $this->conf, true, $_SESSION);
743 $this->assertTrue($updater->updateMethodWebThumbnailer());
744 $this->assertFalse($this->conf->exists('thumbnail'));
745 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
746 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
747 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
748 $this->assertTrue(empty($_SESSION['warnings']));
749 }
750
751 /**
752 * Test updateMethodSetSticky().
753 */
754 public function testUpdateStickyValid()
755 {
756 $blank = [
757 'id' => 1,
758 'url' => 'z',
759 'title' => '',
760 'description' => '',
761 'tags' => '',
762 'created' => new DateTime(),
763 ];
764 $links = [
765 1 => ['id' => 1] + $blank,
766 2 => ['id' => 2] + $blank,
767 ];
768 $refDB = new \ReferenceLinkDB();
769 $refDB->setLinks($links);
770 $refDB->write(self::$testDatastore);
771 $linkDB = new LinkDB(self::$testDatastore, true, false);
772
773 $updater = new Updater(array(), $linkDB, $this->conf, true);
774 $this->assertTrue($updater->updateMethodSetSticky());
775
776 $linkDB = new LinkDB(self::$testDatastore, true, false);
777 foreach ($linkDB as $link) {
778 $this->assertFalse($link['sticky']);
779 }
780 }
781
782 /**
783 * Test updateMethodSetSticky().
784 */
785 public function testUpdateStickyNothingToDo()
786 {
787 $blank = [
788 'id' => 1,
789 'url' => 'z',
790 'title' => '',
791 'description' => '',
792 'tags' => '',
793 'created' => new DateTime(),
794 ];
795 $links = [
796 1 => ['id' => 1, 'sticky' => true] + $blank,
797 2 => ['id' => 2] + $blank,
798 ];
799 $refDB = new \ReferenceLinkDB();
800 $refDB->setLinks($links);
801 $refDB->write(self::$testDatastore);
802 $linkDB = new LinkDB(self::$testDatastore, true, false);
803
804 $updater = new Updater(array(), $linkDB, $this->conf, true);
805 $this->assertTrue($updater->updateMethodSetSticky());
806
807 $linkDB = new LinkDB(self::$testDatastore, true, false);
808 $this->assertTrue($linkDB[1]['sticky']);
809 }
810
811 /**
812 * Test updateMethodRemoveRedirector().
813 */
814 public function testUpdateRemoveRedirector()
815 {
816 $sandboxConf = 'sandbox/config';
817 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
818 $this->conf = new ConfigManager($sandboxConf);
819 $updater = new Updater([], null, $this->conf, true);
820 $this->assertTrue($updater->updateMethodRemoveRedirector());
821 $this->assertFalse($this->conf->exists('redirector'));
822 $this->conf = new ConfigManager($sandboxConf);
823 $this->assertFalse($this->conf->exists('redirector'));
824 }
825}