UpdaterTest.php 28 KB

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