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 5abf29419..735f714d0 100644 --- a/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php +++ b/datamodels/2.x/combodo-db-tools/src/Service/DBToolsUtils.php @@ -139,49 +139,6 @@ 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/datamodels/2.x/itop-hub-connector/launch.php b/datamodels/2.x/itop-hub-connector/launch.php index ffa20366b..1f3e52084 100644 --- a/datamodels/2.x/itop-hub-connector/launch.php +++ b/datamodels/2.x/itop-hub-connector/launch.php @@ -186,7 +186,7 @@ function collect_configuration() // iTop modules $oConfig = MetaModel::GetConfig(); - $aInstalledModules = ModuleInstallationService::GetInstance()->ReadFromDB($oConfig); + $aInstalledModules = ModuleInstallationRepository::GetInstance()->ReadFromDB($oConfig); foreach ($aInstalledModules as $aDBInfo) { $aConfiguration['itop_modules'][$aDBInfo['name']] = $aDBInfo['version']; diff --git a/setup/AnalyzeInstallation.php b/setup/AnalyzeInstallation.php index ca0c35ea1..1335c9aaf 100644 --- a/setup/AnalyzeInstallation.php +++ b/setup/AnalyzeInstallation.php @@ -1,6 +1,6 @@ ReadComputeInstalledModules($oConfig); + $aCurrentlyInstalledModules = ModuleInstallationRepository::GetInstance()->ReadComputeInstalledModules($oConfig); // Adjust the list of proposed modules foreach ($aCurrentlyInstalledModules as $sModuleName => $aModuleDB) { diff --git a/setup/ModuleInstallationService.php b/setup/ModuleInstallationRepository.php similarity index 74% rename from setup/ModuleInstallationService.php rename to setup/ModuleInstallationRepository.php index 259f884ed..f98011dd5 100644 --- a/setup/ModuleInstallationService.php +++ b/setup/ModuleInstallationRepository.php @@ -1,23 +1,23 @@ 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; + } } diff --git a/setup/runtimeenv.class.inc.php b/setup/runtimeenv.class.inc.php index 240d6e43b..1464fd058 100644 --- a/setup/runtimeenv.class.inc.php +++ b/setup/runtimeenv.class.inc.php @@ -630,7 +630,7 @@ class RunTimeEnvironment public function GetApplicationVersion(Config $oConfig) { try { - $aSelectInstall = ModuleInstallationService::GetInstance()->ReadFromDB($oConfig); + $aSelectInstall = ModuleInstallationRepository::GetInstance()->ReadFromDB($oConfig); } catch (MySQLException $e) { // No database or erroneous information $this->log_error('Can not connect to the database: host: '.$oConfig->Get('db_host').', user:'.$oConfig->Get('db_user').', pwd:'.$oConfig->Get('db_pwd').', db name:'.$oConfig->Get('db_name')); 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 d14180fc5..7e1a1580e 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 @@ -46,7 +46,7 @@ class HubControllerTest extends ItopDataTestCase $this->testLaunchCompile(); HubController::GetInstance()->LaunchDeploy(); $this->CheckReport('{"code":0,"message":"Compilation successful.","fields":[]}'); - $this->CompareCurrentAndPreviousModuleInstallations(); + $this->AssertPreviousAndCurrentInstallationAreEquivalent(); } 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 ee00f7c46..9763f7b52 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -35,6 +35,7 @@ use lnkContactToTicket; use lnkFunctionalCIToTicket; use MetaModel; use MissingQueryArgument; +use ModuleInstallationRepository; use MySQLException; use MySQLHasGoneAwayException; use Person; @@ -1558,15 +1559,14 @@ abstract class ItopDataTestCase extends ItopTestCase @unlink($this->sConfigTmpBackupFile); } - public function CompareCurrentAndPreviousModuleInstallations() + public function AssertPreviousAndCurrentInstallationAreEquivalent() { - $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)); + $aPreviousInstallations = ModuleInstallationRepository::GetInstance()->GetPreviousModuleInstallationsByOffset(1); + $aInstallations = ModuleInstallationRepository::GetInstance()->GetPreviousModuleInstallationsByOffset(); + $this->assertEquals($this->GetCanonicalComparableModuleInstallationArray($aPreviousInstallations), $this->GetCanonicalComparableModuleInstallationArray($aInstallations)); } - public function KeepModuleInstallationComparableFields($aInstallations): array + protected function GetCanonicalComparableModuleInstallationArray($aInstallations): array { $aRes = []; $aIgnoredFields = ['id', 'parent_id', 'installed', 'comment']; diff --git a/tests/php-unit-tests/unitary-tests/setup/AnalyzeInstallationTest.php b/tests/php-unit-tests/unitary-tests/setup/AnalyzeInstallationTest.php index 5452d1a34..1f392ff9e 100644 --- a/tests/php-unit-tests/unitary-tests/setup/AnalyzeInstallationTest.php +++ b/tests/php-unit-tests/unitary-tests/setup/AnalyzeInstallationTest.php @@ -4,7 +4,7 @@ namespace Combodo\iTop\Test\UnitTest\Setup; use AnalyzeInstallation; use Combodo\iTop\Test\UnitTest\ItopTestCase; -use ModuleInstallationService; +use ModuleInstallationRepository; class AnalyzeInstallationTest extends ItopTestCase { @@ -12,7 +12,7 @@ class AnalyzeInstallationTest extends ItopTestCase { parent::setUp(); $this->RequireOnceItopFile('setup/AnalyzeInstallation.php'); - $this->RequireOnceItopFile('setup/ModuleInstallationService.php'); + $this->RequireOnceItopFile('setup/ModuleInstallationRepository.php'); $this->RequireOnceItopFile('setup/modulediscovery.class.inc.php'); $this->RequireOnceItopFile('setup/runtimeenv.class.inc.php'); } @@ -151,7 +151,7 @@ class AnalyzeInstallationTest extends ItopTestCase $this->SetNonPublicProperty(AnalyzeInstallation::GetInstance(), "aAvailableModules", $aAvailableModules); //$aModules = json_decode(file_get_contents(__DIR__.'/ressources/priv_modules2.json'), true); - $this->SetNonPublicProperty(ModuleInstallationService::GetInstance(), "aSelectInstall", $aInstalledModules); + $this->SetNonPublicProperty(ModuleInstallationRepository::GetInstance(), "aSelectInstall", $aInstalledModules); $oConfig = $this->createMock(\Config::class);