2015-11-11 22:49:58 +01:00
|
|
|
<?php
|
2018-12-03 23:58:59 +01:00
|
|
|
namespace Shaarli;
|
2015-11-24 02:52:22 +01:00
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2015-11-24 02:52:22 +01:00
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
require_once 'tests/utils/FakeApplicationUtils.php';
|
2015-11-11 22:49:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for Shaarli utilities
|
|
|
|
*/
|
2018-12-03 23:58:59 +01:00
|
|
|
class ApplicationUtilsTest extends \PHPUnit\Framework\TestCase
|
2015-11-11 22:49:58 +01:00
|
|
|
{
|
2015-11-24 02:52:22 +01:00
|
|
|
protected static $testUpdateFile = 'sandbox/update.txt';
|
|
|
|
protected static $testVersion = '0.5.0';
|
|
|
|
protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset test data for each test
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2015-11-24 02:52:22 +01:00
|
|
|
{
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = '';
|
|
|
|
if (file_exists(self::$testUpdateFile)) {
|
|
|
|
unlink(self::$testUpdateFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:08:40 +01:00
|
|
|
/**
|
|
|
|
* Remove test version file if it exists
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function tearDown(): void
|
2017-03-21 20:08:40 +01:00
|
|
|
{
|
|
|
|
if (is_file('sandbox/version.php')) {
|
|
|
|
unlink('sandbox/version.php');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 02:52:22 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the latest version code available on Git
|
|
|
|
*
|
|
|
|
* Expected format: Semantic Versioning - major.minor.patch
|
|
|
|
*/
|
2017-03-21 20:08:40 +01:00
|
|
|
public function testGetVersionCode()
|
2015-11-24 02:52:22 +01:00
|
|
|
{
|
|
|
|
$testTimeout = 10;
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'0.5.4',
|
2017-03-21 20:08:40 +01:00
|
|
|
ApplicationUtils::getVersion(
|
2015-11-24 02:52:22 +01:00
|
|
|
'https://raw.githubusercontent.com/shaarli/Shaarli/'
|
2018-10-13 00:35:47 +02:00
|
|
|
.'v0.5.4/shaarli_version.php',
|
2015-11-24 02:52:22 +01:00
|
|
|
$testTimeout
|
|
|
|
)
|
|
|
|
);
|
2016-05-18 21:48:24 +02:00
|
|
|
$this->assertRegExp(
|
2015-11-24 02:52:22 +01:00
|
|
|
self::$versionPattern,
|
2017-03-21 20:08:40 +01:00
|
|
|
ApplicationUtils::getVersion(
|
2015-11-24 02:52:22 +01:00
|
|
|
'https://raw.githubusercontent.com/shaarli/Shaarli/'
|
2018-10-13 00:35:47 +02:00
|
|
|
.'latest/shaarli_version.php',
|
2015-11-24 02:52:22 +01:00
|
|
|
$testTimeout
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-21 20:08:40 +01:00
|
|
|
* Attempt to retrieve the latest version from an invalid File
|
|
|
|
*/
|
|
|
|
public function testGetVersionCodeFromFile()
|
|
|
|
{
|
|
|
|
file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL);
|
|
|
|
$this->assertEquals(
|
|
|
|
'1.2.3',
|
|
|
|
ApplicationUtils::getVersion('sandbox/version.php', 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to retrieve the latest version from an invalid File
|
2015-11-24 02:52:22 +01:00
|
|
|
*/
|
2017-03-21 20:08:40 +01:00
|
|
|
public function testGetVersionCodeInvalidFile()
|
2015-11-24 02:52:22 +01:00
|
|
|
{
|
2016-07-23 14:16:07 +02:00
|
|
|
$oldlog = ini_get('error_log');
|
|
|
|
ini_set('error_log', '/dev/null');
|
2015-11-24 02:52:22 +01:00
|
|
|
$this->assertFalse(
|
2017-03-21 20:08:40 +01:00
|
|
|
ApplicationUtils::getVersion('idontexist', 1)
|
2015-11-24 02:52:22 +01:00
|
|
|
);
|
2016-07-23 14:16:07 +02:00
|
|
|
ini_set('error_log', $oldlog);
|
2015-11-24 02:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test update checks - the user is logged off
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateLoggedOff()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test update checks - the user has disabled updates
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateUserDisabled()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A newer version is available
|
|
|
|
*/
|
2015-11-27 00:10:43 +01:00
|
|
|
public function testCheckUpdateNewVersionAvailable()
|
2015-11-24 02:52:22 +01:00
|
|
|
{
|
|
|
|
$newVersion = '1.8.3';
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = $newVersion;
|
|
|
|
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($newVersion, $version);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No available information about versions
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateNewVersionUnavailable()
|
|
|
|
{
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($version);
|
|
|
|
}
|
|
|
|
|
2015-11-27 00:10:43 +01:00
|
|
|
/**
|
|
|
|
* Test update checks - invalid Git branch
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionMessageRegExp /Invalid branch selected for updates/
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateInvalidGitBranch()
|
|
|
|
{
|
|
|
|
ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
|
|
|
|
}
|
|
|
|
|
2015-11-24 02:52:22 +01:00
|
|
|
/**
|
|
|
|
* Shaarli is up-to-date
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateNewVersionUpToDate()
|
|
|
|
{
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
|
|
|
|
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($version);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time-traveller's Shaarli
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateNewVersionMaartiMcFly()
|
|
|
|
{
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = '0.4.1';
|
|
|
|
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($version);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The version has been checked recently and Shaarli is up-to-date
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateNewVersionTwiceUpToDate()
|
|
|
|
{
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
|
|
|
|
|
|
|
|
// Create the update file
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($version);
|
|
|
|
|
|
|
|
// Reuse the update file
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($version);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The version has been checked recently and Shaarli is outdated
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateNewVersionTwiceOutdated()
|
|
|
|
{
|
|
|
|
$newVersion = '1.8.3';
|
|
|
|
FakeApplicationUtils::$VERSION_CODE = $newVersion;
|
|
|
|
|
|
|
|
// Create the update file
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertEquals($newVersion, $version);
|
|
|
|
|
|
|
|
// Reuse the update file
|
|
|
|
$version = FakeApplicationUtils::checkUpdate(
|
|
|
|
self::$testVersion,
|
|
|
|
self::$testUpdateFile,
|
|
|
|
100,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertEquals($newVersion, $version);
|
|
|
|
}
|
|
|
|
|
2015-11-24 01:36:12 +01:00
|
|
|
/**
|
|
|
|
* Check supported PHP versions
|
|
|
|
*/
|
|
|
|
public function testCheckSupportedPHPVersion()
|
|
|
|
{
|
|
|
|
$minVersion = '5.3';
|
2019-08-10 12:31:32 +02:00
|
|
|
$this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.4.32'));
|
|
|
|
$this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.5'));
|
|
|
|
$this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.6.10'));
|
2015-11-24 01:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a unsupported PHP version
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionMessageRegExp /Your PHP version is obsolete/
|
|
|
|
*/
|
|
|
|
public function testCheckSupportedPHPVersion51()
|
|
|
|
{
|
2019-08-10 12:31:32 +02:00
|
|
|
$this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0'));
|
2015-11-24 01:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check another unsupported PHP version
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionMessageRegExp /Your PHP version is obsolete/
|
|
|
|
*/
|
|
|
|
public function testCheckSupportedPHPVersion52()
|
|
|
|
{
|
2019-08-10 12:31:32 +02:00
|
|
|
$this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2'));
|
2015-11-24 01:36:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 22:49:58 +01:00
|
|
|
/**
|
|
|
|
* Checks resource permissions for the current Shaarli installation
|
|
|
|
*/
|
|
|
|
public function testCheckCurrentResourcePermissions()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$conf = new ConfigManager('');
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf->set('resource.thumbnails_cache', 'cache');
|
|
|
|
$conf->set('resource.config', 'data/config.php');
|
|
|
|
$conf->set('resource.data_dir', 'data');
|
|
|
|
$conf->set('resource.datastore', 'data/datastore.php');
|
|
|
|
$conf->set('resource.ban_file', 'data/ipbans.php');
|
|
|
|
$conf->set('resource.log', 'data/log.txt');
|
|
|
|
$conf->set('resource.page_cache', 'pagecache');
|
|
|
|
$conf->set('resource.raintpl_tmp', 'tmp');
|
|
|
|
$conf->set('resource.raintpl_tpl', 'tpl');
|
2016-12-07 11:58:25 +01:00
|
|
|
$conf->set('resource.theme', 'default');
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf->set('resource.update_check', 'data/lastupdatecheck.txt');
|
2016-05-18 21:48:24 +02:00
|
|
|
|
2015-11-11 22:49:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array(),
|
2016-06-09 20:04:02 +02:00
|
|
|
ApplicationUtils::checkResourcePermissions($conf)
|
2015-11-11 22:49:58 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks resource permissions for a non-existent Shaarli installation
|
|
|
|
*/
|
|
|
|
public function testCheckCurrentResourcePermissionsErrors()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$conf = new ConfigManager('');
|
2016-06-11 09:08:02 +02:00
|
|
|
$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');
|
2016-12-07 11:58:25 +01:00
|
|
|
$conf->set('resource.raintpl_theme', 'null/tpl/default');
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
|
2015-11-11 22:49:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'"null/tpl" directory is not readable',
|
2016-12-07 11:58:25 +01:00
|
|
|
'"null/tpl/default" directory is not readable',
|
2015-11-11 22:49:58 +01:00
|
|
|
'"null/cache" directory is not readable',
|
|
|
|
'"null/cache" directory is not writable',
|
|
|
|
'"null/data" directory is not readable',
|
|
|
|
'"null/data" directory is not writable',
|
|
|
|
'"null/pagecache" directory is not readable',
|
|
|
|
'"null/pagecache" directory is not writable',
|
|
|
|
'"null/tmp" directory is not readable',
|
|
|
|
'"null/tmp" directory is not writable'
|
|
|
|
),
|
2016-06-09 20:04:02 +02:00
|
|
|
ApplicationUtils::checkResourcePermissions($conf)
|
2015-11-11 22:49:58 +01:00
|
|
|
);
|
|
|
|
}
|
2017-03-12 15:02:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check update with 'dev' as curent version (master branch).
|
|
|
|
* It should always return false.
|
|
|
|
*/
|
|
|
|
public function testCheckUpdateDev()
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true)
|
|
|
|
);
|
|
|
|
}
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|