You are here

public function CaptchaAfterGlobalThresholdTestCase::doTestCaptchaAfterFloodingThreshold in CAPTCHA After 6

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

Testing of flooding threshold for node/add/page form.

1 call to CaptchaAfterGlobalThresholdTestCase::doTestCaptchaAfterFloodingThreshold()
CaptchaAfterGlobalThresholdTestCase::testCaptchaAfterFloodingThresholds in ./captcha_after.test
Testing of flooding thresholds for node/add/page form.

File

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

Class

CaptchaAfterGlobalThresholdTestCase
Global threashold tests for the CAPTCHA After module.

Code

public function doTestCaptchaAfterFloodingThreshold() {

  // Login test user.
  $this
    ->drupalLogin($this->test_user);

  // Test clean page post without captcha.
  $edit = array(
    'title' => $this
      ->randomName(8),
    'body' => $this
      ->randomString(32),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('Page @title has been created', array(
    '@title' => $edit['title'],
  )));

  // Turno on captcha protection.
  module_load_include('inc', 'captcha');
  captcha_set_form_id_setting('page_node_form', 'captcha/Test');

  // Check default captcha protection.
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertField('captcha_response');

  // Enable captcha_after for node/add/page form.
  captcha_after_db_set_form('page_node_form', array(
    'enable' => 1,
  ));

  // Test skiping of all checks - we should NOT see captcha response in this case.
  variable_set('captcha_after_submit_threshold', 0);
  variable_set('captcha_after_flooding_threshold', 0);
  variable_set('captcha_after_global_flooding_threshold', 0);
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertNoField('captcha_response');

  // Test flooding threshold. We need to set submit and global thresholds also to not equal to 0.
  variable_set('captcha_after_submit_threshold', 3);
  variable_set('captcha_after_flooding_threshold', 2);

  // Two submits per hour.
  variable_set('captcha_after_global_flooding_threshold', 1000);
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertNoField('captcha_response');
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('Page @title has been created', array(
    '@title' => $edit['title'],
  )));
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertNoField('captcha_response');
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('Page @title has been created', array(
    '@title' => $edit['title'],
  )));
  $this
    ->drupalGet('node/add/page');
  $this
    ->assertField('captcha_response');

  // Try with bad captcha solution.
  $edit['captcha_response'] = '?';
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE);
  $this
    ->assertField('captcha_response', 'Flooding threshold test finished.');

  // Try with good captcha solution.
  $edit['captcha_response'] = 'Test 123';
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('Page @title has been created', array(
    '@title' => $edit['title'],
  )), 'Flooding threshold test finished.');
}