4 use Shaarli\Config\ConfigManager
;
6 require_once 'tests/utils/FakeApplicationUtils.php';
9 * Unitary tests for Shaarli utilities
11 class ApplicationUtilsTest
extends \Shaarli\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 protected function setUp(): void
22 FakeApplicationUtils
::$VERSION_CODE = '';
23 if (file_exists(self
::$testUpdateFile)) {
24 unlink(self
::$testUpdateFile);
29 * Remove test version file if it exists
31 protected function tearDown(): void
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
148 public function testCheckUpdateInvalidGitBranch()
150 $this->expectException(\Exception
::class);
151 $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/');
153 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
157 * Shaarli is up-to-date
159 public function testCheckUpdateNewVersionUpToDate()
161 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
163 $version = FakeApplicationUtils
::checkUpdate(
165 self
::$testUpdateFile,
171 $this->assertFalse($version);
175 * Time-traveller's Shaarli
177 public function testCheckUpdateNewVersionMaartiMcFly()
179 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
181 $version = FakeApplicationUtils
::checkUpdate(
183 self
::$testUpdateFile,
189 $this->assertFalse($version);
193 * The version has been checked recently and Shaarli is up-to-date
195 public function testCheckUpdateNewVersionTwiceUpToDate()
197 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
199 // Create the update file
200 $version = FakeApplicationUtils
::checkUpdate(
202 self
::$testUpdateFile,
208 $this->assertFalse($version);
210 // Reuse the update file
211 $version = FakeApplicationUtils
::checkUpdate(
213 self
::$testUpdateFile,
219 $this->assertFalse($version);
223 * The version has been checked recently and Shaarli is outdated
225 public function testCheckUpdateNewVersionTwiceOutdated()
227 $newVersion = '1.8.3';
228 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
230 // Create the update file
231 $version = FakeApplicationUtils
::checkUpdate(
233 self
::$testUpdateFile,
238 $this->assertEquals($newVersion, $version);
240 // Reuse the update file
241 $version = FakeApplicationUtils
::checkUpdate(
243 self
::$testUpdateFile,
248 $this->assertEquals($newVersion, $version);
252 * Check supported PHP versions
254 public function testCheckSupportedPHPVersion()
257 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32'));
258 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.5'));
259 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10'));
263 * Check a unsupported PHP version
265 public function testCheckSupportedPHPVersion51()
267 $this->expectException(\Exception
::class);
268 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
270 $this->assertTrue(ApplicationUtils
::checkPHPVersion('5.3', '5.1.0'));
274 * Check another unsupported PHP version
276 public function testCheckSupportedPHPVersion52()
278 $this->expectException(\Exception
::class);
279 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
281 $this->assertTrue(ApplicationUtils
::checkPHPVersion('5.3', '5.2'));
285 * Checks resource permissions for the current Shaarli installation
287 public function testCheckCurrentResourcePermissions()
289 $conf = new ConfigManager('');
290 $conf->set('resource.thumbnails_cache', 'cache');
291 $conf->set('resource.config', 'data/config.php');
292 $conf->set('resource.data_dir', 'data');
293 $conf->set('resource.datastore', 'data/datastore.php');
294 $conf->set('resource.ban_file', 'data/ipbans.php');
295 $conf->set('resource.log', 'data/log.txt');
296 $conf->set('resource.page_cache', 'pagecache');
297 $conf->set('resource.raintpl_tmp', 'tmp');
298 $conf->set('resource.raintpl_tpl', 'tpl');
299 $conf->set('resource.theme', 'default');
300 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
304 ApplicationUtils
::checkResourcePermissions($conf)
309 * Checks resource permissions for a non-existent Shaarli installation
311 public function testCheckCurrentResourcePermissionsErrors()
313 $conf = new ConfigManager('');
314 $conf->set('resource.thumbnails_cache', 'null/cache');
315 $conf->set('resource.config', 'null/data/config.php');
316 $conf->set('resource.data_dir', 'null/data');
317 $conf->set('resource.datastore', 'null/data/store.php');
318 $conf->set('resource.ban_file', 'null/data/ipbans.php');
319 $conf->set('resource.log', 'null/data/log.txt');
320 $conf->set('resource.page_cache', 'null/pagecache');
321 $conf->set('resource.raintpl_tmp', 'null/tmp');
322 $conf->set('resource.raintpl_tpl', 'null/tpl');
323 $conf->set('resource.raintpl_theme', 'null/tpl/default');
324 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
327 '"null/tpl" directory is not readable',
328 '"null/tpl/default" directory is not readable',
329 '"null/cache" directory is not readable',
330 '"null/cache" directory is not writable',
331 '"null/data" directory is not readable',
332 '"null/data" directory is not writable',
333 '"null/pagecache" directory is not readable',
334 '"null/pagecache" directory is not writable',
335 '"null/tmp" directory is not readable',
336 '"null/tmp" directory is not writable'
338 ApplicationUtils
::checkResourcePermissions($conf)
343 * Check update with 'dev' as curent version (master branch).
344 * It should always return false.
346 public function testCheckUpdateDev()
349 ApplicationUtils
::checkUpdate('dev', self
::$testUpdateFile, 100, true, true)