From f1ba57ff4974c90bf68156a6726d1fc62053a1e1 Mon Sep 17 00:00:00 2001 From: "lenaick.moreira" Date: Mon, 1 Jun 2026 10:46:43 +0200 Subject: [PATCH 1/2] =?UTF-8?q?N=C2=B09597=20-=20Switch=20environment=20to?= =?UTF-8?q?=20a=20build=20environment=20must=20not=20be=20available?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/startup.inc.php | 22 +------ lib/composer/autoload_classmap.php | 1 + lib/composer/autoload_static.php | 1 + sources/Service/Startup/StartupService.php | 58 +++++++++++++++++++ .../Service/Startup/StartupServiceTest.php | 47 +++++++++++++++ 5 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 sources/Service/Startup/StartupService.php create mode 100644 tests/php-unit-tests/unitary-tests/sources/Service/Startup/StartupServiceTest.php diff --git a/application/startup.inc.php b/application/startup.inc.php index 73460293b5..11385173da 100644 --- a/application/startup.inc.php +++ b/application/startup.inc.php @@ -17,6 +17,7 @@ // You should have received a copy of the GNU Affero General Public License // along with iTop. If not, see use Combodo\iTop\Application\Helper\Session; +use Combodo\iTop\Service\Startup\StartupService; require_once(APPROOT.'core/cmdbobject.class.inc.php'); require_once(APPROOT.'application/utils.inc.php'); @@ -69,26 +70,7 @@ $sSwitchEnv = utils::ReadParam('switch_env', null); $bAllowCache = true; -if (($sSwitchEnv != null) && file_exists(APPCONF.$sSwitchEnv.'/'.ITOP_CONFIG_FILE) && (Session::Get('itop_env') !== $sSwitchEnv)) { - Session::Set('itop_env', $sSwitchEnv); - $sEnv = $sSwitchEnv; - $bAllowCache = false; - // Reset the opcache since otherwise the PHP "model" files may still be cached !! - if (function_exists('opcache_reset')) { - // Zend opcode cache - opcache_reset(); - } - if (function_exists('apc_clear_cache')) { - // APC(u) cache - apc_clear_cache(); - } - // TODO: reset the credentials as well ?? -} elseif (Session::IsSet('itop_env')) { - $sEnv = Session::Get('itop_env'); -} else { - $sEnv = ITOP_DEFAULT_ENV; - Session::Set('itop_env', ITOP_DEFAULT_ENV); -} +$sEnv = StartupService::SetItopEnvironment($sSwitchEnv, $bAllowCache); $sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE; try { MetaModel::Startup($sConfigFile, false /* $bModelOnly */, $bAllowCache, false /* $bTraceSourceFiles */, $sEnv); diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index 50b73aabc9..626b2db633 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -638,6 +638,7 @@ 'Combodo\\iTop\\Service\\Router\\Exception\\RouteNotFoundException' => $baseDir . '/sources/Service/Router/Exception/RouteNotFoundException.php', 'Combodo\\iTop\\Service\\Router\\Exception\\RouterException' => $baseDir . '/sources/Service/Router/Exception/RouterException.php', 'Combodo\\iTop\\Service\\Router\\Router' => $baseDir . '/sources/Service/Router/Router.php', + 'Combodo\\iTop\\Service\\Startup\\StartupService' => $baseDir . '/sources/Service/Startup/StartupService.php', 'Combodo\\iTop\\Service\\SummaryCard\\SummaryCardService' => $baseDir . '/sources/Service/SummaryCard/SummaryCardService.php', 'Combodo\\iTop\\Service\\TemporaryObjects\\TemporaryObjectConfig' => $baseDir . '/sources/Service/TemporaryObjects/TemporaryObjectConfig.php', 'Combodo\\iTop\\Service\\TemporaryObjects\\TemporaryObjectGC' => $baseDir . '/sources/Service/TemporaryObjects/TemporaryObjectGC.php', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 2c315e4f0e..fe840689d7 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -1039,6 +1039,7 @@ class ComposerStaticInitfc0e9e9dea11dcbb6272414776c30685 'Combodo\\iTop\\Service\\Router\\Exception\\RouteNotFoundException' => __DIR__ . '/../..' . '/sources/Service/Router/Exception/RouteNotFoundException.php', 'Combodo\\iTop\\Service\\Router\\Exception\\RouterException' => __DIR__ . '/../..' . '/sources/Service/Router/Exception/RouterException.php', 'Combodo\\iTop\\Service\\Router\\Router' => __DIR__ . '/../..' . '/sources/Service/Router/Router.php', + 'Combodo\\iTop\\Service\\Startup\\StartupService' => __DIR__ . '/../..' . '/sources/Service/Startup/StartupService.php', 'Combodo\\iTop\\Service\\SummaryCard\\SummaryCardService' => __DIR__ . '/../..' . '/sources/Service/SummaryCard/SummaryCardService.php', 'Combodo\\iTop\\Service\\TemporaryObjects\\TemporaryObjectConfig' => __DIR__ . '/../..' . '/sources/Service/TemporaryObjects/TemporaryObjectConfig.php', 'Combodo\\iTop\\Service\\TemporaryObjects\\TemporaryObjectGC' => __DIR__ . '/../..' . '/sources/Service/TemporaryObjects/TemporaryObjectGC.php', diff --git a/sources/Service/Startup/StartupService.php b/sources/Service/Startup/StartupService.php new file mode 100644 index 0000000000..89e48ec5e9 --- /dev/null +++ b/sources/Service/Startup/StartupService.php @@ -0,0 +1,58 @@ +RequireOnceItopFile('application/utils.inc.php'); + } + + public function testSetItopEnvironmentUsesDefaultWhenEnvironmentIsNull(): void + { + $bAllowCache = true; + $sEnv = StartupService::SetItopEnvironment(null, $bAllowCache); + $this->assertEquals(ITOP_DEFAULT_ENV, $sEnv); + $this->assertTrue($bAllowCache); + } + + public function testSetItopEnvironmentWithValidEnvironment(): void + { + $bAllowCache = true; + $sEnv = StartupService::SetItopEnvironment('test', $bAllowCache); + $this->assertEquals('test', $sEnv); + $this->assertFalse($bAllowCache); + } + + public function testSetItopEnvironmentThrowsForBuildEnvironment() + { + $bAllowCache = true; + $this->expectException(CoreException::class); + $this->expectExceptionMessage("Switching to environment 'test-build' is not allowed since it is a build environment"); + StartupService::SetItopEnvironment('test-build', $bAllowCache); + } + + public function testIsBuildEnvironment() + { + $this->assertTrue(StartupService::IsBuildEnvironment('test-build')); + $this->assertFalse(StartupService::IsBuildEnvironment('test')); + $this->assertFalse(StartupService::IsBuildEnvironment(null)); + } +} From 73fa6c8ff470f8da932bc4132fed083b7d30fb95 Mon Sep 17 00:00:00 2001 From: "lenaick.moreira" Date: Tue, 2 Jun 2026 08:54:54 +0200 Subject: [PATCH 2/2] =?UTF-8?q?N=C2=B09597=20-=20Switch=20environment=20to?= =?UTF-8?q?=20a=20build=20environment=20must=20not=20be=20available?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/startup.inc.php | 2 +- sources/Service/Startup/StartupService.php | 8 ++++---- .../Service/Startup/StartupServiceTest.php | 15 +++++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/application/startup.inc.php b/application/startup.inc.php index 11385173da..2eecc35eae 100644 --- a/application/startup.inc.php +++ b/application/startup.inc.php @@ -70,7 +70,7 @@ $sSwitchEnv = utils::ReadParam('switch_env', null); $bAllowCache = true; -$sEnv = StartupService::SetItopEnvironment($sSwitchEnv, $bAllowCache); +$sEnv = (new StartupService())->SetItopEnvironment($sSwitchEnv, $bAllowCache); $sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE; try { MetaModel::Startup($sConfigFile, false /* $bModelOnly */, $bAllowCache, false /* $bTraceSourceFiles */, $sEnv); diff --git a/sources/Service/Startup/StartupService.php b/sources/Service/Startup/StartupService.php index 89e48ec5e9..7b81c6a6ca 100644 --- a/sources/Service/Startup/StartupService.php +++ b/sources/Service/Startup/StartupService.php @@ -10,14 +10,14 @@ class StartupService { /** * @param string|null $sSwitchEnv - * @param bool $bAllowCache + * @param bool $bAllowCache - whether to allow cache or not (will be set to false if the environment is switched) * * @return string * @throws CoreException */ - public static function SetItopEnvironment(?string $sSwitchEnv, bool &$bAllowCache): string + public function SetItopEnvironment(?string $sSwitchEnv, bool &$bAllowCache): string { - if (static::IsBuildEnvironment($sSwitchEnv)) { + if ($this->IsBuildEnvironment($sSwitchEnv)) { $oException = new CoreException("Switching to environment '$sSwitchEnv' is not allowed since it is a build environment"); IssueLog::Exception("Trying to switch to environment '$sSwitchEnv' is not allowed since it is a build environment", $oException); throw $oException; @@ -51,7 +51,7 @@ public static function SetItopEnvironment(?string $sSwitchEnv, bool &$bAllowCach return $sEnv; } - public static function IsBuildEnvironment(?string $sEnv): bool + public function IsBuildEnvironment(?string $sEnv): bool { return $sEnv != null && str_ends_with($sEnv, '-build'); } diff --git a/tests/php-unit-tests/unitary-tests/sources/Service/Startup/StartupServiceTest.php b/tests/php-unit-tests/unitary-tests/sources/Service/Startup/StartupServiceTest.php index 881a5c1477..25f01f10ba 100644 --- a/tests/php-unit-tests/unitary-tests/sources/Service/Startup/StartupServiceTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/Service/Startup/StartupServiceTest.php @@ -8,16 +8,19 @@ class StartupServiceTest extends ItopTestCase { + private StartupService $oStartupService; + protected function setUp(): void { parent::setUp(); $this->RequireOnceItopFile('application/utils.inc.php'); + $this->oStartupService = new StartupService(); } public function testSetItopEnvironmentUsesDefaultWhenEnvironmentIsNull(): void { $bAllowCache = true; - $sEnv = StartupService::SetItopEnvironment(null, $bAllowCache); + $sEnv = $this->oStartupService->SetItopEnvironment(null, $bAllowCache); $this->assertEquals(ITOP_DEFAULT_ENV, $sEnv); $this->assertTrue($bAllowCache); } @@ -25,7 +28,7 @@ public function testSetItopEnvironmentUsesDefaultWhenEnvironmentIsNull(): void public function testSetItopEnvironmentWithValidEnvironment(): void { $bAllowCache = true; - $sEnv = StartupService::SetItopEnvironment('test', $bAllowCache); + $sEnv = $this->oStartupService->SetItopEnvironment('test', $bAllowCache); $this->assertEquals('test', $sEnv); $this->assertFalse($bAllowCache); } @@ -35,13 +38,13 @@ public function testSetItopEnvironmentThrowsForBuildEnvironment() $bAllowCache = true; $this->expectException(CoreException::class); $this->expectExceptionMessage("Switching to environment 'test-build' is not allowed since it is a build environment"); - StartupService::SetItopEnvironment('test-build', $bAllowCache); + $this->oStartupService->SetItopEnvironment('test-build', $bAllowCache); } public function testIsBuildEnvironment() { - $this->assertTrue(StartupService::IsBuildEnvironment('test-build')); - $this->assertFalse(StartupService::IsBuildEnvironment('test')); - $this->assertFalse(StartupService::IsBuildEnvironment(null)); + $this->assertTrue($this->oStartupService->IsBuildEnvironment('test-build')); + $this->assertFalse($this->oStartupService->IsBuildEnvironment('test')); + $this->assertFalse($this->oStartupService->IsBuildEnvironment(null)); } }