You are here

function LoginSecurityUserBlockingTest::testUserBlocking in Login Security 6

Same name and namespace in other branches
  1. 7 login_security.test \LoginSecurityUserBlockingTest::testUserBlocking()

File

./login_security.test, line 77
Test the basic functions of the Login Security module.

Class

LoginSecurityUserBlockingTest

Code

function testUserBlocking() {
  $login_attempts_limit = 2;

  // allow 2 attempts to login before being blocking is enforced
  variable_set('login_security_user_wrong_count', $login_attempts_limit);

  // in Drupal 7, we can drupalGetMails() to see if a notice went out to admin
  // in the meantime, turn the message off just in case it doesn't get caught properly yet
  variable_set('login_security_user_blocked_email_user', '');
  $normal_user = $this
    ->drupalCreateUser(array(
    'access content',
  ));

  // intentionally break the password to repeat invalid logins
  $normal_user->pass_raw = user_password();
  variable_set('login_security_notice_attempts_available', 1);

  // drupalLogin() has assertions that we know will fail, so we must skip them with an alternate function
  $this
    ->drupalLoginLite($normal_user);
  $this
    ->assertText("You have used 1 out of {$login_attempts_limit} login attempts. After all {$login_attempts_limit} have been used, you will be unable to login.", t('Attempts available message displayed.'));
  $this
    ->assertFieldByName('form_id', 'user_login', t('Login form found.'));

  // turns off the warning message we looked for in the previous assert
  variable_set('login_security_notice_attempts_available', 0);
  $this
    ->drupalLoginLite($normal_user);
  $this
    ->assertNoText("You have used 2 out of {$login_attempts_limit} login attempts. After all {$login_attempts_limit} have been used, you will be unable to login.", t('Attempts available message displayed.'));
  $this
    ->assertFieldByName('form_id', 'user_login', t('Login form found.'));

  // turns back on the warning message we looked for in the previous assert
  $this
    ->assertText("The user {$normal_user->name} has been blocked due to failed login attempts.", t('Blocked message displayed.'));
  $this
    ->assertFieldByName('form_id', 'user_login', t('Login form found.'));
}