You are here

function UserLoginTest::assertFailedLogin in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserLoginTest.php \Drupal\user\Tests\UserLoginTest::assertFailedLogin()

Make an unsuccessful login attempt.

Parameters

\Drupal\user\Entity\User $account: A user object with name and pass_raw attributes for the login attempt.

mixed $flood_trigger: (optional) Whether or not to expect that the flood control mechanism will be triggered. Defaults to NULL.

  • Set to 'user' to expect a 'too many failed logins error.
  • Set to any value to expect an error for too many failed logins per IP

.

  • Set to NULL to expect a failed login.
2 calls to UserLoginTest::assertFailedLogin()
UserLoginTest::testGlobalLoginFloodControl in core/modules/user/src/Tests/UserLoginTest.php
Test the global login flood control.
UserLoginTest::testPerUserLoginFloodControl in core/modules/user/src/Tests/UserLoginTest.php
Test the per-user login flood control.

File

core/modules/user/src/Tests/UserLoginTest.php, line 162
Contains \Drupal\user\Tests\UserLoginTest.

Class

UserLoginTest
Ensure that login works as expected.

Namespace

Drupal\user\Tests

Code

function assertFailedLogin($account, $flood_trigger = NULL) {
  $edit = array(
    'name' => $account
      ->getUsername(),
    'pass' => $account->pass_raw,
  );
  $this
    ->drupalPostForm('user/login', $edit, t('Log in'));
  $this
    ->assertNoFieldByXPath("//input[@name='pass' and @value!='']", NULL, 'Password value attribute is blank.');
  if (isset($flood_trigger)) {
    if ($flood_trigger == 'user') {
      $this
        ->assertRaw(\Drupal::translation()
        ->formatPlural($this
        ->config('user.flood')
        ->get('user_limit'), 'There has been more than one failed login attempt for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', 'There have been more than @count failed login attempts for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', array(
        ':url' => \Drupal::url('user.pass'),
      )));
    }
    else {

      // No uid, so the limit is IP-based.
      $this
        ->assertRaw(t('Too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', array(
        ':url' => \Drupal::url('user.pass'),
      )));
    }
  }
  else {
    $this
      ->assertText(t('Unrecognized username or password. Have you forgotten your password?'));
  }
}