function UserCancelTest::testUserDelete in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/user/src/Tests/UserCancelTest.php \Drupal\user\Tests\UserCancelTest::testUserDelete()
Delete account and remove all content.
File
- core/
modules/ user/ src/ Tests/ UserCancelTest.php, line 379 - Contains \Drupal\user\Tests\UserCancelTest.
Class
- UserCancelTest
- Ensure that account cancellation methods work as expected.
Namespace
Drupal\user\TestsCode
function testUserDelete() {
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$this
->config('user.settings')
->set('cancel_method', 'user_cancel_delete')
->save();
\Drupal::service('module_installer')
->install(array(
'comment',
));
$this
->resetAll();
$this
->addDefaultCommentField('node', 'page');
$user_storage = $this->container
->get('entity.manager')
->getStorage('user');
// Create a user.
$account = $this
->drupalCreateUser(array(
'cancel account',
'post comments',
'skip comment approval',
));
$this
->drupalLogin($account);
// Load a real user object.
$user_storage
->resetCache(array(
$account
->id(),
));
$account = $user_storage
->load($account
->id());
// Create a simple node.
$node = $this
->drupalCreateNode(array(
'uid' => $account
->id(),
));
// Create comment.
$edit = array();
$edit['subject[0][value]'] = $this
->randomMachineName(8);
$edit['comment_body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('comment/reply/node/' . $node
->id() . '/comment', $edit, t('Preview'));
$this
->drupalPostForm(NULL, array(), t('Save'));
$this
->assertText(t('Your comment has been posted.'));
$comments = entity_load_multiple_by_properties('comment', array(
'subject' => $edit['subject[0][value]'],
));
$comment = reset($comments);
$this
->assertTrue($comment
->id(), 'Comment found.');
// Create a node with two revisions, the initial one belonging to the
// cancelling user.
$revision_node = $this
->drupalCreateNode(array(
'uid' => $account
->id(),
));
$revision = $revision_node
->getRevisionId();
$settings = get_object_vars($revision_node);
$settings['revision'] = 1;
$settings['uid'] = 1;
// Set new/current revision to someone else.
$revision_node = $this
->drupalCreateNode($settings);
// 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 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
->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(),
));
$this
->assertFalse($user_storage
->load($account
->id()), 'User is not found in the database.');
// Confirm that user's content has been deleted.
$node_storage
->resetCache(array(
$node
->id(),
));
$this
->assertFalse($node_storage
->load($node
->id()), 'Node of the user has been deleted.');
$this
->assertFalse(node_revision_load($revision), 'Node revision of the user has been deleted.');
$node_storage
->resetCache(array(
$revision_node
->id(),
));
$this
->assertTrue($node_storage
->load($revision_node
->id()), "Current revision of the user's node was not deleted.");
\Drupal::entityManager()
->getStorage('comment')
->resetCache(array(
$comment
->id(),
));
$this
->assertFalse(Comment::load($comment
->id()), '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
->getUsername(),
)), "Confirmation message displayed to user.");
}