You are here

function XMLSitemapUnitTest::testSwitchUser in XML sitemap 6.2

Same name and namespace in other branches
  1. 7.2 xmlsitemap.test \XMLSitemapUnitTest::testSwitchUser()

Tests for xmlsitemap_switch_user and xmlsitemap_restore_user().

File

./xmlsitemap.test, line 492
Unit tests for the xmlsitemap module.

Class

XMLSitemapUnitTest

Code

function testSwitchUser() {
  global $user;
  $original_user = $user;
  $new_user = $this
    ->drupalCreateUser();

  // Switch to a new valid user.
  $this
    ->assertEqual(xmlsitemap_switch_user($new_user), TRUE);
  $this
    ->assertEqual($user->uid, $new_user->uid);

  // Switch again to the anonymous user.
  $this
    ->assertEqual(xmlsitemap_switch_user(0), TRUE);
  $this
    ->assertEqual($user->uid, 0);

  // Switch again to the new user.
  $this
    ->assertEqual(xmlsitemap_switch_user($new_user->uid), TRUE);
  $this
    ->assertEqual($user->uid, $new_user->uid);

  // Test that after two switches the original user was restored.
  $this
    ->assertEqual(xmlsitemap_restore_user(), TRUE);
  $this
    ->assertEqual($user->uid, $original_user->uid);

  // Attempt to switch to the same user.
  $this
    ->assertEqual(xmlsitemap_switch_user($original_user->uid), FALSE);
  $this
    ->assertEqual($user->uid, $original_user->uid);
  $this
    ->assertEqual(xmlsitemap_restore_user(), FALSE);
  $this
    ->assertEqual($user->uid, $original_user->uid);

  // Attempt to switch to an invalid user ID.
  $invalid_uid = db_result(db_query("SELECT MAX(uid) FROM {users}")) + 100;
  $this
    ->assertEqual(xmlsitemap_switch_user($invalid_uid), FALSE);
  $this
    ->assertEqual($user->uid, $original_user->uid);
  $this
    ->assertEqual(xmlsitemap_restore_user(), FALSE);
  $this
    ->assertEqual($user->uid, $original_user->uid);

  // Attempt user switching when the original user is anonymous.
  $user = drupal_anonymous_user();
  $this
    ->assertEqual(xmlsitemap_switch_user(0), FALSE);
  $this
    ->assertEqual($user->uid, 0);
  $this
    ->assertEqual(xmlsitemap_restore_user(), FALSE);
  $this
    ->assertEqual($user->uid, 0);
}