I have SimpleTest code that goes something like this:

    // Create a biography for the basic user.
    $this->drupalLoginUser($this->basic_user);
    $node = $this->bioCreateBio();
    
    // Have user attempt to create another one; confirm they're redirected back
    // to their existing bio node.
    $this->drupalGet('node/add/bio');
    $this->assertText($node->title, t("Checking for redirection to existing bio node"));

    // Create a biography for the admin user.
    $this->drupalLoginUser($this->admin_user);
    $node = $this->bioCreateBio();

This results in multiple failures, because when a logged in user attempts to go to user/login, they receive an access denied. Therefore, there's no form to fill out the values.

It'd be nice if drupalLoginUser would recognize if a user's already logged in, and if so, terminate the session in advance of calling up the form.

I lost a bunch of time on this trying to figure out why my simple calls to DrupalTestCase's built-in methods weren't succeeding.

Comments

webchick’s picture

Category: feature » bug

Hm. I'm upgrading this to a bug report, as the only work around appears to be:

$this->drupalLoginUser($account1);
# $this->drupalGet() does NOT work...?
$this->get(url('logout', NULL, NULL, TRUE));
$this->drupalLoginUser($account2);

Something's not quite right here.

Rok Žlender’s picture

It makes sense that you can't login twice in a row.
This code works for me.

    $this->drupalLoginUser();
    $this->drupalGet('logout');
    $this->drupalLoginUser();

But we could make our lives easier a bit. So how can we be sure that user is logged in. Can we check for 'log out' text on page would this be reliable. We could just put another param in drupalLoginUser with TRUE for log out first and FALSE as default for no log out. What do you think?

webchick’s picture

BIZARRE. I swear that this:

  function testLoginTwice() {
    $test_user = $this->drupalCreateUserRolePerm();
    $test_user_2 = $this->drupalCreateUserRolePerm();
    $this->drupalLoginUser($test_user);
    $this->drupalGet('logout');
    $this->drupalLoginUser($test_user_2);
  }

Spat out failures the other day, but I guess I'm wrong. :)

As for function parameters in drupalLoginUser(), I'd kind of rather the function just be smart and do what I mean. ;)

I think it's safe to assume that when I do:

    $this->drupalLoginUser($test_user);
    // do some stuff as $test_user.
    $this->drupalLoginUser($test_user_2);
    // do some stuff as $test_user_2.

What I mean by that second call is, "I don't want to be logged in as $test_user anymore. Please log me in as $test_user_2 now."

So the function ought to just override the existing session, if it exists, with one for the new user, at least imo. We could make this a flag I suppose, but I'm trying to figure out under what circumstances having to type in an extra line of code each time you switch users is considered a "feature."

However, if you still want to require an explicit log out between logins, then an alternative would be creating a drupalLogoutUser() would be much more intuitive than drupalGet('logout') as the "opposite" or drupalLoginUser().

(As a side note, now that I write it out like this, drupalLoginUser() is another one of those functions like drupalPostReqest() that could stand to drop a word at the end and still be descrptiive. I'll go file a separate patch for that.)

boombatower’s picture

Status: Active » Fixed

Committed.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.