diff --git a/setup/moduleinstallation/ModuleInstallationRepository.php b/setup/moduleinstallation/ModuleInstallationRepository.php index f139941d2e..16a83872f5 100644 --- a/setup/moduleinstallation/ModuleInstallationRepository.php +++ b/setup/moduleinstallation/ModuleInstallationRepository.php @@ -2,7 +2,7 @@ class ModuleInstallationRepository { - private static ModuleInstallationRepository $oInstance; + private static ?ModuleInstallationRepository $oInstance; protected function __construct() { @@ -60,6 +60,7 @@ class ModuleInstallationRepository CMDBSource::InitFromConfig($oConfig); //read db module installations $tableWithPrefix = $this->GetTableWithPrefix($oConfig); + $iRootId = CMDBSource::QueryToScalar("SELECT max(parent_id) FROM $tableWithPrefix"); // Get the latest installed modules, without the "root" ones (iTop version and datamodel version) $sSQL = <<GetTableWithPrefix($oConfig); - $sSQLQuery = "SELECT * FROM $tableWithPrefix"; - $aSelectInstall = CMDBSource::QueryToArray($sSQLQuery); + $aSelectInstall = $this->ReadFromDB($oConfig); } catch (MySQLException $e) { // No database or erroneous information SetupLog::Error( @@ -105,8 +103,6 @@ SQL; null, [ 'host' => $oConfig->Get('db_host'), - 'user' => $oConfig->Get('db_user'), - 'pwd:' => $oConfig->Get('db_pwd'), 'db name' => $oConfig->Get('db_name'), 'msg' => $e->getMessage(), ] diff --git a/setup/runtimeenv.class.inc.php b/setup/runtimeenv.class.inc.php index 1f432c4e1b..9b5852a205 100644 --- a/setup/runtimeenv.class.inc.php +++ b/setup/runtimeenv.class.inc.php @@ -1276,14 +1276,14 @@ class RunTimeEnvironment * - plus the list of modules present in the "extra" directory of the build environment: data/-modules/ * * @param string $sSourceEnv The name of the source environment to 'imitate' - * @param null $bUseSymLinks Whether to create symbolic links instead of copies + * @param bool|null $bUseSymLinks Whether to create symbolic links instead of copies * @param array $aAddedExtensions List of additional extensions to add to the build environment * * @return string[] * @throws \ConfigException * @throws \CoreException */ - public function CompileFrom($sSourceEnv, $bUseSymLinks = null, $aAddedExtensions = []) + public function CompileFrom($sSourceEnv, ?bool $bUseSymLinks = null, $aAddedExtensions = []) { $oConfig = new Config(utils::GetConfigFilePath($sSourceEnv)); $this->InitExtensionMap($oConfig); diff --git a/setup/wizardsteps/WizStepWelcome.php b/setup/wizardsteps/WizStepWelcome.php index 4e6ed444bd..903bb738a8 100644 --- a/setup/wizardsteps/WizStepWelcome.php +++ b/setup/wizardsteps/WizStepWelcome.php @@ -158,15 +158,45 @@ HTML HTML ); - $oPage->add_ready_script( - <<DisplaySetupShortcutButton()) { + $oPage->add_ready_script( + <<'); JS - ); + ); + } } } } + public function DisplaySetupShortcutButton(): bool + { + if ('install' === $this->oWizard->GetParameter('mode', 'install')) { + //fresh install + return false; + } + + $oConfig = utils::GetConfig(); + $res = ModuleInstallationRepository::GetInstance()->GetApplicationVersion($oConfig); + if (false === $res) { + return false; + } + + $sProductName = $res['product_name'] ?? null; + $sProductVersion = $res['product_version'] ?? null; + if (is_null($sProductName) || is_null($sProductVersion)) { + \SetupLog::Error(__METHOD__.": cannot fetch itop version", null, $res); + return false; + } + + if (ITOP_VERSION_FULL !== $sProductVersion) { + return false; + } + + return (ITOP_APPLICATION === $sProductName); + } + public function CanMoveForward() { return $this->bCanMoveForward; diff --git a/tests/php-unit-tests/unitary-tests/setup/WizStepWelcomeTest.php b/tests/php-unit-tests/unitary-tests/setup/WizStepWelcomeTest.php new file mode 100644 index 0000000000..1ac437809c --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/setup/WizStepWelcomeTest.php @@ -0,0 +1,134 @@ +createMock(WizardController::class); + $this->GivenSetupMode($oWizard); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_NoButtonWhenNoItopVersionFetched() + { + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + + $aApplicationVersion = [ + 'product_name' => ITOP_APPLICATION, + ]; + $this->GivenGetApplicationVersion($aApplicationVersion); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_NoButtonWhenNoApplicationVersionFetched() + { + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + + $aApplicationVersion = [ + 'product_version' => ITOP_VERSION_FULL, + ]; + $this->GivenGetApplicationVersion($aApplicationVersion); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_NoButtonWhenApplicationChangeDuringUpgrade() + { + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + + $aApplicationVersion = [ + 'product_version' => ITOP_VERSION_FULL, + 'product_name' => 'toto', + ]; + $this->GivenGetApplicationVersion($aApplicationVersion); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_NoButtonWhenVersionChangeDuringUpgrade() + { + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + + $aApplicationVersion = [ + 'product_version' => '6.6.6', + 'product_name' => ITOP_APPLICATION, + ]; + $this->GivenGetApplicationVersion($aApplicationVersion); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_NoButtonWhenMySQLException() + { + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + $this->GivenGetApplicationVersion(false); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(false, $oWiz->DisplaySetupShortcutButton()); + } + + public function testDisplaySetupShortcutButton_ButtonDisplayed() + { + + $oWizard = $this->createMock(WizardController::class); + $this->GivenSetupMode($oWizard, 'upgrade'); + + $aApplicationVersion = [ + 'product_version' => ITOP_VERSION_FULL, + 'product_name' => ITOP_APPLICATION, + ]; + $this->GivenGetApplicationVersion($aApplicationVersion); + + $oWiz = new \WizStepWelcome($oWizard, ""); + $this->assertEquals(true, $oWiz->DisplaySetupShortcutButton()); + } + + private function GivenSetupMode(WizardController $oWizardMock, $sMode = 'install'): void + { + $oWizardMock->expects($this->once()) + ->method('GetParameter') + ->with('mode') + ->willReturn($sMode); + } + + private function GivenGetApplicationVersion($sExpectedReturnedValue): void + { + $oModuleInstallationRepository = $this->createMock(ModuleInstallationRepository::class); + ModuleInstallationRepository::SetInstance($oModuleInstallationRepository); + + $oModuleInstallationRepository->expects($this->once()) + ->method('GetApplicationVersion') + ->willReturn($sExpectedReturnedValue); + } +}