]>
Commit | Line | Data |
---|---|---|
510377d2 A |
1 | <?php |
2 | ||
684e662a | 3 | require_once 'application/config/ConfigManager.php'; |
510377d2 A |
4 | require_once 'tests/Updater/DummyUpdater.php'; |
5 | ||
6 | /** | |
7 | * Class UpdaterTest. | |
8 | * Runs unit tests against the Updater class. | |
9 | */ | |
10 | class UpdaterTest extends PHPUnit_Framework_TestCase | |
11 | { | |
21979ff1 A |
12 | /** |
13 | * @var string Path to test datastore. | |
14 | */ | |
15 | protected static $testDatastore = 'sandbox/datastore.php'; | |
16 | ||
684e662a | 17 | /** |
b74b96bf | 18 | * @var string Config file path (without extension). |
684e662a | 19 | */ |
da10377b | 20 | protected static $configFile = 'tests/utils/config/configJson'; |
684e662a A |
21 | |
22 | /** | |
23 | * @var ConfigManager | |
24 | */ | |
25 | protected $conf; | |
26 | ||
510377d2 A |
27 | /** |
28 | * Executed before each test. | |
29 | */ | |
30 | public function setUp() | |
31 | { | |
278d9ee2 | 32 | $this->conf = new ConfigManager(self::$configFile); |
510377d2 A |
33 | } |
34 | ||
35 | /** | |
36 | * Test read_updates_file with an empty/missing file. | |
37 | */ | |
38 | public function testReadEmptyUpdatesFile() | |
39 | { | |
40 | $this->assertEquals(array(), read_updates_file('')); | |
894a3c4b | 41 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
42 | touch($updatesFile); |
43 | $this->assertEquals(array(), read_updates_file($updatesFile)); | |
da10377b | 44 | unlink($updatesFile); |
510377d2 A |
45 | } |
46 | ||
47 | /** | |
48 | * Test read/write updates file. | |
49 | */ | |
50 | public function testReadWriteUpdatesFile() | |
51 | { | |
894a3c4b | 52 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
53 | $updatesMethods = array('m1', 'm2', 'm3'); |
54 | ||
55 | write_updates_file($updatesFile, $updatesMethods); | |
56 | $readMethods = read_updates_file($updatesFile); | |
57 | $this->assertEquals($readMethods, $updatesMethods); | |
58 | ||
59 | // Update | |
60 | $updatesMethods[] = 'm4'; | |
61 | write_updates_file($updatesFile, $updatesMethods); | |
62 | $readMethods = read_updates_file($updatesFile); | |
63 | $this->assertEquals($readMethods, $updatesMethods); | |
da10377b | 64 | unlink($updatesFile); |
510377d2 A |
65 | } |
66 | ||
67 | /** | |
68 | * Test errors in write_updates_file(): empty updates file. | |
69 | * | |
70 | * @expectedException Exception | |
71 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | |
72 | */ | |
73 | public function testWriteEmptyUpdatesFile() | |
74 | { | |
75 | write_updates_file('', array('test')); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Test errors in write_updates_file(): not writable updates file. | |
80 | * | |
81 | * @expectedException Exception | |
82 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | |
83 | */ | |
84 | public function testWriteUpdatesFileNotWritable() | |
85 | { | |
894a3c4b | 86 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
87 | touch($updatesFile); |
88 | chmod($updatesFile, 0444); | |
da10377b A |
89 | try { |
90 | @write_updates_file($updatesFile, array('test')); | |
91 | } catch (Exception $e) { | |
92 | unlink($updatesFile); | |
93 | throw $e; | |
94 | } | |
510377d2 A |
95 | } |
96 | ||
97 | /** | |
98 | * Test the update() method, with no update to run. | |
99 | * 1. Everything already run. | |
100 | * 2. User is logged out. | |
101 | */ | |
102 | public function testNoUpdates() | |
103 | { | |
104 | $updates = array( | |
105 | 'updateMethodDummy1', | |
106 | 'updateMethodDummy2', | |
107 | 'updateMethodDummy3', | |
108 | 'updateMethodException', | |
109 | ); | |
278d9ee2 | 110 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
111 | $this->assertEquals(array(), $updater->update()); |
112 | ||
278d9ee2 | 113 | $updater = new DummyUpdater(array(), array(), $this->conf, false); |
510377d2 A |
114 | $this->assertEquals(array(), $updater->update()); |
115 | } | |
116 | ||
117 | /** | |
118 | * Test the update() method, with all updates to run (except the failing one). | |
119 | */ | |
120 | public function testUpdatesFirstTime() | |
121 | { | |
122 | $updates = array('updateMethodException',); | |
123 | $expectedUpdates = array( | |
124 | 'updateMethodDummy1', | |
125 | 'updateMethodDummy2', | |
126 | 'updateMethodDummy3', | |
127 | ); | |
278d9ee2 | 128 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
129 | $this->assertEquals($expectedUpdates, $updater->update()); |
130 | } | |
131 | ||
132 | /** | |
133 | * Test the update() method, only one update to run. | |
134 | */ | |
135 | public function testOneUpdate() | |
136 | { | |
137 | $updates = array( | |
138 | 'updateMethodDummy1', | |
139 | 'updateMethodDummy3', | |
140 | 'updateMethodException', | |
141 | ); | |
142 | $expectedUpdate = array('updateMethodDummy2'); | |
143 | ||
278d9ee2 | 144 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
145 | $this->assertEquals($expectedUpdate, $updater->update()); |
146 | } | |
147 | ||
148 | /** | |
149 | * Test Update failed. | |
150 | * | |
151 | * @expectedException UpdaterException | |
152 | */ | |
153 | public function testUpdateFailed() | |
154 | { | |
155 | $updates = array( | |
156 | 'updateMethodDummy1', | |
157 | 'updateMethodDummy2', | |
158 | 'updateMethodDummy3', | |
159 | ); | |
160 | ||
278d9ee2 | 161 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
162 | $updater->update(); |
163 | } | |
164 | ||
165 | /** | |
166 | * Test update mergeDeprecatedConfig: | |
167 | * 1. init a config file. | |
168 | * 2. init a options.php file with update value. | |
169 | * 3. merge. | |
170 | * 4. check updated value in config file. | |
171 | */ | |
172 | public function testUpdateMergeDeprecatedConfig() | |
173 | { | |
278d9ee2 A |
174 | $this->conf->setConfigFile('tests/utils/config/configPhp'); |
175 | $this->conf->reset(); | |
684e662a A |
176 | |
177 | $optionsFile = 'tests/Updater/options.php'; | |
da10377b A |
178 | $options = '<?php |
179 | $GLOBALS[\'privateLinkByDefault\'] = true;'; | |
180 | file_put_contents($optionsFile, $options); | |
510377d2 | 181 | |
da10377b | 182 | // tmp config file. |
278d9ee2 | 183 | $this->conf->setConfigFile('tests/Updater/config'); |
510377d2 A |
184 | |
185 | // merge configs | |
278d9ee2 | 186 | $updater = new Updater(array(), array(), $this->conf, true); |
b74b96bf | 187 | // This writes a new config file in tests/Updater/config.php |
510377d2 A |
188 | $updater->updateMethodMergeDeprecatedConfigFile(); |
189 | ||
190 | // make sure updated field is changed | |
684e662a | 191 | $this->conf->reload(); |
894a3c4b | 192 | $this->assertTrue($this->conf->get('privacy.default_private_links')); |
684e662a | 193 | $this->assertFalse(is_file($optionsFile)); |
b74b96bf | 194 | // Delete the generated file. |
278d9ee2 | 195 | unlink($this->conf->getConfigFileExt()); |
510377d2 A |
196 | } |
197 | ||
198 | /** | |
199 | * Test mergeDeprecatedConfig in without options file. | |
200 | */ | |
201 | public function testMergeDeprecatedConfigNoFile() | |
202 | { | |
278d9ee2 | 203 | $updater = new Updater(array(), array(), $this->conf, true); |
510377d2 A |
204 | $updater->updateMethodMergeDeprecatedConfigFile(); |
205 | ||
da10377b | 206 | $this->assertEquals('root', $this->conf->get('credentials.login')); |
510377d2 | 207 | } |
21979ff1 | 208 | |
684e662a A |
209 | /** |
210 | * Test renameDashTags update method. | |
211 | */ | |
21979ff1 A |
212 | public function testRenameDashTags() |
213 | { | |
214 | $refDB = new ReferenceLinkDB(); | |
215 | $refDB->write(self::$testDatastore); | |
216 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
c3dfd899 | 217 | |
528a6f8a | 218 | $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
278d9ee2 | 219 | $updater = new Updater(array(), $linkDB, $this->conf, true); |
21979ff1 | 220 | $updater->updateMethodRenameDashTags(); |
528a6f8a | 221 | $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
21979ff1 | 222 | } |
b74b96bf A |
223 | |
224 | /** | |
225 | * Convert old PHP config file to JSON config. | |
226 | */ | |
227 | public function testConfigToJson() | |
228 | { | |
229 | $configFile = 'tests/utils/config/configPhp'; | |
278d9ee2 A |
230 | $this->conf->setConfigFile($configFile); |
231 | $this->conf->reset(); | |
b74b96bf A |
232 | |
233 | // The ConfigIO is initialized with ConfigPhp. | |
278d9ee2 | 234 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); |
b74b96bf | 235 | |
278d9ee2 | 236 | $updater = new Updater(array(), array(), $this->conf, false); |
b74b96bf A |
237 | $done = $updater->updateMethodConfigToJson(); |
238 | $this->assertTrue($done); | |
239 | ||
240 | // The ConfigIO has been updated to ConfigJson. | |
278d9ee2 A |
241 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); |
242 | $this->assertTrue(file_exists($this->conf->getConfigFileExt())); | |
b74b96bf A |
243 | |
244 | // Check JSON config data. | |
278d9ee2 A |
245 | $this->conf->reload(); |
246 | $this->assertEquals('root', $this->conf->get('credentials.login')); | |
894a3c4b A |
247 | $this->assertEquals('lala', $this->conf->get('redirector.url')); |
248 | $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); | |
278d9ee2 | 249 | $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); |
b74b96bf A |
250 | |
251 | rename($configFile . '.save.php', $configFile . '.php'); | |
278d9ee2 | 252 | unlink($this->conf->getConfigFileExt()); |
b74b96bf A |
253 | } |
254 | ||
255 | /** | |
256 | * Launch config conversion update with an existing JSON file => nothing to do. | |
257 | */ | |
258 | public function testConfigToJsonNothingToDo() | |
259 | { | |
278d9ee2 A |
260 | $filetime = filemtime($this->conf->getConfigFileExt()); |
261 | $updater = new Updater(array(), array(), $this->conf, false); | |
b74b96bf A |
262 | $done = $updater->updateMethodConfigToJson(); |
263 | $this->assertTrue($done); | |
278d9ee2 | 264 | $expected = filemtime($this->conf->getConfigFileExt()); |
b74b96bf A |
265 | $this->assertEquals($expected, $filetime); |
266 | } | |
b9f8b837 A |
267 | |
268 | /** | |
269 | * Test escapeUnescapedConfig with valid data. | |
270 | */ | |
271 | public function testEscapeConfig() | |
272 | { | |
273 | $sandbox = 'sandbox/config'; | |
cbfdcff2 | 274 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); |
b9f8b837 A |
275 | $this->conf = new ConfigManager($sandbox); |
276 | $title = '<script>alert("title");</script>'; | |
277 | $headerLink = '<script>alert("header_link");</script>'; | |
278 | $redirectorUrl = '<script>alert("redirector");</script>'; | |
279 | $this->conf->set('general.title', $title); | |
280 | $this->conf->set('general.header_link', $headerLink); | |
281 | $this->conf->set('redirector.url', $redirectorUrl); | |
282 | $updater = new Updater(array(), array(), $this->conf, true); | |
283 | $done = $updater->updateMethodEscapeUnescapedConfig(); | |
284 | $this->assertTrue($done); | |
285 | $this->conf->reload(); | |
286 | $this->assertEquals(escape($title), $this->conf->get('general.title')); | |
287 | $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); | |
288 | $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); | |
cbfdcff2 A |
289 | unlink($sandbox . '.json.php'); |
290 | } | |
291 | ||
292 | /** | |
293 | * Test updateMethodApiSettings(): create default settings for the API (enabled + secret). | |
294 | */ | |
295 | public function testUpdateApiSettings() | |
296 | { | |
297 | $confFile = 'sandbox/config'; | |
298 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | |
299 | $conf = new ConfigManager($confFile); | |
300 | $updater = new Updater(array(), array(), $conf, true); | |
301 | ||
302 | $this->assertFalse($conf->exists('api.enabled')); | |
303 | $this->assertFalse($conf->exists('api.secret')); | |
304 | $updater->updateMethodApiSettings(); | |
305 | $conf->reload(); | |
306 | $this->assertTrue($conf->get('api.enabled')); | |
307 | $this->assertTrue($conf->exists('api.secret')); | |
308 | unlink($confFile .'.json.php'); | |
309 | } | |
310 | ||
311 | /** | |
312 | * Test updateMethodApiSettings(): already set, do nothing. | |
313 | */ | |
314 | public function testUpdateApiSettingsNothingToDo() | |
315 | { | |
316 | $confFile = 'sandbox/config'; | |
317 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | |
318 | $conf = new ConfigManager($confFile); | |
319 | $conf->set('api.enabled', false); | |
320 | $conf->set('api.secret', ''); | |
321 | $updater = new Updater(array(), array(), $conf, true); | |
322 | $updater->updateMethodApiSettings(); | |
323 | $this->assertFalse($conf->get('api.enabled')); | |
324 | $this->assertEmpty($conf->get('api.secret')); | |
325 | unlink($confFile .'.json.php'); | |
b9f8b837 | 326 | } |
c3dfd899 A |
327 | |
328 | /** | |
329 | * Test updateMethodDatastoreIds(). | |
330 | */ | |
331 | public function testDatastoreIds() | |
332 | { | |
333 | $links = array( | |
334 | '20121206_182539' => array( | |
335 | 'linkdate' => '20121206_182539', | |
336 | 'title' => 'Geek and Poke', | |
337 | 'url' => 'http://geek-and-poke.com/', | |
338 | 'description' => 'desc', | |
339 | 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', | |
340 | 'updated' => '20121206_190301', | |
341 | 'private' => false, | |
342 | ), | |
343 | '20121206_172539' => array( | |
344 | 'linkdate' => '20121206_172539', | |
345 | 'title' => 'UserFriendly - Samba', | |
346 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', | |
347 | 'description' => '', | |
348 | 'tags' => 'samba cartoon web', | |
349 | 'private' => false, | |
350 | ), | |
351 | '20121206_142300' => array( | |
352 | 'linkdate' => '20121206_142300', | |
353 | 'title' => 'UserFriendly - Web Designer', | |
354 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', | |
355 | 'description' => 'Naming conventions... #private', | |
356 | 'tags' => 'samba cartoon web', | |
357 | 'private' => true, | |
358 | ), | |
359 | ); | |
360 | $refDB = new ReferenceLinkDB(); | |
361 | $refDB->setLinks($links); | |
362 | $refDB->write(self::$testDatastore); | |
363 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
364 | ||
365 | $checksum = hash_file('sha1', self::$testDatastore); | |
366 | ||
367 | $this->conf->set('resource.data_dir', 'sandbox'); | |
368 | $this->conf->set('resource.datastore', self::$testDatastore); | |
369 | ||
370 | $updater = new Updater(array(), $linkDB, $this->conf, true); | |
371 | $this->assertTrue($updater->updateMethodDatastoreIds()); | |
372 | ||
373 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
374 | ||
375 | $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); | |
376 | $backup = $backup[0]; | |
377 | ||
378 | $this->assertFileExists($backup); | |
379 | $this->assertEquals($checksum, hash_file('sha1', $backup)); | |
380 | unlink($backup); | |
381 | ||
382 | $this->assertEquals(3, count($linkDB)); | |
383 | $this->assertTrue(isset($linkDB[0])); | |
384 | $this->assertFalse(isset($linkDB[0]['linkdate'])); | |
385 | $this->assertEquals(0, $linkDB[0]['id']); | |
386 | $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); | |
387 | $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); | |
388 | $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); | |
389 | $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); | |
390 | $this->assertTrue($linkDB[0]['private']); | |
d592daea | 391 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); |
c3dfd899 A |
392 | |
393 | $this->assertTrue(isset($linkDB[1])); | |
394 | $this->assertFalse(isset($linkDB[1]['linkdate'])); | |
395 | $this->assertEquals(1, $linkDB[1]['id']); | |
396 | $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); | |
d592daea | 397 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); |
c3dfd899 A |
398 | |
399 | $this->assertTrue(isset($linkDB[2])); | |
400 | $this->assertFalse(isset($linkDB[2]['linkdate'])); | |
401 | $this->assertEquals(2, $linkDB[2]['id']); | |
402 | $this->assertEquals('Geek and Poke', $linkDB[2]['title']); | |
d592daea A |
403 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']); |
404 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']); | |
c3dfd899 A |
405 | } |
406 | ||
407 | /** | |
408 | * Test updateMethodDatastoreIds() with the update already applied: nothing to do. | |
409 | */ | |
410 | public function testDatastoreIdsNothingToDo() | |
411 | { | |
412 | $refDB = new ReferenceLinkDB(); | |
413 | $refDB->write(self::$testDatastore); | |
414 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
415 | ||
416 | $this->conf->set('resource.data_dir', 'sandbox'); | |
417 | $this->conf->set('resource.datastore', self::$testDatastore); | |
418 | ||
419 | $checksum = hash_file('sha1', self::$testDatastore); | |
420 | $updater = new Updater(array(), $linkDB, $this->conf, true); | |
421 | $this->assertTrue($updater->updateMethodDatastoreIds()); | |
422 | $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); | |
423 | } | |
510377d2 | 424 | } |