You are here

public function LostCharacterCaptchaTestCase::testLostCharacterCaptchaOnLoginForm in CAPTCHA Pack 8

Testing the protection of the user log in form.

File

text_captcha/modules/lost_character_captcha/src/Tests/LostCharacterCaptchaTestCase.php, line 27

Class

LostCharacterCaptchaTestCase
Tests Lost character CAPTCHA main test case sensitivity.

Namespace

Drupal\lost_character_captcha\Tests

Code

public function testLostCharacterCaptchaOnLoginForm() {

  // 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('lost_character_captcha/Lost characters');
  $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, 'Lost character 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');
}