You are here

public function CaptchaAfterPerFormThresholdTestCase::testCaptchaAfterSubmitThreshold in CAPTCHA After 6

Same name and namespace in other branches
  1. 7 captcha_after.test \CaptchaAfterPerFormThresholdTestCase::testCaptchaAfterSubmitThreshold()

Testing of per form submit threshold for user login form.

File

./captcha_after.test, line 264
Captcha After tests.

Class

CaptchaAfterPerFormThresholdTestCase
Global threshold tests for the CAPTCHA After module.

Code

public function testCaptchaAfterSubmitThreshold() {

  // Configure captcha protection for user login form.
  module_load_include('inc', 'captcha');
  captcha_set_form_id_setting('user_login', 'captcha/Test');

  // Enable captcha_after for user_login form.
  captcha_after_db_set_form('user_login', array(
    'enable' => 1,
    'submit_threshold' => 3,
  ));

  // Test default state.
  $this
    ->drupalGet('user');
  $this
    ->assertNoField('captcha_response');

  // Test disabling of submit global threshold.
  variable_set('captcha_after_submit_threshold', 5);
  $this
    ->drupalGet('user');
  $this
    ->assertNoField('captcha_response');

  // Wrong login params.
  $edit = array(
    'name' => $this->test_user->name,
    'pass' => 'wrong pass',
  );

  // Test difference between global and per form submit threshold = global: 1,
  //  per form: 3.
  variable_set('captcha_after_submit_threshold', 1);

  // Try 1
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText('Sorry, unrecognized username or password.');
  $this
    ->assertNoField('captcha_response');

  // Try 2
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertNoField('captcha_response');

  // Try 3
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertField('captcha_response', 'Finished testing difference betwen global(1) and per form(3) submit threshold.');

  // Try to login with incorect captcha solution.
  $edit['pass'] = $this->test_user->pass_raw;
  $edit['captcha_response'] = '?';
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE);
  $this
    ->assertField('captcha_response');

  // Try to login with correct captcha solution.
  $edit['captcha_response'] = 'Test 123';
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertLink(t('Log out'));
  $this
    ->pass('All Per form threshold tests finished.');
}