🐛 Fix version methods throwing exceptions with version 3.0.0

This commit is contained in:
Pierre Goiffon
2020-10-14 10:59:50 +02:00
parent 8e6351d346
commit 2221e0541c
3 changed files with 57 additions and 28 deletions

View File

@@ -2081,33 +2081,38 @@ class utils
*/
public static function GetCacheBusterTimestamp()
{
if(!defined('COMPILATION_TIMESTAMP'))
{
if (!defined('COMPILATION_TIMESTAMP')) {
return ITOP_VERSION;
}
return COMPILATION_TIMESTAMP;
}
/**
* @return string eg : '2_7_0' ITOP_VERSION is '2.7.1-dev'
*/
public static function GetItopVersionWikiSyntax()
{
public static function GetItopVersionWikiSyntax() {
$sMinorVersion = self::GetItopMinorVersion();
return str_replace('.', '_', $sMinorVersion).'_0';
}
/**
* @param string $sPatchVersion if non provided, will call GetItopPatchVersion
*
* @return string eg 2.7 if ITOP_VERSION is '2.7.0-dev'
* @throws \Exception
*/
public static function GetItopMinorVersion()
{
$sPatchVersion = self::GetItopPatchVersion();
public static function GetItopMinorVersion($sPatchVersion = null) {
if (is_null($sPatchVersion)) {
$sPatchVersion = self::GetItopPatchVersion();
}
$aExplodedVersion = explode('.', $sPatchVersion);
if (empty($aExplodedVersion[0]) || empty($aExplodedVersion[1]))
{
if (count($aExplodedVersion) < 2) {
throw new Exception('iTop version is wrongfully configured!');
}
if (($aExplodedVersion[0] == '') || ($aExplodedVersion[1] == '')) {
throw new Exception('iTop version is wrongfully configured!');
}
@@ -2117,9 +2122,9 @@ class utils
/**
* @return string eg '2.7.0' if ITOP_VERSION is '2.7.0-dev'
*/
public static function GetItopPatchVersion()
{
public static function GetItopPatchVersion() {
$aExplodedVersion = explode('-', ITOP_VERSION);
return $aExplodedVersion[0];
}

View File

@@ -46,7 +46,6 @@ class ItopTestCase extends TestCase
@include_once '../../../../../../../../approot.inc.php';
$this->debug("\n----------\n---------- ".$this->getName()."\n----------\n");
}
protected function debug($sMsg)

View File

@@ -16,7 +16,6 @@
namespace Combodo\iTop\Test\UnitTest\Integration;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use DOMDocument;
use iTopDesignFormat;
@@ -30,9 +29,30 @@ use iTopDesignFormat;
*
* @package Combodo\iTop\Test\UnitTest\Setup
*/
class iTopModulesPhpVersionIntegrationTest extends ItopTestCase
{
class iTopModulesPhpVersionIntegrationTest extends ItopTestCase {
/**
* We had a problem when version was switched from 2.8.0 to 3.0.0, so this test aims to detect such problems
*
* @param string $sVersion
* @param string $sExpectedMinVersion if null the test will expects an exception to occur
*
* @throws \Exception
* @since 3.0.0
* @dataProvider GetItopMinorVersionProvider
*/
public function testGetItopMinorVersion($sVersion, $sExpectedMinVersion) {
if (is_null($sExpectedMinVersion)) {
$this->expectException(\Exception::class);
}
$sActualMinVersion = \utils::GetItopMinorVersion($sVersion);
if (!is_null($sExpectedMinVersion)) {
$this->assertEquals($sExpectedMinVersion, $sActualMinVersion);
}
}
public function GetItopMinorVersionProvider() {
return [['2.8.0', '2.8'], ['3.0.0', '3.0'], ['3.', null], ['3', null]];
}
/**
* Verify if the datamodel.*.xml files refer to the current itop version
@@ -44,6 +64,7 @@ class iTopModulesPhpVersionIntegrationTest extends ItopTestCase
*/
public function testiTopModulesPhpVersion($sExpectedVersion, $sPhpFile)
{
$this->assertNotNull($sExpectedVersion, 'Expected version is null, something went wrong in the dataprovider !');
$sModulePath = realpath($sPhpFile);
$sModuleFileName = basename($sModulePath);
@@ -57,38 +78,43 @@ class iTopModulesPhpVersionIntegrationTest extends ItopTestCase
$matches
);
$this->assertRegExp("#$sExpectedVersion#", $matches[1], " $sPhpFile:2 file refer does not refer to current itop version ($sModuleName/$matches[1] does not match regexp $sModuleName/$sExpectedVersion)");
$this->assertRegExp("#$sExpectedVersion#", $matches[1],
" $sPhpFile:2 file refer does not refer to current itop version ($sModuleName/$matches[1] does not match regexp $sModuleName/$sExpectedVersion)");
}
public function iTopModulesPhpVersionProvider()
{
/**
* @return array
* @throws \Exception
* @uses utils::GetItopMinorVersion()
*/
public function iTopModulesPhpVersionProvider() {
parent::setUp();
require_once APPROOT.'core/config.class.inc.php';
require_once APPROOT.'application/utils.inc.php';
if (is_dir(APPROOT.'datamodels/2.x'))
{
if (is_dir(APPROOT.'datamodels/2.x')) {
$DatamodelsPath = APPROOT.'datamodels/2.x';
}
elseif (is_dir(APPROOT.'datamodels/1.x'))
{
$DatamodelsPath = APPROOT.'datamodels/1.x';
}
else
{
} else {
throw new \Exception('Cannot local the datamodels directory');
}
$sPath = $DatamodelsPath.'/*/module.*.php';
$aPhpFiles = glob($sPath);
$sExpectedVersion = \utils::GetItopMinorVersion().'\.\d+'; // ie: 2.7\.\d+ (and yes, the 1st dot should be escaped, but, hey, it is good enough as it, ans less complex to read)
try {
$sExpectedVersion = \utils::GetItopMinorVersion().'\.\d+';// ie: 2.7\.\d+ (and yes, the 1st dot should be escaped, but, hey, it is good enough as it, ans less complex to read)
}
catch (\Exception $e) {
$sExpectedVersion = null;
}
$aTestCases = array();
foreach ($aPhpFiles as $sPhpFile)
{
foreach ($aPhpFiles as $sPhpFile) {
$aTestCases[$sPhpFile] = array(
'sExpectedVersion' => $sExpectedVersion,
'sPhpFile' => $sPhpFile,
@@ -97,5 +123,4 @@ class iTopModulesPhpVersionIntegrationTest extends ItopTestCase
return $aTestCases;
}
}