You are here

public function UserCancelTest::testUserCancelInvalid 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::testUserCancelInvalid()
  2. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserCancelInvalid()

Attempt invalid account cancellations.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserCancelInvalid() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $this
    ->config('user.settings')
    ->set('cancel_method', 'user_cancel_reassign')
    ->save();
  $user_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user');

  // Create a user.
  $account = $this
    ->drupalCreateUser([
    'cancel account',
  ]);
  $this
    ->drupalLogin($account);

  // Load a real user object.
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = $user_storage
    ->load($account
    ->id());

  // Create a node.
  $node = $this
    ->drupalCreateNode([
    'uid' => $account
      ->id(),
  ]);

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->submitForm([], 'Cancel account');

  // 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.');

  // Attempt bogus account cancellation request confirmation.
  $bogus_timestamp = $timestamp + 60;
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$bogus_timestamp}/" . user_pass_rehash($account, $bogus_timestamp));
  $this
    ->assertSession()
    ->pageTextContains('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.');
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = $user_storage
    ->load($account
    ->id());
  $this
    ->assertTrue($account
    ->isActive(), 'User account was not canceled.');

  // Attempt expired account cancellation request confirmation.
  $bogus_timestamp = $timestamp - 86400 - 60;
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$bogus_timestamp}/" . user_pass_rehash($account, $bogus_timestamp));
  $this
    ->assertSession()
    ->pageTextContains('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.');
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = $user_storage
    ->load($account
    ->id());
  $this
    ->assertTrue($account
    ->isActive(), 'User account was not canceled.');

  // Confirm user's content has not been altered.
  $node_storage
    ->resetCache([
    $node
      ->id(),
  ]);
  $test_node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertEquals($account
    ->id(), $test_node
    ->getOwnerId(), 'Node of the user has not been altered.');
  $this
    ->assertTrue($test_node
    ->isPublished());
}