You are here

public function UserCancelTest::testUserBlockUnpublish in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserBlockUnpublish()
  2. 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 213

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserBlockUnpublish() {
  $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() . '/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 be hidden from everyone but administrators.'), 'Informs that all content will be unpublished.');

  // 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));

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