ApplicationUtilsTest.php 8.9 KB

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