LoginSecuritySoftBlockTest.php in Login Security 2.x
File
tests/src/Functional/LoginSecuritySoftBlockTest.php
View source
<?php
namespace Drupal\Tests\login_security\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class LoginSecuritySoftBlockTest extends LoginSecurityTestBase {
use StringTranslationTrait;
public static $modules = [
'user',
'login_security',
];
protected function assertNoSoftBlocked($account) {
$this
->drupalLoginLite($account);
$this
->assertNoText('This host is not allowed to log in', 'Soft-blocked notice does not display.');
$this
->assertNoText(new FormattableMarkup('The user @user_name has been blocked due to failed login attempts.', [
'@user_name' => $account
->getAccountName(),
]), 'User is not blocked.');
$this
->assertFieldByName('form_id', 'user_login_form', 'Login form found.');
}
protected function assertSoftBlocked($account) {
$this
->drupalLoginLite($account);
$this
->assertText('This host is not allowed to log in', 'Soft-block message displays.');
$this
->assertFieldByName('form_id', 'user_login_form', 'Login form found.');
}
public function testLogin() {
\Drupal::configFactory()
->getEditable('login_security.settings')
->set('user_wrong_count', 5)
->save();
$normal_user = $this
->drupalCreateUser();
$this
->drupalLogin($normal_user);
$warning_message = 'You have used 1 out of 5 login attempts. After all 5 have been used, you will be unable to login.';
$this
->assertNoText($warning_message, 'Attempts available message displayed.');
}
public function testSoftBlocking() {
$config = \Drupal::configFactory()
->getEditable('login_security.settings');
$config
->set('user_wrong_count', 0)
->save();
$config
->set('host_wrong_count', 2)
->save();
$config
->set('notice_attempts_available', 0)
->save();
$normal_user = $this
->drupalCreateUser();
$good_pass = $normal_user
->getPassword();
$new_pass = user_password();
$normal_user
->setPassword($new_pass);
$this
->assertNoSoftBlocked($normal_user);
$this
->assertNoSoftBlocked($normal_user);
$config
->set('disable_core_login_error', 1)
->save();
$this
->assertNoSoftBlocked($normal_user);
$config
->set('disable_core_login_error', 0)
->save();
$this
->assertSoftBlocked($normal_user);
$normal_user
->setPassword($good_pass);
$this
->assertSoftBlocked($normal_user);
}
}