]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/Updater/UpdaterTest.php
fed175df531bd5a336c25efd87d1fd51081ca99c
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
1 <?php
2 use Shaarli\Config\ConfigJson;
3 use Shaarli\Config\ConfigManager;
4 use Shaarli\Config\ConfigPhp;
5
6 require_once 'tests/Updater/DummyUpdater.php';
7 require_once 'inc/rain.tpl.class.php';
8
9 /**
10 * Class UpdaterTest.
11 * Runs unit tests against the Updater class.
12 */
13 class UpdaterTest extends PHPUnit_Framework_TestCase
14 {
15 /**
16 * @var string Path to test datastore.
17 */
18 protected static $testDatastore = 'sandbox/datastore.php';
19
20 /**
21 * @var string Config file path (without extension).
22 */
23 protected static $configFile = 'tests/utils/config/configJson';
24
25 /**
26 * @var ConfigManager
27 */
28 protected $conf;
29
30 /**
31 * Executed before each test.
32 */
33 public function setUp()
34 {
35 $this->conf = new ConfigManager(self::$configFile);
36 }
37
38 /**
39 * Test read_updates_file with an empty/missing file.
40 */
41 public function testReadEmptyUpdatesFile()
42 {
43 $this->assertEquals(array(), read_updates_file(''));
44 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
45 touch($updatesFile);
46 $this->assertEquals(array(), read_updates_file($updatesFile));
47 unlink($updatesFile);
48 }
49
50 /**
51 * Test read/write updates file.
52 */
53 public function testReadWriteUpdatesFile()
54 {
55 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
56 $updatesMethods = array('m1', 'm2', 'm3');
57
58 write_updates_file($updatesFile, $updatesMethods);
59 $readMethods = read_updates_file($updatesFile);
60 $this->assertEquals($readMethods, $updatesMethods);
61
62 // Update
63 $updatesMethods[] = 'm4';
64 write_updates_file($updatesFile, $updatesMethods);
65 $readMethods = read_updates_file($updatesFile);
66 $this->assertEquals($readMethods, $updatesMethods);
67 unlink($updatesFile);
68 }
69
70 /**
71 * Test errors in write_updates_file(): empty updates file.
72 *
73 * @expectedException Exception
74 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
75 */
76 public function testWriteEmptyUpdatesFile()
77 {
78 write_updates_file('', array('test'));
79 }
80
81 /**
82 * Test errors in write_updates_file(): not writable updates file.
83 *
84 * @expectedException Exception
85 * @expectedExceptionMessageRegExp /Unable to write(.*)/
86 */
87 public function testWriteUpdatesFileNotWritable()
88 {
89 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
90 touch($updatesFile);
91 chmod($updatesFile, 0444);
92 try {
93 @write_updates_file($updatesFile, array('test'));
94 } catch (Exception $e) {
95 unlink($updatesFile);
96 throw $e;
97 }
98 }
99
100 /**
101 * Test the update() method, with no update to run.
102 * 1. Everything already run.
103 * 2. User is logged out.
104 */
105 public function testNoUpdates()
106 {
107 $updates = array(
108 'updateMethodDummy1',
109 'updateMethodDummy2',
110 'updateMethodDummy3',
111 'updateMethodException',
112 );
113 $updater = new DummyUpdater($updates, array(), $this->conf, true);
114 $this->assertEquals(array(), $updater->update());
115
116 $updater = new DummyUpdater(array(), array(), $this->conf, false);
117 $this->assertEquals(array(), $updater->update());
118 }
119
120 /**
121 * Test the update() method, with all updates to run (except the failing one).
122 */
123 public function testUpdatesFirstTime()
124 {
125 $updates = array('updateMethodException',);
126 $expectedUpdates = array(
127 'updateMethodDummy1',
128 'updateMethodDummy2',
129 'updateMethodDummy3',
130 );
131 $updater = new DummyUpdater($updates, array(), $this->conf, true);
132 $this->assertEquals($expectedUpdates, $updater->update());
133 }
134
135 /**
136 * Test the update() method, only one update to run.
137 */
138 public function testOneUpdate()
139 {
140 $updates = array(
141 'updateMethodDummy1',
142 'updateMethodDummy3',
143 'updateMethodException',
144 );
145 $expectedUpdate = array('updateMethodDummy2');
146
147 $updater = new DummyUpdater($updates, array(), $this->conf, true);
148 $this->assertEquals($expectedUpdate, $updater->update());
149 }
150
151 /**
152 * Test Update failed.
153 *
154 * @expectedException UpdaterException
155 */
156 public function testUpdateFailed()
157 {
158 $updates = array(
159 'updateMethodDummy1',
160 'updateMethodDummy2',
161 'updateMethodDummy3',
162 );
163
164 $updater = new DummyUpdater($updates, array(), $this->conf, true);
165 $updater->update();
166 }
167
168 /**
169 * Test update mergeDeprecatedConfig:
170 * 1. init a config file.
171 * 2. init a options.php file with update value.
172 * 3. merge.
173 * 4. check updated value in config file.
174 */
175 public function testUpdateMergeDeprecatedConfig()
176 {
177 $this->conf->setConfigFile('tests/utils/config/configPhp');
178 $this->conf->reset();
179
180 $optionsFile = 'tests/Updater/options.php';
181 $options = '<?php
182 $GLOBALS[\'privateLinkByDefault\'] = true;';
183 file_put_contents($optionsFile, $options);
184
185 // tmp config file.
186 $this->conf->setConfigFile('tests/Updater/config');
187
188 // merge configs
189 $updater = new Updater(array(), array(), $this->conf, true);
190 // This writes a new config file in tests/Updater/config.php
191 $updater->updateMethodMergeDeprecatedConfigFile();
192
193 // make sure updated field is changed
194 $this->conf->reload();
195 $this->assertTrue($this->conf->get('privacy.default_private_links'));
196 $this->assertFalse(is_file($optionsFile));
197 // Delete the generated file.
198 unlink($this->conf->getConfigFileExt());
199 }
200
201 /**
202 * Test mergeDeprecatedConfig in without options file.
203 */
204 public function testMergeDeprecatedConfigNoFile()
205 {
206 $updater = new Updater(array(), array(), $this->conf, true);
207 $updater->updateMethodMergeDeprecatedConfigFile();
208
209 $this->assertEquals('root', $this->conf->get('credentials.login'));
210 }
211
212 /**
213 * Test renameDashTags update method.
214 */
215 public function testRenameDashTags()
216 {
217 $refDB = new ReferenceLinkDB();
218 $refDB->write(self::$testDatastore);
219 $linkDB = new LinkDB(self::$testDatastore, true, false);
220
221 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
222 $updater = new Updater(array(), $linkDB, $this->conf, true);
223 $updater->updateMethodRenameDashTags();
224 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
225 }
226
227 /**
228 * Convert old PHP config file to JSON config.
229 */
230 public function testConfigToJson()
231 {
232 $configFile = 'tests/utils/config/configPhp';
233 $this->conf->setConfigFile($configFile);
234 $this->conf->reset();
235
236 // The ConfigIO is initialized with ConfigPhp.
237 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
238
239 $updater = new Updater(array(), array(), $this->conf, false);
240 $done = $updater->updateMethodConfigToJson();
241 $this->assertTrue($done);
242
243 // The ConfigIO has been updated to ConfigJson.
244 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
245 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
246
247 // Check JSON config data.
248 $this->conf->reload();
249 $this->assertEquals('root', $this->conf->get('credentials.login'));
250 $this->assertEquals('lala', $this->conf->get('redirector.url'));
251 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
252 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
253
254 rename($configFile . '.save.php', $configFile . '.php');
255 unlink($this->conf->getConfigFileExt());
256 }
257
258 /**
259 * Launch config conversion update with an existing JSON file => nothing to do.
260 */
261 public function testConfigToJsonNothingToDo()
262 {
263 $filetime = filemtime($this->conf->getConfigFileExt());
264 $updater = new Updater(array(), array(), $this->conf, false);
265 $done = $updater->updateMethodConfigToJson();
266 $this->assertTrue($done);
267 $expected = filemtime($this->conf->getConfigFileExt());
268 $this->assertEquals($expected, $filetime);
269 }
270
271 /**
272 * Test escapeUnescapedConfig with valid data.
273 */
274 public function testEscapeConfig()
275 {
276 $sandbox = 'sandbox/config';
277 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
278 $this->conf = new ConfigManager($sandbox);
279 $title = '<script>alert("title");</script>';
280 $headerLink = '<script>alert("header_link");</script>';
281 $redirectorUrl = '<script>alert("redirector");</script>';
282 $this->conf->set('general.title', $title);
283 $this->conf->set('general.header_link', $headerLink);
284 $this->conf->set('redirector.url', $redirectorUrl);
285 $updater = new Updater(array(), array(), $this->conf, true);
286 $done = $updater->updateMethodEscapeUnescapedConfig();
287 $this->assertTrue($done);
288 $this->conf->reload();
289 $this->assertEquals(escape($title), $this->conf->get('general.title'));
290 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
291 $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url'));
292 unlink($sandbox . '.json.php');
293 }
294
295 /**
296 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
297 */
298 public function testUpdateApiSettings()
299 {
300 $confFile = 'sandbox/config';
301 copy(self::$configFile .'.json.php', $confFile .'.json.php');
302 $conf = new ConfigManager($confFile);
303 $updater = new Updater(array(), array(), $conf, true);
304
305 $this->assertFalse($conf->exists('api.enabled'));
306 $this->assertFalse($conf->exists('api.secret'));
307 $updater->updateMethodApiSettings();
308 $conf->reload();
309 $this->assertTrue($conf->get('api.enabled'));
310 $this->assertTrue($conf->exists('api.secret'));
311 unlink($confFile .'.json.php');
312 }
313
314 /**
315 * Test updateMethodApiSettings(): already set, do nothing.
316 */
317 public function testUpdateApiSettingsNothingToDo()
318 {
319 $confFile = 'sandbox/config';
320 copy(self::$configFile .'.json.php', $confFile .'.json.php');
321 $conf = new ConfigManager($confFile);
322 $conf->set('api.enabled', false);
323 $conf->set('api.secret', '');
324 $updater = new Updater(array(), array(), $conf, true);
325 $updater->updateMethodApiSettings();
326 $this->assertFalse($conf->get('api.enabled'));
327 $this->assertEmpty($conf->get('api.secret'));
328 unlink($confFile .'.json.php');
329 }
330
331 /**
332 * Test updateMethodDatastoreIds().
333 */
334 public function testDatastoreIds()
335 {
336 $links = array(
337 '20121206_182539' => array(
338 'linkdate' => '20121206_182539',
339 'title' => 'Geek and Poke',
340 'url' => 'http://geek-and-poke.com/',
341 'description' => 'desc',
342 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
343 'updated' => '20121206_190301',
344 'private' => false,
345 ),
346 '20121206_172539' => array(
347 'linkdate' => '20121206_172539',
348 'title' => 'UserFriendly - Samba',
349 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
350 'description' => '',
351 'tags' => 'samba cartoon web',
352 'private' => false,
353 ),
354 '20121206_142300' => array(
355 'linkdate' => '20121206_142300',
356 'title' => 'UserFriendly - Web Designer',
357 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
358 'description' => 'Naming conventions... #private',
359 'tags' => 'samba cartoon web',
360 'private' => true,
361 ),
362 );
363 $refDB = new ReferenceLinkDB();
364 $refDB->setLinks($links);
365 $refDB->write(self::$testDatastore);
366 $linkDB = new LinkDB(self::$testDatastore, true, false);
367
368 $checksum = hash_file('sha1', self::$testDatastore);
369
370 $this->conf->set('resource.data_dir', 'sandbox');
371 $this->conf->set('resource.datastore', self::$testDatastore);
372
373 $updater = new Updater(array(), $linkDB, $this->conf, true);
374 $this->assertTrue($updater->updateMethodDatastoreIds());
375
376 $linkDB = new LinkDB(self::$testDatastore, true, false);
377
378 $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
379 $backup = $backup[0];
380
381 $this->assertFileExists($backup);
382 $this->assertEquals($checksum, hash_file('sha1', $backup));
383 unlink($backup);
384
385 $this->assertEquals(3, count($linkDB));
386 $this->assertTrue(isset($linkDB[0]));
387 $this->assertFalse(isset($linkDB[0]['linkdate']));
388 $this->assertEquals(0, $linkDB[0]['id']);
389 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
390 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
391 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
392 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
393 $this->assertTrue($linkDB[0]['private']);
394 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']);
395
396 $this->assertTrue(isset($linkDB[1]));
397 $this->assertFalse(isset($linkDB[1]['linkdate']));
398 $this->assertEquals(1, $linkDB[1]['id']);
399 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
400 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']);
401
402 $this->assertTrue(isset($linkDB[2]));
403 $this->assertFalse(isset($linkDB[2]['linkdate']));
404 $this->assertEquals(2, $linkDB[2]['id']);
405 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
406 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']);
407 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']);
408 }
409
410 /**
411 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
412 */
413 public function testDatastoreIdsNothingToDo()
414 {
415 $refDB = new ReferenceLinkDB();
416 $refDB->write(self::$testDatastore);
417 $linkDB = new LinkDB(self::$testDatastore, true, false);
418
419 $this->conf->set('resource.data_dir', 'sandbox');
420 $this->conf->set('resource.datastore', self::$testDatastore);
421
422 $checksum = hash_file('sha1', self::$testDatastore);
423 $updater = new Updater(array(), $linkDB, $this->conf, true);
424 $this->assertTrue($updater->updateMethodDatastoreIds());
425 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
426 }
427
428 /**
429 * Test defaultTheme update with default settings: nothing to do.
430 */
431 public function testDefaultThemeWithDefaultSettings()
432 {
433 $sandbox = 'sandbox/config';
434 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
435 $this->conf = new ConfigManager($sandbox);
436 $updater = new Updater([], [], $this->conf, true);
437 $this->assertTrue($updater->updateMethodDefaultTheme());
438
439 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
440 $this->assertEquals('default', $this->conf->get('resource.theme'));
441 $this->conf = new ConfigManager($sandbox);
442 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
443 $this->assertEquals('default', $this->conf->get('resource.theme'));
444 unlink($sandbox . '.json.php');
445 }
446
447 /**
448 * Test defaultTheme update with a custom theme in a subfolder
449 */
450 public function testDefaultThemeWithCustomTheme()
451 {
452 $theme = 'iamanartist';
453 $sandbox = 'sandbox/config';
454 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
455 $this->conf = new ConfigManager($sandbox);
456 mkdir('sandbox/'. $theme);
457 touch('sandbox/'. $theme .'/linklist.html');
458 $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
459 $updater = new Updater([], [], $this->conf, true);
460 $this->assertTrue($updater->updateMethodDefaultTheme());
461
462 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
463 $this->assertEquals($theme, $this->conf->get('resource.theme'));
464 $this->conf = new ConfigManager($sandbox);
465 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
466 $this->assertEquals($theme, $this->conf->get('resource.theme'));
467 unlink($sandbox . '.json.php');
468 unlink('sandbox/'. $theme .'/linklist.html');
469 rmdir('sandbox/'. $theme);
470 }
471
472 /**
473 * Test updateMethodEscapeMarkdown with markdown plugin enabled
474 * => setting markdown_escape set to false.
475 */
476 public function testEscapeMarkdownSettingToFalse()
477 {
478 $sandboxConf = 'sandbox/config';
479 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
480 $this->conf = new ConfigManager($sandboxConf);
481
482 $this->conf->set('general.enabled_plugins', ['markdown']);
483 $updater = new Updater([], [], $this->conf, true);
484 $this->assertTrue($updater->updateMethodEscapeMarkdown());
485 $this->assertFalse($this->conf->get('security.markdown_escape'));
486
487 // reload from file
488 $this->conf = new ConfigManager($sandboxConf);
489 $this->assertFalse($this->conf->get('security.markdown_escape'));
490 }
491
492
493 /**
494 * Test updateMethodEscapeMarkdown with markdown plugin disabled
495 * => setting markdown_escape set to true.
496 */
497 public function testEscapeMarkdownSettingToTrue()
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', []);
504 $updater = new Updater([], [], $this->conf, true);
505 $this->assertTrue($updater->updateMethodEscapeMarkdown());
506 $this->assertTrue($this->conf->get('security.markdown_escape'));
507
508 // reload from file
509 $this->conf = new ConfigManager($sandboxConf);
510 $this->assertTrue($this->conf->get('security.markdown_escape'));
511 }
512
513 /**
514 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
515 */
516 public function testEscapeMarkdownSettingNothingToDoEnabled()
517 {
518 $sandboxConf = 'sandbox/config';
519 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
520 $this->conf = new ConfigManager($sandboxConf);
521 $this->conf->set('security.markdown_escape', true);
522 $updater = new Updater([], [], $this->conf, true);
523 $this->assertTrue($updater->updateMethodEscapeMarkdown());
524 $this->assertTrue($this->conf->get('security.markdown_escape'));
525 }
526
527 /**
528 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
529 */
530 public function testEscapeMarkdownSettingNothingToDoDisabled()
531 {
532 $this->conf->set('security.markdown_escape', false);
533 $updater = new Updater([], [], $this->conf, true);
534 $this->assertTrue($updater->updateMethodEscapeMarkdown());
535 $this->assertFalse($this->conf->get('security.markdown_escape'));
536 }
537
538 /**
539 * Test updateMethodPiwikUrl with valid data
540 */
541 public function testUpdatePiwikUrlValid()
542 {
543 $sandboxConf = 'sandbox/config';
544 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
545 $this->conf = new ConfigManager($sandboxConf);
546 $url = 'mypiwik.tld';
547 $this->conf->set('plugins.PIWIK_URL', $url);
548 $updater = new Updater([], [], $this->conf, true);
549 $this->assertTrue($updater->updateMethodPiwikUrl());
550 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
551
552 // reload from file
553 $this->conf = new ConfigManager($sandboxConf);
554 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
555 }
556
557 /**
558 * Test updateMethodPiwikUrl without setting
559 */
560 public function testUpdatePiwikUrlEmpty()
561 {
562 $updater = new Updater([], [], $this->conf, true);
563 $this->assertTrue($updater->updateMethodPiwikUrl());
564 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
565 }
566
567 /**
568 * Test updateMethodPiwikUrl: valid URL, nothing to do
569 */
570 public function testUpdatePiwikUrlNothingToDo()
571 {
572 $url = 'https://mypiwik.tld';
573 $this->conf->set('plugins.PIWIK_URL', $url);
574 $updater = new Updater([], [], $this->conf, true);
575 $this->assertTrue($updater->updateMethodPiwikUrl());
576 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
577 }
578
579 /**
580 * Test updateMethodAtomDefault with show_atom set to false
581 * => update to true.
582 */
583 public function testUpdateMethodAtomDefault()
584 {
585 $sandboxConf = 'sandbox/config';
586 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
587 $this->conf = new ConfigManager($sandboxConf);
588 $this->conf->set('feed.show_atom', false);
589 $updater = new Updater([], [], $this->conf, true);
590 $this->assertTrue($updater->updateMethodAtomDefault());
591 $this->assertTrue($this->conf->get('feed.show_atom'));
592 // reload from file
593 $this->conf = new ConfigManager($sandboxConf);
594 $this->assertTrue($this->conf->get('feed.show_atom'));
595 }
596 /**
597 * Test updateMethodAtomDefault with show_atom not set.
598 * => nothing to do
599 */
600 public function testUpdateMethodAtomDefaultNoExist()
601 {
602 $sandboxConf = 'sandbox/config';
603 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
604 $this->conf = new ConfigManager($sandboxConf);
605 $updater = new Updater([], [], $this->conf, true);
606 $this->assertTrue($updater->updateMethodAtomDefault());
607 $this->assertTrue($this->conf->get('feed.show_atom'));
608 }
609 /**
610 * Test updateMethodAtomDefault with show_atom set to true.
611 * => nothing to do
612 */
613 public function testUpdateMethodAtomDefaultAlreadyTrue()
614 {
615 $sandboxConf = 'sandbox/config';
616 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
617 $this->conf = new ConfigManager($sandboxConf);
618 $this->conf->set('feed.show_atom', true);
619 $updater = new Updater([], [], $this->conf, true);
620 $this->assertTrue($updater->updateMethodAtomDefault());
621 $this->assertTrue($this->conf->get('feed.show_atom'));
622 }
623 }