public function RandomCaptchaTypeTestCase::testRandomCaptchaTypeOnLoginForm in CAPTCHA Pack 8
Testing the protection of the user log in form.
File
- random_captcha_type/
src/ Tests/ RandomCaptchaTypeTestCase.php, line 27
Class
- RandomCaptchaTypeTestCase
- Tests Random CAPTCHA type main test case sensitivity.
Namespace
Drupal\random_captcha_type\TestsCode
public function testRandomCaptchaTypeOnLoginForm() {
// Create user and test log in without CAPTCHA.
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
// Log out again.
$this
->drupalLogout();
// Set a CAPTCHA on login form.
/* @var \Drupal\captcha\Entity\CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('random_captcha_type/Random CAPTCHA type');
$captcha_point
->enable()
->save();
// Check if there is a CAPTCHA on the login form (look for the title).
$this
->drupalGet('');
$this
->assertCaptchaPresence(TRUE);
// Try to log in, which should fail.
$edit = [
'name' => $user
->getUsername(),
'pass' => $user->pass_raw,
'captcha_response' => '?',
];
$this
->drupalPostForm(NULL, $edit, t('Log in'), [], [], self::LOGIN_HTML_FORM_ID);
// Check for error message.
$this
->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'Random CAPTCHA type should block user login form', 'CAPTCHA');
// And make sure that user is not logged in:
// check for name and password fields on ?q=user.
$this
->drupalGet('user');
$this
->assertField('name', t('Username field found.'), 'CAPTCHA');
$this
->assertField('pass', t('Password field found.'), 'CAPTCHA');
}