You are here

public function UserLoginTest::assertFailedLoginUsingEmail in farmOS 2.x

Make an unsuccessful login using the account email.

A copy of the core assertFailedLogin() method, but that uses email instead.

Parameters

\Drupal\user\Entity\User $account: A user object with name and passRaw 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.

Throws

\Behat\Mink\Exception\ExpectationException

See also

UserLoginTest::assertFailedLogin()

1 call to UserLoginTest::assertFailedLoginUsingEmail()
UserLoginTest::testPerUserLoginFloodControl in modules/core/login/tests/src/Functional/UserLoginTest.php
Test the per-user login flood control.

File

modules/core/login/tests/src/Functional/UserLoginTest.php, line 138

Class

UserLoginTest
Test using an email in the UserLoginForm.

Namespace

Drupal\Tests\farm_login\Functional

Code

public function assertFailedLoginUsingEmail(User $account, $flood_trigger = NULL) {
  $database = \Drupal::database();
  $this
    ->drupalGet(Url::fromRoute('user.login'));
  $this
    ->submitForm([
    'name' => $account
      ->getEmail(),
    'pass' => $account->passRaw,
  ], $this
    ->t('Log in'));
  if (isset($flood_trigger)) {
    $this
      ->assertSession()
      ->statusCodeEquals(403);
    $this
      ->assertSession()
      ->fieldNotExists('pass');
    $last_log = $database
      ->select('watchdog', 'w')
      ->fields('w', [
      'message',
    ])
      ->condition('type', 'user')
      ->orderBy('wid', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();
    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>.', [
        ':url' => Url::fromRoute('user.pass')
          ->toString(),
      ]));
      $this
        ->assertEquals('Flood control blocked login attempt for uid %uid from %ip', $last_log, 'A watchdog message was logged for the login attempt blocked by flood control per user.');
    }
    else {

      // No uid, so the limit is IP-based.
      $this
        ->assertRaw($this
        ->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>.', [
        ':url' => Url::fromRoute('user.pass')
          ->toString(),
      ]));
      $this
        ->assertEquals('Flood control blocked login attempt from %ip', $last_log, 'A watchdog message was logged for the login attempt blocked by flood control per IP.');
    }
  }
  else {
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->fieldValueEquals('pass', '');
    $this
      ->assertText('Unrecognized username or password. Forgot your password?');
  }
}