You are here

function CaptchaTestCase::testCaptchaOnLoginForm in CAPTCHA 7

Same name and namespace in other branches
  1. 6.2 captcha.test \CaptchaTestCase::testCaptchaOnLoginForm()

Testing the protection of the user log in form.

File

./captcha.test, line 231
Tests for CAPTCHA module.

Class

CaptchaTestCase

Code

function testCaptchaOnLoginForm() {

  // 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
  captcha_set_form_id_setting('user_login', 'captcha/Math');

  // Check if there is a CAPTCHA on the login form (look for the title).
  $this
    ->drupalGet('user');
  $this
    ->assertCaptchaPresence(TRUE);

  // Try to log in, which should fail.
  $edit = array(
    'name' => $user->name,
    'pass' => $user->pass_raw,
    'captcha_response' => '?',
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));

  // Check for error message.
  $this
    ->assertText(t(CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE), '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');
}