function UserCancelTest::testUserBlockUnpublish

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlockUnpublish()
  2. 8.9.x core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlockUnpublish()
  3. 10 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlockUnpublish()

Disable account and unpublish all content.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserBlockUnpublish() : void {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $this->config('user.settings')
        ->set('cancel_method', 'user_cancel_block_unpublish')
        ->save();
    // Create comment field on page.
    $this->addDefaultCommentField('node', 'page');
    $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 with two revisions.
    $node = $this->drupalCreateNode([
        'uid' => $account->id(),
    ]);
    $settings = get_object_vars($node);
    $settings['revision'] = 1;
    $node = $this->drupalCreateNode($settings);
    // Add a comment to the page.
    $comment_subject = $this->randomMachineName(8);
    $comment_body = $this->randomMachineName(8);
    $comment = Comment::create([
        'subject' => $comment_subject,
        'comment_body' => $comment_body,
        'entity_id' => $node->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
        'status' => CommentInterface::PUBLISHED,
        'uid' => $account->id(),
    ]);
    $comment->save();
    // Attempt to cancel account.
    $this->drupalGet('user/' . $account->id() . '/cancel');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to cancel your account?');
    $this->assertSession()
        ->pageTextContains('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.');
    // Confirm account cancellation.
    $timestamp = time();
    $this->submitForm([], 'Confirm');
    $this->assertSession()
        ->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');
    // Confirm account cancellation request.
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
    // Confirm that the user was redirected to the front page.
    $this->assertSession()
        ->addressEquals('');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Confirm that the confirmation message made it through to the end user.
    $this->assertSession()
        ->pageTextContains("Account {$account->getAccountName()} has been disabled.");
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    $this->assertTrue($account->isBlocked(), 'User has been blocked.');
    // Confirm user's content has been unpublished.
    $node_storage->resetCache([
        $node->id(),
    ]);
    $test_node = $node_storage->load($node->id());
    $this->assertFalse($test_node->isPublished(), 'Node of the user has been unpublished.');
    $test_node = $node_storage->loadRevision($node->getRevisionId());
    $this->assertFalse($test_node->isPublished(), 'Node revision of the user has been unpublished.');
    $storage = \Drupal::entityTypeManager()->getStorage('comment');
    $storage->resetCache([
        $comment->id(),
    ]);
    $comment = $storage->load($comment->id());
    $this->assertFalse($comment->isPublished(), 'Comment of the user has been unpublished.');
}

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