From 2ac03fc382e88d789133937731f8301581ab6c16 Mon Sep 17 00:00:00 2001
From: sun <sun@unleashedmind.com>
Date: Tue, 16 Apr 2013 23:26:53 +0200
Subject: [PATCH] - #1971992 by sun: Fixed Text analysis should validate
 CAPTCHA before re-checking content to prevent
 false-positives.

---
 mollom.module     | 47 ++++++++++++++++++++++++++++++++++-------------
 tests/mollom.test | 38 +++++++++++++++++---------------------
 2 files changed, 51 insertions(+), 34 deletions(-)

diff --git a/mollom.module b/mollom.module
index 93fdd3c..47e874b 100644
--- a/mollom.module
+++ b/mollom.module
@@ -740,8 +740,8 @@ function mollom_form_alter(&$form, &$form_state, $form_id) {
       // Form-level validation handlers are required, since we need access to
       // all validated and submitted form values. _form_validate() invokes
       // #element_validate handlers while it is recursing into the form.
-      $form['#validate'][] = 'mollom_validate_analysis';
       $form['#validate'][] = 'mollom_validate_captcha';
+      $form['#validate'][] = 'mollom_validate_analysis';
       $form['#validate'][] = 'mollom_validate_post';
 
       // Append a submit handler to store Mollom session data. Requires that
@@ -1879,7 +1879,16 @@ function mollom_validate_analysis(&$form, &$form_state) {
         else {
           $form_state['mollom']['require_captcha'] = TRUE;
           $form_state['mollom']['passed_captcha'] = FALSE;
+
+          // Retrieve a new CAPTCHA and throw an error.
+          mollom_form_add_captcha($form['mollom'], $form_state);
           $form['mollom']['captcha']['#access'] = TRUE;
+          if (!empty($form_state['temporary']['mollom']['had_captcha'])) {
+            form_error($form['mollom']['captcha'], t('The word verification was not completed correctly. Please complete this new word verification and try again.') . ' ' . _mollom_format_message_falsepositive($form_state, $data));
+          }
+          else {
+            form_error($form['mollom']['captcha'], t('To complete this form, please complete the word verification below.'));
+          }
         }
         mollom_log(array(
           'message' => 'Unsure: %teaser',
@@ -1910,16 +1919,23 @@ function mollom_validate_analysis(&$form, &$form_state) {
  * text analysis result is "unsure".
  */
 function mollom_validate_captcha(&$form, &$form_state) {
-  if (!$form_state['mollom']['require_captcha']) {
-    return;
+  if ($form_state['mollom']['require_analysis']) {
+    if (empty($form_state['values']['mollom']['captchaId'])) {
+      return;
+    }
   }
-
-  // If there is no CAPTCHA ID yet, retrieve one and throw an error.
-  if (empty($form_state['values']['mollom']['captchaId'])) {
-    mollom_form_add_captcha($form['mollom'], $form_state);
-    form_error($form['mollom']['captcha'], t('To complete this form, please complete the word verification below.'));
-    return;
+  else {
+    if (!$form_state['mollom']['require_captcha']) {
+      return;
+    }
+    // If there is no CAPTCHA ID yet, retrieve one and throw an error.
+    if (empty($form_state['values']['mollom']['captchaId'])) {
+      mollom_form_add_captcha($form['mollom'], $form_state);
+      form_error($form['mollom']['captcha'], t('To complete this form, please complete the word verification below.'));
+      return;
+    }
   }
+  $form_state['temporary']['mollom']['had_captcha'] = TRUE;
 
   // $form_state['mollom']['passed_captcha'] may only ever be set by this
   // validation handler and must not be changed elsewhere.
@@ -1976,6 +1992,9 @@ function mollom_validate_captcha(&$form, &$form_state) {
   $form['mollom']['captchaId']['#value'] = $result['captchaId'];
 
   if (!empty($result['solved'])) {
+    if ($form_state['mollom']['require_analysis']) {
+      $form['mollom']['captchaId']['#value'] = '';
+    }
     $form_state['mollom']['passed_captcha'] = TRUE;
     $form['mollom']['captcha']['#access'] = FALSE;
     $form['mollom']['captcha']['#solved'] = TRUE;
@@ -1985,8 +2004,10 @@ function mollom_validate_captcha(&$form, &$form_state) {
     ), WATCHDOG_INFO);
   }
   else {
-    form_set_error('mollom][captcha', t('The word verification was not completed correctly. Please complete this new word verification and try again.') . ' ' . _mollom_format_message_falsepositive($form_state, $data));
-    mollom_form_add_captcha($form['mollom'], $form_state);
+    if (!$form_state['mollom']['require_analysis']) {
+      form_set_error('mollom][captcha', t('The word verification was not completed correctly. Please complete this new word verification and try again.') . ' ' . _mollom_format_message_falsepositive($form_state, $data));
+      mollom_form_add_captcha($form['mollom'], $form_state);
+    }
 
     mollom_log(array(
       'message' => 'Incorrect CAPTCHA',
@@ -2019,8 +2040,8 @@ function mollom_pre_render_mollom($element) {
   $element['captcha']['#value'] = '';
 
   // DX: Debugging helpers.
-//  $element['captcha']['#suffix'] = 'contentId: ' . $element['contentId']['#value'];
-//  $element['captcha']['#suffix'] .= '<br />captchaId: ' . $element['captchaId']['#value'];
+  $element['#suffix'] = 'contentId: ' . $element['contentId']['#value'] . '<br>';
+  $element['#suffix'] .= 'captchaId: ' . $element['captchaId']['#value'] . '<br>';
 
   return $element;
 }
diff --git a/tests/mollom.test b/tests/mollom.test
index 81ff136..fb4587e 100644
--- a/tests/mollom.test
+++ b/tests/mollom.test
@@ -3966,14 +3966,12 @@ class MollomAnalysisTestCase extends MollomWebTestCase {
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-    $this->assertResponseIDInForm('captchaId');
     $this->assertNoCaptchaField();
     $edit = array(
       'body' => 'unsure unsure unsure',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-    $this->assertResponseIDInForm('captchaId');
     $this->assertNoCaptchaField();
     $edit = array(
       'title' => 'unsure',
@@ -3986,14 +3984,11 @@ class MollomAnalysisTestCase extends MollomWebTestCase {
 
   /**
    * Tests unsure posts turning into ham.
-   *
-   * @todo CAPTCHA ID should stay the same.
-   * @see http://drupal.org/node/1959904
    */
   function subtestUnsureHam() {
     $this->drupalGet('mollom-test/form');
     $this->assertNoCaptchaField();
-    // Posting ham as title would submit the form, so post as body instead.
+    // Posting unsure as title would submit the form, so post as body instead.
     $edit = array(
       'body' => 'unsure',
     );
@@ -4004,10 +3999,10 @@ class MollomAnalysisTestCase extends MollomWebTestCase {
     // Turn the post into ham.
     $edit = array(
       'body' => 'ham',
+      'mollom[captcha]' => 'correct',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-//    $this->assertResponseIDInForm('captchaId');
     $this->assertNoCaptchaField();
     // Turn the post back into unsure.
     $edit = array(
@@ -4015,12 +4010,10 @@ class MollomAnalysisTestCase extends MollomWebTestCase {
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-//    $this->assertResponseIDInForm('captchaId');
-    $this->assertCaptchaField();
+    $this->assertNoCaptchaField();
 
     $edit = array(
       'title' => 'irrelevant',
-      'mollom[captcha]' => 'correct',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertNoCaptchaField();
@@ -4037,31 +4030,33 @@ class MollomAnalysisTestCase extends MollomWebTestCase {
     $this->drupalGet('mollom-test/form');
     $this->assertNoCaptchaField();
     $edit = array(
-      'title' => 'unsure',
+      'body' => 'unsure',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId', TRUE);
     $this->assertResponseIDInForm('captchaId', TRUE);
     $this->assertCaptchaField();
-    // Turn the post into spam.
+
     $edit = array(
-      'title' => 'spam',
+      'mollom[captcha]' => 'correct',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-//    $this->assertResponseIDInForm('captchaId');
     $this->assertNoCaptchaField();
-    // Turn the post back into unsure.
+
+    // Turn the post into spam.
     $edit = array(
-      'title' => 'unsure',
+      'title' => 'spam',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertResponseIDInForm('contentId');
-//    $this->assertResponseIDInForm('captchaId');
-    $this->assertCaptchaField();
+    $this->assertNoCaptchaField();
+    $this->assertText($this->spam_message);
+    $this->assertNoText('Successful form submission.');
 
+    // Turn the post back into unsure.
     $edit = array(
-      'mollom[captcha]' => 'correct',
+      'title' => 'unsure',
     );
     $this->drupalPost(NULL, $edit, 'Submit');
     $this->assertNoCaptchaField();
@@ -4376,8 +4371,8 @@ class MollomAnalysisOptionsTestCase extends MollomWebTestCase {
     $this->drupalLogout();
     $expectations = array(
       'ham' => array('spamScore' => 0.0, 'spamClassification' => 'ham', 'profanityScore' => 0),
-      'unsure' => array('spamScore' => 0.5, 'spamClassification' => 'unsure', 'profanityScore' => 0),
-      $this->randomString() => array('spamScore' => 0.5, 'spamClassification' => 'unsure', 'profanityScore' => 0),
+      'unsure' => array('spamScore' => 0.0, 'spamClassification' => 'unsure', 'profanityScore' => 0),
+      $this->randomString() => array('spamScore' => 0.0, 'spamClassification' => 'unsure', 'profanityScore' => 0),
     );
     foreach ($expectations as $body => $expected) {
       $edit = array(
@@ -4387,6 +4382,7 @@ class MollomAnalysisOptionsTestCase extends MollomWebTestCase {
       $this->drupalPost('mollom-test/form', $edit, 'Submit');
       if ($expected['spamClassification'] == 'unsure') {
         $this->postCorrectCaptcha(NULL, array(), 'Submit');
+        $expected['spamClassification'] = 'ham';
       }
       $mid = $this->assertTestSubmitData();
       $data = $this->assertMollomData('mollom_test', $mid);
-- 
1.7.11.msysgit.1

