diff --git a/application/logindefault.class.inc.php b/application/logindefault.class.inc.php index 85f8e4609..508a2623e 100644 --- a/application/logindefault.class.inc.php +++ b/application/logindefault.class.inc.php @@ -35,8 +35,6 @@ class LoginDefaultBefore extends AbstractLoginFSMExtension if ($index !== false) { // Force login mode Session::Set('login_mode', $sProposedLoginMode); - } elseif (!Session::IsSet('login_mode')) { - Session::Unset('login_mode'); } return LoginWebPage::LOGIN_FSM_CONTINUE; } diff --git a/tests/php-unit-tests/unitary-tests/application/LoginDefaultBeforeTest.php b/tests/php-unit-tests/unitary-tests/application/LoginDefaultBeforeTest.php new file mode 100644 index 000000000..96c05d46e --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/application/LoginDefaultBeforeTest.php @@ -0,0 +1,69 @@ +RequireOnceItopFile('application/logindefault.class.inc.php'); + } + + protected function tearDown(): void + { + parent::tearDown(); + Session::$bAllowCLI = false; + } + + public function testOnStart_NoLoginModeRequiredNorStoredInSession() + { + $this->CallStartAndCheckLoginModeStored(null, null); + } + + public function testOnStart_StoreLoginModeRequiredWhenConfigured() + { + MetaModel::GetConfig()->SetAllowedLoginTypes(["SAML"]); + $_REQUEST['login_mode'] = "SAML"; + + $this->CallStartAndCheckLoginModeStored("SAML", null); + } + + public function testOnStart_SwitchLoginModeWhenNewOneRequiredIsConfigured() + { + MetaModel::GetConfig()->SetAllowedLoginTypes(["SAML"]); + $_REQUEST['login_mode'] = "SAML"; + + $this->CallStartAndCheckLoginModeStored("SAML"); + } + + public function testOnStart_PreserveCurrentLoginModeInSessionWhenNewOneRequiredIsNotConfigured() + { + $_REQUEST['login_mode'] = "SAML"; + + $this->CallStartAndCheckLoginModeStored("previous_mode"); + } + + public function testOnStart_PreserveCurrentLoginModeInSessionWhenNoOtherRequired() + { + $this->CallStartAndCheckLoginModeStored('previous_mode'); + } + + private function CallStartAndCheckLoginModeStored($expected, ?string $sPreviousLoginMode = 'previous_mode') + { + Session::Set('login_mode', $sPreviousLoginMode); + + $iErrorCode = 666; + $res = $this->InvokeNonPublicMethod(LoginDefaultBefore::class, 'OnStart', new LoginDefaultBefore(), [&$iErrorCode]); + $this->assertEquals(LoginWebPage::EXIT_CODE_OK, $iErrorCode); + $this->assertEquals(LoginWebPage::LOGIN_FSM_CONTINUE, $res); + $this->assertEquals($expected, Session::Get('login_mode')); + } +}