View source
<?php
namespace Drupal\Tests\autologout\Functional;
use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Session;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
class AutologoutSessionCleanupOnLoginTest extends BrowserTestBase {
public static $modules = [
'autologout',
'node',
];
protected $loggedInUsers = [];
protected $privilegedUser;
protected $database;
protected $configFactory;
public function setUp() {
parent::setUp();
$this->privilegedUser = $this
->drupalCreateUser([
'access content overview',
'administer site configuration',
'access site reports',
'access administration pages',
'bypass node access',
'administer content types',
'administer nodes',
'administer autologout',
'change own logout threshold',
]);
$this->configFactory = $this->container
->get('config.factory');
$this->database = $this->container
->get('database');
}
public function testSessionCleanupAtLogin() {
$config = $this->container
->get('config.factory')
->getEditable('autologout.settings');
$config
->set('timeout', 5)
->set('padding', 0)
->save();
$this
->drupalLogin($this->privilegedUser);
$this->mink
->registerSession($this->privilegedUser->sessionId, $this
->getSession());
$sessions = $this
->getSessions($this->privilegedUser);
self::assertEquals(1, count($sessions), 'After initial login there is one active session');
$session1 = $this
->stashSession();
$this
->drupalLogin($this->privilegedUser);
$sessions = $this
->getSessions($this->privilegedUser);
self::assertEquals(2, count($sessions), 'After second login there is now two active session');
$this
->stashSession();
sleep(6);
$this
->drupalLogin($this->privilegedUser);
$sessions = $this
->getSessions($this->privilegedUser);
self::assertEquals(1, count($sessions), 'After third login, there is 1 active session, two stale sessions were cleaned up.');
$this
->restoreSession($session1);
$this
->drupalGet('node');
self::assertFalse($this
->drupalUserIsLoggedIn($this->privilegedUser));
$this
->closeAllSessions();
}
public function getSessions(User $account) {
$result = $this->database
->select('sessions', 's')
->fields('s')
->condition('uid', $account
->id())
->orderBy('timestamp', 'DESC')
->execute();
$sessions = [];
foreach ($result as $session) {
$sessions[] = $session;
}
return $sessions;
}
public function stashSession() {
if (empty($this
->getSessionName())) {
return 0;
}
$session_id = $this->privilegedUser->sessionId;
do {
$this
->generateSessionName($this
->randomMachineName());
} while (isset($this->loggedInUsers[$this
->getSessionName()]));
$this->loggedInUsers[$session_id] = clone $this->privilegedUser;
$this->mink
->registerSession($this
->getSessionName(), new Session(new GoutteDriver()));
$this->mink
->setDefaultSessionName($this
->getSessionName());
$this->loggedInUser = FALSE;
return $session_id;
}
public function restoreSession($session_id) {
$old_session_id = NULL;
if (isset($this->loggedInUsers[$session_id])) {
$old_session_id = $this
->stashSession();
}
$this->mink
->setDefaultSessionName($session_id);
$this->loggedInUser = $this->loggedInUsers[$session_id];
$this->privilegedUser = $this->loggedInUsers[$session_id];
$this->loggedInUser->sessionId = $session_id;
$this->privilegedUser->sessionId = $session_id;
return $old_session_id;
}
public function closeAllSessions() {
$this->database
->truncate('sessions')
->execute();
$this->loggedInUsers = [];
$this->sessionName = NULL;
$this->loggedInUser = FALSE;
$this->mink
->resetSessions();
}
}