N°7407 - refactor unattended tests to make work anywhere

This commit is contained in:
odain
2024-04-12 10:19:46 +02:00
parent bbfa601ab1
commit b5af30a93f
4 changed files with 222 additions and 39 deletions

View File

@@ -14,6 +14,9 @@ if (version_compare(ITOP_DESIGN_LATEST_VERSION, '2.7', '<=')) {
} }
class InstallationFileService { class InstallationFileService {
/** @var \RunTimeEnvironment $oProductionEnv */
private $oProductionEnv;
private $sTargetEnvironment; private $sTargetEnvironment;
private $sInstallationPath; private $sInstallationPath;
private $aSelectedModules; private $aSelectedModules;
@@ -37,6 +40,21 @@ class InstallationFileService {
$this->bInstallationOptionalChoicesChecked = $bInstallationOptionalChoicesChecked; $this->bInstallationOptionalChoicesChecked = $bInstallationOptionalChoicesChecked;
} }
public function GetProductionEnv(): RunTimeEnvironment {
if (is_null($this->oProductionEnv)){
$this->oProductionEnv = new RunTimeEnvironment();
}
return $this->oProductionEnv;
}
public function SetProductionEnv(RunTimeEnvironment $oProductionEnv): void {
$this->oProductionEnv = $oProductionEnv;
}
public function GetAutoSelectModules(): array {
return $this->aAutoSelectModules;
}
public function GetSelectedModules(): array { public function GetSelectedModules(): array {
return $this->aSelectedModules; return $this->aSelectedModules;
} }
@@ -206,8 +224,7 @@ class InstallationFileService {
public function ProcessDefaultModules() : void { public function ProcessDefaultModules() : void {
$sProductionModuleDir = APPROOT.'data/' . $this->sTargetEnvironment . '-modules/'; $sProductionModuleDir = APPROOT.'data/' . $this->sTargetEnvironment . '-modules/';
$oProductionEnv = new RunTimeEnvironment(); $aAvailableModules = $this->GetProductionEnv()->AnalyzeInstallation(MetaModel::GetConfig(), $this->GetExtraDirs(), false, null);
$aAvailableModules = $oProductionEnv->AnalyzeInstallation(MetaModel::GetConfig(), $this->GetExtraDirs(), false, null);
$this->aAutoSelectModules = []; $this->aAutoSelectModules = [];
foreach ($aAvailableModules as $sModuleId => $aModule) { foreach ($aAvailableModules as $sModuleId => $aModule) {
@@ -221,7 +238,6 @@ class InstallationFileService {
$this->aSelectedModules[$sModuleId] = true; $this->aSelectedModules[$sModuleId] = true;
continue; continue;
} }
$bIsExtra = (array_key_exists('root_dir', $aModule) && (strpos($aModule['root_dir'], $bIsExtra = (array_key_exists('root_dir', $aModule) && (strpos($aModule['root_dir'],
$sProductionModuleDir) !== false)); // Some modules (root, datamodel) have no 'root_dir' $sProductionModuleDir) !== false)); // Some modules (root, datamodel) have no 'root_dir'
if ($bIsExtra) { if ($bIsExtra) {
@@ -233,7 +249,7 @@ class InstallationFileService {
} }
public function ProcessAutoSelectModules() : void { public function ProcessAutoSelectModules() : void {
foreach($this->aAutoSelectModules as $sModuleId => $aModule) foreach($this->GetAutoSelectModules() as $sModuleId => $aModule)
{ {
try { try {
$bSelected = false; $bSelected = false;

View File

@@ -2,16 +2,17 @@
namespace Combodo\iTop\Test\UnitTest\Setup\UnattendedInstall; namespace Combodo\iTop\Test\UnitTest\Setup\UnattendedInstall;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
* @group itop-clone-only * @group itop-clone-only
*/ */
class InstallationFileServiceTest extends TestCase { class InstallationFileServiceTest extends ItopTestCase {
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
require_once(dirname(__FILE__, 6) . '/setup/unattended-install/InstallationFileService.php'); require_once(dirname(__FILE__, 6) . '/setup/unattended-install/InstallationFileService.php');
$this->sFolderToCleanup = null;
\ModuleDiscovery::ResetCache(); \ModuleDiscovery::ResetCache();
} }
@@ -23,10 +24,112 @@ class InstallationFileServiceTest extends TestCase {
} }
private function GetInstallationPath() : string { private function GetInstallationPath() : string {
return realpath(__DIR__ . '/installation.xml'); return realpath(__DIR__ . '/resources/installation.xml');
} }
public function GetDefaultModulesProvider() { private function GetModuleData($sCategory, bool $bIsVisible, bool $bIsAutoSelect, bool $bProductionModulesInRootDir=false) : array {
$sRootDir = $bProductionModulesInRootDir ? APPROOT.'data/production-modules/' : '';
$aModuleData = [
'category' => $sCategory,
'visible' => $bIsVisible,
'root_dir' => $sRootDir,
];
if ($bIsAutoSelect){
$aModuleData['auto_select'] = true;
}
return $aModuleData;
}
public function ProcessDefaultModulesProvider() {
parent::setUp();
return [
'root module' => [
'aAllFoundModules' => [
'_Root_' => $this->GetModuleData('authentication', false, false, true),
],
'aExpectedSelectedModules' => [],
'aExpectedAutoSelectModules' => [],
],
'auto-select root module' => [
'aAllFoundModules' => [
'_Root_' => $this->GetModuleData('authentication', false, true, true),
],
'aExpectedSelectedModules' => [],
'aExpectedAutoSelectModules' => [],
],
'autoselect module only' => [
'aAllFoundModules' => [
'autoselect-only' => $this->GetModuleData('mycategory', true, true),
],
'aExpectedSelectedModules' => [],
'aExpectedAutoSelectModules' => ['autoselect-only'],
],
'autoselect/invisible module' => [
'aAllFoundModules' => [
'autoselect-only' => $this->GetModuleData('mycategory', false, true),
],
'aExpectedSelectedModules' => [],
'aExpectedAutoSelectModules' => ['autoselect-only'],
],
'autoselect/invisible/in-root-dir module' => [
'aAllFoundModules' => [
'autoselect-only' => $this->GetModuleData('mycategory', false, true , true),
],
'aExpectedSelectedModules' => [],
'aExpectedAutoSelectModules' => ['autoselect-only'],
],
'visible/authent module' => [
'aAllFoundModules' => [
'authent-module' => $this->GetModuleData('authentication', true, false , false),
],
'aExpectedSelectedModules' => ['authent-module'],
'aExpectedAutoSelectModules' => [],
],
'invisible module' => [
'aAllFoundModules' => [
'visible-module' => $this->GetModuleData('mycategory', false, false , false),
],
'aExpectedSelectedModules' => ['visible-module'],
'aExpectedAutoSelectModules' => [],
],
'in-root-dir module' => [
'aAllFoundModules' => [
'in-root-dir-module' => $this->GetModuleData('mycategory', true, false , true),
],
'aExpectedSelectedModules' => ['in-root-dir-module'],
'aExpectedAutoSelectModules' => [],
],
];
}
/**
* @dataProvider ProcessDefaultModulesProvider
*/
public function testProcessDefaultModules(array $aAllFoundModules, array $aExpectedSelectedModules, array $aExpectedAutoSelectModules) {
$oInstallationFileService = new \InstallationFileService('', 'production', [], true);
$oProductionEnv = $this->createMock(\RunTimeEnvironment::class);
$oProductionEnv->expects($this->once())
->method('AnalyzeInstallation')
->willReturn($aAllFoundModules);
$oInstallationFileService->SetProductionEnv($oProductionEnv);
$oInstallationFileService->ProcessDefaultModules();
sort($aExpectedSelectedModules);
$aModules = array_keys($oInstallationFileService->GetSelectedModules());
sort($aModules);
$this->assertEquals($aExpectedSelectedModules, $aModules);
$aAutoSelectModules = array_keys($oInstallationFileService->GetAutoSelectModules());
sort($aAutoSelectModules);
$this->assertEquals($aExpectedAutoSelectModules, $aAutoSelectModules);
}
public function ProcessInstallationChoicesProvider() {
return [ return [
'all checked' => [ true ], 'all checked' => [ true ],
'only defaut + mandatory' => [ false ], 'only defaut + mandatory' => [ false ],
@@ -34,12 +137,16 @@ class InstallationFileServiceTest extends TestCase {
} }
/** /**
* @dataProvider GetDefaultModulesProvider * @dataProvider ProcessInstallationChoicesProvider
*/ */
public function testProcessInstallationChoices($bInstallationOptionalChoicesChecked=false) { public function testProcessInstallationChoices($bInstallationOptionalChoicesChecked) {
$sPath = $this->GetInstallationPath(); $sPath = $this->GetInstallationPath();
$this->assertTrue(is_file($sPath));
$oInstallationFileService = new \InstallationFileService($sPath, 'production', [], $bInstallationOptionalChoicesChecked); $oInstallationFileService = new \InstallationFileService($sPath, 'production', [], $bInstallationOptionalChoicesChecked);
$oProductionEnv = $this->createMock(\RunTimeEnvironment::class);
$oProductionEnv->expects($this->never())
->method('AnalyzeInstallation');
$oInstallationFileService->SetProductionEnv($oProductionEnv);
$oInstallationFileService->ProcessInstallationChoices(); $oInstallationFileService->ProcessInstallationChoices();
$aExpectedModules = [ $aExpectedModules = [
"itop-config-mgmt", "itop-config-mgmt",
@@ -90,12 +197,92 @@ class InstallationFileServiceTest extends TestCase {
$this->assertEquals($aExpectedUnselectedModules, $aUnselectedModules); $this->assertEquals($aExpectedUnselectedModules, $aUnselectedModules);
} }
/**
* @dataProvider ItilExtensionProvider
*/
public function testProcessInstallationChoicesWithItilChoices(array $aSelectedExtensions, bool $bKnownMgtSelected) {
$sPath = $this->GetInstallationPath();
$oInstallationFileService = new \InstallationFileService($sPath, 'production', $aSelectedExtensions, false);
$oProductionEnv = $this->createMock(\RunTimeEnvironment::class);
$oProductionEnv->expects($this->never())
->method('AnalyzeInstallation');
$oInstallationFileService->SetProductionEnv($oProductionEnv);
$oInstallationFileService->ProcessInstallationChoices();
$aExpectedInstallationModules = [
"itop-config-mgmt",
"itop-attachments",
"itop-profiles-itil",
"itop-welcome-itil",
"itop-tickets",
"itop-files-information",
"combodo-db-tools",
"itop-core-update",
"itop-hub-connector",
"itop-oauth-client",
"itop-datacenter-mgmt",
"itop-endusers-devices",
"itop-storage-mgmt",
"itop-virtualization-mgmt",
"itop-service-mgmt",
"itop-request-mgmt-itil",
"itop-incident-mgmt-itil",
"itop-portal",
"itop-portal-base",
"itop-change-mgmt-itil",
];
if ($bKnownMgtSelected){
$aExpectedInstallationModules []= "itop-knownerror-mgmt";
}
sort($aExpectedInstallationModules);
$aModules = array_keys($oInstallationFileService->GetSelectedModules());
sort($aModules);
$this->assertEquals($aExpectedInstallationModules, $aModules);
$aExpectedUnselectedModules = [
0 => 'itop-change-mgmt',
1 => 'itop-problem-mgmt',
2 => 'itop-request-mgmt',
3 => 'itop-service-mgmt-provider',
];
if (!$bKnownMgtSelected){
$aExpectedUnselectedModules[]='itop-knownerror-mgmt';
}
$aUnselectedModules = array_keys($oInstallationFileService->GetUnSelectedModules());
sort($aExpectedUnselectedModules);
sort($aUnselectedModules);
$this->assertEquals($aExpectedUnselectedModules, $aUnselectedModules);
}
public function GetDefaultModulesProvider() {
return [
'check all possible modules' => [true],
'only minimum defaul/mandatory from installation.xml' => [false],
];
}
private function GetMockListOfFoundModules() : array {
$sJsonContent = file_get_contents(realpath(__DIR__ . '/resources/AnalyzeInstallation.json'));
$sJsonContent = str_replace('ROOTDIR_TOREPLACE', APPROOT, $sJsonContent);
return json_decode($sJsonContent, true);
}
/** /**
* @dataProvider GetDefaultModulesProvider * @dataProvider GetDefaultModulesProvider
*/ */
public function testGetAllSelectedModules($bInstallationOptionalChoicesChecked=false) { public function testGetAllSelectedModules($bInstallationOptionalChoicesChecked=false) {
$sPath = $this->GetInstallationPath(); $sPath = $this->GetInstallationPath();
$oInstallationFileService = new \InstallationFileService($sPath, 'production', [], $bInstallationOptionalChoicesChecked); $oInstallationFileService = new \InstallationFileService($sPath, 'production', [], $bInstallationOptionalChoicesChecked);
$oProductionEnv = $this->createMock(\RunTimeEnvironment::class);
$oProductionEnv->expects($this->once())
->method('AnalyzeInstallation')
->willReturn($this->GetMockListOfFoundModules());
$oInstallationFileService->SetProductionEnv($oProductionEnv);
$oInstallationFileService->Init(); $oInstallationFileService->Init();
$aSelectedModules = $oInstallationFileService->GetSelectedModules(); $aSelectedModules = $oInstallationFileService->GetSelectedModules();
@@ -202,6 +389,13 @@ class InstallationFileServiceTest extends TestCase {
public function testGetAllSelectedModules_withItilExtensions(array $aSelectedExtensions, bool $bKnownMgtSelected) { public function testGetAllSelectedModules_withItilExtensions(array $aSelectedExtensions, bool $bKnownMgtSelected) {
$sPath = $this->GetInstallationPath(); $sPath = $this->GetInstallationPath();
$oInstallationFileService = new \InstallationFileService($sPath, 'production', $aSelectedExtensions); $oInstallationFileService = new \InstallationFileService($sPath, 'production', $aSelectedExtensions);
$oProductionEnv = $this->createMock(\RunTimeEnvironment::class);
$oProductionEnv->expects($this->once())
->method('AnalyzeInstallation')
->willReturn($this->GetMockListOfFoundModules());
$oInstallationFileService->SetProductionEnv($oProductionEnv);
$oInstallationFileService->Init(); $oInstallationFileService->Init();
$aSelectedModules = $oInstallationFileService->GetSelectedModules(); $aSelectedModules = $oInstallationFileService->GetSelectedModules();
@@ -271,34 +465,6 @@ class InstallationFileServiceTest extends TestCase {
} }
public function ProductionModulesProvider() {
return [
'module autoload as located in production-modules' => [ true ],
'module not loaded' => [ false ],
];
}
/**
* @dataProvider ProductionModulesProvider
*/
public function testGetAllSelectedModules_ProductionModules(bool $bModuleInProductionModulesFolder) {
$sModuleId = "itop-problem-mgmt";
if ($bModuleInProductionModulesFolder){
if (! is_dir(APPROOT."data/production-modules")){
@mkdir(APPROOT."data/production-modules");
}
$this->RecurseMoveDir(APPROOT . "datamodels/2.x/$sModuleId", APPROOT."data/production-modules/$sModuleId");
}
$sPath = $this->GetInstallationPath();
$oInstallationFileService = new \InstallationFileService($sPath, 'production', [], false);
$oInstallationFileService->Init();
$aSelectedModules = $oInstallationFileService->GetSelectedModules();
$this->assertEquals($bModuleInProductionModulesFolder, array_key_exists($sModuleId, $aSelectedModules));
}
private function RecurseMoveDir($sFromDir, $sToDir) { private function RecurseMoveDir($sFromDir, $sToDir) {
if (! is_dir($sFromDir)){ if (! is_dir($sFromDir)){
return; return;

File diff suppressed because one or more lines are too long