In my automated tests, I create some users.

  debug('moderator_user: '.$this->moderator_user->uid);
  debug('editor_user: '.$this->editor_user->uid);


'moderator_user: 2'
'editor_user: 4'

So I would think that

global $user;
debug('currently logged in as '.$user->uid);

would output

'currently logged in as: 4'

but instead it shows,

'currently logged in as: 1'

It seems to be correctly logging user 4 because

$edit = array(
  'title' => $this->randomName(),
$this->drupalPost("node/add/{$this->content_type}", $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit['title']);

debug('Draft was created by user with uid '.$node->workbench_moderation['current']->uid);


'Draft was created by user with uid 4'

So what's going on?


It happens exactly what described in the documentation page for DrupalWebTestCase::drupalLogin().

Please note that neither the global $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:

  // Create a user.
  $account = $this->drupalCreateUser(array());
  // Load real user object.
  $pass_raw = $account->pass_raw;
  $account = user_load($account->uid);
  $account->pass_raw = $pass_raw;

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.