View source
<?php
namespace Drupal\Tests\farm_login\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
use Drupal\user\Entity\User;
class UserLoginTest extends FarmBrowserTestBase {
use StringTranslationTrait;
protected static $modules = [
'farm_login',
];
public function testValidLoginWithDestination() {
$this
->drupalGet('user/login');
$this
->assertSession()
->pageTextContains($this
->t('Email or username'));
$this
->assertSession()
->pageTextContains($this
->t('Enter your @s email address or username.', [
'@s' => $this
->config('system.site')
->get('name'),
]));
$user = $this
->drupalCreateUser([]);
$this
->drupalGet('user/login', [
'query' => [
'destination' => 'foo',
],
]);
$edit = [
'name' => $user
->getAccountName(),
'pass' => $user->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->addressEquals('foo');
$this
->drupalLogout();
$user = $this
->drupalCreateUser([]);
$this
->drupalGet('user/login', [
'query' => [
'destination' => 'foo',
],
]);
$edit = [
'name' => $user
->getEmail(),
'pass' => $user->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->addressEquals('foo');
$this
->drupalLogout();
$user = $this
->drupalCreateUser([]);
$this
->drupalGet('user/login', [
'query' => [
'destination' => 'foo',
],
]);
$edit = [
'name' => 'invalid@email.com',
'pass' => $user->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldValueEquals('pass', '');
$this
->assertSession()
->pageTextcontains('Unrecognized username or password. Forgot your password?');
}
public function testPerUserLoginFloodControl() {
$this
->config('user.flood')
->set('ip_limit', 4000)
->set('user_limit', 3)
->save();
$user1 = $this
->drupalCreateUser([]);
$incorrect_user1 = clone $user1;
$incorrect_user1->passRaw .= 'incorrect';
$user2 = $this
->drupalCreateUser([]);
for ($i = 0; $i < 2; $i++) {
$this
->assertFailedLoginUsingEmail($incorrect_user1);
}
$this
->drupalLoginUsingEmail($user1);
$this
->drupalLogout();
for ($i = 0; $i < 3; $i++) {
$this
->assertFailedLoginUsingEmail($incorrect_user1);
}
$this
->drupalLoginUsingEmail($user2);
$this
->drupalLogout();
$this
->assertFailedLoginUsingEmail($user1, 'user');
}
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 {
$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?');
}
}
protected function drupalLoginUsingEmail(AccountInterface $account) {
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$this
->drupalGet(Url::fromRoute('user.login'));
$this
->submitForm([
'name' => $account
->getEmail(),
'pass' => $account->passRaw,
], $this
->t('Log in'));
$account->sessionId = $this
->getSession()
->getCookie(\Drupal::service('session_configuration')
->getOptions(\Drupal::request())['name']);
$this
->assertTrue($this
->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', [
'%name' => $account
->getAccountName(),
]));
$this->loggedInUser = $account;
$this->container
->get('current_user')
->setAccount($account);
}
}