aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/updater
diff options
context:
space:
mode:
Diffstat (limited to 'tests/updater')
-rw-r--r--tests/updater/DummyUpdater.php21
-rw-r--r--tests/updater/UpdaterTest.php727
2 files changed, 72 insertions, 676 deletions
diff --git a/tests/updater/DummyUpdater.php b/tests/updater/DummyUpdater.php
index 9e866f1f..3403233f 100644
--- a/tests/updater/DummyUpdater.php
+++ b/tests/updater/DummyUpdater.php
@@ -4,6 +4,7 @@ namespace Shaarli\Updater;
4use Exception; 4use Exception;
5use ReflectionClass; 5use ReflectionClass;
6use ReflectionMethod; 6use ReflectionMethod;
7use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Bookmark\LinkDB; 8use Shaarli\Bookmark\LinkDB;
8use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
9 10
@@ -16,14 +17,14 @@ class DummyUpdater extends Updater
16 /** 17 /**
17 * Object constructor. 18 * Object constructor.
18 * 19 *
19 * @param array $doneUpdates Updates which are already done. 20 * @param array $doneUpdates Updates which are already done.
20 * @param LinkDB $linkDB LinkDB instance. 21 * @param BookmarkFileService $bookmarkService LinkDB instance.
21 * @param ConfigManager $conf Configuration Manager instance. 22 * @param ConfigManager $conf Configuration Manager instance.
22 * @param boolean $isLoggedIn True if the user is logged in. 23 * @param boolean $isLoggedIn True if the user is logged in.
23 */ 24 */
24 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) 25 public function __construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn)
25 { 26 {
26 parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn); 27 parent::__construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn);
27 28
28 // Retrieve all update methods. 29 // Retrieve all update methods.
29 // For unit test, only retrieve final methods, 30 // For unit test, only retrieve final methods,
@@ -36,7 +37,7 @@ class DummyUpdater extends Updater
36 * 37 *
37 * @return bool true. 38 * @return bool true.
38 */ 39 */
39 final private function updateMethodDummy1() 40 final protected function updateMethodDummy1()
40 { 41 {
41 return true; 42 return true;
42 } 43 }
@@ -46,7 +47,7 @@ class DummyUpdater extends Updater
46 * 47 *
47 * @return bool true. 48 * @return bool true.
48 */ 49 */
49 final private function updateMethodDummy2() 50 final protected function updateMethodDummy2()
50 { 51 {
51 return true; 52 return true;
52 } 53 }
@@ -56,7 +57,7 @@ class DummyUpdater extends Updater
56 * 57 *
57 * @return bool true. 58 * @return bool true.
58 */ 59 */
59 final private function updateMethodDummy3() 60 final protected function updateMethodDummy3()
60 { 61 {
61 return true; 62 return true;
62 } 63 }
@@ -66,7 +67,7 @@ class DummyUpdater extends Updater
66 * 67 *
67 * @throws Exception error. 68 * @throws Exception error.
68 */ 69 */
69 final private function updateMethodException() 70 final protected function updateMethodException()
70 { 71 {
71 throw new Exception('whatever'); 72 throw new Exception('whatever');
72 } 73 }
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php
index 93bc86c1..a6280b8c 100644
--- a/tests/updater/UpdaterTest.php
+++ b/tests/updater/UpdaterTest.php
@@ -1,24 +1,19 @@
1<?php 1<?php
2namespace Shaarli\Updater; 2namespace Shaarli\Updater;
3 3
4use DateTime;
5use Exception; 4use Exception;
6use Shaarli\Bookmark\LinkDB; 5use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Config\ConfigJson; 6use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
9use Shaarli\Config\ConfigPhp; 8use Shaarli\History;
10use Shaarli\Thumbnailer; 9use Shaarli\TestCase;
11 10
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 11
17/** 12/**
18 * Class UpdaterTest. 13 * Class UpdaterTest.
19 * Runs unit tests against the updater class. 14 * Runs unit tests against the updater class.
20 */ 15 */
21class UpdaterTest extends \PHPUnit\Framework\TestCase 16class UpdaterTest extends TestCase
22{ 17{
23 /** 18 /**
24 * @var string Path to test datastore. 19 * @var string Path to test datastore.
@@ -35,24 +30,38 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
35 */ 30 */
36 protected $conf; 31 protected $conf;
37 32
33 /** @var BookmarkServiceInterface */
34 protected $bookmarkService;
35
36 /** @var \ReferenceLinkDB */
37 protected $refDB;
38
39 /** @var Updater */
40 protected $updater;
41
38 /** 42 /**
39 * Executed before each test. 43 * Executed before each test.
40 */ 44 */
41 public function setUp() 45 protected function setUp(): void
42 { 46 {
47 $this->refDB = new \ReferenceLinkDB();
48 $this->refDB->write(self::$testDatastore);
49
43 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); 50 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
44 $this->conf = new ConfigManager(self::$configFile); 51 $this->conf = new ConfigManager(self::$configFile);
52 $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true);
53 $this->updater = new Updater([], $this->bookmarkService, $this->conf, true);
45 } 54 }
46 55
47 /** 56 /**
48 * Test read_updates_file with an empty/missing file. 57 * Test UpdaterUtils::read_updates_file with an empty/missing file.
49 */ 58 */
50 public function testReadEmptyUpdatesFile() 59 public function testReadEmptyUpdatesFile()
51 { 60 {
52 $this->assertEquals(array(), read_updates_file('')); 61 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
53 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; 62 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
54 touch($updatesFile); 63 touch($updatesFile);
55 $this->assertEquals(array(), read_updates_file($updatesFile)); 64 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
56 unlink($updatesFile); 65 unlink($updatesFile);
57 } 66 }
58 67
@@ -64,42 +73,42 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
64 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; 73 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
65 $updatesMethods = array('m1', 'm2', 'm3'); 74 $updatesMethods = array('m1', 'm2', 'm3');
66 75
67 write_updates_file($updatesFile, $updatesMethods); 76 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
68 $readMethods = read_updates_file($updatesFile); 77 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
69 $this->assertEquals($readMethods, $updatesMethods); 78 $this->assertEquals($readMethods, $updatesMethods);
70 79
71 // Update 80 // Update
72 $updatesMethods[] = 'm4'; 81 $updatesMethods[] = 'm4';
73 write_updates_file($updatesFile, $updatesMethods); 82 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
74 $readMethods = read_updates_file($updatesFile); 83 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
75 $this->assertEquals($readMethods, $updatesMethods); 84 $this->assertEquals($readMethods, $updatesMethods);
76 unlink($updatesFile); 85 unlink($updatesFile);
77 } 86 }
78 87
79 /** 88 /**
80 * Test errors in write_updates_file(): empty updates file. 89 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
81 *
82 * @expectedException Exception
83 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
84 */ 90 */
85 public function testWriteEmptyUpdatesFile() 91 public function testWriteEmptyUpdatesFile()
86 { 92 {
87 write_updates_file('', array('test')); 93 $this->expectException(\Exception::class);
94 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
95
96 UpdaterUtils::write_updates_file('', array('test'));
88 } 97 }
89 98
90 /** 99 /**
91 * Test errors in write_updates_file(): not writable updates file. 100 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
92 *
93 * @expectedException Exception
94 * @expectedExceptionMessageRegExp /Unable to write(.*)/
95 */ 101 */
96 public function testWriteUpdatesFileNotWritable() 102 public function testWriteUpdatesFileNotWritable()
97 { 103 {
104 $this->expectException(\Exception::class);
105 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
106
98 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; 107 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
99 touch($updatesFile); 108 touch($updatesFile);
100 chmod($updatesFile, 0444); 109 chmod($updatesFile, 0444);
101 try { 110 try {
102 @write_updates_file($updatesFile, array('test')); 111 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
103 } catch (Exception $e) { 112 } catch (Exception $e) {
104 unlink($updatesFile); 113 unlink($updatesFile);
105 throw $e; 114 throw $e;
@@ -159,11 +168,11 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
159 168
160 /** 169 /**
161 * Test Update failed. 170 * Test Update failed.
162 *
163 * @expectedException \Exception
164 */ 171 */
165 public function testUpdateFailed() 172 public function testUpdateFailed()
166 { 173 {
174 $this->expectException(\Exception::class);
175
167 $updates = array( 176 $updates = array(
168 'updateMethodDummy1', 177 'updateMethodDummy1',
169 'updateMethodDummy2', 178 'updateMethodDummy2',
@@ -174,653 +183,39 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
174 $updater->update(); 183 $updater->update();
175 } 184 }
176 185
177 /** 186 public function testUpdateMethodRelativeHomeLinkRename(): void
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 { 187 {
325 $confFile = 'sandbox/config'; 188 $this->updater->setBasePath('/subfolder');
326 copy(self::$configFile .'.json.php', $confFile .'.json.php'); 189 $this->conf->set('general.header_link', '?');
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 190
337 /** 191 $this->updater->updateMethodRelativeHomeLink();
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 192
405 $this->assertTrue(isset($linkDB[1])); 193 static::assertSame('/subfolder/', $this->conf->get('general.header_link'));
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 } 194 }
427 195
428 /** 196 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
429 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
430 */
431 public function testDatastoreIdsNothingToDo()
432 { 197 {
433 $refDB = new \ReferenceLinkDB(); 198 $this->conf->set('general.header_link', '~/my-blog');
434 $refDB->write(self::$testDatastore);
435 $linkDB = new LinkDB(self::$testDatastore, true, false);
436 199
437 $this->conf->set('resource.data_dir', 'sandbox'); 200 $this->updater->updateMethodRelativeHomeLink();
438 $this->conf->set('resource.datastore', self::$testDatastore);
439 201
440 $checksum = hash_file('sha1', self::$testDatastore); 202 static::assertSame('~/my-blog', $this->conf->get('general.header_link'));
441 $updater = new Updater(array(), $linkDB, $this->conf, true);
442 $this->assertTrue($updater->updateMethodDatastoreIds());
443 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
444 } 203 }
445 204
446 /** 205 public function testUpdateMethodMigrateExistingNotesUrl(): void
447 * Test defaultTheme update with default settings: nothing to do.
448 */
449 public function testDefaultThemeWithDefaultSettings()
450 { 206 {
451 $sandbox = 'sandbox/config'; 207 $this->updater->updateMethodMigrateExistingNotesUrl();
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 208
465 /** 209 static::assertSame($this->refDB->getLinks()[0]->getUrl(), $this->bookmarkService->get(0)->getUrl());
466 * Test defaultTheme update with a custom theme in a subfolder 210 static::assertSame($this->refDB->getLinks()[1]->getUrl(), $this->bookmarkService->get(1)->getUrl());
467 */ 211 static::assertSame($this->refDB->getLinks()[4]->getUrl(), $this->bookmarkService->get(4)->getUrl());
468 public function testDefaultThemeWithCustomTheme() 212 static::assertSame($this->refDB->getLinks()[6]->getUrl(), $this->bookmarkService->get(6)->getUrl());
469 { 213 static::assertSame($this->refDB->getLinks()[7]->getUrl(), $this->bookmarkService->get(7)->getUrl());
470 $theme = 'iamanartist'; 214 static::assertSame($this->refDB->getLinks()[8]->getUrl(), $this->bookmarkService->get(8)->getUrl());
471 $sandbox = 'sandbox/config'; 215 static::assertSame($this->refDB->getLinks()[9]->getUrl(), $this->bookmarkService->get(9)->getUrl());
472 copy(self::$configFile . '.json.php', $sandbox . '.json.php'); 216 static::assertSame('/shaare/WDWyig', $this->bookmarkService->get(42)->getUrl());
473 $this->conf = new ConfigManager($sandbox); 217 static::assertSame('/shaare/WDWyig', $this->bookmarkService->get(41)->getUrl());
474 mkdir('sandbox/'. $theme); 218 static::assertSame('/shaare/0gCTjQ', $this->bookmarkService->get(10)->getUrl());
475 touch('sandbox/'. $theme .'/linklist.html'); 219 static::assertSame('/shaare/PCRizQ', $this->bookmarkService->get(11)->getUrl());
476 $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
477 $updater = new Updater([], [], $this->conf, true);
478 $this->assertTrue($updater->updateMethodDefaultTheme());
479
480 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
481 $this->assertEquals($theme, $this->conf->get('resource.theme'));
482 $this->conf = new ConfigManager($sandbox);
483 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
484 $this->assertEquals($theme, $this->conf->get('resource.theme'));
485 unlink($sandbox . '.json.php');
486 unlink('sandbox/'. $theme .'/linklist.html');
487 rmdir('sandbox/'. $theme);
488 }
489
490 /**
491 * Test updateMethodEscapeMarkdown with markdown plugin enabled
492 * => setting markdown_escape set to false.
493 */
494 public function testEscapeMarkdownSettingToFalse()
495 {
496 $sandboxConf = 'sandbox/config';
497 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
498 $this->conf = new ConfigManager($sandboxConf);
499
500 $this->conf->set('general.enabled_plugins', ['markdown']);
501 $updater = new Updater([], [], $this->conf, true);
502 $this->assertTrue($updater->updateMethodEscapeMarkdown());
503 $this->assertFalse($this->conf->get('security.markdown_escape'));
504
505 // reload from file
506 $this->conf = new ConfigManager($sandboxConf);
507 $this->assertFalse($this->conf->get('security.markdown_escape'));
508 }
509
510
511 /**
512 * Test updateMethodEscapeMarkdown with markdown plugin disabled
513 * => setting markdown_escape set to true.
514 */
515 public function testEscapeMarkdownSettingToTrue()
516 {
517 $sandboxConf = 'sandbox/config';
518 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
519 $this->conf = new ConfigManager($sandboxConf);
520
521 $this->conf->set('general.enabled_plugins', []);
522 $updater = new Updater([], [], $this->conf, true);
523 $this->assertTrue($updater->updateMethodEscapeMarkdown());
524 $this->assertTrue($this->conf->get('security.markdown_escape'));
525
526 // reload from file
527 $this->conf = new ConfigManager($sandboxConf);
528 $this->assertTrue($this->conf->get('security.markdown_escape'));
529 }
530
531 /**
532 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
533 */
534 public function testEscapeMarkdownSettingNothingToDoEnabled()
535 {
536 $sandboxConf = 'sandbox/config';
537 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
538 $this->conf = new ConfigManager($sandboxConf);
539 $this->conf->set('security.markdown_escape', true);
540 $updater = new Updater([], [], $this->conf, true);
541 $this->assertTrue($updater->updateMethodEscapeMarkdown());
542 $this->assertTrue($this->conf->get('security.markdown_escape'));
543 }
544
545 /**
546 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
547 */
548 public function testEscapeMarkdownSettingNothingToDoDisabled()
549 {
550 $this->conf->set('security.markdown_escape', false);
551 $updater = new Updater([], [], $this->conf, true);
552 $this->assertTrue($updater->updateMethodEscapeMarkdown());
553 $this->assertFalse($this->conf->get('security.markdown_escape'));
554 }
555
556 /**
557 * Test updateMethodPiwikUrl with valid data
558 */
559 public function testUpdatePiwikUrlValid()
560 {
561 $sandboxConf = 'sandbox/config';
562 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
563 $this->conf = new ConfigManager($sandboxConf);
564 $url = 'mypiwik.tld';
565 $this->conf->set('plugins.PIWIK_URL', $url);
566 $updater = new Updater([], [], $this->conf, true);
567 $this->assertTrue($updater->updateMethodPiwikUrl());
568 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
569
570 // reload from file
571 $this->conf = new ConfigManager($sandboxConf);
572 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
573 }
574
575 /**
576 * Test updateMethodPiwikUrl without setting
577 */
578 public function testUpdatePiwikUrlEmpty()
579 {
580 $updater = new Updater([], [], $this->conf, true);
581 $this->assertTrue($updater->updateMethodPiwikUrl());
582 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
583 }
584
585 /**
586 * Test updateMethodPiwikUrl: valid URL, nothing to do
587 */
588 public function testUpdatePiwikUrlNothingToDo()
589 {
590 $url = 'https://mypiwik.tld';
591 $this->conf->set('plugins.PIWIK_URL', $url);
592 $updater = new Updater([], [], $this->conf, true);
593 $this->assertTrue($updater->updateMethodPiwikUrl());
594 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
595 }
596
597 /**
598 * Test updateMethodAtomDefault with show_atom set to false
599 * => update to true.
600 */
601 public function testUpdateMethodAtomDefault()
602 {
603 $sandboxConf = 'sandbox/config';
604 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
605 $this->conf = new ConfigManager($sandboxConf);
606 $this->conf->set('feed.show_atom', false);
607 $updater = new Updater([], [], $this->conf, true);
608 $this->assertTrue($updater->updateMethodAtomDefault());
609 $this->assertTrue($this->conf->get('feed.show_atom'));
610 // reload from file
611 $this->conf = new ConfigManager($sandboxConf);
612 $this->assertTrue($this->conf->get('feed.show_atom'));
613 }
614 /**
615 * Test updateMethodAtomDefault with show_atom not set.
616 * => nothing to do
617 */
618 public function testUpdateMethodAtomDefaultNoExist()
619 {
620 $sandboxConf = 'sandbox/config';
621 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
622 $this->conf = new ConfigManager($sandboxConf);
623 $updater = new Updater([], [], $this->conf, true);
624 $this->assertTrue($updater->updateMethodAtomDefault());
625 $this->assertTrue($this->conf->get('feed.show_atom'));
626 }
627 /**
628 * Test updateMethodAtomDefault with show_atom set to true.
629 * => nothing to do
630 */
631 public function testUpdateMethodAtomDefaultAlreadyTrue()
632 {
633 $sandboxConf = 'sandbox/config';
634 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
635 $this->conf = new ConfigManager($sandboxConf);
636 $this->conf->set('feed.show_atom', true);
637 $updater = new Updater([], [], $this->conf, true);
638 $this->assertTrue($updater->updateMethodAtomDefault());
639 $this->assertTrue($this->conf->get('feed.show_atom'));
640 }
641
642 /**
643 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
644 */
645 public function testUpdateMethodDownloadSizeAndTimeoutConf()
646 {
647 $sandboxConf = 'sandbox/config';
648 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
649 $this->conf = new ConfigManager($sandboxConf);
650 $updater = new Updater([], [], $this->conf, true);
651 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
652 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
653 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
654
655 $this->conf = new ConfigManager($sandboxConf);
656 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
657 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
658 }
659
660 /**
661 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
662 */
663 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
664 {
665 $sandboxConf = 'sandbox/config';
666 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
667 $this->conf = new ConfigManager($sandboxConf);
668 $this->conf->set('general.download_max_size', 38);
669 $this->conf->set('general.download_timeout', 70);
670 $updater = new Updater([], [], $this->conf, true);
671 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
672 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
673 $this->assertEquals(70, $this->conf->get('general.download_timeout'));
674 }
675
676 /**
677 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
678 */
679 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
680 {
681 $sandboxConf = 'sandbox/config';
682 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
683 $this->conf = new ConfigManager($sandboxConf);
684 $this->conf->set('general.download_max_size', 38);
685 $updater = new Updater([], [], $this->conf, true);
686 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
687 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
688 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
689 }
690
691 /**
692 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
693 */
694 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
695 {
696 $sandboxConf = 'sandbox/config';
697 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
698 $this->conf = new ConfigManager($sandboxConf);
699 $this->conf->set('general.download_timeout', 3);
700 $updater = new Updater([], [], $this->conf, true);
701 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
702 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
703 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
704 }
705
706 /**
707 * Test updateMethodWebThumbnailer with thumbnails enabled.
708 */
709 public function testUpdateMethodWebThumbnailerEnabled()
710 {
711 $this->conf->remove('thumbnails');
712 $this->conf->set('thumbnail.enable_thumbnails', true);
713 $updater = new Updater([], [], $this->conf, true, $_SESSION);
714 $this->assertTrue($updater->updateMethodWebThumbnailer());
715 $this->assertFalse($this->conf->exists('thumbnail'));
716 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
717 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
718 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
719 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
720 }
721
722 /**
723 * Test updateMethodWebThumbnailer with thumbnails disabled.
724 */
725 public function testUpdateMethodWebThumbnailerDisabled()
726 {
727 $this->conf->remove('thumbnails');
728 $this->conf->set('thumbnail.enable_thumbnails', false);
729 $updater = new Updater([], [], $this->conf, true, $_SESSION);
730 $this->assertTrue($updater->updateMethodWebThumbnailer());
731 $this->assertFalse($this->conf->exists('thumbnail'));
732 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
733 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
734 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
735 $this->assertTrue(empty($_SESSION['warnings']));
736 }
737
738 /**
739 * Test updateMethodWebThumbnailer with thumbnails disabled.
740 */
741 public function testUpdateMethodWebThumbnailerNothingToDo()
742 {
743 $updater = new Updater([], [], $this->conf, true, $_SESSION);
744 $this->assertTrue($updater->updateMethodWebThumbnailer());
745 $this->assertFalse($this->conf->exists('thumbnail'));
746 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
747 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
748 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
749 $this->assertTrue(empty($_SESSION['warnings']));
750 }
751
752 /**
753 * Test updateMethodSetSticky().
754 */
755 public function testUpdateStickyValid()
756 {
757 $blank = [
758 'id' => 1,
759 'url' => 'z',
760 'title' => '',
761 'description' => '',
762 'tags' => '',
763 'created' => new DateTime(),
764 ];
765 $links = [
766 1 => ['id' => 1] + $blank,
767 2 => ['id' => 2] + $blank,
768 ];
769 $refDB = new \ReferenceLinkDB();
770 $refDB->setLinks($links);
771 $refDB->write(self::$testDatastore);
772 $linkDB = new LinkDB(self::$testDatastore, true, false);
773
774 $updater = new Updater(array(), $linkDB, $this->conf, true);
775 $this->assertTrue($updater->updateMethodSetSticky());
776
777 $linkDB = new LinkDB(self::$testDatastore, true, false);
778 foreach ($linkDB as $link) {
779 $this->assertFalse($link['sticky']);
780 }
781 }
782
783 /**
784 * Test updateMethodSetSticky().
785 */
786 public function testUpdateStickyNothingToDo()
787 {
788 $blank = [
789 'id' => 1,
790 'url' => 'z',
791 'title' => '',
792 'description' => '',
793 'tags' => '',
794 'created' => new DateTime(),
795 ];
796 $links = [
797 1 => ['id' => 1, 'sticky' => true] + $blank,
798 2 => ['id' => 2] + $blank,
799 ];
800 $refDB = new \ReferenceLinkDB();
801 $refDB->setLinks($links);
802 $refDB->write(self::$testDatastore);
803 $linkDB = new LinkDB(self::$testDatastore, true, false);
804
805 $updater = new Updater(array(), $linkDB, $this->conf, true);
806 $this->assertTrue($updater->updateMethodSetSticky());
807
808 $linkDB = new LinkDB(self::$testDatastore, true, false);
809 $this->assertTrue($linkDB[1]['sticky']);
810 }
811
812 /**
813 * Test updateMethodRemoveRedirector().
814 */
815 public function testUpdateRemoveRedirector()
816 {
817 $sandboxConf = 'sandbox/config';
818 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
819 $this->conf = new ConfigManager($sandboxConf);
820 $updater = new Updater([], null, $this->conf, true);
821 $this->assertTrue($updater->updateMethodRemoveRedirector());
822 $this->assertFalse($this->conf->exists('redirector'));
823 $this->conf = new ConfigManager($sandboxConf);
824 $this->assertFalse($this->conf->exists('redirector'));
825 } 220 }
826} 221}