You are here

captcha_after.test in CAPTCHA After 7

Same filename and directory in other branches
  1. 6 captcha_after.test

Captcha After tests.

File

captcha_after.test
View source
<?php

/**
 * @file
 * Captcha After tests.
 */

/**
 * Global threashold tests for the CAPTCHA After module.
 */
class CaptchaAfterGlobalThresholdTestCase extends DrupalWebTestCase {

  /**
   * Test users.
   */
  protected $test_user, $test_user1;

  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => 'CAPTCHA After global threshold',
      'description' => 'Testing of the global threshold CAPTCHA After functionality.',
      'group' => 'CAPTCHA',
    );
  }

  /**
   * setUp() performs any pre-requisite tasks that need to happen.
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp('captcha', 'captcha_after');

    // So we can reuse CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE constant.
    module_load_include('test', 'captcha');

    // Create our test users.
    $this->test_user = $this
      ->drupalCreateUser(array(
      'create page content',
    ));
    $this->test_user1 = $this
      ->drupalCreateUser(array(
      'create page content',
    ));
  }

  /**
   * Testing of submit threshold for user login form.
   */
  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,
    ));

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

    // Test clean login.
    $edit = array(
      'name' => $this->test_user->name,
      'pass' => $this->test_user->pass_raw,
    );
    $this
      ->drupalPost('user', $edit, t('Log in'));
    $this
      ->assertLink(t('Log out'));
    $this
      ->drupalLogout();

    // Test bad login threshold limit 1.
    $edit['pass'] .= 'wrong pass';
    $this
      ->drupalPost('user', $edit, t('Log in'));
    $this
      ->assertText('Sorry, unrecognized username or password.');
    $this
      ->assertNoField('captcha_response');

    // Test bad login threshold limit 2.
    $this
      ->drupalPost('user', $edit, t('Log in'));
    $this
      ->assertText('Sorry, unrecognized username or password.');
    $this
      ->assertNoField('captcha_response');

    // Test bad login threshold limit 3.
    $this
      ->drupalPost('user', $edit, t('Log in'));
    $this
      ->assertText('Sorry, unrecognized username or password.');
    $this
      ->assertField('captcha_response');

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

    // Try to login with incorect captcha solution.
    variable_set('captcha_after_submit_threshold', 3);
    $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
      ->assertNoField('captcha_response', 'Submit threshold test finished.');
  }

  /**
   * Testing of flooding thresholds for node/add/page form.
   */
  public function testCaptchaAfterFloodingThresholds() {

    // Order of execution is important thats why we are calling manualy this methods.
    $this
      ->doTestCaptchaAfterFloodingThreshold();
    $this
      ->doTestCaptchaAfterGlobalFloodingThreshold();
    $this
      ->pass('All global threshold tests finished.');
  }

  /**
   * Testing of flooding threshold for node/add/page form.
   */
  public function doTestCaptchaAfterFloodingThreshold() {

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

    // Test clean page post without captcha.
    $edit = array(
      'title' => $this
        ->randomName(8),
      'body[und][0][value]' => $this
        ->randomString(32),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $this
      ->assertText(t('Basic 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('Basic 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('Basic 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('Basic page @title has been created', array(
      '@title' => $edit['title'],
    )), 'Flooding threshold test finished.');
  }

  /**
   * Testing of global flooding threshold.
   */
  public function doTestCaptchaAfterGlobalFloodingThreshold() {

    // Test global flooding threshold.
    variable_set('captcha_after_submit_threshold', 3);
    variable_set('captcha_after_flooding_threshold', 10);

    // While testing we are on a same ip so this needs to be bigger then global floding value.
    variable_set('captcha_after_global_flooding_threshold', 5);

    // Four global submits per hour.
    // Lets test two more submits for other test user.
    $this
      ->drupalLogin($this->test_user1);
    $this
      ->drupalGet('node/add/page');
    $this
      ->assertNoField('captcha_response');
    $edit = array(
      'title' => $this
        ->randomName(8),
      'body[und][0][value]' => $this
        ->randomString(32),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $this
      ->assertText(t('Basic 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('Basic 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', 'Global 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('Basic page @title has been created', array(
      '@title' => $edit['title'],
    )), 'Global flooding threshold test finished.');
  }

}

/**
 * Global threshold tests for the CAPTCHA After module.
 */
class CaptchaAfterPerFormThresholdTestCase extends DrupalWebTestCase {

  /**
   * Test users.
   */
  protected $test_user, $test_user1;

  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => 'CAPTCHA After per form threshold',
      'description' => 'Testing of the per form threshold CAPTCHA After functionality.',
      'group' => 'CAPTCHA',
    );
  }

  /**
   * setUp() performs any pre-requisite tasks that need to happen.
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp('captcha', 'captcha_after');

    // So we can reuse CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE constant.
    module_load_include('test', 'captcha');

    // Create our test users.
    $this->test_user = $this
      ->drupalCreateUser(array(
      'create page content',
    ));

    //$this->test_user1 = $this->drupalCreateUser(array('create page content'));
  }

  /**
   * Testing of per form submit threshold for user login form.
   */
  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.');
  }

}

Classes

Namesort descending Description
CaptchaAfterGlobalThresholdTestCase Global threashold tests for the CAPTCHA After module.
CaptchaAfterPerFormThresholdTestCase Global threshold tests for the CAPTCHA After module.