1 user.test UserCancelTestCase::testUserDelete()

Delete account and remove all content.

File

core/modules/user/tests/user.test, line 819
Tests for user.module.

Class

UserCancelTestCase
Test cancelling a user.

Code

function testUserDelete() {
  config_set('system.core', 'user_cancel_method', 'user_cancel_delete');

  // Create a user.
  $account = $this->backdropCreateUser(array('cancel account', 'post comments', 'skip comment approval'));
  $this->backdropLogin($account);
  // Load real user object.
  $account = user_load($account->uid, TRUE);

  // Enable comment subjects on 'page' nodes
  $node_type = node_type_get_type('page');
  $node_type->settings['comment_subject_field'] = TRUE;
  node_type_save($node_type);

  // Create a simple node.
  $node = $this->backdropCreateNode(array('uid' => $account->uid));

  // Create comment.
  $langcode = LANGUAGE_NONE;
  $edit = array();
  $edit['subject'] = $this->randomName(8);
  $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);

  $this->backdropPost('comment/reply/' . $node->nid, $edit, t('Preview'));
  $this->backdropPost(NULL, array(), t('Save'));
  $this->assertText(t('Your comment has been posted.'));
  $comments = comment_load_multiple(FALSE, array('subject' => $edit['subject']));
  $comment = reset($comments);
  $this->assertTrue($comment->cid, 'Comment found.');

  // Create a node with two revisions, the initial one belonging to the
  // cancelling user.
  $revision_node = $this->backdropCreateNode(array('uid' => $account->uid));
  $revision = $revision_node->vid;
  $settings = get_object_vars($revision_node);
  $settings['revision'] = 1;
  $settings['uid'] = 1; // Set new/current revision to someone else.
  $revision_node = $this->backdropCreateNode($settings);

  // Attempt to cancel account.
  $this->backdropGet('user/' . $account->uid . '/edit');
  $this->backdropPost(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 removed and all account information deleted. All of your content will also be deleted.'), 'Informs that all content will be deleted.');

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

  // Confirm account cancellation request.
  $this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
  $this->assertFalse(user_load($account->uid, TRUE), 'User is not found in the database.');

  // Confirm that user's content has been deleted.
  $this->assertFalse(node_load($node->nid, NULL, TRUE), 'Node of the user has been deleted.');
  $this->assertFalse(node_load($node->nid, $revision, TRUE), 'Node revision of the user has been deleted.');
  $this->assertTrue(node_load($revision_node->nid, NULL, TRUE), "Current revision of the user's node was not deleted.");
  $this->assertFalse(comment_load($comment->cid), 'Comment of the user has been deleted.');

  // Confirm that the confirmation message made it through to the end user.
  $this->assertRaw(t('%name has been deleted.', array('%name' => $account->name)), 'Confirmation message displayed to user.');
}