You are here

function UserCancelTest::testUserBlock 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::testUserBlock()

Disable account and keep all content.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

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

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

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

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, NULL, t('Cancel account'));
  $this
    ->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
  $this
    ->assertText(t('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.'), 'Informs that all content will be remain as is.');
  $this
    ->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');

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

  // Confirm account cancellation request.
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $user_storage
    ->resetCache(array(
    $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
    ->assertRaw(t('%name has been disabled.', array(
    '%name' => $account
      ->getUsername(),
  )), "Confirmation message displayed to user.");
}