3 * ApplicationUtils' tests
6 require_once 'application/ApplicationUtils.php';
9 * Fake ApplicationUtils class to avoid HTTP requests
11 class FakeApplicationUtils
extends ApplicationUtils
13 public static $VERSION_CODE = '';
16 * Toggle HTTP requests, allow overriding the version code
18 public static function getLatestGitVersionCode($url, $timeout=0)
20 return self
::$VERSION_CODE;
26 * Unitary tests for Shaarli utilities
28 class ApplicationUtilsTest
extends PHPUnit_Framework_TestCase
30 protected static $testUpdateFile = 'sandbox/update.txt';
31 protected static $testVersion = '0.5.0';
32 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
35 * Reset test data for each test
37 public function setUp()
39 FakeApplicationUtils
::$VERSION_CODE = '';
40 if (file_exists(self
::$testUpdateFile)) {
41 unlink(self
::$testUpdateFile);
46 * Retrieve the latest version code available on Git
48 * Expected format: Semantic Versioning - major.minor.patch
50 public function testGetLatestGitVersionCode()
56 ApplicationUtils
::getLatestGitVersionCode(
57 'https://raw.githubusercontent.com/shaarli/Shaarli/'
58 .'v0.5.4/shaarli_version.php',
63 self
::$versionPattern,
64 ApplicationUtils
::getLatestGitVersionCode(
65 'https://raw.githubusercontent.com/shaarli/Shaarli/'
66 .'master/shaarli_version.php',
73 * Attempt to retrieve the latest version from an invalid URL
75 public function testGetLatestGitVersionCodeInvalidUrl()
78 ApplicationUtils
::getLatestGitVersionCode('htttp://null.io', 1)
83 * Test update checks - the user is logged off
85 public function testCheckUpdateLoggedOff()
88 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
93 * Test update checks - the user has disabled updates
95 public function testCheckUpdateUserDisabled()
98 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
103 * A newer version is available
105 public function testCheckUpdateNewVersionAvailable()
107 $newVersion = '1.8.3';
108 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
110 $version = FakeApplicationUtils
::checkUpdate(
112 self
::$testUpdateFile,
118 $this->assertEquals($newVersion, $version);
122 * No available information about versions
124 public function testCheckUpdateNewVersionUnavailable()
126 $version = FakeApplicationUtils
::checkUpdate(
128 self
::$testUpdateFile,
134 $this->assertFalse($version);
138 * Test update checks - invalid Git branch
139 * @expectedException Exception
140 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
142 public function testCheckUpdateInvalidGitBranch()
144 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
148 * Shaarli is up-to-date
150 public function testCheckUpdateNewVersionUpToDate()
152 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
154 $version = FakeApplicationUtils
::checkUpdate(
156 self
::$testUpdateFile,
162 $this->assertFalse($version);
166 * Time-traveller's Shaarli
168 public function testCheckUpdateNewVersionMaartiMcFly()
170 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
172 $version = FakeApplicationUtils
::checkUpdate(
174 self
::$testUpdateFile,
180 $this->assertFalse($version);
184 * The version has been checked recently and Shaarli is up-to-date
186 public function testCheckUpdateNewVersionTwiceUpToDate()
188 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
190 // Create the update file
191 $version = FakeApplicationUtils
::checkUpdate(
193 self
::$testUpdateFile,
199 $this->assertFalse($version);
201 // Reuse the update file
202 $version = FakeApplicationUtils
::checkUpdate(
204 self
::$testUpdateFile,
210 $this->assertFalse($version);
214 * The version has been checked recently and Shaarli is outdated
216 public function testCheckUpdateNewVersionTwiceOutdated()
218 $newVersion = '1.8.3';
219 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
221 // Create the update file
222 $version = FakeApplicationUtils
::checkUpdate(
224 self
::$testUpdateFile,
229 $this->assertEquals($newVersion, $version);
231 // Reuse the update file
232 $version = FakeApplicationUtils
::checkUpdate(
234 self
::$testUpdateFile,
239 $this->assertEquals($newVersion, $version);
243 * Check supported PHP versions
245 public function testCheckSupportedPHPVersion()
248 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
249 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
250 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
254 * Check a unsupported PHP version
255 * @expectedException Exception
256 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
258 public function testCheckSupportedPHPVersion51()
260 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
264 * Check another unsupported PHP version
265 * @expectedException Exception
266 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
268 public function testCheckSupportedPHPVersion52()
270 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
274 * Checks resource permissions for the current Shaarli installation
276 public function testCheckCurrentResourcePermissions()
279 'CACHEDIR' => 'cache',
280 'CONFIG_FILE' => 'data/config.php',
282 'DATASTORE' => 'data/datastore.php',
283 'IPBANS_FILENAME' => 'data/ipbans.php',
284 'LOG_FILE' => 'data/log.txt',
285 'PAGECACHE' => 'pagecache',
286 'RAINTPL_TMP' => 'tmp',
287 'RAINTPL_TPL' => 'tpl',
288 'UPDATECHECK_FILENAME' => 'data/lastupdatecheck.txt'
292 ApplicationUtils
::checkResourcePermissions($config)
297 * Checks resource permissions for a non-existent Shaarli installation
299 public function testCheckCurrentResourcePermissionsErrors()
302 'CACHEDIR' => 'null/cache',
303 'CONFIG_FILE' => 'null/data/config.php',
304 'DATADIR' => 'null/data',
305 'DATASTORE' => 'null/data/store.php',
306 'IPBANS_FILENAME' => 'null/data/ipbans.php',
307 'LOG_FILE' => 'null/data/log.txt',
308 'PAGECACHE' => 'null/pagecache',
309 'RAINTPL_TMP' => 'null/tmp',
310 'RAINTPL_TPL' => 'null/tpl',
311 'UPDATECHECK_FILENAME' => 'null/data/lastupdatecheck.txt'
315 '"null/tpl" directory is not readable',
316 '"null/cache" directory is not readable',
317 '"null/cache" directory is not writable',
318 '"null/data" directory is not readable',
319 '"null/data" directory is not writable',
320 '"null/pagecache" directory is not readable',
321 '"null/pagecache" directory is not writable',
322 '"null/tmp" directory is not readable',
323 '"null/tmp" directory is not writable'
325 ApplicationUtils
::checkResourcePermissions($config)