Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.27 diff -u -p -r1.27 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 18 Jul 2008 07:30:34 -0000 1.27 +++ modules/simpletest/drupal_web_test_case.php 26 Jul 2008 23:31:42 -0000 @@ -562,6 +562,56 @@ class DrupalWebTestCase { } /** + * Set the current user stored in $GLOBALS['user']. + * + * When this function is called for the first time the user is saved, + * subsequent calls that set a user do not overwrite the value of the + * original user. The user is restored when this function is called with + * a NULL value. + * + * @param $new_user User to switch to, either UID or user object. If no + * nothing is provided the user will be reset to the original. + * @return Current user object. + */ + function drupalSetLocalUser($new_user = NULL) { + global $user; + static $user_original; + + if (!$new_user) { + if (isset($user_original)) { + // Restore the original user. + $user = $user_original; + unset($user_original); + session_save_session(TRUE); + } + return $user; + } + + // Backup the user the first time this is called. + if (!isset($user_original)) { + $user_original = $user; + session_save_session(FALSE); + } + + if (is_int($user)) { + $user = user_load(array('uid' => $new_user)); + } + else { + $user = $new_user; + } + return $user; + } + + /** + * Restore the user that was originally loaded. + * + * @return Current user. + */ + function drupalRestoreLocalUser() { + return $this->drupalSetLocalUser(); + } + + /** * Logs in a user with the internal browser. If already logged in then logs the current * user out before logging in the specified user. If no user is specified then a new * user will be created and logged in.