function LogoutFinalizerTest::testLogout

Tests logout.

File

core/modules/user/tests/src/Kernel/LogoutFinalizerTest.php, line 48

Class

LogoutFinalizerTest
Tests logout finalization.

Namespace

Drupal\Tests\user\Kernel

Code

public function testLogout() : void {
  // Create a user.
  $user = $this->createUser();
  // Create a mock session manager.
  $sessionManager = $this->createMock(SessionManagerInterface::class);
  $sessionManager->expects($this->once())
    ->method('destroy');
  $this->container
    ->set('session_manager', $sessionManager);
  // Log the user in first.
  $this->container
    ->get(LoginFinalizer::class)
    ->finalizeLogin($user);
  // Handle the logout.
  $this->container
    ->get(LogoutFinalizer::class)
    ->finalizeLogout();
  // Assert our user logout hook was called.
  $this->assertEquals($user->id(), $this->testUserId);
  // Assert the session is now anonymous.
  /** @var \Drupal\Core\Session\AccountProxyInterface $accountProxy */
  $accountProxy = $this->container
    ->get('current_user');
  $this->assertInstanceOf(AnonymousUserSession::class, $accountProxy->getAccount());
}

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