🔨 N°5779 update-xml : if XML files already at current version, tries to convert from previous one

This commit is contained in:
Pierre Goiffon
2022-12-08 16:32:34 +01:00
parent 7f88a6aa1a
commit 3270c3f775
4 changed files with 112 additions and 8 deletions

View File

@@ -202,6 +202,13 @@ class DatamodelsXmlFiles extends AbstractGlobFileVersionUpdater
$oFileXml->loadXML($sFileContent);
$oFileItopFormat = new iTopDesignFormat($oFileXml);
$sDesignVersionToSet = static::GetDesignVersionToSet($oFileItopFormat->GetVersion());
if (false === is_null($sDesignVersionToSet)) {
// N°5779 if same as target version, we will try to convert from version below
$oFileItopFormat->GetITopDesignNode()->setAttribute('version', $sDesignVersionToSet);
}
$bConversionResult = $oFileItopFormat->Convert($sVersionLabel);
if (false === $bConversionResult) {
@@ -210,4 +217,16 @@ class DatamodelsXmlFiles extends AbstractGlobFileVersionUpdater
return $oFileItopFormat->GetXmlAsString();
}
/**
* @return ?string version to use : if file version is same as current version then return previous version, else return null
* @since 3.1.0 N°5779
*/
protected static function GetDesignVersionToSet($sFileDesignVersion):?string {
if ($sFileDesignVersion !== ITOP_DESIGN_LATEST_VERSION) {
return null;
}
return iTopDesignFormat::GetPreviousDesignVersion(ITOP_DESIGN_LATEST_VERSION);
}
}

View File

@@ -40,6 +40,17 @@
*/
class iTopDesignFormat
{
/**
* @var array{
* string,
* array{
* previous: string,
* go_to_previous: string,
* next: string,
* go_to_next: string
* }
* }
*/
public static $aVersions = array(
'1.0' => array(
'previous' => null,
@@ -162,6 +173,29 @@ class iTopDesignFormat
);
}
/**
* @param string $sCurrentDesignVersion A design version like 3.0
*
* @return ?string the previous design version from the one passed, null if passed version unknown or 1.0
* @since 3.1.0 N°5779
*/
public static function GetPreviousDesignVersion(string $sCurrentDesignVersion): ?string
{
$aDesignVersions = array_keys(self::$aVersions);
$iCurrentDesignVersionIndex = array_search($sCurrentDesignVersion, $aDesignVersions, true);
if (false === $iCurrentDesignVersionIndex) {
return null;
}
$iPreviousDesignVersionIndex = $iCurrentDesignVersionIndex - 1;
if ($iPreviousDesignVersionIndex < 0) {
return null;
}
return $aDesignVersions[$iPreviousDesignVersionIndex];
}
/**
* Get all the errors in one single array
*/
@@ -1013,7 +1047,7 @@ class iTopDesignFormat
// N°5563 AttributeLinkedSet
// - move edit_mode attribute to legacy_edit_mode attribute
// - fill relation_type & read_only
// - fill relation_type & read_only if non-existing
$oLinkedSetNodes = $oXPath->query("/itop_design/classes/class/fields/field[@xsi:type='AttributeLinkedSet']");
/** @var \DOMElement $oNode */
foreach ($oLinkedSetNodes as $oNode) {
@@ -1047,19 +1081,29 @@ class iTopDesignFormat
break;
}
$oRelationTypeNode = $oNode->ownerDocument->createElement('relation_type', $sRelationType);
$oNode->appendChild($oRelationTypeNode);
$oReadOnlyNode = $oNode->ownerDocument->createElement('read_only', $sReadOnly);
$oNode->appendChild($oReadOnlyNode);
$bHasRelationType = ($oNode->getElementsByTagName('relation_type')->count() > 0);
if (false === $bHasRelationType) {
$oRelationTypeNode = $oNode->ownerDocument->createElement('relation_type', $sRelationType);
$oNode->appendChild($oRelationTypeNode);
}
$bHasReadOnly = ($oNode->getElementsByTagName('read_only')->count() > 0);
if (false === $bHasReadOnly) {
$oReadOnlyNode = $oNode->ownerDocument->createElement('read_only', $sReadOnly);
$oNode->appendChild($oReadOnlyNode);
}
}
}
// N°5563 AttributeLinkedSetIndirect
// - fill read_only attribute
// - fill read_only attribute if non-existing
$oLinkedSetIndirectNodes = $oXPath->query("/itop_design/classes/class/fields/field[@xsi:type='AttributeLinkedSetIndirect']");
foreach ($oLinkedSetIndirectNodes as $oNode) {
$oReadOnlyNode = $oNode->ownerDocument->createElement('read_only', 'false');
$oNode->appendChild($oReadOnlyNode);
$bHasReadOnly = ($oNode->getElementsByTagName('read_only')->count() > 0);
if (false === $bHasReadOnly) {
$oReadOnlyNode = $oNode->ownerDocument->createElement('read_only', 'false');
$oNode->appendChild($oReadOnlyNode);
}
}
}
/**

View File

@@ -0,0 +1,31 @@
<?php
/*
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\Test\UnitTest\ItopTestCase;
class DatamodelsXmlFilesTest extends ItopTestCase
{
protected function setUp(): void
{
parent::setUp();
require_once APPROOT.'.make/release/update.classes.inc.php';
require_once APPROOT.'setup/itopdesignformat.class.inc.php';
}
public function testGetDesignVersionToSet() {
$sVersionFor10 = $this->InvokeNonPublicStaticMethod(DatamodelsXmlFiles::class, 'GetDesignVersionToSet', ['1.0']);
$this->assertNull($sVersionFor10);
$sVersionFor26 = $this->InvokeNonPublicStaticMethod(DatamodelsXmlFiles::class, 'GetDesignVersionToSet', ['2.6']);
$this->assertNull($sVersionFor26);
$sVersionFor27 = $this->InvokeNonPublicStaticMethod(DatamodelsXmlFiles::class, 'GetDesignVersionToSet', ['2.7']);
$this->assertNull($sVersionFor27);
$sPreviousDesignVersion = iTopDesignFormat::GetPreviousDesignVersion(ITOP_DESIGN_LATEST_VERSION);
$sVersionForLatest = $this->InvokeNonPublicStaticMethod(DatamodelsXmlFiles::class, 'GetDesignVersionToSet', [ITOP_DESIGN_LATEST_VERSION]);
$this->assertNotNull($sVersionForLatest);
$this->assertSame($sPreviousDesignVersion, $sVersionForLatest);
}
}

View File

@@ -24,6 +24,16 @@ class iTopDesignFormatTest extends ItopTestCase
require_once APPROOT.'setup/itopdesignformat.class.inc.php';
}
public function testGetPreviousDesignVersion() {
$this->assertSame('3.0', iTopDesignFormat::GetPreviousDesignVersion('3.1'));
$this->assertSame('1.7', iTopDesignFormat::GetPreviousDesignVersion('3.0'));
$this->assertSame('1.6', iTopDesignFormat::GetPreviousDesignVersion('1.7'));
$this->assertSame('1.5', iTopDesignFormat::GetPreviousDesignVersion('1.6'));
$this->assertNull(iTopDesignFormat::GetPreviousDesignVersion('1.0'));
$this->assertNull(iTopDesignFormat::GetPreviousDesignVersion(''));
$this->assertNull(iTopDesignFormat::GetPreviousDesignVersion('NonExistingVersion'));
}
/**
* @covers iTopDesignFormat::Convert
* @dataProvider ConvertProvider