You are here

protected function CaptchaBaseWebTestCase::getMathCaptchaSolutionFromForm in CAPTCHA 7

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

Get the solution of the math CAPTCHA from the current form in the browser.

3 calls to CaptchaBaseWebTestCase::getMathCaptchaSolutionFromForm()
CaptchaSessionReuseAttackTestCase::testCaptchaSessionReuseAttackDetectionOnCommentPreview in ./captcha.test
CaptchaSessionReuseAttackTestCase::testCaptchaSessionReuseAttackDetectionOnLoginForm in ./captcha.test
CaptchaSessionReuseAttackTestCase::testCaptchaSessionReuseAttackDetectionOnNodeForm in ./captcha.test

File

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

Class

CaptchaBaseWebTestCase
Base class for CAPTCHA tests.

Code

protected function getMathCaptchaSolutionFromForm() {

  // Get the math challenge.
  $elements = $this
    ->xpath('//div[@class="form-item form-type-textfield form-item-captcha-response"]/span[@class="field-prefix"]');
  $challenge = (string) $elements[0];

  // Extract terms and operator from challenge.
  $matches = array();
  $ret = preg_match('/\\s*(\\d+)\\s*(-|\\+)\\s*(\\d+)\\s*=\\s*/', $challenge, $matches);

  // Solve the challenge
  $a = (int) $matches[1];
  $b = (int) $matches[3];
  $solution = $matches[2] == '-' ? $a - $b : $a + $b;
  return $solution;
}