N°9009 Add phpunit test to GetSelectedModules

This commit is contained in:
Timothee
2026-01-07 17:02:42 +01:00
parent 7844c9ffd0
commit c64320b317
4 changed files with 102 additions and 9 deletions

View File

@@ -2155,7 +2155,7 @@ class SetupInfo
/** /**
* Called by the setup process to initializes the list of selected modules. Do not call this method * Called by the setup process to initializes the list of selected modules. Do not call this method
* from an 'auto_select' rule * from an 'auto_select' rule
* @param hash $aModules * @param array $aModules
* @return void * @return void
*/ */
public static function SetSelectedModules($aModules) public static function SetSelectedModules($aModules)

View File

@@ -1415,9 +1415,6 @@ class WizStepModulesChoice extends WizardStep
$sDisplayChoices .= $this->GetSelectedModules($aStepInfo, $aSelectedChoices[$i], $aModules, '', '', $aExtensions); $sDisplayChoices .= $this->GetSelectedModules($aStepInfo, $aSelectedChoices[$i], $aModules, '', '', $aExtensions);
} }
$sDisplayChoices .= '</ul>'; $sDisplayChoices .= '</ul>';
if (class_exists('CreateITILProfilesInstaller')) {
$this->oWizard->SetParameter('old_addon', true);
}
[$aExtensionsAdded, $aExtensionsRemoved, $aExtensionsNotUninstallable] = $this->GetAddedAndRemovedExtensions($aExtensions); [$aExtensionsAdded, $aExtensionsRemoved, $aExtensionsNotUninstallable] = $this->GetAddedAndRemovedExtensions($aExtensions);
$this->oWizard->SetParameter('selected_modules', json_encode(array_keys($aModules))); $this->oWizard->SetParameter('selected_modules', json_encode(array_keys($aModules)));
@@ -1743,7 +1740,7 @@ EOF
* *
* @return string A text representation of what will be installed * @return string A text representation of what will be installed
*/ */
protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sParentId = '', $sDisplayChoices = '', &$aSelectedExtensions = null) public function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sParentId = '', $sDisplayChoices = '', &$aSelectedExtensions = null)
{ {
if ($sParentId == '') { if ($sParentId == '') {
// Check once (before recursing) that the hidden modules are selected // Check once (before recursing) that the hidden modules are selected
@@ -1756,7 +1753,7 @@ EOF
} }
} }
} }
$aOptions = isset($aInfo['options']) ? $aInfo['options'] : []; $aOptions = $aInfo['options'] ?? [];
foreach ($aOptions as $index => $aChoice) { foreach ($aOptions as $index => $aChoice) {
$sChoiceId = $sParentId.self::$SEP.$index; $sChoiceId = $sParentId.self::$SEP.$index;
$aModuleInfo = []; $aModuleInfo = [];
@@ -1771,6 +1768,9 @@ EOF
(isset($aSelectedChoices[$sChoiceId]) && ($aSelectedChoices[$sChoiceId] == $sChoiceId))) { (isset($aSelectedChoices[$sChoiceId]) && ($aSelectedChoices[$sChoiceId] == $sChoiceId))) {
$sDisplayChoices .= '<li>'.$aChoice['title'].'</li>'; $sDisplayChoices .= '<li>'.$aChoice['title'].'</li>';
if (isset($aChoice['modules'])) { if (isset($aChoice['modules'])) {
if (count($aChoice['modules']) === 0) {
throw new Exception('Setup option does not have any module associated');
}
foreach ($aChoice['modules'] as $sModuleId) { foreach ($aChoice['modules'] as $sModuleId) {
$bSelected = true; $bSelected = true;
if (isset($aModuleInfo[$sModuleId])) { if (isset($aModuleInfo[$sModuleId])) {
@@ -1793,7 +1793,6 @@ EOF
} }
} }
} }
$sChoiceType = isset($aChoice['type']) ? $aChoice['type'] : 'wizard_option';
if ($aSelectedExtensions !== null) { if ($aSelectedExtensions !== null) {
$aSelectedExtensions[] = $aChoice['extension_code']; $aSelectedExtensions[] = $aChoice['extension_code'];
} }
@@ -1807,7 +1806,7 @@ EOF
} }
} }
$aAlternatives = isset($aInfo['alternatives']) ? $aInfo['alternatives'] : []; $aAlternatives = $aInfo['alternatives'] ?? [];
$sChoiceName = null; $sChoiceName = null;
foreach ($aAlternatives as $index => $aChoice) { foreach ($aAlternatives as $index => $aChoice) {
$sChoiceId = $sParentId.self::$SEP.$index; $sChoiceId = $sParentId.self::$SEP.$index;

View File

@@ -4,7 +4,7 @@ class WizStepModulesChoiceFake extends WizStepModulesChoice
{ {
public function __construct(WizardController $oWizard, $sCurrentState) public function __construct(WizardController $oWizard, $sCurrentState)
{ {
$this->oWizard = $oWizard;
} }
public function setExtensionMap(iTopExtensionsMap $oMap) public function setExtensionMap(iTopExtensionsMap $oMap)

View File

@@ -350,4 +350,98 @@ class WizStepModulesChoiceTest extends ItopTestCase
$this->assertEquals($aExpectedRemovedList, $aRemovedList); $this->assertEquals($aExpectedRemovedList, $aRemovedList);
} }
public function ProviderGetSelectedModules()
{
return [
'No extension selected' => [
'aSelected' => [],
'aExpectedModules' => [],
'aExpectedExtensions' => [],
],
'One extension selected' => [
'aSelected' => ['_0' => '_0'],
'aExpectedModules' => ['combodo-sample-module' => true],
'aExpectedExtensions' => ['combodo-sample'],
],
];
}
/**
* @dataProvider ProviderGetSelectedModules
*/
public function testGetSelectedModules($aSelectedExtensions, $aExpectedModules, $aExpectedExtensions)
{
$aExtensionsMapData = [
'combodo-sample' => [
'installed' => false,
],
];
$this->oStep->setExtensionMap(iTopExtensionsMapFake::createFromArray($aExtensionsMapData, ));
//GetSelectedModules
$aStepInfo = [
'title' => 'Extensions',
'description' => '',
'banner' => '',
'options' => [
[
'extension_code' => 'combodo-sample',
'title' => 'Sample extension',
'description' => '',
'more_info' => '',
'default' => true,
'modules' => [
'combodo-sample-module',
],
'mandatory' => false,
'source_label' => '',
'uninstallable' => true,
'missing' => false,
],
],
];
$aModules = [];
$aExtensions = [];
$this->oStep->GetSelectedModules($aStepInfo, $aSelectedExtensions, $aModules, '', '', $aExtensions);
$this->assertEquals($aExpectedModules, $aModules);
$this->assertEquals($aExpectedExtensions, $aExtensions);
}
public function testGetSelectedModulesShouldThrowAnExceptionWhenAnySelectedExtensionDoesNotHaveAnyAssociatedModules()
{
$aExtensionsMapData = [
'combodo-sample' => [
'installed' => false,
],
];
$this->oStep->setExtensionMap(iTopExtensionsMapFake::createFromArray($aExtensionsMapData, ));
//GetSelectedModules
$aStepInfo = [
'title' => 'Extensions',
'description' => '',
'banner' => '',
'options' => [
[
'extension_code' => 'combodo-sample',
'title' => 'Sample extension',
'description' => '',
'more_info' => '',
'default' => true,
'modules' => [],
'mandatory' => false,
'source_label' => '',
'uninstallable' => true,
'missing' => false,
],
],
];
$aModules = [];
$aExtensions = [];
$this->expectException('Exception');
$this->oStep->GetSelectedModules($aStepInfo, ['_0' => '_0'], $aModules, '', '', $aExtensions);
}
} }