function UserCancelTest::testUserCancelWithoutPermission

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

Attempt to cancel account without permission.

File

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

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserCancelWithoutPermission() {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $this->config('user.settings')
        ->set('cancel_method', 'user_cancel_reassign')
        ->save();
    $user_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('user');
    // Create a user.
    $account = $this->drupalCreateUser([]);
    $this->drupalLogin($account);
    // Load a real user object.
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    // Create a node.
    $node = $this->drupalCreateNode([
        'uid' => $account->id(),
    ]);
    // Attempt to cancel account.
    $this->drupalGet('user/' . $account->id() . '/edit');
    $this->assertSession()
        ->pageTextNotContains("Cancel account");
    // Attempt bogus account cancellation request confirmation.
    $timestamp = $account->getLastLoginTime();
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
    $this->assertSession()
        ->statusCodeEquals(403);
    $user_storage->resetCache([
        $account->id(),
    ]);
    $account = $user_storage->load($account->id());
    $this->assertTrue($account->isActive(), 'User account was not canceled.');
    // Confirm user's content has not been altered.
    $node_storage->resetCache([
        $node->id(),
    ]);
    $test_node = $node_storage->load($node->id());
    $this->assertEquals($account->id(), $test_node->getOwnerId(), 'Node of the user has not been altered.');
    $this->assertTrue($test_node->isPublished());
}

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