added check 'php' version for extension/upgrade packages

This commit is contained in:
Taras Machyshyn
2015-02-09 17:10:46 +02:00
parent 2bc7f85a58
commit ce55866445
3 changed files with 48 additions and 43 deletions
+27 -16
View File
@@ -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()
+14 -26
View File
@@ -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()
+7 -1
View File
@@ -1,4 +1,4 @@
<?php
<?php
/************************************************************************
* This file is part of EspoCRM.
*
@@ -23,6 +23,7 @@
namespace tests\Espo\Core\Utils;
use Espo\Core\Utils\Util;
use Espo\Core\Utils\System;
class SystemTest extends \PHPUnit_Framework_TestCase
{
@@ -86,4 +87,9 @@ class SystemTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($phpBin, $this->object->getPhpBin());
}
}
public function testGetPhpVersion()
{
$this->assertTrue( (bool) preg_match('/^[0-9\.]+$/', System::getPhpVersion()) );
}
}