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