function UserCancelTest::testUserCancelInvalid

Attempt invalid account cancellations.

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserCancelTest.php, line 105
Definition of Drupal\user\Tests\UserCancelTest.

Class

UserCancelTest
Test cancelling a user.

Namespace

Drupal\user\Tests

Code

function testUserCancelInvalid() {
  config('user.settings')
    ->set('cancel_method', 'user_cancel_reassign')
    ->save();

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

  // Load real user object.
  $account = user_load($account->uid, TRUE);

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

  // Attempt to cancel account.
  $this
    ->drupalPost('user/' . $account->uid . '/edit', NULL, t('Cancel account'));

  // Confirm account cancellation.
  $timestamp = time();
  $this
    ->drupalPost(NULL, NULL, t('Cancel account'));
  $this
    ->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), 'Account cancellation request mailed message displayed.');

  // Attempt bogus account cancellation request confirmation.
  $bogus_timestamp = $timestamp + 60;
  $this
    ->drupalGet("user/{$account->uid}/cancel/confirm/{$bogus_timestamp}/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
  $this
    ->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Bogus cancelling request rejected.');
  $account = user_load($account->uid);
  $this
    ->assertTrue($account->status == 1, 'User account was not canceled.');

  // Attempt expired account cancellation request confirmation.
  $bogus_timestamp = $timestamp - 86400 - 60;
  $this
    ->drupalGet("user/{$account->uid}/cancel/confirm/{$bogus_timestamp}/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
  $this
    ->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Expired cancel account request rejected.');
  $account = user_load($account->uid, TRUE);
  $this
    ->assertTrue($account->status, 'User account was not canceled.');

  // Confirm user's content has not been altered.
  $test_node = node_load($node->nid, TRUE);
  $this
    ->assertTrue($test_node->uid == $account->uid && $test_node->status == 1, 'Node of the user has not been altered.');
}