You are here

public function UserCancelTest::testMassUserCancelByAdmin in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testMassUserCancelByAdmin()
  2. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testMassUserCancelByAdmin()

Create an administrative user and mass-delete other users.

File

core/modules/user/tests/src/Functional/UserCancelTest.php, line 543

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

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

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

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

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

  // Cancel user accounts, including own one.
  $edit = [];
  $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 to selected items'));
  $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
      ->getTextContent(), $account
      ->getAccountName() . ' has been deleted.') !== FALSE;
    $user_storage
      ->resetCache([
      $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([
    1,
  ]);
  $user1 = $user_storage
    ->load(1);
  $this
    ->assertTrue($user1
    ->isActive(), 'User #1 still exists and is not blocked.');
}