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