function UserCancelTest::testMassUserCancelByAdmin

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testMassUserCancelByAdmin() : void {
    \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 && str_contains($this->getTextContent(), "Account {$account->getAccountName()} has been deleted.");
        $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.