]>
Commit | Line | Data |
---|---|---|
510377d2 | 1 | <?php |
3c66e564 V |
2 | use Shaarli\Config\ConfigJson; |
3 | use Shaarli\Config\ConfigManager; | |
4 | use Shaarli\Config\ConfigPhp; | |
510377d2 A |
5 | |
6 | require_once 'tests/Updater/DummyUpdater.php'; | |
a0df0651 | 7 | require_once 'inc/rain.tpl.class.php'; |
510377d2 A |
8 | |
9 | /** | |
10 | * Class UpdaterTest. | |
11 | * Runs unit tests against the Updater class. | |
12 | */ | |
13 | class UpdaterTest extends PHPUnit_Framework_TestCase | |
14 | { | |
21979ff1 A |
15 | /** |
16 | * @var string Path to test datastore. | |
17 | */ | |
18 | protected static $testDatastore = 'sandbox/datastore.php'; | |
19 | ||
684e662a | 20 | /** |
b74b96bf | 21 | * @var string Config file path (without extension). |
684e662a | 22 | */ |
da10377b | 23 | protected static $configFile = 'tests/utils/config/configJson'; |
684e662a A |
24 | |
25 | /** | |
26 | * @var ConfigManager | |
27 | */ | |
28 | protected $conf; | |
29 | ||
510377d2 A |
30 | /** |
31 | * Executed before each test. | |
32 | */ | |
33 | public function setUp() | |
34 | { | |
278d9ee2 | 35 | $this->conf = new ConfigManager(self::$configFile); |
510377d2 A |
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('')); | |
894a3c4b | 44 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
45 | touch($updatesFile); |
46 | $this->assertEquals(array(), read_updates_file($updatesFile)); | |
da10377b | 47 | unlink($updatesFile); |
510377d2 A |
48 | } |
49 | ||
50 | /** | |
51 | * Test read/write updates file. | |
52 | */ | |
53 | public function testReadWriteUpdatesFile() | |
54 | { | |
894a3c4b | 55 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
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); | |
da10377b | 67 | unlink($updatesFile); |
510377d2 A |
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 | { | |
894a3c4b | 89 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
90 | touch($updatesFile); |
91 | chmod($updatesFile, 0444); | |
da10377b A |
92 | try { |
93 | @write_updates_file($updatesFile, array('test')); | |
94 | } catch (Exception $e) { | |
95 | unlink($updatesFile); | |
96 | throw $e; | |
97 | } | |
510377d2 A |
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 | ); | |
278d9ee2 | 113 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
114 | $this->assertEquals(array(), $updater->update()); |
115 | ||
278d9ee2 | 116 | $updater = new DummyUpdater(array(), array(), $this->conf, false); |
510377d2 A |
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 | ); | |
278d9ee2 | 131 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
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 | ||
278d9ee2 | 147 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
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 | ||
278d9ee2 | 164 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
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 | { | |
278d9ee2 A |
177 | $this->conf->setConfigFile('tests/utils/config/configPhp'); |
178 | $this->conf->reset(); | |
684e662a A |
179 | |
180 | $optionsFile = 'tests/Updater/options.php'; | |
da10377b A |
181 | $options = '<?php |
182 | $GLOBALS[\'privateLinkByDefault\'] = true;'; | |
183 | file_put_contents($optionsFile, $options); | |
510377d2 | 184 | |
da10377b | 185 | // tmp config file. |
278d9ee2 | 186 | $this->conf->setConfigFile('tests/Updater/config'); |
510377d2 A |
187 | |
188 | // merge configs | |
278d9ee2 | 189 | $updater = new Updater(array(), array(), $this->conf, true); |
b74b96bf | 190 | // This writes a new config file in tests/Updater/config.php |
510377d2 A |
191 | $updater->updateMethodMergeDeprecatedConfigFile(); |
192 | ||
193 | // make sure updated field is changed | |
684e662a | 194 | $this->conf->reload(); |
894a3c4b | 195 | $this->assertTrue($this->conf->get('privacy.default_private_links')); |
684e662a | 196 | $this->assertFalse(is_file($optionsFile)); |
b74b96bf | 197 | // Delete the generated file. |
278d9ee2 | 198 | unlink($this->conf->getConfigFileExt()); |
510377d2 A |
199 | } |
200 | ||
201 | /** | |
202 | * Test mergeDeprecatedConfig in without options file. | |
203 | */ | |
204 | public function testMergeDeprecatedConfigNoFile() | |
205 | { | |
278d9ee2 | 206 | $updater = new Updater(array(), array(), $this->conf, true); |
510377d2 A |
207 | $updater->updateMethodMergeDeprecatedConfigFile(); |
208 | ||
da10377b | 209 | $this->assertEquals('root', $this->conf->get('credentials.login')); |
510377d2 | 210 | } |
21979ff1 | 211 | |
684e662a A |
212 | /** |
213 | * Test renameDashTags update method. | |
214 | */ | |
21979ff1 A |
215 | public function testRenameDashTags() |
216 | { | |
217 | $refDB = new ReferenceLinkDB(); | |
218 | $refDB->write(self::$testDatastore); | |
219 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
c3dfd899 | 220 | |
528a6f8a | 221 | $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
278d9ee2 | 222 | $updater = new Updater(array(), $linkDB, $this->conf, true); |
21979ff1 | 223 | $updater->updateMethodRenameDashTags(); |
528a6f8a | 224 | $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
21979ff1 | 225 | } |
b74b96bf A |
226 | |
227 | /** | |
228 | * Convert old PHP config file to JSON config. | |
229 | */ | |
230 | public function testConfigToJson() | |
231 | { | |
232 | $configFile = 'tests/utils/config/configPhp'; | |
278d9ee2 A |
233 | $this->conf->setConfigFile($configFile); |
234 | $this->conf->reset(); | |
b74b96bf A |
235 | |
236 | // The ConfigIO is initialized with ConfigPhp. | |
278d9ee2 | 237 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); |
b74b96bf | 238 | |
278d9ee2 | 239 | $updater = new Updater(array(), array(), $this->conf, false); |
b74b96bf A |
240 | $done = $updater->updateMethodConfigToJson(); |
241 | $this->assertTrue($done); | |
242 | ||
243 | // The ConfigIO has been updated to ConfigJson. | |
278d9ee2 A |
244 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); |
245 | $this->assertTrue(file_exists($this->conf->getConfigFileExt())); | |
b74b96bf A |
246 | |
247 | // Check JSON config data. | |
278d9ee2 A |
248 | $this->conf->reload(); |
249 | $this->assertEquals('root', $this->conf->get('credentials.login')); | |
894a3c4b A |
250 | $this->assertEquals('lala', $this->conf->get('redirector.url')); |
251 | $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); | |
278d9ee2 | 252 | $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); |
b74b96bf A |
253 | |
254 | rename($configFile . '.save.php', $configFile . '.php'); | |
278d9ee2 | 255 | unlink($this->conf->getConfigFileExt()); |
b74b96bf A |
256 | } |
257 | ||
258 | /** | |
259 | * Launch config conversion update with an existing JSON file => nothing to do. | |
260 | */ | |
261 | public function testConfigToJsonNothingToDo() | |
262 | { | |
278d9ee2 A |
263 | $filetime = filemtime($this->conf->getConfigFileExt()); |
264 | $updater = new Updater(array(), array(), $this->conf, false); | |
b74b96bf A |
265 | $done = $updater->updateMethodConfigToJson(); |
266 | $this->assertTrue($done); | |
278d9ee2 | 267 | $expected = filemtime($this->conf->getConfigFileExt()); |
b74b96bf A |
268 | $this->assertEquals($expected, $filetime); |
269 | } | |
b9f8b837 A |
270 | |
271 | /** | |
272 | * Test escapeUnescapedConfig with valid data. | |
273 | */ | |
274 | public function testEscapeConfig() | |
275 | { | |
276 | $sandbox = 'sandbox/config'; | |
cbfdcff2 | 277 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); |
b9f8b837 A |
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')); | |
cbfdcff2 A |
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'); | |
b9f8b837 | 329 | } |
c3dfd899 A |
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']); | |
d592daea | 394 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); |
c3dfd899 A |
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']); | |
d592daea | 400 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); |
c3dfd899 A |
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']); | |
d592daea A |
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']); | |
c3dfd899 A |
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 | } | |
04a0e8ea A |
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 | } | |
7dcbfde5 A |
471 | |
472 | /** | |
473 | * Test updateMethodDefaultThemeVintage with the default theme enabled. | |
474 | */ | |
475 | public function testSetDefaultThemeToVintage() | |
476 | { | |
477 | $sandboxConf = 'sandbox/config'; | |
478 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
479 | $this->conf = new ConfigManager($sandboxConf); | |
480 | ||
481 | $this->conf->set('resource.theme', 'default'); | |
482 | $updater = new Updater([], [], $this->conf, true); | |
483 | $this->assertTrue($updater->updateMethodDefaultThemeVintage()); | |
484 | $this->assertEquals('vintage', $this->conf->get('resource.theme')); | |
485 | ||
486 | // reload from file | |
487 | $this->conf = new ConfigManager($sandboxConf); | |
488 | $this->assertEquals('vintage', $this->conf->get('resource.theme')); | |
489 | } | |
490 | ||
491 | /** | |
492 | * Test updateMethodDefaultThemeVintage with custom theme enabled => nothing to do. | |
493 | */ | |
494 | public function testSetDefaultThemeNothingToDo() | |
495 | { | |
496 | $sandboxConf = 'sandbox/config'; | |
497 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
498 | $this->conf = new ConfigManager($sandboxConf); | |
499 | ||
500 | $theme = 'myawesometheme'; | |
501 | $this->conf->set('resource.theme', $theme); | |
502 | $this->conf->write(true); | |
503 | $updater = new Updater([], [], $this->conf, true); | |
504 | $this->assertTrue($updater->updateMethodDefaultThemeVintage()); | |
505 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | |
506 | ||
507 | // reload from file | |
508 | $this->conf = new ConfigManager($sandboxConf); | |
509 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | |
510 | } | |
e0376101 A |
511 | |
512 | /** | |
513 | * Test updateMethodEscapeMarkdown with markdown plugin enabled | |
514 | * => setting markdown_escape set to false. | |
515 | */ | |
516 | public function testEscapeMarkdownSettingToFalse() | |
517 | { | |
518 | $sandboxConf = 'sandbox/config'; | |
519 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
520 | $this->conf = new ConfigManager($sandboxConf); | |
521 | ||
522 | $this->conf->set('general.enabled_plugins', ['markdown']); | |
523 | $updater = new Updater([], [], $this->conf, true); | |
524 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
525 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
526 | ||
527 | // reload from file | |
528 | $this->conf = new ConfigManager($sandboxConf); | |
529 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
530 | } | |
531 | ||
532 | ||
533 | /** | |
534 | * Test updateMethodEscapeMarkdown with markdown plugin disabled | |
535 | * => setting markdown_escape set to true. | |
536 | */ | |
537 | public function testEscapeMarkdownSettingToTrue() | |
538 | { | |
539 | $sandboxConf = 'sandbox/config'; | |
540 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
541 | $this->conf = new ConfigManager($sandboxConf); | |
542 | ||
543 | $this->conf->set('general.enabled_plugins', []); | |
544 | $updater = new Updater([], [], $this->conf, true); | |
545 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
546 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
547 | ||
548 | // reload from file | |
549 | $this->conf = new ConfigManager($sandboxConf); | |
550 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
551 | } | |
552 | ||
553 | /** | |
554 | * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled) | |
555 | */ | |
556 | public function testEscapeMarkdownSettingNothingToDoEnabled() | |
557 | { | |
558 | $sandboxConf = 'sandbox/config'; | |
559 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
560 | $this->conf = new ConfigManager($sandboxConf); | |
561 | $this->conf->set('security.markdown_escape', true); | |
562 | $updater = new Updater([], [], $this->conf, true); | |
563 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
564 | $this->assertTrue($this->conf->get('security.markdown_escape')); | |
565 | } | |
566 | ||
567 | /** | |
568 | * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled) | |
569 | */ | |
570 | public function testEscapeMarkdownSettingNothingToDoDisabled() | |
571 | { | |
572 | $this->conf->set('security.markdown_escape', false); | |
573 | $updater = new Updater([], [], $this->conf, true); | |
574 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | |
575 | $this->assertFalse($this->conf->get('security.markdown_escape')); | |
576 | } | |
fe83d45c A |
577 | |
578 | /** | |
579 | * Test updateMethodPiwikUrl with valid data | |
580 | */ | |
581 | public function testUpdatePiwikUrlValid() | |
582 | { | |
583 | $sandboxConf = 'sandbox/config'; | |
584 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
585 | $this->conf = new ConfigManager($sandboxConf); | |
586 | $url = 'mypiwik.tld'; | |
587 | $this->conf->set('plugins.PIWIK_URL', $url); | |
588 | $updater = new Updater([], [], $this->conf, true); | |
589 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
590 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | |
591 | ||
592 | // reload from file | |
593 | $this->conf = new ConfigManager($sandboxConf); | |
594 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | |
595 | } | |
596 | ||
597 | /** | |
598 | * Test updateMethodPiwikUrl without setting | |
599 | */ | |
600 | public function testUpdatePiwikUrlEmpty() | |
601 | { | |
602 | $updater = new Updater([], [], $this->conf, true); | |
603 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
604 | $this->assertEmpty($this->conf->get('plugins.PIWIK_URL')); | |
605 | } | |
606 | ||
607 | /** | |
608 | * Test updateMethodPiwikUrl: valid URL, nothing to do | |
609 | */ | |
610 | public function testUpdatePiwikUrlNothingToDo() | |
611 | { | |
612 | $url = 'https://mypiwik.tld'; | |
613 | $this->conf->set('plugins.PIWIK_URL', $url); | |
614 | $updater = new Updater([], [], $this->conf, true); | |
615 | $this->assertTrue($updater->updateMethodPiwikUrl()); | |
616 | $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL')); | |
617 | } | |
2ea89aba A |
618 | |
619 | /** | |
620 | * Test updateMethodAtomDefault with show_atom set to false | |
621 | * => update to true. | |
622 | */ | |
623 | public function testUpdateMethodAtomDefault() | |
624 | { | |
625 | $sandboxConf = 'sandbox/config'; | |
626 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
627 | $this->conf = new ConfigManager($sandboxConf); | |
628 | $this->conf->set('feed.show_atom', false); | |
629 | $updater = new Updater([], [], $this->conf, true); | |
630 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
631 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
632 | // reload from file | |
633 | $this->conf = new ConfigManager($sandboxConf); | |
634 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
635 | } | |
636 | /** | |
637 | * Test updateMethodAtomDefault with show_atom not set. | |
638 | * => nothing to do | |
639 | */ | |
640 | public function testUpdateMethodAtomDefaultNoExist() | |
641 | { | |
642 | $sandboxConf = 'sandbox/config'; | |
643 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
644 | $this->conf = new ConfigManager($sandboxConf); | |
645 | $updater = new Updater([], [], $this->conf, true); | |
646 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
647 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
648 | } | |
649 | /** | |
650 | * Test updateMethodAtomDefault with show_atom set to true. | |
651 | * => nothing to do | |
652 | */ | |
653 | public function testUpdateMethodAtomDefaultAlreadyTrue() | |
654 | { | |
655 | $sandboxConf = 'sandbox/config'; | |
656 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | |
657 | $this->conf = new ConfigManager($sandboxConf); | |
658 | $this->conf->set('feed.show_atom', true); | |
659 | $updater = new Updater([], [], $this->conf, true); | |
660 | $this->assertTrue($updater->updateMethodAtomDefault()); | |
661 | $this->assertTrue($this->conf->get('feed.show_atom')); | |
662 | } | |
510377d2 | 663 | } |