You are here

function UserCancelTest::testUserBlockUnpublish in Zircon Profile 8

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

Disable account and unpublish all content.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\user\Tests

Code

function testUserBlockUnpublish() {
  $node_storage = $this->container
    ->get('entity.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.manager')
    ->getStorage('user');

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

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

  // Create a node with two revisions.
  $node = $this
    ->drupalCreateNode(array(
    '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 = entity_create('comment', array(
    '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));
  $user_storage
    ->resetCache(array(
    $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(array(
    $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::entityManager()
    ->getStorage('comment');
  $storage
    ->resetCache(array(
    $comment
      ->id(),
  ));
  $comment = $storage
    ->load($comment
    ->id());
  $this
    ->assertFalse($comment
    ->isPublished(), 'Comment of the user has been unpublished.');

  // 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.");
}