function UserCancelTest::testUserBlock

Same name in other branches
  1. 9 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()
  3. 11.x 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 175

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->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([
        $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.', [
        '%name' => $account->getAccountName(),
    ]), "Confirmation message displayed to user.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.