2 namespace Shaarli\Helper
;
4 use Shaarli\Config\ConfigManager
;
5 use Shaarli\FakeApplicationUtils
;
7 require_once 'tests/utils/FakeApplicationUtils.php';
10 * Unitary tests for Shaarli utilities
12 class ApplicationUtilsTest
extends \Shaarli\TestCase
14 protected static $testUpdateFile = 'sandbox/update.txt';
15 protected static $testVersion = '0.5.0';
16 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
19 * Reset test data for each test
21 protected function setUp(): void
23 FakeApplicationUtils
::$VERSION_CODE = '';
24 if (file_exists(self
::$testUpdateFile)) {
25 unlink(self
::$testUpdateFile);
30 * Remove test version file if it exists
32 protected function tearDown(): void
34 if (is_file('sandbox/version.php')) {
35 unlink('sandbox/version.php');
40 * Retrieve the latest version code available on Git
42 * Expected format: Semantic Versioning - major.minor.patch
44 public function testGetVersionCode()
50 ApplicationUtils
::getVersion(
51 'https://raw.githubusercontent.com/shaarli/Shaarli/'
52 .'v0.5.4/shaarli_version.php',
57 self
::$versionPattern,
58 ApplicationUtils
::getVersion(
59 'https://raw.githubusercontent.com/shaarli/Shaarli/'
60 .'latest/shaarli_version.php',
67 * Attempt to retrieve the latest version from an invalid File
69 public function testGetVersionCodeFromFile()
71 file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL
);
74 ApplicationUtils
::getVersion('sandbox/version.php', 1)
79 * Attempt to retrieve the latest version from an invalid File
81 public function testGetVersionCodeInvalidFile()
83 $oldlog = ini_get('error_log');
84 ini_set('error_log', '/dev/null');
86 ApplicationUtils
::getVersion('idontexist', 1)
88 ini_set('error_log', $oldlog);
92 * Test update checks - the user is logged off
94 public function testCheckUpdateLoggedOff()
97 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
102 * Test update checks - the user has disabled updates
104 public function testCheckUpdateUserDisabled()
107 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
112 * A newer version is available
114 public function testCheckUpdateNewVersionAvailable()
116 $newVersion = '1.8.3';
117 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
119 $version = FakeApplicationUtils
::checkUpdate(
121 self
::$testUpdateFile,
127 $this->assertEquals($newVersion, $version);
131 * No available information about versions
133 public function testCheckUpdateNewVersionUnavailable()
135 $version = FakeApplicationUtils
::checkUpdate(
137 self
::$testUpdateFile,
143 $this->assertFalse($version);
147 * Test update checks - invalid Git branch
149 public function testCheckUpdateInvalidGitBranch()
151 $this->expectException(\Exception
::class);
152 $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/');
154 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
158 * Shaarli is up-to-date
160 public function testCheckUpdateNewVersionUpToDate()
162 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
164 $version = FakeApplicationUtils
::checkUpdate(
166 self
::$testUpdateFile,
172 $this->assertFalse($version);
176 * Time-traveller's Shaarli
178 public function testCheckUpdateNewVersionMaartiMcFly()
180 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
182 $version = FakeApplicationUtils
::checkUpdate(
184 self
::$testUpdateFile,
190 $this->assertFalse($version);
194 * The version has been checked recently and Shaarli is up-to-date
196 public function testCheckUpdateNewVersionTwiceUpToDate()
198 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
200 // Create the update file
201 $version = FakeApplicationUtils
::checkUpdate(
203 self
::$testUpdateFile,
209 $this->assertFalse($version);
211 // Reuse the update file
212 $version = FakeApplicationUtils
::checkUpdate(
214 self
::$testUpdateFile,
220 $this->assertFalse($version);
224 * The version has been checked recently and Shaarli is outdated
226 public function testCheckUpdateNewVersionTwiceOutdated()
228 $newVersion = '1.8.3';
229 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
231 // Create the update file
232 $version = FakeApplicationUtils
::checkUpdate(
234 self
::$testUpdateFile,
239 $this->assertEquals($newVersion, $version);
241 // Reuse the update file
242 $version = FakeApplicationUtils
::checkUpdate(
244 self
::$testUpdateFile,
249 $this->assertEquals($newVersion, $version);
253 * Check supported PHP versions
255 public function testCheckSupportedPHPVersion()
258 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32'));
259 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.5'));
260 $this->assertTrue(ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10'));
264 * Check a unsupported PHP version
266 public function testCheckSupportedPHPVersion51()
268 $this->expectException(\Exception
::class);
269 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
271 $this->assertTrue(ApplicationUtils
::checkPHPVersion('5.3', '5.1.0'));
275 * Check another unsupported PHP version
277 public function testCheckSupportedPHPVersion52()
279 $this->expectException(\Exception
::class);
280 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
282 $this->assertTrue(ApplicationUtils
::checkPHPVersion('5.3', '5.2'));
286 * Checks resource permissions for the current Shaarli installation
288 public function testCheckCurrentResourcePermissions()
290 $conf = new ConfigManager('');
291 $conf->set('resource.thumbnails_cache', 'cache');
292 $conf->set('resource.config', 'data/config.php');
293 $conf->set('resource.data_dir', 'data');
294 $conf->set('resource.datastore', 'data/datastore.php');
295 $conf->set('resource.ban_file', 'data/ipbans.php');
296 $conf->set('resource.log', 'data/log.txt');
297 $conf->set('resource.page_cache', 'pagecache');
298 $conf->set('resource.raintpl_tmp', 'tmp');
299 $conf->set('resource.raintpl_tpl', 'tpl');
300 $conf->set('resource.theme', 'default');
301 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
305 ApplicationUtils
::checkResourcePermissions($conf)
310 * Checks resource permissions for a non-existent Shaarli installation
312 public function testCheckCurrentResourcePermissionsErrors()
314 $conf = new ConfigManager('');
315 $conf->set('resource.thumbnails_cache', 'null/cache');
316 $conf->set('resource.config', 'null/data/config.php');
317 $conf->set('resource.data_dir', 'null/data');
318 $conf->set('resource.datastore', 'null/data/store.php');
319 $conf->set('resource.ban_file', 'null/data/ipbans.php');
320 $conf->set('resource.log', 'null/data/log.txt');
321 $conf->set('resource.page_cache', 'null/pagecache');
322 $conf->set('resource.raintpl_tmp', 'null/tmp');
323 $conf->set('resource.raintpl_tpl', 'null/tpl');
324 $conf->set('resource.raintpl_theme', 'null/tpl/default');
325 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
328 '"null/tpl" directory is not readable',
329 '"null/tpl/default" directory is not readable',
330 '"null/cache" directory is not readable',
331 '"null/cache" directory is not writable',
332 '"null/data" directory is not readable',
333 '"null/data" directory is not writable',
334 '"null/pagecache" directory is not readable',
335 '"null/pagecache" directory is not writable',
336 '"null/tmp" directory is not readable',
337 '"null/tmp" directory is not writable'
339 ApplicationUtils
::checkResourcePermissions($conf)
344 * Checks resource permissions in minimal mode.
346 public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void
348 $conf = new ConfigManager('');
349 $conf->set('resource.thumbnails_cache', 'null/cache');
350 $conf->set('resource.config', 'null/data/config.php');
351 $conf->set('resource.data_dir', 'null/data');
352 $conf->set('resource.datastore', 'null/data/store.php');
353 $conf->set('resource.ban_file', 'null/data/ipbans.php');
354 $conf->set('resource.log', 'null/data/log.txt');
355 $conf->set('resource.page_cache', 'null/pagecache');
356 $conf->set('resource.raintpl_tmp', 'null/tmp');
357 $conf->set('resource.raintpl_tpl', 'null/tpl');
358 $conf->set('resource.raintpl_theme', 'null/tpl/default');
359 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
363 '"null/tpl" directory is not readable',
364 '"null/tpl/default" directory is not readable',
365 '"null/tmp" directory is not readable',
366 '"null/tmp" directory is not writable'
368 ApplicationUtils
::checkResourcePermissions($conf, true)
373 * Check update with 'dev' as curent version (master branch).
374 * It should always return false.
376 public function testCheckUpdateDev()
379 ApplicationUtils
::checkUpdate('dev', self
::$testUpdateFile, 100, true, true)
384 * Basic test of getPhpExtensionsRequirement()
386 public function testGetPhpExtensionsRequirementSimple(): void
388 static::assertCount(8, ApplicationUtils
::getPhpExtensionsRequirement());
392 'desc' => 'Configuration parsing',
394 ], ApplicationUtils
::getPhpExtensionsRequirement()[0]);
398 * Test getPhpEol with a known version: 7.4 -> 2022
400 public function testGetKnownPhpEol(): void
402 static::assertSame('2022-11-28', ApplicationUtils
::getPhpEol('7.4.7'));
406 * Test getPhpEol with an unknown version: 7.4 -> 2022
408 public function testGetUnknownPhpEol(): void
411 (((int) (new \
DateTime())->format('Y')) +
2) . (new \
DateTime())->format('-m-d'),
412 ApplicationUtils
::getPhpEol('7.51.34')