aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/updater
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2018-12-03 23:49:20 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-12 23:11:19 +0100
commitbcf056c9d92e5240e645c76a4cdc8ae159693f9a (patch)
tree3991d2e0703276474a3fa5b0f3cccc63b2e74f19 /tests/updater
parent92c6439dbc41d8a2873dfebbc44e30d75c0c473d (diff)
downloadShaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.tar.gz
Shaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.tar.zst
Shaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.zip
namespacing: \Shaarli\Updater
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/updater')
-rw-r--r--tests/updater/DummyUpdater.php73
-rw-r--r--tests/updater/UpdaterTest.php815
2 files changed, 888 insertions, 0 deletions
diff --git a/tests/updater/DummyUpdater.php b/tests/updater/DummyUpdater.php
new file mode 100644
index 00000000..9e866f1f
--- /dev/null
+++ b/tests/updater/DummyUpdater.php
@@ -0,0 +1,73 @@
1<?php
2namespace Shaarli\Updater;
3
4use Exception;
5use ReflectionClass;
6use ReflectionMethod;
7use Shaarli\Bookmark\LinkDB;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class DummyUpdater.
12 * Extends updater to add update method designed for unit tests.
13 */
14class DummyUpdater extends Updater
15{
16 /**
17 * Object constructor.
18 *
19 * @param array $doneUpdates Updates which are already done.
20 * @param LinkDB $linkDB LinkDB instance.
21 * @param ConfigManager $conf Configuration Manager instance.
22 * @param boolean $isLoggedIn True if the user is logged in.
23 */
24 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
25 {
26 parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn);
27
28 // Retrieve all update methods.
29 // For unit test, only retrieve final methods,
30 $class = new ReflectionClass($this);
31 $this->methods = $class->getMethods(ReflectionMethod::IS_FINAL);
32 }
33
34 /**
35 * Update method 1.
36 *
37 * @return bool true.
38 */
39 final private function updateMethodDummy1()
40 {
41 return true;
42 }
43
44 /**
45 * Update method 2.
46 *
47 * @return bool true.
48 */
49 final private function updateMethodDummy2()
50 {
51 return true;
52 }
53
54 /**
55 * Update method 3.
56 *
57 * @return bool true.
58 */
59 final private function updateMethodDummy3()
60 {
61 return true;
62 }
63
64 /**
65 * Update method 4, raise an exception.
66 *
67 * @throws Exception error.
68 */
69 final private function updateMethodException()
70 {
71 throw new Exception('whatever');
72 }
73}
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php
new file mode 100644
index 00000000..d7df5963
--- /dev/null
+++ b/tests/updater/UpdaterTest.php
@@ -0,0 +1,815 @@
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 $redirectorUrl = '<script>alert("redirector");</script>';
291 $this->conf->set('general.title', $title);
292 $this->conf->set('general.header_link', $headerLink);
293 $this->conf->set('redirector.url', $redirectorUrl);
294 $updater = new Updater(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 $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url'));
301 unlink($sandbox . '.json.php');
302 }
303
304 /**
305 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
306 */
307 public function testUpdateApiSettings()
308 {
309 $confFile = 'sandbox/config';
310 copy(self::$configFile .'.json.php', $confFile .'.json.php');
311 $conf = new ConfigManager($confFile);
312 $updater = new Updater(array(), array(), $conf, true);
313
314 $this->assertFalse($conf->exists('api.enabled'));
315 $this->assertFalse($conf->exists('api.secret'));
316 $updater->updateMethodApiSettings();
317 $conf->reload();
318 $this->assertTrue($conf->get('api.enabled'));
319 $this->assertTrue($conf->exists('api.secret'));
320 unlink($confFile .'.json.php');
321 }
322
323 /**
324 * Test updateMethodApiSettings(): already set, do nothing.
325 */
326 public function testUpdateApiSettingsNothingToDo()
327 {
328 $confFile = 'sandbox/config';
329 copy(self::$configFile .'.json.php', $confFile .'.json.php');
330 $conf = new ConfigManager($confFile);
331 $conf->set('api.enabled', false);
332 $conf->set('api.secret', '');
333 $updater = new Updater(array(), array(), $conf, true);
334 $updater->updateMethodApiSettings();
335 $this->assertFalse($conf->get('api.enabled'));
336 $this->assertEmpty($conf->get('api.secret'));
337 unlink($confFile .'.json.php');
338 }
339
340 /**
341 * Test updateMethodDatastoreIds().
342 */
343 public function testDatastoreIds()
344 {
345 $links = array(
346 '20121206_182539' => array(
347 'linkdate' => '20121206_182539',
348 'title' => 'Geek and Poke',
349 'url' => 'http://geek-and-poke.com/',
350 'description' => 'desc',
351 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
352 'updated' => '20121206_190301',
353 'private' => false,
354 ),
355 '20121206_172539' => array(
356 'linkdate' => '20121206_172539',
357 'title' => 'UserFriendly - Samba',
358 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
359 'description' => '',
360 'tags' => 'samba cartoon web',
361 'private' => false,
362 ),
363 '20121206_142300' => array(
364 'linkdate' => '20121206_142300',
365 'title' => 'UserFriendly - Web Designer',
366 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
367 'description' => 'Naming conventions... #private',
368 'tags' => 'samba cartoon web',
369 'private' => true,
370 ),
371 );
372 $refDB = new \ReferenceLinkDB();
373 $refDB->setLinks($links);
374 $refDB->write(self::$testDatastore);
375 $linkDB = new LinkDB(self::$testDatastore, true, false);
376
377 $checksum = hash_file('sha1', self::$testDatastore);
378
379 $this->conf->set('resource.data_dir', 'sandbox');
380 $this->conf->set('resource.datastore', self::$testDatastore);
381
382 $updater = new Updater(array(), $linkDB, $this->conf, true);
383 $this->assertTrue($updater->updateMethodDatastoreIds());
384
385 $linkDB = new LinkDB(self::$testDatastore, true, false);
386
387 $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
388 $backup = $backup[0];
389
390 $this->assertFileExists($backup);
391 $this->assertEquals($checksum, hash_file('sha1', $backup));
392 unlink($backup);
393
394 $this->assertEquals(3, count($linkDB));
395 $this->assertTrue(isset($linkDB[0]));
396 $this->assertFalse(isset($linkDB[0]['linkdate']));
397 $this->assertEquals(0, $linkDB[0]['id']);
398 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
399 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
400 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
401 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
402 $this->assertTrue($linkDB[0]['private']);
403 $this->assertEquals(
404 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
405 $linkDB[0]['created']
406 );
407
408 $this->assertTrue(isset($linkDB[1]));
409 $this->assertFalse(isset($linkDB[1]['linkdate']));
410 $this->assertEquals(1, $linkDB[1]['id']);
411 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
412 $this->assertEquals(
413 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
414 $linkDB[1]['created']
415 );
416
417 $this->assertTrue(isset($linkDB[2]));
418 $this->assertFalse(isset($linkDB[2]['linkdate']));
419 $this->assertEquals(2, $linkDB[2]['id']);
420 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
421 $this->assertEquals(
422 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
423 $linkDB[2]['created']
424 );
425 $this->assertEquals(
426 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'),
427 $linkDB[2]['updated']
428 );
429 }
430
431 /**
432 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
433 */
434 public function testDatastoreIdsNothingToDo()
435 {
436 $refDB = new \ReferenceLinkDB();
437 $refDB->write(self::$testDatastore);
438 $linkDB = new LinkDB(self::$testDatastore, true, false);
439
440 $this->conf->set('resource.data_dir', 'sandbox');
441 $this->conf->set('resource.datastore', self::$testDatastore);
442
443 $checksum = hash_file('sha1', self::$testDatastore);
444 $updater = new Updater(array(), $linkDB, $this->conf, true);
445 $this->assertTrue($updater->updateMethodDatastoreIds());
446 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
447 }
448
449 /**
450 * Test defaultTheme update with default settings: nothing to do.
451 */
452 public function testDefaultThemeWithDefaultSettings()
453 {
454 $sandbox = 'sandbox/config';
455 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
456 $this->conf = new ConfigManager($sandbox);
457 $updater = new Updater([], [], $this->conf, true);
458 $this->assertTrue($updater->updateMethodDefaultTheme());
459
460 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
461 $this->assertEquals('default', $this->conf->get('resource.theme'));
462 $this->conf = new ConfigManager($sandbox);
463 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
464 $this->assertEquals('default', $this->conf->get('resource.theme'));
465 unlink($sandbox . '.json.php');
466 }
467
468 /**
469 * Test defaultTheme update with a custom theme in a subfolder
470 */
471 public function testDefaultThemeWithCustomTheme()
472 {
473 $theme = 'iamanartist';
474 $sandbox = 'sandbox/config';
475 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
476 $this->conf = new ConfigManager($sandbox);
477 mkdir('sandbox/'. $theme);
478 touch('sandbox/'. $theme .'/linklist.html');
479 $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
480 $updater = new Updater([], [], $this->conf, true);
481 $this->assertTrue($updater->updateMethodDefaultTheme());
482
483 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
484 $this->assertEquals($theme, $this->conf->get('resource.theme'));
485 $this->conf = new ConfigManager($sandbox);
486 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
487 $this->assertEquals($theme, $this->conf->get('resource.theme'));
488 unlink($sandbox . '.json.php');
489 unlink('sandbox/'. $theme .'/linklist.html');
490 rmdir('sandbox/'. $theme);
491 }
492
493 /**
494 * Test updateMethodEscapeMarkdown with markdown plugin enabled
495 * => setting markdown_escape set to false.
496 */
497 public function testEscapeMarkdownSettingToFalse()
498 {
499 $sandboxConf = 'sandbox/config';
500 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
501 $this->conf = new ConfigManager($sandboxConf);
502
503 $this->conf->set('general.enabled_plugins', ['markdown']);
504 $updater = new Updater([], [], $this->conf, true);
505 $this->assertTrue($updater->updateMethodEscapeMarkdown());
506 $this->assertFalse($this->conf->get('security.markdown_escape'));
507
508 // reload from file
509 $this->conf = new ConfigManager($sandboxConf);
510 $this->assertFalse($this->conf->get('security.markdown_escape'));
511 }
512
513
514 /**
515 * Test updateMethodEscapeMarkdown with markdown plugin disabled
516 * => setting markdown_escape set to true.
517 */
518 public function testEscapeMarkdownSettingToTrue()
519 {
520 $sandboxConf = 'sandbox/config';
521 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
522 $this->conf = new ConfigManager($sandboxConf);
523
524 $this->conf->set('general.enabled_plugins', []);
525 $updater = new Updater([], [], $this->conf, true);
526 $this->assertTrue($updater->updateMethodEscapeMarkdown());
527 $this->assertTrue($this->conf->get('security.markdown_escape'));
528
529 // reload from file
530 $this->conf = new ConfigManager($sandboxConf);
531 $this->assertTrue($this->conf->get('security.markdown_escape'));
532 }
533
534 /**
535 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
536 */
537 public function testEscapeMarkdownSettingNothingToDoEnabled()
538 {
539 $sandboxConf = 'sandbox/config';
540 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
541 $this->conf = new ConfigManager($sandboxConf);
542 $this->conf->set('security.markdown_escape', true);
543 $updater = new Updater([], [], $this->conf, true);
544 $this->assertTrue($updater->updateMethodEscapeMarkdown());
545 $this->assertTrue($this->conf->get('security.markdown_escape'));
546 }
547
548 /**
549 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
550 */
551 public function testEscapeMarkdownSettingNothingToDoDisabled()
552 {
553 $this->conf->set('security.markdown_escape', false);
554 $updater = new Updater([], [], $this->conf, true);
555 $this->assertTrue($updater->updateMethodEscapeMarkdown());
556 $this->assertFalse($this->conf->get('security.markdown_escape'));
557 }
558
559 /**
560 * Test updateMethodPiwikUrl with valid data
561 */
562 public function testUpdatePiwikUrlValid()
563 {
564 $sandboxConf = 'sandbox/config';
565 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
566 $this->conf = new ConfigManager($sandboxConf);
567 $url = 'mypiwik.tld';
568 $this->conf->set('plugins.PIWIK_URL', $url);
569 $updater = new Updater([], [], $this->conf, true);
570 $this->assertTrue($updater->updateMethodPiwikUrl());
571 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
572
573 // reload from file
574 $this->conf = new ConfigManager($sandboxConf);
575 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
576 }
577
578 /**
579 * Test updateMethodPiwikUrl without setting
580 */
581 public function testUpdatePiwikUrlEmpty()
582 {
583 $updater = new Updater([], [], $this->conf, true);
584 $this->assertTrue($updater->updateMethodPiwikUrl());
585 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
586 }
587
588 /**
589 * Test updateMethodPiwikUrl: valid URL, nothing to do
590 */
591 public function testUpdatePiwikUrlNothingToDo()
592 {
593 $url = 'https://mypiwik.tld';
594 $this->conf->set('plugins.PIWIK_URL', $url);
595 $updater = new Updater([], [], $this->conf, true);
596 $this->assertTrue($updater->updateMethodPiwikUrl());
597 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
598 }
599
600 /**
601 * Test updateMethodAtomDefault with show_atom set to false
602 * => update to true.
603 */
604 public function testUpdateMethodAtomDefault()
605 {
606 $sandboxConf = 'sandbox/config';
607 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
608 $this->conf = new ConfigManager($sandboxConf);
609 $this->conf->set('feed.show_atom', false);
610 $updater = new Updater([], [], $this->conf, true);
611 $this->assertTrue($updater->updateMethodAtomDefault());
612 $this->assertTrue($this->conf->get('feed.show_atom'));
613 // reload from file
614 $this->conf = new ConfigManager($sandboxConf);
615 $this->assertTrue($this->conf->get('feed.show_atom'));
616 }
617 /**
618 * Test updateMethodAtomDefault with show_atom not set.
619 * => nothing to do
620 */
621 public function testUpdateMethodAtomDefaultNoExist()
622 {
623 $sandboxConf = 'sandbox/config';
624 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
625 $this->conf = new ConfigManager($sandboxConf);
626 $updater = new Updater([], [], $this->conf, true);
627 $this->assertTrue($updater->updateMethodAtomDefault());
628 $this->assertTrue($this->conf->get('feed.show_atom'));
629 }
630 /**
631 * Test updateMethodAtomDefault with show_atom set to true.
632 * => nothing to do
633 */
634 public function testUpdateMethodAtomDefaultAlreadyTrue()
635 {
636 $sandboxConf = 'sandbox/config';
637 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
638 $this->conf = new ConfigManager($sandboxConf);
639 $this->conf->set('feed.show_atom', true);
640 $updater = new Updater([], [], $this->conf, true);
641 $this->assertTrue($updater->updateMethodAtomDefault());
642 $this->assertTrue($this->conf->get('feed.show_atom'));
643 }
644
645 /**
646 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
647 */
648 public function testUpdateMethodDownloadSizeAndTimeoutConf()
649 {
650 $sandboxConf = 'sandbox/config';
651 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
652 $this->conf = new ConfigManager($sandboxConf);
653 $updater = new Updater([], [], $this->conf, true);
654 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
655 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
656 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
657
658 $this->conf = new ConfigManager($sandboxConf);
659 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
660 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
661 }
662
663 /**
664 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
665 */
666 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
667 {
668 $sandboxConf = 'sandbox/config';
669 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
670 $this->conf = new ConfigManager($sandboxConf);
671 $this->conf->set('general.download_max_size', 38);
672 $this->conf->set('general.download_timeout', 70);
673 $updater = new Updater([], [], $this->conf, true);
674 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
675 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
676 $this->assertEquals(70, $this->conf->get('general.download_timeout'));
677 }
678
679 /**
680 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
681 */
682 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
683 {
684 $sandboxConf = 'sandbox/config';
685 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
686 $this->conf = new ConfigManager($sandboxConf);
687 $this->conf->set('general.download_max_size', 38);
688 $updater = new Updater([], [], $this->conf, true);
689 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
690 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
691 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
692 }
693
694 /**
695 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
696 */
697 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
698 {
699 $sandboxConf = 'sandbox/config';
700 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
701 $this->conf = new ConfigManager($sandboxConf);
702 $this->conf->set('general.download_timeout', 3);
703 $updater = new Updater([], [], $this->conf, true);
704 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
705 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
706 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
707 }
708
709 /**
710<<<<<<< HEAD
711 * Test updateMethodWebThumbnailer with thumbnails enabled.
712 */
713 public function testUpdateMethodWebThumbnailerEnabled()
714 {
715 $this->conf->remove('thumbnails');
716 $this->conf->set('thumbnail.enable_thumbnails', true);
717 $updater = new Updater([], [], $this->conf, true, $_SESSION);
718 $this->assertTrue($updater->updateMethodWebThumbnailer());
719 $this->assertFalse($this->conf->exists('thumbnail'));
720 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
721 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
722 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
723 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
724 }
725
726 /**
727 * Test updateMethodWebThumbnailer with thumbnails disabled.
728 */
729 public function testUpdateMethodWebThumbnailerDisabled()
730 {
731 $this->conf->remove('thumbnails');
732 $this->conf->set('thumbnail.enable_thumbnails', false);
733 $updater = new Updater([], [], $this->conf, true, $_SESSION);
734 $this->assertTrue($updater->updateMethodWebThumbnailer());
735 $this->assertFalse($this->conf->exists('thumbnail'));
736 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
737 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
738 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
739 $this->assertTrue(empty($_SESSION['warnings']));
740 }
741
742 /**
743 * Test updateMethodWebThumbnailer with thumbnails disabled.
744 */
745 public function testUpdateMethodWebThumbnailerNothingToDo()
746 {
747 $updater = new Updater([], [], $this->conf, true, $_SESSION);
748 $this->assertTrue($updater->updateMethodWebThumbnailer());
749 $this->assertFalse($this->conf->exists('thumbnail'));
750 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
751 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
752 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
753 $this->assertTrue(empty($_SESSION['warnings']));
754 }
755
756 /**
757 * Test updateMethodSetSticky().
758 */
759 public function testUpdateStickyValid()
760 {
761 $blank = [
762 'id' => 1,
763 'url' => 'z',
764 'title' => '',
765 'description' => '',
766 'tags' => '',
767 'created' => new DateTime(),
768 ];
769 $links = [
770 1 => ['id' => 1] + $blank,
771 2 => ['id' => 2] + $blank,
772 ];
773 $refDB = new \ReferenceLinkDB();
774 $refDB->setLinks($links);
775 $refDB->write(self::$testDatastore);
776 $linkDB = new LinkDB(self::$testDatastore, true, false);
777
778 $updater = new Updater(array(), $linkDB, $this->conf, true);
779 $this->assertTrue($updater->updateMethodSetSticky());
780
781 $linkDB = new LinkDB(self::$testDatastore, true, false);
782 foreach ($linkDB as $link) {
783 $this->assertFalse($link['sticky']);
784 }
785 }
786
787 /**
788 * Test updateMethodSetSticky().
789 */
790 public function testUpdateStickyNothingToDo()
791 {
792 $blank = [
793 'id' => 1,
794 'url' => 'z',
795 'title' => '',
796 'description' => '',
797 'tags' => '',
798 'created' => new DateTime(),
799 ];
800 $links = [
801 1 => ['id' => 1, 'sticky' => true] + $blank,
802 2 => ['id' => 2] + $blank,
803 ];
804 $refDB = new \ReferenceLinkDB();
805 $refDB->setLinks($links);
806 $refDB->write(self::$testDatastore);
807 $linkDB = new LinkDB(self::$testDatastore, true, false);
808
809 $updater = new Updater(array(), $linkDB, $this->conf, true);
810 $this->assertTrue($updater->updateMethodSetSticky());
811
812 $linkDB = new LinkDB(self::$testDatastore, true, false);
813 $this->assertTrue($linkDB[1]['sticky']);
814 }
815}