You are here

public function AutologoutSessionCleanupOnLoginTest::testSessionCleanupAtLogin in Automated Logout 8

Tests that stale sessions are cleaned up at login.

File

tests/src/Functional/AutologoutSessionCleanupOnLoginTest.php, line 78

Class

AutologoutSessionCleanupOnLoginTest
Tests session cleanup on login.

Namespace

Drupal\Tests\autologout\Functional

Code

public function testSessionCleanupAtLogin() {
  $config = $this->container
    ->get('config.factory')
    ->getEditable('autologout.settings');

  // For the purposes of the test, set the timeout periods to 5 seconds.
  $config
    ->set('timeout', 5)
    ->set('padding', 0)
    ->save();

  // Login in session 1.
  $this
    ->drupalLogin($this->privilegedUser);
  $this->mink
    ->registerSession($this->privilegedUser->sessionId, $this
    ->getSession());

  // Check one active session.
  $sessions = $this
    ->getSessions($this->privilegedUser);
  self::assertEquals(1, count($sessions), 'After initial login there is one active session');

  // Switch sessions.
  $session1 = $this
    ->stashSession();

  // Login to session 2.
  $this
    ->drupalLogin($this->privilegedUser);

  // Check two active sessions.
  $sessions = $this
    ->getSessions($this->privilegedUser);
  self::assertEquals(2, count($sessions), 'After second login there is now two active session');
  $this
    ->stashSession();

  // Switch sessions.
  // Wait for sessions to expire.
  sleep(6);

  // Login to session 3.
  $this
    ->drupalLogin($this->privilegedUser);

  // Check one active session.
  $sessions = $this
    ->getSessions($this->privilegedUser);
  self::assertEquals(1, count($sessions), 'After third login, there is 1 active session, two stale sessions were cleaned up.');

  // Switch back to session 1 and check no longer logged in.
  $this
    ->restoreSession($session1);
  $this
    ->drupalGet('node');
  self::assertFalse($this
    ->drupalUserIsLoggedIn($this->privilegedUser));
  $this
    ->closeAllSessions();
}