You are here

public function SessionLimitBaseTestCase::assertSessionPrevent in Session Limit 7.2

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

Test that new sessions cannot be created past a maximum.

This tests the session_limit 'prevent new sessions' behaviour once the maximum is reached.

Parameters

int $session_limit: The maximum number of allowed sessions.

5 calls to SessionLimitBaseTestCase::assertSessionPrevent()
SessionLimitPreventTestCase::testSessionLimitRoles in tests/session_limit.test
Checks that the session limit is returned correctly by a role.
SessionLimitPreventTestCase::testSessionLimitUser in tests/session_limit.test
Checks that the session limit is returned correctly by a user override.
SessionLimitPreventTestCase::testSessionLimitUserMaxPrecedence in tests/session_limit.test
Checks that a user session limit is always used in preference to all others.
SessionLimitPreventTestCase::testSessionPreventOnMax1 in tests/session_limit.test
Test user can only have 1 session, prevent new sessions.
SessionLimitPreventTestCase::testSessionPreventOnMax2 in tests/session_limit.test
Test user can only have 2 sessions, prevent new sessions.

File

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

Class

SessionLimitBaseTestCase
Base test for session limits.

Code

public function assertSessionPrevent($session_limit) {

  // Set the session limit behaviour to prevent new sessions.
  variable_set('session_limit_behaviour', 2);

  // Set the default session limit to 1.
  variable_set('session_limit_max', $session_limit);

  // Create the user to test with.
  $user = $this
    ->drupalCreateUser(array(
    'access content',
  ));
  $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();
  }

  // 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,
    )));
  }

  // Try to login on a further session.
  $this
    ->stashSession();
  $this
    ->drupalLoginExpectFail($user);
  $this
    ->assertText(t('The maximum number of simultaneous sessions (@session_limit) for your account has been reached. You did not log off from a previous session or someone else is logged on to your account. This may indicate that your account has been compromised or that account sharing is limited on this site. Please contact the site administrator if you suspect your account has been compromised.', array(
    '@session_limit' => $session_limit,
  )), t('User sees the session limit login prevention message.'));

  // Switch back to session 1 and logout.
  $extra_session_number = $session_limit + 1;
  $sessions[$extra_session_number] = $this
    ->stashSession();
  $this
    ->restoreSession($sessions[1]);
  $this
    ->drupalLogout($user);
  $this
    ->drupalGet('node');
  $this
    ->assertNoText(t('Log out'), t('User has logged out of session 1.'));

  // Switch back to extra session and check they can now login.
  $this
    ->restoreSession($sessions[$extra_session_number]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('node');
  $this
    ->assertText(t('Log out'), t('User has logged into the extra session now they have logged out of session 1.'));
}