Skip to content

Commit

Permalink
Clone drupalLogin without assert
Browse files Browse the repository at this point in the history
  • Loading branch information
attiks committed Dec 10, 2024
1 parent 93bde1f commit ce88071
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ce88071

Please sign in to comment.