ApplicationUtilsTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * ApplicationUtils' tests
  4. */
  5. require_once 'application/config/ConfigManager.php';
  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 getLatestGitVersionCode($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. * Retrieve the latest version code available on Git
  41. *
  42. * Expected format: Semantic Versioning - major.minor.patch
  43. */
  44. public function testGetLatestGitVersionCode()
  45. {
  46. $testTimeout = 10;
  47. $this->assertEquals(
  48. '0.5.4',
  49. ApplicationUtils::getLatestGitVersionCode(
  50. 'https://raw.githubusercontent.com/shaarli/Shaarli/'
  51. .'v0.5.4/shaarli_version.php',
  52. $testTimeout
  53. )
  54. );
  55. $this->assertRegExp(
  56. self::$versionPattern,
  57. ApplicationUtils::getLatestGitVersionCode(
  58. 'https://raw.githubusercontent.com/shaarli/Shaarli/'
  59. .'master/shaarli_version.php',
  60. $testTimeout
  61. )
  62. );
  63. }
  64. /**
  65. * Attempt to retrieve the latest version from an invalid URL
  66. */
  67. public function testGetLatestGitVersionCodeInvalidUrl()
  68. {
  69. $oldlog = ini_get('error_log');
  70. ini_set('error_log', '/dev/null');
  71. $this->assertFalse(
  72. ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1)
  73. );
  74. ini_set('error_log', $oldlog);
  75. }
  76. /**
  77. * Test update checks - the user is logged off
  78. */
  79. public function testCheckUpdateLoggedOff()
  80. {
  81. $this->assertFalse(
  82. ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false)
  83. );
  84. }
  85. /**
  86. * Test update checks - the user has disabled updates
  87. */
  88. public function testCheckUpdateUserDisabled()
  89. {
  90. $this->assertFalse(
  91. ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true)
  92. );
  93. }
  94. /**
  95. * A newer version is available
  96. */
  97. public function testCheckUpdateNewVersionAvailable()
  98. {
  99. $newVersion = '1.8.3';
  100. FakeApplicationUtils::$VERSION_CODE = $newVersion;
  101. $version = FakeApplicationUtils::checkUpdate(
  102. self::$testVersion,
  103. self::$testUpdateFile,
  104. 100,
  105. true,
  106. true
  107. );
  108. $this->assertEquals($newVersion, $version);
  109. }
  110. /**
  111. * No available information about versions
  112. */
  113. public function testCheckUpdateNewVersionUnavailable()
  114. {
  115. $version = FakeApplicationUtils::checkUpdate(
  116. self::$testVersion,
  117. self::$testUpdateFile,
  118. 100,
  119. true,
  120. true
  121. );
  122. $this->assertFalse($version);
  123. }
  124. /**
  125. * Test update checks - invalid Git branch
  126. * @expectedException Exception
  127. * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
  128. */
  129. public function testCheckUpdateInvalidGitBranch()
  130. {
  131. ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
  132. }
  133. /**
  134. * Shaarli is up-to-date
  135. */
  136. public function testCheckUpdateNewVersionUpToDate()
  137. {
  138. FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
  139. $version = FakeApplicationUtils::checkUpdate(
  140. self::$testVersion,
  141. self::$testUpdateFile,
  142. 100,
  143. true,
  144. true
  145. );
  146. $this->assertFalse($version);
  147. }
  148. /**
  149. * Time-traveller's Shaarli
  150. */
  151. public function testCheckUpdateNewVersionMaartiMcFly()
  152. {
  153. FakeApplicationUtils::$VERSION_CODE = '0.4.1';
  154. $version = FakeApplicationUtils::checkUpdate(
  155. self::$testVersion,
  156. self::$testUpdateFile,
  157. 100,
  158. true,
  159. true
  160. );
  161. $this->assertFalse($version);
  162. }
  163. /**
  164. * The version has been checked recently and Shaarli is up-to-date
  165. */
  166. public function testCheckUpdateNewVersionTwiceUpToDate()
  167. {
  168. FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
  169. // Create the update file
  170. $version = FakeApplicationUtils::checkUpdate(
  171. self::$testVersion,
  172. self::$testUpdateFile,
  173. 100,
  174. true,
  175. true
  176. );
  177. $this->assertFalse($version);
  178. // Reuse the update file
  179. $version = FakeApplicationUtils::checkUpdate(
  180. self::$testVersion,
  181. self::$testUpdateFile,
  182. 100,
  183. true,
  184. true
  185. );
  186. $this->assertFalse($version);
  187. }
  188. /**
  189. * The version has been checked recently and Shaarli is outdated
  190. */
  191. public function testCheckUpdateNewVersionTwiceOutdated()
  192. {
  193. $newVersion = '1.8.3';
  194. FakeApplicationUtils::$VERSION_CODE = $newVersion;
  195. // Create the update file
  196. $version = FakeApplicationUtils::checkUpdate(
  197. self::$testVersion,
  198. self::$testUpdateFile,
  199. 100,
  200. true,
  201. true
  202. );
  203. $this->assertEquals($newVersion, $version);
  204. // Reuse the update file
  205. $version = FakeApplicationUtils::checkUpdate(
  206. self::$testVersion,
  207. self::$testUpdateFile,
  208. 100,
  209. true,
  210. true
  211. );
  212. $this->assertEquals($newVersion, $version);
  213. }
  214. /**
  215. * Check supported PHP versions
  216. */
  217. public function testCheckSupportedPHPVersion()
  218. {
  219. $minVersion = '5.3';
  220. ApplicationUtils::checkPHPVersion($minVersion, '5.4.32');
  221. ApplicationUtils::checkPHPVersion($minVersion, '5.5');
  222. ApplicationUtils::checkPHPVersion($minVersion, '5.6.10');
  223. }
  224. /**
  225. * Check a unsupported PHP version
  226. * @expectedException Exception
  227. * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
  228. */
  229. public function testCheckSupportedPHPVersion51()
  230. {
  231. ApplicationUtils::checkPHPVersion('5.3', '5.1.0');
  232. }
  233. /**
  234. * Check another unsupported PHP version
  235. * @expectedException Exception
  236. * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
  237. */
  238. public function testCheckSupportedPHPVersion52()
  239. {
  240. ApplicationUtils::checkPHPVersion('5.3', '5.2');
  241. }
  242. /**
  243. * Checks resource permissions for the current Shaarli installation
  244. */
  245. public function testCheckCurrentResourcePermissions()
  246. {
  247. $conf = new ConfigManager('');
  248. $conf->set('resource.thumbnails_cache', 'cache');
  249. $conf->set('resource.config', 'data/config.php');
  250. $conf->set('resource.data_dir', 'data');
  251. $conf->set('resource.datastore', 'data/datastore.php');
  252. $conf->set('resource.ban_file', 'data/ipbans.php');
  253. $conf->set('resource.log', 'data/log.txt');
  254. $conf->set('resource.page_cache', 'pagecache');
  255. $conf->set('resource.raintpl_tmp', 'tmp');
  256. $conf->set('resource.raintpl_tpl', 'tpl');
  257. $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
  258. $this->assertEquals(
  259. array(),
  260. ApplicationUtils::checkResourcePermissions($conf)
  261. );
  262. }
  263. /**
  264. * Checks resource permissions for a non-existent Shaarli installation
  265. */
  266. public function testCheckCurrentResourcePermissionsErrors()
  267. {
  268. $conf = new ConfigManager('');
  269. $conf->set('resource.thumbnails_cache', 'null/cache');
  270. $conf->set('resource.config', 'null/data/config.php');
  271. $conf->set('resource.data_dir', 'null/data');
  272. $conf->set('resource.datastore', 'null/data/store.php');
  273. $conf->set('resource.ban_file', 'null/data/ipbans.php');
  274. $conf->set('resource.log', 'null/data/log.txt');
  275. $conf->set('resource.page_cache', 'null/pagecache');
  276. $conf->set('resource.raintpl_tmp', 'null/tmp');
  277. $conf->set('resource.raintpl_tpl', 'null/tpl');
  278. $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
  279. $this->assertEquals(
  280. array(
  281. '"null/tpl" directory is not readable',
  282. '"null/cache" directory is not readable',
  283. '"null/cache" directory is not writable',
  284. '"null/data" directory is not readable',
  285. '"null/data" directory is not writable',
  286. '"null/pagecache" directory is not readable',
  287. '"null/pagecache" directory is not writable',
  288. '"null/tmp" directory is not readable',
  289. '"null/tmp" directory is not writable'
  290. ),
  291. ApplicationUtils::checkResourcePermissions($conf)
  292. );
  293. }
  294. }