4

How do you retrieve the current 'simpletest' user during a simpletest.

During a simpletest is the $user GLOBAL from the test environment or the live environment?

1 Answer 1

3

Inside the test method?

No idea what in $GLOBALS['user'] is, but you certainly can't rely on it. You are never "logged in" in that context, you are only logged in when doing requests with drupalGet()/drupalPost().

If you used drupalLogin(), you can look for $this->loggedInUser. If not, then it's not possible, other than maybe doing a GET to /user and then trying to get the username from title.

Inside a page callback, $GLOBALS['user'] is the current user, just like you'd expect.

1
  • Thank you @Berdir. $this->loggedInUser and user_load did the trick and makes for a way cleaner test class.
    – Citricguy
    Commented Dec 18, 2011 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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