You are here

public function XMLSitemapUnitTest::testSwitchUser in XML sitemap 7.2

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

Tests for xmlsitemap_switch_user and xmlsitemap_restore_user().

File

./xmlsitemap.test, line 592
Unit tests for the xmlsitemap.

Class

XMLSitemapUnitTest
XML Sitemap UnitTest.

Code

public 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_query("SELECT MAX(uid) FROM {users}")
    ->fetchField() + 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);
}