0

How can I get the information for the super user (uid 1) in a functional test?

I want to test the external auth logic I have written, and one of the steps is authenticating the Drupal superuser locally instead of in the remote system. I am not seeing a way to get the username/password of that user in order to simulate a login.

Test:

  /**
   * The superuser should not authenticate to Netforum.
   */
  public function testSuperUserUsesDrupalAuthentication() {
    $this->drupalGet('user/login');
    $edit = ['name' => $this->rootUser->getEmail(), 'pass' => $this->rootUser->pass_raw];
    $this->drupalPostForm(NULL, $edit, t('Log in'));

    $user = \Drupal::currentUser();
    $this->assertEquals(1, $user->id());
  }
  • Hi @Kevin, did you get a chance to try out my suggestion below? – Stefanos Petrakis Aug 15 '17 at 7:07
0

The problem occurs because the password is not set to start with. You can give this a try:

// Load real user object from UserSession object.
$admin_account = User::load($this->rootUser->id());
// The password is empty.
$this->assertFalse($admin_account->pass_raw);
// Set the password and save the User.
$new_password = $this->randomString();
$admin_account->setPassword($new_password);
$admin_account->save();
// Set the pass_raw directly on the User.
$admin_account->pass_raw = $new_password;
$this->drupalLogin($admin_account);

This is for the WebTestBase, if you use the BrowserTestBase, you should use the following (pass_raw ≠ passRaw)

// Load real user object from UserSession object.
$admin_account = User::load($this->rootUser->id());
// The password is empty.
$this->assertFalse($admin_account->passRaw);
// Set the password and save the User.
$new_password = $this->randomString();
$admin_account->setPassword($new_password);
$admin_account->save();
// Set the passRaw directly on the User.
$admin_account->passRaw = $new_password;
$this->drupalLogin($admin_account);

Good luck!

References

The details regarding the passRaw/pass_raw setting on the User object are available here:

| improve this answer | |
1

Looking into BrowserTestBase I accidentally found $this->rootUser->name and $this->rootUser->pass_raw last week. These are being used in installParameters(). I have not tested it, but I suppose you should be able to use those in other functions as well.

rootUser appears to be a full AccountInterface object, so getName() and other methods should work as well..

| improve this answer | |
  • Gotcha, I updated my question with the test after you provided that information. It is returning 0, instead of 1. Am I getting the user wrong? – Kevin Jul 28 '17 at 14:22
  • I am not sure, I did not dig that deep. You can compare \Drupal::currentUser() after $this->drupalLogin($this->rootUser);. I'd expect it to be 1 though.... – Neograph734 Jul 28 '17 at 14:25
  • For some reason drupalLogin fails because it can't find the "User %user successfully logged in." message on the page. So I use drupalPostForm, to simulate a log in – Kevin Jul 28 '17 at 14:32
  • But if you get user 0... That is the anonymous user. I get the idea your user is not getting logged in. (Bad credentials?) What if you use the name instead of the email address? – Neograph734 Jul 28 '17 at 14:33
  • I am enforcing the email address, as that is what the remote system requires. My module has amended the login form to reflect that (but I did not change the form field key of "name"). It works when I do it manually, I just want to automate it. – Kevin Jul 28 '17 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.