You are here

public function UserCancelTest::testMassUserCancelByAdmin in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testMassUserCancelByAdmin()
  2. 9 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 591

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',
  ]);
  $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
    ->drupalGet('admin/people');
  $this
    ->submitForm($edit, 'Apply to selected items');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to cancel these user accounts?');
  $this
    ->assertSession()
    ->pageTextContains('Cancellation method');
  $this
    ->assertSession()
    ->pageTextContains('Require email confirmation');
  $this
    ->assertSession()
    ->pageTextContains('Notify user when account is canceled');

  // Confirm deletion.
  $this
    ->submitForm([], 'Confirm');
  $status = TRUE;
  foreach ($users as $account) {
    $status = $status && strpos($this
      ->getTextContent(), "Account {$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
    ->assertSession()
    ->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');
  $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.');
}