3 * ApplicationUtils' tests
6 require_once 'application/config/ConfigManager.php';
7 require_once 'application/ApplicationUtils.php';
10 * Fake ApplicationUtils class to avoid HTTP requests
12 class FakeApplicationUtils
extends ApplicationUtils
14 public static $VERSION_CODE = '';
17 * Toggle HTTP requests, allow overriding the version code
19 public static function getLatestGitVersionCode($url, $timeout=0)
21 return self
::$VERSION_CODE;
27 * Unitary tests for Shaarli utilities
29 class ApplicationUtilsTest
extends PHPUnit_Framework_TestCase
31 protected static $testUpdateFile = 'sandbox/update.txt';
32 protected static $testVersion = '0.5.0';
33 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
36 * Reset test data for each test
38 public function setUp()
40 FakeApplicationUtils
::$VERSION_CODE = '';
41 if (file_exists(self
::$testUpdateFile)) {
42 unlink(self
::$testUpdateFile);
47 * Retrieve the latest version code available on Git
49 * Expected format: Semantic Versioning - major.minor.patch
51 public function testGetLatestGitVersionCode()
57 ApplicationUtils
::getLatestGitVersionCode(
58 'https://raw.githubusercontent.com/shaarli/Shaarli/'
59 .'v0.5.4/shaarli_version.php',
64 self
::$versionPattern,
65 ApplicationUtils
::getLatestGitVersionCode(
66 'https://raw.githubusercontent.com/shaarli/Shaarli/'
67 .'master/shaarli_version.php',
74 * Attempt to retrieve the latest version from an invalid URL
76 public function testGetLatestGitVersionCodeInvalidUrl()
79 ApplicationUtils
::getLatestGitVersionCode('htttp://null.io', 1)
84 * Test update checks - the user is logged off
86 public function testCheckUpdateLoggedOff()
89 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
94 * Test update checks - the user has disabled updates
96 public function testCheckUpdateUserDisabled()
99 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
104 * A newer version is available
106 public function testCheckUpdateNewVersionAvailable()
108 $newVersion = '1.8.3';
109 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
111 $version = FakeApplicationUtils
::checkUpdate(
113 self
::$testUpdateFile,
119 $this->assertEquals($newVersion, $version);
123 * No available information about versions
125 public function testCheckUpdateNewVersionUnavailable()
127 $version = FakeApplicationUtils
::checkUpdate(
129 self
::$testUpdateFile,
135 $this->assertFalse($version);
139 * Test update checks - invalid Git branch
140 * @expectedException Exception
141 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
143 public function testCheckUpdateInvalidGitBranch()
145 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
149 * Shaarli is up-to-date
151 public function testCheckUpdateNewVersionUpToDate()
153 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
155 $version = FakeApplicationUtils
::checkUpdate(
157 self
::$testUpdateFile,
163 $this->assertFalse($version);
167 * Time-traveller's Shaarli
169 public function testCheckUpdateNewVersionMaartiMcFly()
171 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
173 $version = FakeApplicationUtils
::checkUpdate(
175 self
::$testUpdateFile,
181 $this->assertFalse($version);
185 * The version has been checked recently and Shaarli is up-to-date
187 public function testCheckUpdateNewVersionTwiceUpToDate()
189 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
191 // Create the update file
192 $version = FakeApplicationUtils
::checkUpdate(
194 self
::$testUpdateFile,
200 $this->assertFalse($version);
202 // Reuse the update file
203 $version = FakeApplicationUtils
::checkUpdate(
205 self
::$testUpdateFile,
211 $this->assertFalse($version);
215 * The version has been checked recently and Shaarli is outdated
217 public function testCheckUpdateNewVersionTwiceOutdated()
219 $newVersion = '1.8.3';
220 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
222 // Create the update file
223 $version = FakeApplicationUtils
::checkUpdate(
225 self
::$testUpdateFile,
230 $this->assertEquals($newVersion, $version);
232 // Reuse the update file
233 $version = FakeApplicationUtils
::checkUpdate(
235 self
::$testUpdateFile,
240 $this->assertEquals($newVersion, $version);
244 * Check supported PHP versions
246 public function testCheckSupportedPHPVersion()
249 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
250 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
251 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
255 * Check a unsupported PHP version
256 * @expectedException Exception
257 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
259 public function testCheckSupportedPHPVersion51()
261 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
265 * Check another unsupported PHP version
266 * @expectedException Exception
267 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
269 public function testCheckSupportedPHPVersion52()
271 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
275 * Checks resource permissions for the current Shaarli installation
277 public function testCheckCurrentResourcePermissions()
279 $conf = new ConfigManager('');
280 $conf->set('resource.thumbnails_cache', 'cache');
281 $conf->set('resource.config', 'data/config.php');
282 $conf->set('resource.data_dir', 'data');
283 $conf->set('resource.datastore', 'data/datastore.php');
284 $conf->set('resource.ban_file', 'data/ipbans.php');
285 $conf->set('resource.log', 'data/log.txt');
286 $conf->set('resource.page_cache', 'pagecache');
287 $conf->set('resource.raintpl_tmp', 'tmp');
288 $conf->set('resource.raintpl_tpl', 'tpl');
289 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
293 ApplicationUtils
::checkResourcePermissions($conf)
298 * Checks resource permissions for a non-existent Shaarli installation
300 public function testCheckCurrentResourcePermissionsErrors()
302 $conf = new ConfigManager('');
303 $conf->set('resource.thumbnails_cache', 'null/cache');
304 $conf->set('resource.config', 'null/data/config.php');
305 $conf->set('resource.data_dir', 'null/data');
306 $conf->set('resource.datastore', 'null/data/store.php');
307 $conf->set('resource.ban_file', 'null/data/ipbans.php');
308 $conf->set('resource.log', 'null/data/log.txt');
309 $conf->set('resource.page_cache', 'null/pagecache');
310 $conf->set('resource.raintpl_tmp', 'null/tmp');
311 $conf->set('resource.raintpl_tpl', 'null/tpl');
312 $conf->set('resource.update_check', '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($conf)