From f0ecbb4645a0a0abb9128aa66d27823e7516c7ec Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Thu, 17 May 2018 15:33:10 -0700 Subject: [PATCH 1/3] Authenticate user in the backend bootstrap process on login. - Fixes #488 --- composer.json | 2 +- features/backend_login.feature | 15 ++++ features/bootstrap/FeatureContext.php | 37 ++++++++++ src/Drupal/DrupalDriverManager.php | 68 +++++------------- src/Drupal/DrupalDriverManagerInterface.php | 69 +++++++++++++++++++ .../Manager/DrupalAuthenticationManager.php | 30 +++++++- .../ServiceContainer/config/services.yml | 1 + 7 files changed, 171 insertions(+), 51 deletions(-) create mode 100644 features/backend_login.feature create mode 100644 src/Drupal/DrupalDriverManagerInterface.php diff --git a/composer.json b/composer.json index 734f9465..fb603de5 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "behat/mink-extension": "~2.0", "behat/mink-goutte-driver": "~1.0", "behat/mink-selenium2-driver": "~1.1", - "drupal/drupal-driver": "^2.0.0", + "drupal/drupal-driver": "dev-186-backend-auth", "symfony/dependency-injection": "~3.0", "symfony/event-dispatcher": "~3.0" }, diff --git a/features/backend_login.feature b/features/backend_login.feature new file mode 100644 index 00000000..93a5926c --- /dev/null +++ b/features/backend_login.feature @@ -0,0 +1,15 @@ +@api @d8 +Feature: Backend login/logout + In order to prove that backend authentication is working + As a developer + I need to utilize the backend login functionality of the authentication manager + + Scenario: Log a user in on the backend + 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 + 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 diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 196b4894..4546c897 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -190,6 +190,43 @@ public function assertAuthenticatedByUsernameAndPassword($name, $password) { $this->login($user); } + /** + * Verifies a user is logged in on the backend. + * + * @Then I should be logged in on the backend + * @Then I am logged in on the backend + */ + public function assertBackendLogin() + { + if (!$user = $this->getUserManager()->getCurrentUser()) { + throw new \LogicException('No current user in the user manager.'); + } + if (!$account = \Drupal::entityTypeManager()->getStorage('user')->load($user->uid)) { + throw new \LogicException('No user found in the system.'); + } + if (!$account->id()) { + throw new \LogicException('Current user is anonymous.'); + } + if ($account->id() != \Drupal::currentUser()->id()) { + throw new \LogicException('User logged in on the backend does not match current user.'); + } + } + + /** + * Verifies there is no user logged in on the backend. + * + * @Then I should be logged out on the backend + */ + public function assertBackendLoggedOut() + { + if ($this->getUserManager()->getCurrentUser()) { + throw new \LogicException('User is still logged in in the manager.'); + } + if (!\Drupal::currentUser()->isAnonymous()) { + throw new \LogicException('User is still logged in on the backend.'); + } + } + /** * From here down is the Behat FeatureContext. * diff --git a/src/Drupal/DrupalDriverManager.php b/src/Drupal/DrupalDriverManager.php index c4d990d3..279d80cd 100644 --- a/src/Drupal/DrupalDriverManager.php +++ b/src/Drupal/DrupalDriverManager.php @@ -13,7 +13,7 @@ /** * Drupal driver manager. */ -class DrupalDriverManager +class DrupalDriverManager implements DrupalDriverManagerInterface { /** @@ -50,33 +50,18 @@ public function __construct(array $drivers = array()) } } - /** - * Register a new driver. - * - * @param string $name - * Driver name. - * @param \Drupal\Driver\DriverInterface $driver - * An instance of a DriverInterface. - */ + /** + * {@inheritdoc} + */ public function registerDriver($name, DriverInterface $driver) { $name = strtolower($name); $this->drivers[$name] = $driver; } - /** - * Return a registered driver by name, or the default driver. - * - * @param string $name - * The name of the driver to return. If omitted the default driver is - * returned. - * - * @return \Drupal\Driver\DriverInterface - * The requested driver. - * - * @throws \InvalidArgumentException - * Thrown when the requested driver is not registered. - */ + /** + * {@inheritdoc} + */ public function getDriver($name = null) { $name = strtolower($name) ?: $this->defaultDriverName; @@ -99,15 +84,9 @@ public function getDriver($name = null) return $driver; } - /** - * Set the default driver name. - * - * @param string $name - * Default driver name to set. - * - * @throws \InvalidArgumentException - * Thrown when the driver is not registered. - */ + /** + * {@inheritdoc} + */ public function setDefaultDriverName($name) { $name = strtolower($name); @@ -119,34 +98,25 @@ public function setDefaultDriverName($name) $this->defaultDriverName = $name; } - /** - * Returns all registered drivers. - * - * @return \Drupal\Driver\DriverInterface[] - * An array of drivers. - */ + /** + * {@inheritdoc} + */ public function getDrivers() { return $this->drivers; } - /** - * Sets the Behat Environment. - * - * @param \Behat\Testwork\Environment\Environment $environment - * The Behat Environment to set. - */ + /** + * {@inheritdoc} + */ public function setEnvironment(Environment $environment) { $this->environment = $environment; } - /** - * Returns the Behat Environment. - * - * @return \Behat\Testwork\Environment\Environment - * The Behat Environment. - */ + /** + * {@inheritdoc} + */ public function getEnvironment() { return $this->environment; diff --git a/src/Drupal/DrupalDriverManagerInterface.php b/src/Drupal/DrupalDriverManagerInterface.php new file mode 100644 index 00000000..2d678307 --- /dev/null +++ b/src/Drupal/DrupalDriverManagerInterface.php @@ -0,0 +1,69 @@ +setMink($mink); $this->userManager = $drupalUserManager; + $this->driverManager = $driverManager; $this->setMinkParameters($minkParameters); $this->setDrupalParameters($drupalParameters); } @@ -68,6 +81,11 @@ public function logIn(\stdClass $user) } $this->userManager->setCurrentUser($user); + + // Log the user in on the backend if possible. + if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) { + $this->driverManager->getDriver()->login($user); + } } /** @@ -77,6 +95,11 @@ public function logout() { $this->getSession()->visit($this->locatePath('/user/logout')); $this->userManager->setCurrentUser(false); + + // Log the user out on the backend if possible. + if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) { + $this->driverManager->getDriver()->logout(); + } } /** @@ -135,5 +158,10 @@ public function fastLogout() $session->reset(); } $this->userManager->setCurrentUser(false); + + // Log the user out on the backend if possible. + if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) { + $this->driverManager->getDriver()->logout(); + } } } diff --git a/src/Drupal/DrupalExtension/ServiceContainer/config/services.yml b/src/Drupal/DrupalExtension/ServiceContainer/config/services.yml index f763a170..0247c44c 100644 --- a/src/Drupal/DrupalExtension/ServiceContainer/config/services.yml +++ b/src/Drupal/DrupalExtension/ServiceContainer/config/services.yml @@ -46,6 +46,7 @@ services: arguments: - "@mink" - "@drupal.user_manager" + - "@drupal.drupal" - "%mink.parameters%" - "%drupal.parameters%" drupal.user_manager: From 8cd5ff481aab4ea0595a3c483fe330ea3738c489 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Tue, 31 Dec 2019 09:57:33 -0800 Subject: [PATCH 2/3] Add to CHANGELOG.md. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78d753cb..959432f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + * [#488](https://github.com/jhedstrom/drupalextension/issues/488) Authenticate user in the backend bootstrap process on login. ## [4.0.1] 2019-10-08 ### Fixed * [#552](https://github.com/jhedstrom/drupalextension/issue/552) Remove hard-coded symfony/event-dispatcher requirement. From e000511317d2739fa350eca0a336369993cb3d84 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Tue, 31 Dec 2019 11:10:47 -0800 Subject: [PATCH 3/3] Require drupal driver 2.1. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7ca95fee..101da69d 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "behat/mink-extension": "~2.0", "behat/mink-goutte-driver": "~1.0", "behat/mink-selenium2-driver": "~1.1", - "drupal/drupal-driver": "dev-186-backend-auth", + "drupal/drupal-driver": "^2.1.0", "symfony/browser-kit": "^3.4", "symfony/dependency-injection": "~3.0", "symfony/translation": "^3.4"