class LoginFinalizerTest

Tests login finalization.

Attributes

#[Group('user')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of LoginFinalizerTest

File

core/modules/user/tests/src/Kernel/LoginFinalizerTest.php, line 20

Namespace

Drupal\Tests\user\Kernel
View source
class LoginFinalizerTest extends KernelTestBase {
  use UserCreationTrait;
  
  /**
   * The test user ID.
   */
  protected int|string|null $testUserId;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'user_hooks_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('user');
    // Create a stub time service.
    $time = $this->createStub(TimeInterface::class);
    $time->method('getRequestTime')
      ->willReturn(1234567890);
    $this->container
      ->set('datetime.time', $time);
  }
  
  /**
   * Tests login.
   */
  public function testLogin() : void {
    // Create a user.
    $user = $this->createUser();
    // Get the session.
    $session = $this->container
      ->get('request_stack')
      ->getSession();
    // Assert no user is logged in.
    $this->assertFalse($session->has('uid'));
    // Handle the login.
    $finalize = $this->container
      ->get(LoginFinalizer::class);
    $finalize->finalizeLogin($user);
    // Assert our user is logged in.
    $this->assertEquals($user->id(), $session->get('uid'));
    $this->assertTrue($session->get('check_logged_in'));
    // Assert last login time is set.
    $user = User::load($user->id());
    $this->assertEquals(1234567890, $user->getLastLoginTime());
    // Assert our user login hook was called.
    $this->assertEquals($user->id(), $this->testUserId);
  }
  
  /**
   * Implements hook_user_login().
   *
   * @param \Drupal\user\UserInterface $account
   *   The user object on which the operation was just performed.
   */
  public function userLogin(UserInterface $account) : void {
    if (!isset($this->testUserId)) {
      $this->testUserId = $account->id();
    }
  }

}

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