diff --git a/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php b/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php index 09dcf4824..5abf29419 100644 --- a/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php +++ b/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php @@ -10,6 +10,7 @@ namespace Combodo\iTop\DBTools\Service; use CMDBSource; use DBObjectSearch; use DBObjectSet; +use IssueLog; class DBToolsUtils { @@ -138,6 +139,49 @@ EOF; return $aValues; } + /** + * Return previous module installation. offset is applied on parent_id. + * @param $iOffset + * @return array + */ + public static function GetPreviousModuleInstallationsByOffset(int $iOffset = 0): array + { + $oFilter = DBObjectSearch::FromOQL('SELECT ModuleInstallation AS mi WHERE mi.parent_id=0 AND mi.name!="datamodel"'); + $oSet = new DBObjectSet($oFilter, ['installed' => false]); // Most recent first + $oSet->SetLimit($iOffset + 1); + + $iParentId = 0; + /** @var \DBObject $oModuleInstallation */ + while ($oModuleInstallation = $oSet->Fetch()) { + if ($iOffset == 0) { + $iParentId = $oModuleInstallation->Get('id'); + break; + } + $iOffset--; + } + + if ($iParentId === 0) { + IssueLog::Error("no ITOP_APPLICATION ModuleInstallation found", null, ['offset' => $iOffset]); + throw new \Exception("no ITOP_APPLICATION ModuleInstallation found"); + } + + $oFilter = DBObjectSearch::FromOQL("SELECT ModuleInstallation AS mi WHERE mi.id=$iParentId OR mi.parent_id=$iParentId"); + $oSet = new DBObjectSet($oFilter); // Most recent first + $aRawValues = $oSet->ToArrayOfValues(); + $aValues = []; + foreach ($aRawValues as $aRawValue) { + $aValue = []; + foreach ($aRawValue as $sAliasAttCode => $sValue) { + // remove 'mi.' from AttCode + $sAttCode = substr($sAliasAttCode, 3); + $aValue[$sAttCode] = $sValue; + } + + $aValues[] = $aValue; + } + return $aValues; + } + public static function GetDBTablesInfo() { self::AnalyzeTables(); diff --git a/tests/php-unit-tests/integration-tests/itop-hub-connector/HubControllerTest.php b/tests/php-unit-tests/integration-tests/itop-hub-connector/HubControllerTest.php index fc34a6208..d14180fc5 100644 --- a/tests/php-unit-tests/integration-tests/itop-hub-connector/HubControllerTest.php +++ b/tests/php-unit-tests/integration-tests/itop-hub-connector/HubControllerTest.php @@ -2,6 +2,7 @@ namespace Combodo\iTop\Test\UnitTest\HubConnector; +use Combodo\iTop\DBTools\Service\DBToolsUtils; use Combodo\iTop\HubConnector\Controller\HubController; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use DOMFormatException; @@ -45,6 +46,7 @@ class HubControllerTest extends ItopDataTestCase $this->testLaunchCompile(); HubController::GetInstance()->LaunchDeploy(); $this->CheckReport('{"code":0,"message":"Compilation successful.","fields":[]}'); + $this->CompareCurrentAndPreviousModuleInstallations(); } private function CheckReport($sExpected) diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php index f2086f8bb..ee00f7c46 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -17,6 +17,7 @@ namespace Combodo\iTop\Test\UnitTest; use ArchivedObjectException; use CMDBObject; use CMDBSource; +use Combodo\iTop\DBTools\Service\DBToolsUtils; use Combodo\iTop\Service\Events\EventService; use Config; use Contact; @@ -1556,4 +1557,32 @@ abstract class ItopDataTestCase extends ItopTestCase @chmod($sConfigPath, 0440); @unlink($this->sConfigTmpBackupFile); } + + public function CompareCurrentAndPreviousModuleInstallations() + { + $this->RequireOnceItopFile('env-production/combodo-db-tools/src/Service/DBToolsUtils.php'); + $aPreviousInstallations = DBToolsUtils::GetPreviousModuleInstallationsByOffset(1); + $aInstallations = DBToolsUtils::GetPreviousModuleInstallationsByOffset(); + $this->assertEquals($this->KeepModuleInstallationComparableFields($aPreviousInstallations), $this->KeepModuleInstallationComparableFields($aInstallations)); + } + + public function KeepModuleInstallationComparableFields($aInstallations): array + { + $aRes = []; + $aIgnoredFields = ['id', 'parent_id', 'installed', 'comment']; + foreach ($aInstallations as $aData) { + $aNewData = []; + foreach ($aData as $sKey => $val) { + if (in_array($sKey, $aIgnoredFields)) { + continue; + } + $aNewData[$sKey] = $val; + } + $sName = $aNewData['name']; + $aRes[$sName] = $aNewData; + } + + asort($aRes); + return $aRes; + } }