<?php
-use Shaarli\Config\ConfigManager;
-
-/**
- * ApplicationUtils' tests
- */
+namespace Shaarli;
-require_once 'application/ApplicationUtils.php';
-
-/**
- * Fake ApplicationUtils class to avoid HTTP requests
- */
-class FakeApplicationUtils extends ApplicationUtils
-{
- public static $VERSION_CODE = '';
-
- /**
- * Toggle HTTP requests, allow overriding the version code
- */
- public static function getVersion($url, $timeout=0)
- {
- return self::$VERSION_CODE;
- }
-}
+use Shaarli\Config\ConfigManager;
+require_once 'tests/utils/FakeApplicationUtils.php';
/**
* Unitary tests for Shaarli utilities
*/
-class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
+class ApplicationUtilsTest extends \Shaarli\TestCase
{
protected static $testUpdateFile = 'sandbox/update.txt';
protected static $testVersion = '0.5.0';
/**
* Reset test data for each test
*/
- public function setUp()
+ protected function setUp(): void
{
FakeApplicationUtils::$VERSION_CODE = '';
if (file_exists(self::$testUpdateFile)) {
/**
* Remove test version file if it exists
*/
- public function tearDown()
+ protected function tearDown(): void
{
if (is_file('sandbox/version.php')) {
unlink('sandbox/version.php');
'0.5.4',
ApplicationUtils::getVersion(
'https://raw.githubusercontent.com/shaarli/Shaarli/'
- .'v0.5.4/shaarli_version.php',
+ .'v0.5.4/shaarli_version.php',
$testTimeout
)
);
self::$versionPattern,
ApplicationUtils::getVersion(
'https://raw.githubusercontent.com/shaarli/Shaarli/'
- .'master/shaarli_version.php',
+ .'latest/shaarli_version.php',
$testTimeout
)
);
/**
* Test update checks - invalid Git branch
- * @expectedException Exception
- * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
*/
public function testCheckUpdateInvalidGitBranch()
{
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/');
+
ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
}
public function testCheckSupportedPHPVersion()
{
$minVersion = '5.3';
- ApplicationUtils::checkPHPVersion($minVersion, '5.4.32');
- ApplicationUtils::checkPHPVersion($minVersion, '5.5');
- ApplicationUtils::checkPHPVersion($minVersion, '5.6.10');
+ $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.4.32'));
+ $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.5'));
+ $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.6.10'));
}
/**
* Check a unsupported PHP version
- * @expectedException Exception
- * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
*/
public function testCheckSupportedPHPVersion51()
{
- ApplicationUtils::checkPHPVersion('5.3', '5.1.0');
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
+
+ $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0'));
}
/**
* Check another unsupported PHP version
- * @expectedException Exception
- * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
*/
public function testCheckSupportedPHPVersion52()
{
- ApplicationUtils::checkPHPVersion('5.3', '5.2');
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
+
+ $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2'));
}
/**
);
}
+ /**
+ * Checks resource permissions in minimal mode.
+ */
+ public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void
+ {
+ $conf = new ConfigManager('');
+ $conf->set('resource.thumbnails_cache', 'null/cache');
+ $conf->set('resource.config', 'null/data/config.php');
+ $conf->set('resource.data_dir', 'null/data');
+ $conf->set('resource.datastore', 'null/data/store.php');
+ $conf->set('resource.ban_file', 'null/data/ipbans.php');
+ $conf->set('resource.log', 'null/data/log.txt');
+ $conf->set('resource.page_cache', 'null/pagecache');
+ $conf->set('resource.raintpl_tmp', 'null/tmp');
+ $conf->set('resource.raintpl_tpl', 'null/tpl');
+ $conf->set('resource.raintpl_theme', 'null/tpl/default');
+ $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
+
+ static::assertSame(
+ [
+ '"null/tpl" directory is not readable',
+ '"null/tpl/default" directory is not readable',
+ '"null/tmp" directory is not readable',
+ '"null/tmp" directory is not writable'
+ ],
+ ApplicationUtils::checkResourcePermissions($conf, true)
+ );
+ }
+
/**
* Check update with 'dev' as curent version (master branch).
* It should always return false.
ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true)
);
}
+
+ /**
+ * Basic test of getPhpExtensionsRequirement()
+ */
+ public function testGetPhpExtensionsRequirementSimple(): void
+ {
+ static::assertCount(8, ApplicationUtils::getPhpExtensionsRequirement());
+ static::assertSame([
+ 'name' => 'json',
+ 'required' => true,
+ 'desc' => 'Configuration parsing',
+ 'loaded' => true,
+ ], ApplicationUtils::getPhpExtensionsRequirement()[0]);
+ }
+
+ /**
+ * Test getPhpEol with a known version: 7.4 -> 2022
+ */
+ public function testGetKnownPhpEol(): void
+ {
+ static::assertSame('2022-11-28', ApplicationUtils::getPhpEol('7.4.7'));
+ }
+
+ /**
+ * Test getPhpEol with an unknown version: 7.4 -> 2022
+ */
+ public function testGetUnknownPhpEol(): void
+ {
+ static::assertSame(
+ (((int) (new \DateTime())->format('Y')) + 2) . (new \DateTime())->format('-m-d'),
+ ApplicationUtils::getPhpEol('7.51.34')
+ );
+ }
}