You are here

public function PhraseCaptchaTestCase::testPhraseArtCaptchaOnLoginForm in CAPTCHA Pack 8

Testing the protection of the user log in form.

File

text_captcha/modules/phrase_captcha/src/Test/PhraseCaptchaTestCase.php, line 27

Class

PhraseCaptchaTestCase
Phrase CAPTCHA main test case sensitivity.

Namespace

Drupal\phrase_captcha\Tests

Code

public function testPhraseArtCaptchaOnLoginForm() {

  // 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('phrase_captcha/Phrase 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, 'Phrase 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');
}