diff --git a/application/Espo/Core/Upgrades/Actions/Base.php b/application/Espo/Core/Upgrades/Actions/Base.php index 777968d485..52e7f7ad0a 100644 --- a/application/Espo/Core/Upgrades/Actions/Base.php +++ b/application/Espo/Core/Upgrades/Actions/Base.php @@ -23,6 +23,7 @@ namespace Espo\Core\Upgrades\Actions; use Espo\Core\Utils\Util; +use Espo\Core\Utils\System; use Espo\Core\Utils\Json; use Espo\Core\Exceptions\Error; use vierbergenlars\SemVer; @@ -180,37 +181,47 @@ abstract class Base */ protected function isAcceptable() { + $manifest = $this->getManifest(); + $res = $this->checkPackageType(); - $res &= $this->checkVersions(); + + //check php version + if (isset($manifest['php'])) { + $res &= $this->checkVersions($manifest['php'], System::getPhpVersion(), 'Your PHP version does not support this installation package.'); + } + + //check acceptableVersions + if (isset($manifest['acceptableVersions'])) { + $res &= $this->checkVersions($manifest['acceptableVersions'], $this->getConfig()->get('version'), 'Your EspoCRM version doesn\'t match for this installation package.'); + } return (bool) $res; } - protected function checkVersions() + protected function checkVersions($versionList, $currentVersion, $errorMessage = '') { - $manifest = $this->getManifest(); - - /** check acceptable versions */ - $version = $manifest['acceptableVersions']; - if (empty($version)) { + if (empty($versionList)) { return true; } - if (is_string($version)) { - $version = (array) $version; + if (is_string($versionList)) { + $versionList = (array) $versionList; } - $currentVersion = $this->getConfig()->get('version'); + try { + $semver = new SemVer\version($currentVersion); + } catch (\Exception $e) { + $GLOBALS['log']->error('Cannot recognize currentVersion ['.$currentVersion.'], error: '.$e->getMessage().'.'); + return; + } - $semver = new SemVer\version($currentVersion); - - foreach ($version as $strVersion) { + foreach ($versionList as $version) { $isInRange = false; try { - $isInRange = $semver->satisfies(new SemVer\expression($strVersion)); + $isInRange = $semver->satisfies(new SemVer\expression($version)); } catch (\Exception $e) { - $GLOBALS['log']->error('Installer [acceptableVersions]: '.$e->getMessage().'.'); + $GLOBALS['log']->error('Version identification error: '.$e->getMessage().'.'); } if ($isInRange) { @@ -218,7 +229,7 @@ abstract class Base } } - $this->throwErrorAndRemovePackage('Your EspoCRM version doesn\'t match for this installation package.'); + $this->throwErrorAndRemovePackage($errorMessage); } protected function checkPackageType() diff --git a/tests/Espo/Core/Upgrades/Actions/BaseTest.php b/tests/Espo/Core/Upgrades/Actions/BaseTest.php index fa2afc6e67..4cf802803e 100644 --- a/tests/Espo/Core/Upgrades/Actions/BaseTest.php +++ b/tests/Espo/Core/Upgrades/Actions/BaseTest.php @@ -162,7 +162,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals( json_decode($manifest,true), $this->reflection->invokeMethod('getManifest') ); } - public function acceptableData() + public function acceptableVersions() { return array( array( '11.5.2' ), @@ -182,32 +182,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider acceptableData + * @dataProvider acceptableVersions */ - public function testIsAcceptable($version, $currentVersion = null) + public function testCheckVersions($versions, $currentVersion = null) { if (!isset($currentVersion)) { $currentVersion = $this->currentVersion; } - $this->objects['config'] - ->expects($this->once()) - ->method('get') - ->will($this->returnValue($currentVersion)); - - $this->reflection->setProperty('data', array('manifest' => array('acceptableVersions' => $version))); - $this->assertTrue( $this->reflection->invokeMethod('isAcceptable') ); + $this->assertTrue( $this->reflection->invokeMethod('checkVersions', array($versions, $currentVersion, 'error') ) ); } - public function testIsAcceptableEmpty() - { - $version = array(); - - $this->reflection->setProperty('data', array('manifest' => array('acceptableVersions' => $version))); - $this->assertTrue( $this->reflection->invokeMethod('isAcceptable') ); - } - - public function acceptableDataFalse() + public function unacceptableVersions() { return array( array( '1.*', ), @@ -219,9 +205,9 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider acceptableDataFalse + * @dataProvider unacceptableVersions */ - public function testIsAcceptableFalse($version, $currentVersion = null) + public function testCheckVersionsException($versions, $currentVersion = null) { if (!isset($currentVersion)) { $currentVersion = $this->currentVersion; @@ -229,13 +215,15 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('\Espo\Core\Exceptions\Error'); - $this->objects['config'] - ->expects($this->once()) - ->method('get') - ->will($this->returnValue($currentVersion)); + $this->reflection->invokeMethod('checkVersions', array($versions, $currentVersion, 'error')); + } + + public function testIsAcceptableEmpty() + { + $version = array(); $this->reflection->setProperty('data', array('manifest' => array('acceptableVersions' => $version))); - $this->assertFalse( $this->reflection->invokeMethod('isAcceptable', array()) ); + $this->assertTrue( $this->reflection->invokeMethod('isAcceptable') ); } public function testGetPath() diff --git a/tests/Espo/Core/Utils/SystemTest.php b/tests/Espo/Core/Utils/SystemTest.php index 6f2e525da0..9871e75864 100644 --- a/tests/Espo/Core/Utils/SystemTest.php +++ b/tests/Espo/Core/Utils/SystemTest.php @@ -1,4 +1,4 @@ -assertEquals($phpBin, $this->object->getPhpBin()); } } + + public function testGetPhpVersion() + { + $this->assertTrue( (bool) preg_match('/^[0-9\.]+$/', System::getPhpVersion()) ); + } } \ No newline at end of file