You are here

function UserCancelTest::testUserCancelWithoutPermission in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserCancelTest.php \Drupal\user\Tests\UserCancelTest::testUserCancelWithoutPermission()

Attempt to cancel account without permission.

File

core/modules/user/src/Tests/UserCancelTest.php, line 41
Contains \Drupal\user\Tests\UserCancelTest.

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

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

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

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

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

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->assertNoRaw(t('Cancel account'), 'No cancel account button displayed.');

  // Attempt bogus account cancellation request confirmation.
  $timestamp = $account
    ->getLastLoginTime();
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $this
    ->assertResponse(403, 'Bogus cancelling request rejected.');
  $user_storage
    ->resetCache(array(
    $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(array(
    $node
      ->id(),
  ));
  $test_node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertTrue($test_node
    ->getOwnerId() == $account
    ->id() && $test_node
    ->isPublished(), 'Node of the user has not been altered.');
}