public function CSSCaptchaTestCase::testCssCaptchaOnLoginForm in CAPTCHA Pack 8
Testing the protection of the user log in form.
File
- css_captcha/
src/ Tests/ CSSCaptchaTestCase.php, line 27
Class
- CSSCaptchaTestCase
- Tests CSS CAPTCHA main test case sensitivity.
Namespace
Drupal\css_captcha\TestsCode
public function testCssCaptchaOnLoginForm() {
// 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('css_captcha/CSS CAPTCHA');
$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, 'CSS CAPTCHA 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');
}