You are here

function UserCancelTest::testUserCancelUid1 in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserCancelTest.php \Drupal\user\Tests\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

core/modules/user/src/Tests/UserCancelTest.php, line 102
Contains \Drupal\user\Tests\UserCancelTest.

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

function testUserCancelUid1() {
  $user_storage = $this->container
    ->get('entity.manager')
    ->getStorage('user');
  \Drupal::service('module_installer')
    ->install(array(
    'views',
  ));
  \Drupal::service('router.builder')
    ->rebuild();

  // Update uid 1's name and password to we know it.
  $password = user_password();
  $account = array(
    'name' => 'user1',
    'pass' => $this->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_field_data')
    ->fields($account)
    ->condition('uid', 1)
    ->execute();

  // Reload and log in uid 1.
  $user_storage
    ->resetCache(array(
    1,
  ));
  $user1 = $user_storage
    ->load(1);
  $user1->pass_raw = $password;

  // Try to cancel uid 1's account with a different user.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);
  $edit = array(
    'action' => 'user_cancel_user_action',
    'user_bulk_form[0]' => TRUE,
  );
  $this
    ->drupalPostForm('admin/people', $edit, t('Apply'));

  // Verify that uid 1's account was not cancelled.
  $user_storage
    ->resetCache(array(
    1,
  ));
  $user1 = $user_storage
    ->load(1);
  $this
    ->assertTrue($user1
    ->isActive(), 'User #1 still exists and is not blocked.');
}