From ce880718f5b412f077c905888985ad18880d6eb9 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 10 Dec 2024 16:35:18 +0100 Subject: [PATCH] Clone drupalLogin without assert --- .../tests/src/ExistingSite/RwReportBase.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php index 905bdb5e7..f16ebb8d9 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\reliefweb_entities\ExistingSite; +use Drupal\Core\Session\AccountInterface; use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\User; @@ -51,4 +52,58 @@ protected function createUserIfNeeded($id, $name, array $extra = []) : User { return $user; } + /** + * Logs in a user using the Mink controlled browser. + * + * If a user is already logged in, then the current user is logged out before + * logging in the specified user. + * + * Note that neither the current user nor the passed-in user object is + * populated with data of the logged in user. If you need full access to the + * user object after logging in, it must be updated manually. If you also need + * access to the plain-text password of the user (set by drupalCreateUser()), + * e.g. to log in the same user again, then it must be re-assigned manually. + * For example: + * @code + * // Create a user. + * $account = $this->drupalCreateUser([]); + * $this->drupalLogin($account); + * // Load real user object. + * $pass_raw = $account->passRaw; + * $account = User::load($account->id()); + * $account->passRaw = $pass_raw; + * @endcode + * + * @param \Drupal\Core\Session\AccountInterface $account + * User object representing the user to log in. + * + * @see drupalCreateUser() + */ + protected function drupalLogin(AccountInterface $account) { + if ($this->loggedInUser) { + $this->drupalLogout(); + } + + if ($this->useOneTimeLoginLinks) { + // Reload to get latest login timestamp. + $storage = \Drupal::entityTypeManager()->getStorage('user'); + /** @var \Drupal\user\UserInterface $accountUnchanged */ + $accountUnchanged = $storage->loadUnchanged($account->id()); + $login = user_pass_reset_url($accountUnchanged) . '/login?destination=user/' . $account->id(); + $this->drupalGet($login); + } + else { + $this->drupalGet(Url::fromRoute('user.login')); + $this->submitForm([ + 'name' => $account->getAccountName(), + 'pass' => $account->passRaw, + ], 'Log in'); + } + + // @see ::drupalUserIsLoggedIn() + $account->sessionId = $this->getSession()->getCookie(\Drupal::service('session_configuration')->getOptions(\Drupal::request())['name']); + + $this->loggedInUser = $account; + $this->container->get('current_user')->setAccount($account); + } }