Skip to content

Commit c64320b

Browse files
committed
N°9009 Add phpunit test to GetSelectedModules
1 parent 7844c9f commit c64320b

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed

setup/setuputils.class.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ class SetupInfo
21552155
/**
21562156
* Called by the setup process to initializes the list of selected modules. Do not call this method
21572157
* from an 'auto_select' rule
2158-
* @param hash $aModules
2158+
* @param array $aModules
21592159
* @return void
21602160
*/
21612161
public static function SetSelectedModules($aModules)

setup/wizardsteps.class.inc.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,9 +1415,6 @@ public function ProcessParams($bMoveForward = true)
14151415
$sDisplayChoices .= $this->GetSelectedModules($aStepInfo, $aSelectedChoices[$i], $aModules, '', '', $aExtensions);
14161416
}
14171417
$sDisplayChoices .= '</ul>';
1418-
if (class_exists('CreateITILProfilesInstaller')) {
1419-
$this->oWizard->SetParameter('old_addon', true);
1420-
}
14211418

14221419
[$aExtensionsAdded, $aExtensionsRemoved, $aExtensionsNotUninstallable] = $this->GetAddedAndRemovedExtensions($aExtensions);
14231420
$this->oWizard->SetParameter('selected_modules', json_encode(array_keys($aModules)));
@@ -1743,7 +1740,7 @@ private function GetPhpExpressionEvaluator(): PhpExpressionEvaluator
17431740
*
17441741
* @return string A text representation of what will be installed
17451742
*/
1746-
protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sParentId = '', $sDisplayChoices = '', &$aSelectedExtensions = null)
1743+
public function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sParentId = '', $sDisplayChoices = '', &$aSelectedExtensions = null)
17471744
{
17481745
if ($sParentId == '') {
17491746
// Check once (before recursing) that the hidden modules are selected
@@ -1756,7 +1753,7 @@ protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sP
17561753
}
17571754
}
17581755
}
1759-
$aOptions = isset($aInfo['options']) ? $aInfo['options'] : [];
1756+
$aOptions = $aInfo['options'] ?? [];
17601757
foreach ($aOptions as $index => $aChoice) {
17611758
$sChoiceId = $sParentId.self::$SEP.$index;
17621759
$aModuleInfo = [];
@@ -1771,6 +1768,9 @@ protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sP
17711768
(isset($aSelectedChoices[$sChoiceId]) && ($aSelectedChoices[$sChoiceId] == $sChoiceId))) {
17721769
$sDisplayChoices .= '<li>'.$aChoice['title'].'</li>';
17731770
if (isset($aChoice['modules'])) {
1771+
if (count($aChoice['modules']) === 0) {
1772+
throw new Exception('Setup option does not have any module associated');
1773+
}
17741774
foreach ($aChoice['modules'] as $sModuleId) {
17751775
$bSelected = true;
17761776
if (isset($aModuleInfo[$sModuleId])) {
@@ -1793,7 +1793,6 @@ protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sP
17931793
}
17941794
}
17951795
}
1796-
$sChoiceType = isset($aChoice['type']) ? $aChoice['type'] : 'wizard_option';
17971796
if ($aSelectedExtensions !== null) {
17981797
$aSelectedExtensions[] = $aChoice['extension_code'];
17991798
}
@@ -1807,7 +1806,7 @@ protected function GetSelectedModules($aInfo, $aSelectedChoices, &$aModules, $sP
18071806
}
18081807
}
18091808

1810-
$aAlternatives = isset($aInfo['alternatives']) ? $aInfo['alternatives'] : [];
1809+
$aAlternatives = $aInfo['alternatives'] ?? [];
18111810
$sChoiceName = null;
18121811
foreach ($aAlternatives as $index => $aChoice) {
18131812
$sChoiceId = $sParentId.self::$SEP.$index;

tests/php-unit-tests/unitary-tests/setup/WizStepModulesChoiceFake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class WizStepModulesChoiceFake extends WizStepModulesChoice
44
{
55
public function __construct(WizardController $oWizard, $sCurrentState)
66
{
7-
7+
$this->oWizard = $oWizard;
88
}
99

1010
public function setExtensionMap(iTopExtensionsMap $oMap)

tests/php-unit-tests/unitary-tests/setup/WizStepModulesChoiceTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,98 @@ public function testGetAddedAndRemovedExtensions($aExtensionsOnDiskOrDb, $aSelec
350350
$this->assertEquals($aExpectedRemovedList, $aRemovedList);
351351
}
352352

353+
public function ProviderGetSelectedModules()
354+
{
355+
return [
356+
'No extension selected' => [
357+
'aSelected' => [],
358+
'aExpectedModules' => [],
359+
'aExpectedExtensions' => [],
360+
],
361+
'One extension selected' => [
362+
'aSelected' => ['_0' => '_0'],
363+
'aExpectedModules' => ['combodo-sample-module' => true],
364+
'aExpectedExtensions' => ['combodo-sample'],
365+
],
366+
];
367+
}
368+
369+
/**
370+
* @dataProvider ProviderGetSelectedModules
371+
*/
372+
public function testGetSelectedModules($aSelectedExtensions, $aExpectedModules, $aExpectedExtensions)
373+
{
374+
$aExtensionsMapData = [
375+
'combodo-sample' => [
376+
'installed' => false,
377+
],
378+
];
379+
$this->oStep->setExtensionMap(iTopExtensionsMapFake::createFromArray($aExtensionsMapData, ));
380+
381+
//GetSelectedModules
382+
$aStepInfo = [
383+
'title' => 'Extensions',
384+
'description' => '',
385+
'banner' => '',
386+
'options' => [
387+
[
388+
'extension_code' => 'combodo-sample',
389+
'title' => 'Sample extension',
390+
'description' => '',
391+
'more_info' => '',
392+
'default' => true,
393+
'modules' => [
394+
'combodo-sample-module',
395+
],
396+
'mandatory' => false,
397+
'source_label' => '',
398+
'uninstallable' => true,
399+
'missing' => false,
400+
],
401+
],
402+
];
403+
404+
$aModules = [];
405+
$aExtensions = [];
406+
$this->oStep->GetSelectedModules($aStepInfo, $aSelectedExtensions, $aModules, '', '', $aExtensions);
407+
$this->assertEquals($aExpectedModules, $aModules);
408+
$this->assertEquals($aExpectedExtensions, $aExtensions);
409+
}
410+
411+
public function testGetSelectedModulesShouldThrowAnExceptionWhenAnySelectedExtensionDoesNotHaveAnyAssociatedModules()
412+
{
413+
$aExtensionsMapData = [
414+
'combodo-sample' => [
415+
'installed' => false,
416+
],
417+
];
418+
$this->oStep->setExtensionMap(iTopExtensionsMapFake::createFromArray($aExtensionsMapData, ));
419+
420+
//GetSelectedModules
421+
$aStepInfo = [
422+
'title' => 'Extensions',
423+
'description' => '',
424+
'banner' => '',
425+
'options' => [
426+
[
427+
'extension_code' => 'combodo-sample',
428+
'title' => 'Sample extension',
429+
'description' => '',
430+
'more_info' => '',
431+
'default' => true,
432+
'modules' => [],
433+
'mandatory' => false,
434+
'source_label' => '',
435+
'uninstallable' => true,
436+
'missing' => false,
437+
],
438+
],
439+
];
440+
441+
$aModules = [];
442+
$aExtensions = [];
443+
$this->expectException('Exception');
444+
$this->oStep->GetSelectedModules($aStepInfo, ['_0' => '_0'], $aModules, '', '', $aExtensions);
445+
}
446+
353447
}

0 commit comments

Comments
 (0)