UpdaterTest.php 30 KB

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