aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Updater/UpdaterTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Updater/UpdaterTest.php')
-rw-r--r--tests/Updater/UpdaterTest.php347
1 files changed, 278 insertions, 69 deletions
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index a29d9067..a3e8a4d2 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -1,5 +1,6 @@
1<?php 1<?php
2 2
3require_once 'application/config/ConfigManager.php';
3require_once 'tests/Updater/DummyUpdater.php'; 4require_once 'tests/Updater/DummyUpdater.php';
4 5
5/** 6/**
@@ -9,58 +10,26 @@ require_once 'tests/Updater/DummyUpdater.php';
9class UpdaterTest extends PHPUnit_Framework_TestCase 10class UpdaterTest extends PHPUnit_Framework_TestCase
10{ 11{
11 /** 12 /**
12 * @var array Configuration input set. 13 * @var string Path to test datastore.
13 */ 14 */
14 private static $configFields; 15 protected static $testDatastore = 'sandbox/datastore.php';
15 16
16 /** 17 /**
17 * @var string Path to test datastore. 18 * @var string Config file path (without extension).
18 */ 19 */
19 protected static $testDatastore = 'sandbox/datastore.php'; 20 protected static $configFile = 'tests/utils/config/configJson';
20 21
21 /** 22 /**
22 * Executed before each test. 23 * @var ConfigManager
23 */ 24 */
24 public function setUp() 25 protected $conf;
25 {
26 self::$configFields = array(
27 'login' => 'login',
28 'hash' => 'hash',
29 'salt' => 'salt',
30 'timezone' => 'Europe/Paris',
31 'title' => 'title',
32 'titleLink' => 'titleLink',
33 'redirector' => '',
34 'disablesessionprotection' => false,
35 'privateLinkByDefault' => false,
36 'config' => array(
37 'CONFIG_FILE' => 'tests/Updater/config.php',
38 'DATADIR' => 'tests/Updater',
39 'PAGECACHE' => 'sandbox/pagecache',
40 'config1' => 'config1data',
41 'config2' => 'config2data',
42 )
43 );
44 }
45 26
46 /** 27 /**
47 * Executed after each test. 28 * Executed before each test.
48 *
49 * @return void
50 */ 29 */
51 public function tearDown() 30 public function setUp()
52 { 31 {
53 if (is_file(self::$configFields['config']['CONFIG_FILE'])) { 32 $this->conf = new ConfigManager(self::$configFile);
54 unlink(self::$configFields['config']['CONFIG_FILE']);
55 }
56
57 if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) {
58 unlink(self::$configFields['config']['DATADIR'] . '/options.php');
59 }
60
61 if (is_file(self::$configFields['config']['DATADIR'] . '/updates.json')) {
62 unlink(self::$configFields['config']['DATADIR'] . '/updates.json');
63 }
64 } 33 }
65 34
66 /** 35 /**
@@ -69,9 +38,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
69 public function testReadEmptyUpdatesFile() 38 public function testReadEmptyUpdatesFile()
70 { 39 {
71 $this->assertEquals(array(), read_updates_file('')); 40 $this->assertEquals(array(), read_updates_file(''));
72 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 41 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
73 touch($updatesFile); 42 touch($updatesFile);
74 $this->assertEquals(array(), read_updates_file($updatesFile)); 43 $this->assertEquals(array(), read_updates_file($updatesFile));
44 unlink($updatesFile);
75 } 45 }
76 46
77 /** 47 /**
@@ -79,7 +49,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
79 */ 49 */
80 public function testReadWriteUpdatesFile() 50 public function testReadWriteUpdatesFile()
81 { 51 {
82 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 52 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
83 $updatesMethods = array('m1', 'm2', 'm3'); 53 $updatesMethods = array('m1', 'm2', 'm3');
84 54
85 write_updates_file($updatesFile, $updatesMethods); 55 write_updates_file($updatesFile, $updatesMethods);
@@ -91,6 +61,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
91 write_updates_file($updatesFile, $updatesMethods); 61 write_updates_file($updatesFile, $updatesMethods);
92 $readMethods = read_updates_file($updatesFile); 62 $readMethods = read_updates_file($updatesFile);
93 $this->assertEquals($readMethods, $updatesMethods); 63 $this->assertEquals($readMethods, $updatesMethods);
64 unlink($updatesFile);
94 } 65 }
95 66
96 /** 67 /**
@@ -112,10 +83,15 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
112 */ 83 */
113 public function testWriteUpdatesFileNotWritable() 84 public function testWriteUpdatesFileNotWritable()
114 { 85 {
115 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; 86 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
116 touch($updatesFile); 87 touch($updatesFile);
117 chmod($updatesFile, 0444); 88 chmod($updatesFile, 0444);
118 @write_updates_file($updatesFile, array('test')); 89 try {
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception $e) {
92 unlink($updatesFile);
93 throw $e;
94 }
119 } 95 }
120 96
121 /** 97 /**
@@ -131,10 +107,10 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
131 'updateMethodDummy3', 107 'updateMethodDummy3',
132 'updateMethodException', 108 'updateMethodException',
133 ); 109 );
134 $updater = new DummyUpdater($updates, array(), array(), true); 110 $updater = new DummyUpdater($updates, array(), $this->conf, true);
135 $this->assertEquals(array(), $updater->update()); 111 $this->assertEquals(array(), $updater->update());
136 112
137 $updater = new DummyUpdater(array(), array(), array(), false); 113 $updater = new DummyUpdater(array(), array(), $this->conf, false);
138 $this->assertEquals(array(), $updater->update()); 114 $this->assertEquals(array(), $updater->update());
139 } 115 }
140 116
@@ -149,7 +125,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
149 'updateMethodDummy2', 125 'updateMethodDummy2',
150 'updateMethodDummy3', 126 'updateMethodDummy3',
151 ); 127 );
152 $updater = new DummyUpdater($updates, array(), array(), true); 128 $updater = new DummyUpdater($updates, array(), $this->conf, true);
153 $this->assertEquals($expectedUpdates, $updater->update()); 129 $this->assertEquals($expectedUpdates, $updater->update());
154 } 130 }
155 131
@@ -165,7 +141,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
165 ); 141 );
166 $expectedUpdate = array('updateMethodDummy2'); 142 $expectedUpdate = array('updateMethodDummy2');
167 143
168 $updater = new DummyUpdater($updates, array(), array(), true); 144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
169 $this->assertEquals($expectedUpdate, $updater->update()); 145 $this->assertEquals($expectedUpdate, $updater->update());
170 } 146 }
171 147
@@ -182,7 +158,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
182 'updateMethodDummy3', 158 'updateMethodDummy3',
183 ); 159 );
184 160
185 $updater = new DummyUpdater($updates, array(), array(), true); 161 $updater = new DummyUpdater($updates, array(), $this->conf, true);
186 $updater->update(); 162 $updater->update();
187 } 163 }
188 164
@@ -195,26 +171,28 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
195 */ 171 */
196 public function testUpdateMergeDeprecatedConfig() 172 public function testUpdateMergeDeprecatedConfig()
197 { 173 {
198 // init 174 $this->conf->setConfigFile('tests/utils/config/configPhp');
199 writeConfig(self::$configFields, true); 175 $this->conf->reset();
200 $configCopy = self::$configFields;
201 $invert = !$configCopy['privateLinkByDefault'];
202 $configCopy['privateLinkByDefault'] = $invert;
203 176
204 // Use writeConfig to create a options.php 177 $optionsFile = 'tests/Updater/options.php';
205 $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php'; 178 $options = '<?php
206 writeConfig($configCopy, true); 179$GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
207 181
208 $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); 182 // tmp config file.
183 $this->conf->setConfigFile('tests/Updater/config');
209 184
210 // merge configs 185 // merge configs
211 $updater = new Updater(array(), self::$configFields, array(), true); 186 $updater = new Updater(array(), array(), $this->conf, true);
187 // This writes a new config file in tests/Updater/config.php
212 $updater->updateMethodMergeDeprecatedConfigFile(); 188 $updater->updateMethodMergeDeprecatedConfigFile();
213 189
214 // make sure updated field is changed 190 // make sure updated field is changed
215 include self::$configFields['config']['CONFIG_FILE']; 191 $this->conf->reload();
216 $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']); 192 $this->assertTrue($this->conf->get('privacy.default_private_links'));
217 $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE'])); 193 $this->assertFalse(is_file($optionsFile));
194 // Delete the generated file.
195 unlink($this->conf->getConfigFileExt());
218 } 196 }
219 197
220 /** 198 /**
@@ -222,23 +200,254 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
222 */ 200 */
223 public function testMergeDeprecatedConfigNoFile() 201 public function testMergeDeprecatedConfigNoFile()
224 { 202 {
225 writeConfig(self::$configFields, true); 203 $updater = new Updater(array(), array(), $this->conf, true);
226
227 $updater = new Updater(array(), self::$configFields, array(), true);
228 $updater->updateMethodMergeDeprecatedConfigFile(); 204 $updater->updateMethodMergeDeprecatedConfigFile();
229 205
230 include self::$configFields['config']['CONFIG_FILE']; 206 $this->assertEquals('root', $this->conf->get('credentials.login'));
231 $this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
232 } 207 }
233 208
209 /**
210 * Test renameDashTags update method.
211 */
234 public function testRenameDashTags() 212 public function testRenameDashTags()
235 { 213 {
236 $refDB = new ReferenceLinkDB(); 214 $refDB = new ReferenceLinkDB();
237 $refDB->write(self::$testDatastore); 215 $refDB->write(self::$testDatastore);
238 $linkDB = new LinkDB(self::$testDatastore, true, false); 216 $linkDB = new LinkDB(self::$testDatastore, true, false);
217
239 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); 218 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
240 $updater = new Updater(array(), self::$configFields, $linkDB, true); 219 $updater = new Updater(array(), $linkDB, $this->conf, true);
241 $updater->updateMethodRenameDashTags(); 220 $updater->updateMethodRenameDashTags();
242 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); 221 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
243 } 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 updateMethodDatastoreIds().
294 */
295 public function testDatastoreIds()
296 {
297 $links = array(
298 '20121206_182539' => array(
299 'linkdate' => '20121206_182539',
300 'title' => 'Geek and Poke',
301 'url' => 'http://geek-and-poke.com/',
302 'description' => 'desc',
303 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
304 'updated' => '20121206_190301',
305 'private' => false,
306 ),
307 '20121206_172539' => array(
308 'linkdate' => '20121206_172539',
309 'title' => 'UserFriendly - Samba',
310 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
311 'description' => '',
312 'tags' => 'samba cartoon web',
313 'private' => false,
314 ),
315 '20121206_142300' => array(
316 'linkdate' => '20121206_142300',
317 'title' => 'UserFriendly - Web Designer',
318 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
319 'description' => 'Naming conventions... #private',
320 'tags' => 'samba cartoon web',
321 'private' => true,
322 ),
323 );
324 $refDB = new ReferenceLinkDB();
325 $refDB->setLinks($links);
326 $refDB->write(self::$testDatastore);
327 $linkDB = new LinkDB(self::$testDatastore, true, false);
328
329 $checksum = hash_file('sha1', self::$testDatastore);
330
331 $this->conf->set('resource.data_dir', 'sandbox');
332 $this->conf->set('resource.datastore', self::$testDatastore);
333
334 $updater = new Updater(array(), $linkDB, $this->conf, true);
335 $this->assertTrue($updater->updateMethodDatastoreIds());
336
337 $linkDB = new LinkDB(self::$testDatastore, true, false);
338
339 $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
340 $backup = $backup[0];
341
342 $this->assertFileExists($backup);
343 $this->assertEquals($checksum, hash_file('sha1', $backup));
344 unlink($backup);
345
346 $this->assertEquals(3, count($linkDB));
347 $this->assertTrue(isset($linkDB[0]));
348 $this->assertFalse(isset($linkDB[0]['linkdate']));
349 $this->assertEquals(0, $linkDB[0]['id']);
350 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
351 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
352 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
353 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
354 $this->assertTrue($linkDB[0]['private']);
355 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']);
356
357 $this->assertTrue(isset($linkDB[1]));
358 $this->assertFalse(isset($linkDB[1]['linkdate']));
359 $this->assertEquals(1, $linkDB[1]['id']);
360 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
361 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']);
362
363 $this->assertTrue(isset($linkDB[2]));
364 $this->assertFalse(isset($linkDB[2]['linkdate']));
365 $this->assertEquals(2, $linkDB[2]['id']);
366 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
367 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']);
368 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']);
369 }
370
371 /**
372 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
373 */
374 public function testDatastoreIdsNothingToDo()
375 {
376 $refDB = new ReferenceLinkDB();
377 $refDB->write(self::$testDatastore);
378 $linkDB = new LinkDB(self::$testDatastore, true, false);
379
380 $this->conf->set('resource.data_dir', 'sandbox');
381 $this->conf->set('resource.datastore', self::$testDatastore);
382
383 $checksum = hash_file('sha1', self::$testDatastore);
384 $updater = new Updater(array(), $linkDB, $this->conf, true);
385 $this->assertTrue($updater->updateMethodDatastoreIds());
386 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
387 }
388
389 /**
390 * Test updateMethodEscapeMarkdown with markdown plugin enabled
391 * => setting markdown_escape set to false.
392 */
393 public function testEscapeMarkdownSettingToFalse()
394 {
395 $sandboxConf = 'sandbox/config';
396 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
397 $this->conf = new ConfigManager($sandboxConf);
398
399 $this->conf->set('general.enabled_plugins', array('markdown'));
400 $updater = new Updater(array(), array(), $this->conf, true);
401 $this->assertTrue($updater->updateMethodEscapeMarkdown());
402 $this->assertFalse($this->conf->get('security.markdown_escape'));
403
404 // reload from file
405 $this->conf = new ConfigManager($sandboxConf);
406 $this->assertFalse($this->conf->get('security.markdown_escape'));
407 }
408
409 /**
410 * Test updateMethodEscapeMarkdown with markdown plugin disabled
411 * => setting markdown_escape set to true.
412 */
413 public function testEscapeMarkdownSettingToTrue()
414 {
415 $sandboxConf = 'sandbox/config';
416 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
417 $this->conf = new ConfigManager($sandboxConf);
418
419 $this->conf->set('general.enabled_plugins', array());
420 $updater = new Updater(array(), array(), $this->conf, true);
421 $this->assertTrue($updater->updateMethodEscapeMarkdown());
422 $this->assertTrue($this->conf->get('security.markdown_escape'));
423
424 // reload from file
425 $this->conf = new ConfigManager($sandboxConf);
426 $this->assertTrue($this->conf->get('security.markdown_escape'));
427 }
428
429 /**
430 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
431 */
432 public function testEscapeMarkdownSettingNothingToDoEnabled()
433 {
434 $sandboxConf = 'sandbox/config';
435 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
436 $this->conf = new ConfigManager($sandboxConf);
437 $this->conf->set('security.markdown_escape', true);
438 $updater = new Updater(array(), array(), $this->conf, true);
439 $this->assertTrue($updater->updateMethodEscapeMarkdown());
440 $this->assertTrue($this->conf->get('security.markdown_escape'));
441 }
442
443 /**
444 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
445 */
446 public function testEscapeMarkdownSettingNothingToDoDisabled()
447 {
448 $this->conf->set('security.markdown_escape', false);
449 $updater = new Updater(array(), array(), $this->conf, true);
450 $this->assertTrue($updater->updateMethodEscapeMarkdown());
451 $this->assertFalse($this->conf->get('security.markdown_escape'));
452 }
244} 453}