4 use Shaarli\Config\ConfigManager
;
6 require_once 'tests/utils/FakeApplicationUtils.php';
9 * Unitary tests for Shaarli utilities
11 class ApplicationUtilsTest
extends \PHPUnit\Framework\TestCase
13 protected static $testUpdateFile = 'sandbox/update.txt';
14 protected static $testVersion = '0.5.0';
15 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
18 * Reset test data for each test
20 public function setUp()
22 FakeApplicationUtils
::$VERSION_CODE = '';
23 if (file_exists(self
::$testUpdateFile)) {
24 unlink(self
::$testUpdateFile);
29 * Remove test version file if it exists
31 public function tearDown()
33 if (is_file('sandbox/version.php')) {
34 unlink('sandbox/version.php');
39 * Retrieve the latest version code available on Git
41 * Expected format: Semantic Versioning - major.minor.patch
43 public function testGetVersionCode()
49 ApplicationUtils
::getVersion(
50 'https://raw.githubusercontent.com/shaarli/Shaarli/'
51 .'v0.5.4/shaarli_version.php',
56 self
::$versionPattern,
57 ApplicationUtils
::getVersion(
58 'https://raw.githubusercontent.com/shaarli/Shaarli/'
59 .'latest/shaarli_version.php',
66 * Attempt to retrieve the latest version from an invalid File
68 public function testGetVersionCodeFromFile()
70 file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL
);
73 ApplicationUtils
::getVersion('sandbox/version.php', 1)
78 * Attempt to retrieve the latest version from an invalid File
80 public function testGetVersionCodeInvalidFile()
82 $oldlog = ini_get('error_log');
83 ini_set('error_log', '/dev/null');
85 ApplicationUtils
::getVersion('idontexist', 1)
87 ini_set('error_log', $oldlog);
91 * Test update checks - the user is logged off
93 public function testCheckUpdateLoggedOff()
96 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
101 * Test update checks - the user has disabled updates
103 public function testCheckUpdateUserDisabled()
106 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
111 * A newer version is available
113 public function testCheckUpdateNewVersionAvailable()
115 $newVersion = '1.8.3';
116 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
118 $version = FakeApplicationUtils
::checkUpdate(
120 self
::$testUpdateFile,
126 $this->assertEquals($newVersion, $version);
130 * No available information about versions
132 public function testCheckUpdateNewVersionUnavailable()
134 $version = FakeApplicationUtils
::checkUpdate(
136 self
::$testUpdateFile,
142 $this->assertFalse($version);
146 * Test update checks - invalid Git branch
147 * @expectedException Exception
148 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
150 public function testCheckUpdateInvalidGitBranch()
152 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
156 * Shaarli is up-to-date
158 public function testCheckUpdateNewVersionUpToDate()
160 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
162 $version = FakeApplicationUtils
::checkUpdate(
164 self
::$testUpdateFile,
170 $this->assertFalse($version);
174 * Time-traveller's Shaarli
176 public function testCheckUpdateNewVersionMaartiMcFly()
178 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
180 $version = FakeApplicationUtils
::checkUpdate(
182 self
::$testUpdateFile,
188 $this->assertFalse($version);
192 * The version has been checked recently and Shaarli is up-to-date
194 public function testCheckUpdateNewVersionTwiceUpToDate()
196 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
198 // Create the update file
199 $version = FakeApplicationUtils
::checkUpdate(
201 self
::$testUpdateFile,
207 $this->assertFalse($version);
209 // Reuse the update file
210 $version = FakeApplicationUtils
::checkUpdate(
212 self
::$testUpdateFile,
218 $this->assertFalse($version);
222 * The version has been checked recently and Shaarli is outdated
224 public function testCheckUpdateNewVersionTwiceOutdated()
226 $newVersion = '1.8.3';
227 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
229 // Create the update file
230 $version = FakeApplicationUtils
::checkUpdate(
232 self
::$testUpdateFile,
237 $this->assertEquals($newVersion, $version);
239 // Reuse the update file
240 $version = FakeApplicationUtils
::checkUpdate(
242 self
::$testUpdateFile,
247 $this->assertEquals($newVersion, $version);
251 * Check supported PHP versions
253 public function testCheckSupportedPHPVersion()
256 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
257 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
258 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
262 * Check a unsupported PHP version
263 * @expectedException Exception
264 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
266 public function testCheckSupportedPHPVersion51()
268 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
272 * Check another unsupported PHP version
273 * @expectedException Exception
274 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
276 public function testCheckSupportedPHPVersion52()
278 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
282 * Checks resource permissions for the current Shaarli installation
284 public function testCheckCurrentResourcePermissions()
286 $conf = new ConfigManager('');
287 $conf->set('resource.thumbnails_cache', 'cache');
288 $conf->set('resource.config', 'data/config.php');
289 $conf->set('resource.data_dir', 'data');
290 $conf->set('resource.datastore', 'data/datastore.php');
291 $conf->set('resource.ban_file', 'data/ipbans.php');
292 $conf->set('resource.log', 'data/log.txt');
293 $conf->set('resource.page_cache', 'pagecache');
294 $conf->set('resource.raintpl_tmp', 'tmp');
295 $conf->set('resource.raintpl_tpl', 'tpl');
296 $conf->set('resource.theme', 'default');
297 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
301 ApplicationUtils
::checkResourcePermissions($conf)
306 * Checks resource permissions for a non-existent Shaarli installation
308 public function testCheckCurrentResourcePermissionsErrors()
310 $conf = new ConfigManager('');
311 $conf->set('resource.thumbnails_cache', 'null/cache');
312 $conf->set('resource.config', 'null/data/config.php');
313 $conf->set('resource.data_dir', 'null/data');
314 $conf->set('resource.datastore', 'null/data/store.php');
315 $conf->set('resource.ban_file', 'null/data/ipbans.php');
316 $conf->set('resource.log', 'null/data/log.txt');
317 $conf->set('resource.page_cache', 'null/pagecache');
318 $conf->set('resource.raintpl_tmp', 'null/tmp');
319 $conf->set('resource.raintpl_tpl', 'null/tpl');
320 $conf->set('resource.raintpl_theme', 'null/tpl/default');
321 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
324 '"null/tpl" directory is not readable',
325 '"null/tpl/default" directory is not readable',
326 '"null/cache" directory is not readable',
327 '"null/cache" directory is not writable',
328 '"null/data" directory is not readable',
329 '"null/data" directory is not writable',
330 '"null/pagecache" directory is not readable',
331 '"null/pagecache" directory is not writable',
332 '"null/tmp" directory is not readable',
333 '"null/tmp" directory is not writable'
335 ApplicationUtils
::checkResourcePermissions($conf)
340 * Check update with 'dev' as curent version (master branch).
341 * It should always return false.
343 public function testCheckUpdateDev()
346 ApplicationUtils
::checkUpdate('dev', self
::$testUpdateFile, 100, true, true)