]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/Updater/UpdaterTest.php
Merge pull request #666 from ArthurHoaro/slim-api
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
1 <?php
2
3 require_once 'application/config/ConfigManager.php';
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 {
12 /**
13 * @var string Path to test datastore.
14 */
15 protected static $testDatastore = 'sandbox/datastore.php';
16
17 /**
18 * @var string Config file path (without extension).
19 */
20 protected static $configFile = 'tests/utils/config/configJson';
21
22 /**
23 * @var ConfigManager
24 */
25 protected $conf;
26
27 /**
28 * Executed before each test.
29 */
30 public function setUp()
31 {
32 $this->conf = new ConfigManager(self::$configFile);
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(''));
41 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
42 touch($updatesFile);
43 $this->assertEquals(array(), read_updates_file($updatesFile));
44 unlink($updatesFile);
45 }
46
47 /**
48 * Test read/write updates file.
49 */
50 public function testReadWriteUpdatesFile()
51 {
52 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
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);
64 unlink($updatesFile);
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 {
86 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
87 touch($updatesFile);
88 chmod($updatesFile, 0444);
89 try {
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception $e) {
92 unlink($updatesFile);
93 throw $e;
94 }
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 );
110 $updater = new DummyUpdater($updates, array(), $this->conf, true);
111 $this->assertEquals(array(), $updater->update());
112
113 $updater = new DummyUpdater(array(), array(), $this->conf, false);
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 );
128 $updater = new DummyUpdater($updates, array(), $this->conf, true);
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
144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
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
161 $updater = new DummyUpdater($updates, array(), $this->conf, true);
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 {
174 $this->conf->setConfigFile('tests/utils/config/configPhp');
175 $this->conf->reset();
176
177 $optionsFile = 'tests/Updater/options.php';
178 $options = '<?php
179 $GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
181
182 // tmp config file.
183 $this->conf->setConfigFile('tests/Updater/config');
184
185 // merge configs
186 $updater = new Updater(array(), array(), $this->conf, true);
187 // This writes a new config file in tests/Updater/config.php
188 $updater->updateMethodMergeDeprecatedConfigFile();
189
190 // make sure updated field is changed
191 $this->conf->reload();
192 $this->assertTrue($this->conf->get('privacy.default_private_links'));
193 $this->assertFalse(is_file($optionsFile));
194 // Delete the generated file.
195 unlink($this->conf->getConfigFileExt());
196 }
197
198 /**
199 * Test mergeDeprecatedConfig in without options file.
200 */
201 public function testMergeDeprecatedConfigNoFile()
202 {
203 $updater = new Updater(array(), array(), $this->conf, true);
204 $updater->updateMethodMergeDeprecatedConfigFile();
205
206 $this->assertEquals('root', $this->conf->get('credentials.login'));
207 }
208
209 /**
210 * Test renameDashTags update method.
211 */
212 public function testRenameDashTags()
213 {
214 $refDB = new ReferenceLinkDB();
215 $refDB->write(self::$testDatastore);
216 $linkDB = new LinkDB(self::$testDatastore, true, false);
217
218 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
219 $updater = new Updater(array(), $linkDB, $this->conf, true);
220 $updater->updateMethodRenameDashTags();
221 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
222 }
223
224 /**
225 * Convert old PHP config file to JSON config.
226 */
227 public function testConfigToJson()
228 {
229 $configFile = 'tests/utils/config/configPhp';
230 $this->conf->setConfigFile($configFile);
231 $this->conf->reset();
232
233 // The ConfigIO is initialized with ConfigPhp.
234 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
235
236 $updater = new Updater(array(), array(), $this->conf, false);
237 $done = $updater->updateMethodConfigToJson();
238 $this->assertTrue($done);
239
240 // The ConfigIO has been updated to ConfigJson.
241 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
242 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
243
244 // Check JSON config data.
245 $this->conf->reload();
246 $this->assertEquals('root', $this->conf->get('credentials.login'));
247 $this->assertEquals('lala', $this->conf->get('redirector.url'));
248 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
249 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
250
251 rename($configFile . '.save.php', $configFile . '.php');
252 unlink($this->conf->getConfigFileExt());
253 }
254
255 /**
256 * Launch config conversion update with an existing JSON file => nothing to do.
257 */
258 public function testConfigToJsonNothingToDo()
259 {
260 $filetime = filemtime($this->conf->getConfigFileExt());
261 $updater = new Updater(array(), array(), $this->conf, false);
262 $done = $updater->updateMethodConfigToJson();
263 $this->assertTrue($done);
264 $expected = filemtime($this->conf->getConfigFileExt());
265 $this->assertEquals($expected, $filetime);
266 }
267
268 /**
269 * Test escapeUnescapedConfig with valid data.
270 */
271 public function testEscapeConfig()
272 {
273 $sandbox = 'sandbox/config';
274 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
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'));
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');
326 }
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']);
391 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']);
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']);
397 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']);
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']);
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']);
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 }
424 }