You are here

function LinkCheckerImpersonatingUserTestCase::testLinkCheckerImpersonateUser in Link checker 7

File

./linkchecker.test, line 607
Test file for Link checker module.

Class

LinkCheckerImpersonatingUserTestCase
Test case for impersonating users.

Code

function testLinkCheckerImpersonateUser() {
  global $user;
  $original_user = $user;

  // If not currently logged in, use linkchecker_impersonate_user() to switch to
  // user 1. If logged in, switch to the anonymous user instead.
  if (user_is_anonymous()) {
    linkchecker_impersonate_user(1);
  }
  else {
    linkchecker_impersonate_user(0);
  }

  // Verify that the active user has changed, and that session saving is
  // disabled.
  $this
    ->assertEqual($user->uid, $original_user->uid == 0 ? 1 : 0, 'User switched');
  $this
    ->assertFalse(drupal_save_session(), 'Session saving is disabled.');

  // Perform a second (nested) impersonation.
  linkchecker_impersonate_user(1);
  $this
    ->assertEqual($user->uid, 1, 'User switched.');

  // Revert to the user which was active between the first and second
  // impersonation attempt.
  linkchecker_revert_user();

  // Since we are still impersonating the user from the first attempt,
  // session handling still needs to be disabled.
  $this
    ->assertEqual($user->uid, $original_user->uid == 0 ? 1 : 0, 'User switched.');
  $this
    ->assertFalse(drupal_save_session(), 'Session saving is disabled.');

  // Revert to the original user which was active before the first
  // impersonation attempt.
  linkchecker_revert_user();

  // Assert that the original user is the active user again, and that session
  // saving has been re-enabled.
  $this
    ->assertEqual($user->uid, $original_user->uid, 'Original user successfully restored.');

  // Simpletest uses linkchecker_impersonate_user() too, revert the impersonation by
  // Simpletest to enable session saving again. This is safe because calling
  // linkchecker_revert_user() too often simply results in returning the active user.
  linkchecker_revert_user();
  $this
    ->assertTrue(drupal_save_session(), 'Session saving is enabled.');
}