You are here

public function UserCancelTest::testUserBlock in Drupal 9

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserBlock() {
  $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() . '/edit');
  $this
    ->submitForm([], 'Cancel account');
  $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('Select the method to cancel the account above.');

  // Confirm account cancellation.
  $timestamp = time();
  $this
    ->submitForm([], 'Cancel account');
  $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->getAccountName()} has been disabled.");
}