function UserCancelTest::testUserCancelUid1

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

drupal/core/modules/user/lib/Drupal/user/Tests/UserCancelTest.php, line 68
Definition of Drupal\user\Tests\UserCancelTest.

Class

UserCancelTest
Test cancelling a user.

Namespace

Drupal\user\Tests

Code

function testUserCancelUid1() {

  // Update uid 1's name and password to we know it.
  $password = user_password();
  $account = array(
    'name' => 'user1',
    'pass' => drupal_container()
      ->get('password')
      ->hash(trim($password)),
  );

  // We cannot use $account->save() here, because this would result in the
  // password being 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.');
}