From 099cc025bfa6b82fa970223ac8f5b8c8861bbd41 Mon Sep 17 00:00:00 2001 From: Terri-Anne Eeles Date: Fri, 18 Oct 2024 13:31:27 -0700 Subject: [PATCH 1/2] Fix for issue #680: Add testing for the bug Refactor \FeatureContext::assertBackendLoggedOut to also ensure the login page displays the login form. Add a new testing step definition that logs out via the logout url rather than fast logout. Add a new test to backend_login.feature to ensure logout via the url is working correctly. --- features/backend_login.feature | 8 +++++++- features/bootstrap/FeatureContext.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/features/backend_login.feature b/features/backend_login.feature index e0942dc9..cb1cc791 100644 --- a/features/backend_login.feature +++ b/features/backend_login.feature @@ -8,8 +8,14 @@ Feature: Backend login/logout Given I am logged in as a user with the "authenticated user" role Then I should be logged in on the backend - Scenario: Logout on the backend + Scenario: Logout on the backend via fast logout Given I am logged in as a user with the "authenticated user" role And I am logged in on the backend When I log out Then I should be logged out on the backend + + Scenario: Logout on the backend via url + Given I am logged in as a user with the "authenticated user" role + And I am logged in on the backend + When I log out via the logout url + Then I should be logged out on the backend diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index e99e128e..560353b2 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -229,6 +229,20 @@ public function assertBackendLoggedOut() if (!\Drupal::currentUser()->isAnonymous()) { throw new \LogicException('User is still logged in on the backend.'); } + // Visit login page and ensure login form is present. + $this->getSession()->visit($this->locatePath($this->getDrupalText('login_url'))); + $element = $this->getSession()->getPage(); + $element->fillField($this->getDrupalText('username_field'), 'foo'); + } + + /** + * Logs out via the logout url rather than fast logout. + * + * @Then I log out via the logout url + */ + public function logoutViaUrl() + { + $this->logout(false); } /** From 9deecf33262ea9e637ab03002a27aba7aeb7f982 Mon Sep 17 00:00:00 2001 From: Terri-Anne Eeles Date: Fri, 18 Oct 2024 13:32:48 -0700 Subject: [PATCH 2/2] Fix for issue #680: Refactor DrupalAuthenticationManager::logout to fix Add new Drupal Text name and url for the logout confirmation page. Refactor DrupalAuthenticationManager::logout to check if a logout confirmation page is present and if it is click submit. --- .../Manager/DrupalAuthenticationManager.php | 16 +++++++++++++++- .../ServiceContainer/DrupalExtension.php | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php b/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php index 2b5ee3f3..c9da8a76 100644 --- a/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php +++ b/src/Drupal/DrupalExtension/Manager/DrupalAuthenticationManager.php @@ -61,6 +61,14 @@ protected function getLoginSubmitElement(DocumentElement $element) return $element->findButton($this->getDrupalText('log_in')); } + /** + * Helper to get the submit element for the logout confirmation form. + */ + protected function getLogoutConfirmSubmitElement(DocumentElement $element) + { + return $element->findButton($this->getDrupalText('log_out')); + } + /** * {@inheritdoc} */ @@ -102,7 +110,13 @@ public function logIn(\stdClass $user) */ public function logout() { - $this->getSession()->visit($this->locatePath($this->getDrupalText('logout_url'))); + $session = $this->getSession(); + $session->visit($this->locatePath($this->getDrupalText('logout_url'))); + // Check to see if the user is on the logout confirm page (10.3+). + if ($session->getCurrentUrl() === $this->locatePath($this->getDrupalText('logout_confirm_url'))) { + $submit = $this->getLogoutConfirmSubmitElement($session->getPage()); + $submit->click(); + } $this->userManager->setCurrentUser(false); // Log the user out on the backend if possible. diff --git a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php index a72a406f..35464119 100644 --- a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php +++ b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php @@ -131,6 +131,7 @@ public function configure(ArrayNodeDefinition $builder) 'Text strings, such as Log out or the Username field can be altered via behat.yml if they vary from the default values.' . PHP_EOL . ' login_url: "/user"' . PHP_EOL . ' logout_url: "/user/logout"' . PHP_EOL + . ' logout_confirm_url: "/user/logout/confirm"' . PHP_EOL . ' log_out: "Sign out"' . PHP_EOL . ' log_in: "Sign in"' . PHP_EOL . ' password_field: "Enter your password"' . PHP_EOL @@ -144,6 +145,9 @@ public function configure(ArrayNodeDefinition $builder) scalarNode('logout_url')-> defaultValue('/user/logout')-> end()-> + scalarNode('logout_confirm_url')-> + defaultValue('/user/logout/confirm')-> + end()-> scalarNode('log_in')-> defaultValue('Log in')-> end()->