You are here

function UserCancelTestCase::testUserCancelUid1 in Drupal 7

Tests that user account for uid 1 cannot be cancelled.

This should never be possible, or the site owner would become unable to administer the site.

File

modules/user/user.test, line 910
Tests for user.module.

Class

UserCancelTestCase
Test cancelling a user.

Code

function testUserCancelUid1() {

  // Update uid 1's name and password to we know it.
  $password = user_password();
  require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  $account = array(
    'name' => 'user1',
    'pass' => user_hash_password(trim($password)),
  );

  // We cannot use user_save() here or the password would be hashed again.
  db_update('users')
    ->fields($account)
    ->condition('uid', 1)
    ->execute();

  // Reload and log in uid 1.
  $user1 = user_load(1, TRUE);
  $user1->pass_raw = $password;

  // Try to cancel uid 1's account with a different user.
  $this->admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->drupalLogin($this->admin_user);
  $edit = array(
    'operation' => 'cancel',
    'accounts[1]' => TRUE,
  );
  $this
    ->drupalPost('admin/people', $edit, t('Update'));

  // Verify that uid 1's account was not cancelled.
  $user1 = user_load(1, TRUE);
  $this
    ->assertEqual($user1->status, 1, 'User #1 still exists and is not blocked.');
}