2 use Shaarli\Config\ConfigManager
;
5 * ApplicationUtils' tests
8 require_once 'application/ApplicationUtils.php';
11 * Fake ApplicationUtils class to avoid HTTP requests
13 class FakeApplicationUtils
extends ApplicationUtils
15 public static $VERSION_CODE = '';
18 * Toggle HTTP requests, allow overriding the version code
20 public static function getVersion($url, $timeout=0)
22 return self
::$VERSION_CODE;
28 * Unitary tests for Shaarli utilities
30 class ApplicationUtilsTest
extends PHPUnit_Framework_TestCase
32 protected static $testUpdateFile = 'sandbox/update.txt';
33 protected static $testVersion = '0.5.0';
34 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
37 * Reset test data for each test
39 public function setUp()
41 FakeApplicationUtils
::$VERSION_CODE = '';
42 if (file_exists(self
::$testUpdateFile)) {
43 unlink(self
::$testUpdateFile);
48 * Remove test version file if it exists
50 public function tearDown()
52 if (is_file('sandbox/version.php')) {
53 unlink('sandbox/version.php');
58 * Retrieve the latest version code available on Git
60 * Expected format: Semantic Versioning - major.minor.patch
62 public function testGetVersionCode()
68 ApplicationUtils
::getVersion(
69 'https://raw.githubusercontent.com/shaarli/Shaarli/'
70 .'v0.5.4/shaarli_version.php',
75 self
::$versionPattern,
76 ApplicationUtils
::getVersion(
77 'https://raw.githubusercontent.com/shaarli/Shaarli/'
78 .'latest/shaarli_version.php',
85 * Attempt to retrieve the latest version from an invalid File
87 public function testGetVersionCodeFromFile()
89 file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL
);
92 ApplicationUtils
::getVersion('sandbox/version.php', 1)
97 * Attempt to retrieve the latest version from an invalid File
99 public function testGetVersionCodeInvalidFile()
101 $oldlog = ini_get('error_log');
102 ini_set('error_log', '/dev/null');
104 ApplicationUtils
::getVersion('idontexist', 1)
106 ini_set('error_log', $oldlog);
110 * Test update checks - the user is logged off
112 public function testCheckUpdateLoggedOff()
115 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
120 * Test update checks - the user has disabled updates
122 public function testCheckUpdateUserDisabled()
125 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
130 * A newer version is available
132 public function testCheckUpdateNewVersionAvailable()
134 $newVersion = '1.8.3';
135 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
137 $version = FakeApplicationUtils
::checkUpdate(
139 self
::$testUpdateFile,
145 $this->assertEquals($newVersion, $version);
149 * No available information about versions
151 public function testCheckUpdateNewVersionUnavailable()
153 $version = FakeApplicationUtils
::checkUpdate(
155 self
::$testUpdateFile,
161 $this->assertFalse($version);
165 * Test update checks - invalid Git branch
166 * @expectedException Exception
167 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
169 public function testCheckUpdateInvalidGitBranch()
171 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
175 * Shaarli is up-to-date
177 public function testCheckUpdateNewVersionUpToDate()
179 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
181 $version = FakeApplicationUtils
::checkUpdate(
183 self
::$testUpdateFile,
189 $this->assertFalse($version);
193 * Time-traveller's Shaarli
195 public function testCheckUpdateNewVersionMaartiMcFly()
197 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
199 $version = FakeApplicationUtils
::checkUpdate(
201 self
::$testUpdateFile,
207 $this->assertFalse($version);
211 * The version has been checked recently and Shaarli is up-to-date
213 public function testCheckUpdateNewVersionTwiceUpToDate()
215 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
217 // Create the update file
218 $version = FakeApplicationUtils
::checkUpdate(
220 self
::$testUpdateFile,
226 $this->assertFalse($version);
228 // Reuse the update file
229 $version = FakeApplicationUtils
::checkUpdate(
231 self
::$testUpdateFile,
237 $this->assertFalse($version);
241 * The version has been checked recently and Shaarli is outdated
243 public function testCheckUpdateNewVersionTwiceOutdated()
245 $newVersion = '1.8.3';
246 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
248 // Create the update file
249 $version = FakeApplicationUtils
::checkUpdate(
251 self
::$testUpdateFile,
256 $this->assertEquals($newVersion, $version);
258 // Reuse the update file
259 $version = FakeApplicationUtils
::checkUpdate(
261 self
::$testUpdateFile,
266 $this->assertEquals($newVersion, $version);
270 * Check supported PHP versions
272 public function testCheckSupportedPHPVersion()
275 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
276 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
277 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
281 * Check a unsupported PHP version
282 * @expectedException Exception
283 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
285 public function testCheckSupportedPHPVersion51()
287 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
291 * Check another unsupported PHP version
292 * @expectedException Exception
293 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
295 public function testCheckSupportedPHPVersion52()
297 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
301 * Checks resource permissions for the current Shaarli installation
303 public function testCheckCurrentResourcePermissions()
305 $conf = new ConfigManager('');
306 $conf->set('resource.thumbnails_cache', 'cache');
307 $conf->set('resource.config', 'data/config.php');
308 $conf->set('resource.data_dir', 'data');
309 $conf->set('resource.datastore', 'data/datastore.php');
310 $conf->set('resource.ban_file', 'data/ipbans.php');
311 $conf->set('resource.log', 'data/log.txt');
312 $conf->set('resource.page_cache', 'pagecache');
313 $conf->set('resource.raintpl_tmp', 'tmp');
314 $conf->set('resource.raintpl_tpl', 'tpl');
315 $conf->set('resource.theme', 'default');
316 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
320 ApplicationUtils
::checkResourcePermissions($conf)
325 * Checks resource permissions for a non-existent Shaarli installation
327 public function testCheckCurrentResourcePermissionsErrors()
329 $conf = new ConfigManager('');
330 $conf->set('resource.thumbnails_cache', 'null/cache');
331 $conf->set('resource.config', 'null/data/config.php');
332 $conf->set('resource.data_dir', 'null/data');
333 $conf->set('resource.datastore', 'null/data/store.php');
334 $conf->set('resource.ban_file', 'null/data/ipbans.php');
335 $conf->set('resource.log', 'null/data/log.txt');
336 $conf->set('resource.page_cache', 'null/pagecache');
337 $conf->set('resource.raintpl_tmp', 'null/tmp');
338 $conf->set('resource.raintpl_tpl', 'null/tpl');
339 $conf->set('resource.raintpl_theme', 'null/tpl/default');
340 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
343 '"null/tpl" directory is not readable',
344 '"null/tpl/default" directory is not readable',
345 '"null/cache" directory is not readable',
346 '"null/cache" directory is not writable',
347 '"null/data" directory is not readable',
348 '"null/data" directory is not writable',
349 '"null/pagecache" directory is not readable',
350 '"null/pagecache" directory is not writable',
351 '"null/tmp" directory is not readable',
352 '"null/tmp" directory is not writable'
354 ApplicationUtils
::checkResourcePermissions($conf)
359 * Check update with 'dev' as curent version (master branch).
360 * It should always return false.
362 public function testCheckUpdateDev()
365 ApplicationUtils
::checkUpdate('dev', self
::$testUpdateFile, 100, true, true)