LoginSecuritySoftBlockTest.php in Login Security 8
File
src/Tests/LoginSecuritySoftBlockTest.php
View source
<?php
namespace Drupal\login_security\Tests;
use Drupal\Component\Utility\SafeMarkup;
class LoginSecuritySoftBlockTest extends LoginSecurityTestBase {
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(SafeMarkup::format('The user @user_name has been blocked due to failed login attempts.', [
'@user_name' => $account
->getUsername(),
]), '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);
$site_name = \Drupal::config('system.site')
->get('name');
$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);
}
}