UpdaterTest.php 26 KB

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