function UserCancelTest::testUserBlock

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlock()
  2. 8.9.x core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlock()
  3. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlock()

Disable account and keep all content.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserBlock() : void {
    $this->config('user.settings')
        ->set('cancel_method', 'user_cancel_block')
        ->save();
    $user_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('user');
    // Create a user.
    $web_user = $this->drupalCreateUser([
        'cancel account',
    ]);
    $this->drupalLogin($web_user);
    // Load a real user object.
    $user_storage->resetCache([
        $web_user->id(),
    ]);
    $account = $user_storage->load($web_user->id());
    // Attempt to cancel account.
    $this->drupalGet('user/' . $account->id() . '/cancel');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to cancel your account?');
    $this->assertSession()
        ->pageTextContains('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your username.');
    $this->assertSession()
        ->pageTextNotContains('Cancellation method');
    // Confirm account cancellation.
    $timestamp = time();
    $this->submitForm([], 'Confirm');
    $this->assertSession()
        ->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');
    // Confirm account cancellation request.
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
    // Confirm that the confirmation message made it through to the end user.
    $this->assertSession()
        ->pageTextContains("Account {$account->getAccountName()} has been disabled.");
}

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