ApplicationUtilsTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. use Shaarli\Config\ConfigManager;
  3. /**
  4. * ApplicationUtils' tests
  5. */
  6. require_once 'application/ApplicationUtils.php';
  7. /**
  8. * Fake ApplicationUtils class to avoid HTTP requests
  9. */
  10. class FakeApplicationUtils extends ApplicationUtils
  11. {
  12. public static $VERSION_CODE = '';
  13. /**
  14. * Toggle HTTP requests, allow overriding the version code
  15. */
  16. public static function getVersion($url, $timeout = 0)
  17. {
  18. return self::$VERSION_CODE;
  19. }
  20. }
  21. /**
  22. * Unitary tests for Shaarli utilities
  23. */
  24. class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
  25. {
  26. protected static $testUpdateFile = 'sandbox/update.txt';
  27. protected static $testVersion = '0.5.0';
  28. protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
  29. /**
  30. * Reset test data for each test
  31. */
  32. public function setUp()
  33. {
  34. FakeApplicationUtils::$VERSION_CODE = '';
  35. if (file_exists(self::$testUpdateFile)) {
  36. unlink(self::$testUpdateFile);
  37. }
  38. }
  39. /**
  40. * Remove test version file if it exists
  41. */
  42. public function tearDown()
  43. {
  44. if (is_file('sandbox/version.php')) {
  45. unlink('sandbox/version.php');
  46. }
  47. }
  48. /**
  49. * Retrieve the latest version code available on Git
  50. *
  51. * Expected format: Semantic Versioning - major.minor.patch
  52. */
  53. public function testGetVersionCode()
  54. {
  55. $testTimeout = 10;
  56. $this->assertEquals(
  57. '0.5.4',
  58. ApplicationUtils::getVersion(
  59. 'https://raw.githubusercontent.com/shaarli/Shaarli/'
  60. .'v0.5.4/shaarli_version.php',
  61. $testTimeout
  62. )
  63. );
  64. $this->assertRegExp(
  65. self::$versionPattern,
  66. ApplicationUtils::getVersion(
  67. 'https://raw.githubusercontent.com/shaarli/Shaarli/'
  68. .'latest/shaarli_version.php',
  69. $testTimeout
  70. )
  71. );
  72. }
  73. /**
  74. * Attempt to retrieve the latest version from an invalid File
  75. */
  76. public function testGetVersionCodeFromFile()
  77. {
  78. file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL);
  79. $this->assertEquals(
  80. '1.2.3',
  81. ApplicationUtils::getVersion('sandbox/version.php', 1)
  82. );
  83. }
  84. /**
  85. * Attempt to retrieve the latest version from an invalid File
  86. */
  87. public function testGetVersionCodeInvalidFile()
  88. {
  89. $oldlog = ini_get('error_log');
  90. ini_set('error_log', '/dev/null');
  91. $this->assertFalse(
  92. ApplicationUtils::getVersion('idontexist', 1)
  93. );
  94. ini_set('error_log', $oldlog);
  95. }
  96. /**
  97. * Test update checks - the user is logged off
  98. */
  99. public function testCheckUpdateLoggedOff()
  100. {
  101. $this->assertFalse(
  102. ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false)
  103. );
  104. }
  105. /**
  106. * Test update checks - the user has disabled updates
  107. */
  108. public function testCheckUpdateUserDisabled()
  109. {
  110. $this->assertFalse(
  111. ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true)
  112. );
  113. }
  114. /**
  115. * A newer version is available
  116. */
  117. public function testCheckUpdateNewVersionAvailable()
  118. {
  119. $newVersion = '1.8.3';
  120. FakeApplicationUtils::$VERSION_CODE = $newVersion;
  121. $version = FakeApplicationUtils::checkUpdate(
  122. self::$testVersion,
  123. self::$testUpdateFile,
  124. 100,
  125. true,
  126. true
  127. );
  128. $this->assertEquals($newVersion, $version);
  129. }
  130. /**
  131. * No available information about versions
  132. */
  133. public function testCheckUpdateNewVersionUnavailable()
  134. {
  135. $version = FakeApplicationUtils::checkUpdate(
  136. self::$testVersion,
  137. self::$testUpdateFile,
  138. 100,
  139. true,
  140. true
  141. );
  142. $this->assertFalse($version);
  143. }
  144. /**
  145. * Test update checks - invalid Git branch
  146. * @expectedException Exception
  147. * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
  148. */
  149. public function testCheckUpdateInvalidGitBranch()
  150. {
  151. ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
  152. }
  153. /**
  154. * Shaarli is up-to-date
  155. */
  156. public function testCheckUpdateNewVersionUpToDate()
  157. {
  158. FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
  159. $version = FakeApplicationUtils::checkUpdate(
  160. self::$testVersion,
  161. self::$testUpdateFile,
  162. 100,
  163. true,
  164. true
  165. );
  166. $this->assertFalse($version);
  167. }
  168. /**
  169. * Time-traveller's Shaarli
  170. */
  171. public function testCheckUpdateNewVersionMaartiMcFly()
  172. {
  173. FakeApplicationUtils::$VERSION_CODE = '0.4.1';
  174. $version = FakeApplicationUtils::checkUpdate(
  175. self::$testVersion,
  176. self::$testUpdateFile,
  177. 100,
  178. true,
  179. true
  180. );
  181. $this->assertFalse($version);
  182. }
  183. /**
  184. * The version has been checked recently and Shaarli is up-to-date
  185. */
  186. public function testCheckUpdateNewVersionTwiceUpToDate()
  187. {
  188. FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
  189. // Create the update file
  190. $version = FakeApplicationUtils::checkUpdate(
  191. self::$testVersion,
  192. self::$testUpdateFile,
  193. 100,
  194. true,
  195. true
  196. );
  197. $this->assertFalse($version);
  198. // Reuse the update file
  199. $version = FakeApplicationUtils::checkUpdate(
  200. self::$testVersion,
  201. self::$testUpdateFile,
  202. 100,
  203. true,
  204. true
  205. );
  206. $this->assertFalse($version);
  207. }
  208. /**
  209. * The version has been checked recently and Shaarli is outdated
  210. */
  211. public function testCheckUpdateNewVersionTwiceOutdated()
  212. {
  213. $newVersion = '1.8.3';
  214. FakeApplicationUtils::$VERSION_CODE = $newVersion;
  215. // Create the update file
  216. $version = FakeApplicationUtils::checkUpdate(
  217. self::$testVersion,
  218. self::$testUpdateFile,
  219. 100,
  220. true,
  221. true
  222. );
  223. $this->assertEquals($newVersion, $version);
  224. // Reuse the update file
  225. $version = FakeApplicationUtils::checkUpdate(
  226. self::$testVersion,
  227. self::$testUpdateFile,
  228. 100,
  229. true,
  230. true
  231. );
  232. $this->assertEquals($newVersion, $version);
  233. }
  234. /**
  235. * Check supported PHP versions
  236. */
  237. public function testCheckSupportedPHPVersion()
  238. {
  239. $minVersion = '5.3';
  240. ApplicationUtils::checkPHPVersion($minVersion, '5.4.32');
  241. ApplicationUtils::checkPHPVersion($minVersion, '5.5');
  242. ApplicationUtils::checkPHPVersion($minVersion, '5.6.10');
  243. }
  244. /**
  245. * Check a unsupported PHP version
  246. * @expectedException Exception
  247. * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
  248. */
  249. public function testCheckSupportedPHPVersion51()
  250. {
  251. ApplicationUtils::checkPHPVersion('5.3', '5.1.0');
  252. }
  253. /**
  254. * Check another unsupported PHP version
  255. * @expectedException Exception
  256. * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
  257. */
  258. public function testCheckSupportedPHPVersion52()
  259. {
  260. ApplicationUtils::checkPHPVersion('5.3', '5.2');
  261. }
  262. /**
  263. * Checks resource permissions for the current Shaarli installation
  264. */
  265. public function testCheckCurrentResourcePermissions()
  266. {
  267. $conf = new ConfigManager('');
  268. $conf->set('resource.thumbnails_cache', 'cache');
  269. $conf->set('resource.config', 'data/config.php');
  270. $conf->set('resource.data_dir', 'data');
  271. $conf->set('resource.datastore', 'data/datastore.php');
  272. $conf->set('resource.ban_file', 'data/ipbans.php');
  273. $conf->set('resource.log', 'data/log.txt');
  274. $conf->set('resource.page_cache', 'pagecache');
  275. $conf->set('resource.raintpl_tmp', 'tmp');
  276. $conf->set('resource.raintpl_tpl', 'tpl');
  277. $conf->set('resource.theme', 'default');
  278. $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
  279. $this->assertEquals(
  280. array(),
  281. ApplicationUtils::checkResourcePermissions($conf)
  282. );
  283. }
  284. /**
  285. * Checks resource permissions for a non-existent Shaarli installation
  286. */
  287. public function testCheckCurrentResourcePermissionsErrors()
  288. {
  289. $conf = new ConfigManager('');
  290. $conf->set('resource.thumbnails_cache', 'null/cache');
  291. $conf->set('resource.config', 'null/data/config.php');
  292. $conf->set('resource.data_dir', 'null/data');
  293. $conf->set('resource.datastore', 'null/data/store.php');
  294. $conf->set('resource.ban_file', 'null/data/ipbans.php');
  295. $conf->set('resource.log', 'null/data/log.txt');
  296. $conf->set('resource.page_cache', 'null/pagecache');
  297. $conf->set('resource.raintpl_tmp', 'null/tmp');
  298. $conf->set('resource.raintpl_tpl', 'null/tpl');
  299. $conf->set('resource.raintpl_theme', 'null/tpl/default');
  300. $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
  301. $this->assertEquals(
  302. array(
  303. '"null/tpl" directory is not readable',
  304. '"null/tpl/default" directory is not readable',
  305. '"null/cache" directory is not readable',
  306. '"null/cache" directory is not writable',
  307. '"null/data" directory is not readable',
  308. '"null/data" directory is not writable',
  309. '"null/pagecache" directory is not readable',
  310. '"null/pagecache" directory is not writable',
  311. '"null/tmp" directory is not readable',
  312. '"null/tmp" directory is not writable'
  313. ),
  314. ApplicationUtils::checkResourcePermissions($conf)
  315. );
  316. }
  317. /**
  318. * Check update with 'dev' as curent version (master branch).
  319. * It should always return false.
  320. */
  321. public function testCheckUpdateDev()
  322. {
  323. $this->assertFalse(
  324. ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true)
  325. );
  326. }
  327. }