You are here

public function SessionLimitBaseTestCase::assertSessionLogout in Session Limit 6.2

Same name and namespace in other branches
  1. 8 tests/session_limit.test \SessionLimitBaseTestCase::assertSessionLogout()
  2. 7.2 tests/session_limit.test \SessionLimitBaseTestCase::assertSessionLogout()
  3. 2.x tests/session_limit.test \SessionLimitBaseTestCase::assertSessionLogout()

Test that an individual user can have up to a specifed number of sessions.

Once the maximum is reached, the oldest session is logged out.

Parameters

int $session_limit: The max number of sessions the specified user should be able to access.

stdClass $user: (optional) The user to test this with. Leave blank to create a user.

5 calls to SessionLimitBaseTestCase::assertSessionLogout()
SessionLimitLogoutTestCase::testSessionLimitRoles in tests/session_limit.test
Checks that the session limit is returned correctly by a role.
SessionLimitLogoutTestCase::testSessionLimitUser in tests/session_limit.test
Checks that the session limit is returned correctly by a user override.
SessionLimitLogoutTestCase::testSessionLimitUserMaxPrecedence in tests/session_limit.test
Check that user override takes precedence over default and role regardless of max.
SessionLimitLogoutTestCase::testSessionLogoutOnMax1 in tests/session_limit.test
Test user can only have 1 session, logout oldest.
SessionLimitLogoutTestCase::testSessionLogoutOnMax2 in tests/session_limit.test
Test user can only have 2 sessions, logout oldest.

File

tests/session_limit.test, line 63
Simpletest tests for session_limit.

Class

SessionLimitBaseTestCase
Base test for session limits.

Code

public function assertSessionLogout($session_limit, stdClass $user = NULL) {

  // Set the session limit behaviour to log out of old sessions.
  variable_set('session_limit_behaviour', 1);

  // Create the user to test with.
  $user = empty($user) ? $this
    ->drupalCreateUser() : $user;
  $sessions = array();
  for ($session_number = 1; $session_number <= $session_limit; $session_number++) {

    // Log user into each session.
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('user');
    $this
      ->assertText(t('Log out'), t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));
    $this
      ->assertText($user->name, t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));

    // Backup session.
    $sessions[$session_number] = $this
      ->stashSession();

    // Wait briefly to prevent race conditions.
    sleep(1);
  }

  // Check all allowed sessions are currently accessible.
  foreach ($sessions as $session_number => $session_id) {
    $this
      ->restoreSession($session_id);
    $this
      ->drupalGet('user');
    $this
      ->assertText(t('Log out'), t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));
    $this
      ->assertText($user->name, t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));
    sleep(1);
  }

  // Create a further session.
  $extra_session_number = $session_limit + 1;
  $this
    ->stashSession();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('user');
  $this
    ->assertText(t('Log out'), t('User is logged in under session @no.', array(
    '@no' => $extra_session_number,
  )));
  $this
    ->assertText($user->name, t('User is logged in under session @no.', array(
    '@no' => $extra_session_number,
  )));

  // Check user 1 is no longer logged in on session 1.
  $sessions[$extra_session_number] = $this
    ->stashSession();
  $this
    ->restoreSession($sessions[1]);
  $this
    ->drupalGet('node');
  $this
    ->assertNoText(t('Log out'), t('User 1 is not logged in under session 1.'));
  $this
    ->assertText(t('You have been automatically logged out. Someone else has logged in with your username and password and the maximum number of @limit simultaneous sessions was exceeded. This may indicate that your account has been compromised or that account sharing is not allowed on this site. Please contact the site administrator if you suspect your account has been compromised.', array(
    '@limit' => $session_limit,
  )), t('User was shown message about being logged out due to too many sessions'));

  // Check user 1 is logged in on all other sessions.
  foreach ($sessions as $session_number => $session_id) {
    if ($session_number == 1) {

      // We know they have been logged out of session 1.
      continue;
    }
    $this
      ->restoreSession($session_id);
    $this
      ->drupalGet('user');
    $this
      ->assertText(t('Log out'), t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));
    $this
      ->assertText($user->name, t('User is logged in under session @no.', array(
      '@no' => $session_number,
    )));
  }
}