function UserLogoutTest::testLogout

Same name in other branches
  1. 10 core/modules/user/tests/src/Functional/UserLogoutTest.php \Drupal\Tests\user\Functional\UserLogoutTest::testLogout()

Tests user logout functionality.

File

core/modules/user/tests/src/Functional/UserLogoutTest.php, line 38

Class

UserLogoutTest
Tests user logout.

Namespace

Drupal\Tests\user\Functional

Code

public function testLogout() : void {
    $account = $this->createUser();
    $this->drupalLogin($account);
    // Test missing csrf token does not log the user out.
    $logoutUrl = Url::fromRoute('user.logout');
    $confirmUrl = Url::fromRoute('user.logout.confirm');
    $this->drupalGet($logoutUrl);
    $this->assertTrue($this->drupalUserIsLoggedIn($account));
    $this->assertSession()
        ->addressEquals($confirmUrl);
    // Test invalid csrf token does not log the user out.
    $this->drupalGet($logoutUrl, [
        'query' => [
            'token' => '123',
        ],
    ]);
    $this->assertTrue($this->drupalUserIsLoggedIn($account));
    $this->assertSession()
        ->addressEquals($confirmUrl);
    // Submitting the confirmation form correctly logs the user out.
    $this->submitForm([], 'Log out');
    $this->assertFalse($this->drupalUserIsLoggedIn($account));
    $this->drupalResetSession();
    $this->drupalLogin($account);
    // Test with valid logout link.
    $this->drupalGet('user');
    $this->getSession()
        ->getPage()
        ->clickLink('Log out');
    $this->assertFalse($this->drupalUserIsLoggedIn($account));
    // Test hitting the confirm form while logged out redirects to the
    // frontpage.
    $this->drupalGet($confirmUrl);
    $this->assertSession()
        ->addressEquals(Url::fromRoute('<front>'));
}

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