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()
78 $oldlog = ini_get('error_log');
79 ini_set('error_log', '/dev/null');
81 ApplicationUtils
::getLatestGitVersionCode('htttp://null.io', 1)
83 ini_set('error_log', $oldlog);
87 * Test update checks - the user is logged off
89 public function testCheckUpdateLoggedOff()
92 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
97 * Test update checks - the user has disabled updates
99 public function testCheckUpdateUserDisabled()
102 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
107 * A newer version is available
109 public function testCheckUpdateNewVersionAvailable()
111 $newVersion = '1.8.3';
112 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
114 $version = FakeApplicationUtils
::checkUpdate(
116 self
::$testUpdateFile,
122 $this->assertEquals($newVersion, $version);
126 * No available information about versions
128 public function testCheckUpdateNewVersionUnavailable()
130 $version = FakeApplicationUtils
::checkUpdate(
132 self
::$testUpdateFile,
138 $this->assertFalse($version);
142 * Test update checks - invalid Git branch
143 * @expectedException Exception
144 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
146 public function testCheckUpdateInvalidGitBranch()
148 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
152 * Shaarli is up-to-date
154 public function testCheckUpdateNewVersionUpToDate()
156 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
158 $version = FakeApplicationUtils
::checkUpdate(
160 self
::$testUpdateFile,
166 $this->assertFalse($version);
170 * Time-traveller's Shaarli
172 public function testCheckUpdateNewVersionMaartiMcFly()
174 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
176 $version = FakeApplicationUtils
::checkUpdate(
178 self
::$testUpdateFile,
184 $this->assertFalse($version);
188 * The version has been checked recently and Shaarli is up-to-date
190 public function testCheckUpdateNewVersionTwiceUpToDate()
192 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
194 // Create the update file
195 $version = FakeApplicationUtils
::checkUpdate(
197 self
::$testUpdateFile,
203 $this->assertFalse($version);
205 // Reuse the update file
206 $version = FakeApplicationUtils
::checkUpdate(
208 self
::$testUpdateFile,
214 $this->assertFalse($version);
218 * The version has been checked recently and Shaarli is outdated
220 public function testCheckUpdateNewVersionTwiceOutdated()
222 $newVersion = '1.8.3';
223 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
225 // Create the update file
226 $version = FakeApplicationUtils
::checkUpdate(
228 self
::$testUpdateFile,
233 $this->assertEquals($newVersion, $version);
235 // Reuse the update file
236 $version = FakeApplicationUtils
::checkUpdate(
238 self
::$testUpdateFile,
243 $this->assertEquals($newVersion, $version);
247 * Check supported PHP versions
249 public function testCheckSupportedPHPVersion()
252 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
253 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
254 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
258 * Check a unsupported PHP version
259 * @expectedException Exception
260 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
262 public function testCheckSupportedPHPVersion51()
264 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
268 * Check another unsupported PHP version
269 * @expectedException Exception
270 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
272 public function testCheckSupportedPHPVersion52()
274 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
278 * Checks resource permissions for the current Shaarli installation
280 public function testCheckCurrentResourcePermissions()
282 $conf = new ConfigManager('');
283 $conf->set('resource.thumbnails_cache', 'cache');
284 $conf->set('resource.config', 'data/config.php');
285 $conf->set('resource.data_dir', 'data');
286 $conf->set('resource.datastore', 'data/datastore.php');
287 $conf->set('resource.ban_file', 'data/ipbans.php');
288 $conf->set('resource.log', 'data/log.txt');
289 $conf->set('resource.page_cache', 'pagecache');
290 $conf->set('resource.raintpl_tmp', 'tmp');
291 $conf->set('resource.raintpl_tpl', 'tpl');
292 $conf->set('resource.theme', 'default');
293 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
297 ApplicationUtils
::checkResourcePermissions($conf)
302 * Checks resource permissions for a non-existent Shaarli installation
304 public function testCheckCurrentResourcePermissionsErrors()
306 $conf = new ConfigManager('');
307 $conf->set('resource.thumbnails_cache', 'null/cache');
308 $conf->set('resource.config', 'null/data/config.php');
309 $conf->set('resource.data_dir', 'null/data');
310 $conf->set('resource.datastore', 'null/data/store.php');
311 $conf->set('resource.ban_file', 'null/data/ipbans.php');
312 $conf->set('resource.log', 'null/data/log.txt');
313 $conf->set('resource.page_cache', 'null/pagecache');
314 $conf->set('resource.raintpl_tmp', 'null/tmp');
315 $conf->set('resource.raintpl_tpl', 'null/tpl');
316 $conf->set('resource.raintpl_theme', 'null/tpl/default');
317 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
320 '"null/tpl" directory is not readable',
321 '"null/tpl/default" directory is not readable',
322 '"null/cache" directory is not readable',
323 '"null/cache" directory is not writable',
324 '"null/data" directory is not readable',
325 '"null/data" directory is not writable',
326 '"null/pagecache" directory is not readable',
327 '"null/pagecache" directory is not writable',
328 '"null/tmp" directory is not readable',
329 '"null/tmp" directory is not writable'
331 ApplicationUtils
::checkResourcePermissions($conf)