You are here

function UserCancelTest::testMassUserCancelByAdmin 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::testMassUserCancelByAdmin()

Create an administrative user and mass-delete other users.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

function testMassUserCancelByAdmin() {
  \Drupal::service('module_installer')
    ->install(array(
    'views',
  ));
  \Drupal::service('router.builder')
    ->rebuild();
  $this
    ->config('user.settings')
    ->set('cancel_method', 'user_cancel_reassign')
    ->save();
  $user_storage = $this->container
    ->get('entity.manager')
    ->getStorage('user');

  // Enable account cancellation notification.
  $this
    ->config('user.settings')
    ->set('notify.status_canceled', TRUE)
    ->save();

  // Create administrative user.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);

  // Create some users.
  $users = array();
  for ($i = 0; $i < 3; $i++) {
    $account = $this
      ->drupalCreateUser(array());
    $users[$account
      ->id()] = $account;
  }

  // Cancel user accounts, including own one.
  $edit = array();
  $edit['action'] = 'user_cancel_user_action';
  for ($i = 0; $i <= 4; $i++) {
    $edit['user_bulk_form[' . $i . ']'] = TRUE;
  }
  $this
    ->drupalPostForm('admin/people', $edit, t('Apply'));
  $this
    ->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
  $this
    ->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
  $this
    ->assertText(t('Require email confirmation to cancel account'), 'Allows to send confirmation mail.');
  $this
    ->assertText(t('Notify user when account is canceled'), 'Allows to send notification mail.');

  // Confirm deletion.
  $this
    ->drupalPostForm(NULL, NULL, t('Cancel accounts'));
  $status = TRUE;
  foreach ($users as $account) {
    $status = $status && strpos($this->content, $account
      ->getUsername() . '</em> has been deleted.') !== FALSE;
    $user_storage
      ->resetCache(array(
      $account
        ->id(),
    ));
    $status = $status && !$user_storage
      ->load($account
      ->id());
  }
  $this
    ->assertTrue($status, 'Users deleted and not found in the database.');

  // Ensure that admin account was not cancelled.
  $this
    ->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
  $admin_user = $user_storage
    ->load($admin_user
    ->id());
  $this
    ->assertTrue($admin_user
    ->isActive(), 'Administrative user is found in the database and enabled.');

  // 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.');
}