#348372 by Damien Tournoud: the javascript portion actually need the session_id, put it back on the form.

From: Damien Tournoud <damien@tournoud.net>


---

 mollom.js         |    4 ++--
 mollom.module     |   24 ++++++++++--------------
 tests/mollom.test |   34 +++++++++++++++++-----------------
 3 files changed, 29 insertions(+), 33 deletions(-)


diff --git mollom.js mollom.js
index 3e66b41..d156447 100644
--- mollom.js
+++ mollom.js
@@ -8,7 +8,7 @@ Drupal.behaviors.mollom = function() {
 
 function getAudioCaptcha() {
   // Extract the Mollom session ID from the form:
-  var mollomSessionId = $("input#edit-session-id").val();
+  var mollomSessionId = $("input#edit-mollom-session-id").val();
 
   // Retrieve an audio CAPTCHA:
   var data = $.get(Drupal.settings.basePath + 'mollom/captcha/audio/' + mollomSessionId,
@@ -25,7 +25,7 @@ function getAudioCaptcha() {
 
 function getImageCaptcha() {
   // Extract the Mollom session ID from the form:
-  var mollomSessionId = $('input#edit-session-id').val();
+  var mollomSessionId = $('input#edit-mollom-session-id').val();
 
   // Retrieve an image CAPTCHA:
   var data = $.get(Drupal.settings.basePath + 'mollom/captcha/image/' + mollomSessionId,
diff --git mollom.module mollom.module
index 7ee1df4..453b9d1 100644
--- mollom.module
+++ mollom.module
@@ -798,20 +798,17 @@ function mollom_expand_element($element, $edit, &$form_state) {
   // The current state can come either from the $form_state, if the form
   // was just rebuilt in the same request...
   if (!empty($form_state['mollom'])) {
-    $mollom_id = $form_state['mollom']['mollom_id'];
-    $mollom_state = $form_state['mollom']['state'];
+    $mollom_state = $form_state['mollom'];
   }
   // ... or from data posted by the user. In that case we validate that the correct
   // form_id and token is used...
-  else if (!empty($edit['mollom_id']) && ($cache = cache_get($edit['mollom_id'], 'cache_mollom'))
+  else if (!empty($edit['session_id']) && ($cache = cache_get($edit['session_id'], 'cache_mollom'))
     && $cache->data['#form_id'] === $form_state['values']['form_id'] && $cache->data['#user_session_id'] === session_id()) {
-    $mollom_id = $edit['mollom_id'];
     $mollom_state = $cache->data;
   }
 
   // ... finally, if no valid state has been found, we generate an empty one.
-  if (empty($mollom_id)) {
-    $mollom_id = 'mollom-'. md5(uniqid(mt_rand(), TRUE));
+  if (empty($mollom_state)) {
     $mollom_state = array(
       '#session_id' => NULL,
       '#form_id' => $form_state['values']['form_id'],
@@ -841,19 +838,18 @@ function mollom_expand_element($element, $edit, &$form_state) {
     _mollom_insert_captcha($mollom_state, $element);
   }
   
-  if (!empty($edit) || !empty($form_state['submitted']) || !empty($mollom_state['#require_captcha'])) {
+  if (!empty($mollom_state['#session_id'])) {
     // We store the Mollom session only if something useful was done.
     // We save it in two places: as an hidden form field and in the cache
     // so that it persists form submission, and in $form_state so that it
     // persists form rebuilds.
-    $element['mollom_id'] = array(
+    $element['session_id'] = array(
       '#type' => 'hidden',
-      '#value' => $mollom_id,
+      '#value' => $mollom_state['#session_id'],
     );
-    cache_set($mollom_id, $mollom_state, 'cache_mollom', 60*30);
+    cache_set($mollom_state['#session_id'], $mollom_state, 'cache_mollom', 60*30);
 
-    $form_state['mollom']['mollom_id'] = $mollom_id;
-    $form_state['mollom']['state'] = $mollom_state;
+    $form_state['mollom'] = $mollom_state;
 
     // This prevents the Drupal page cache from storing the page when we generated a captcha
     // or when the user already passed the captcha. This is not required for
@@ -870,8 +866,8 @@ function mollom_expand_element($element, $edit, &$form_state) {
  * Clean the Mollom state as soon as the form has been submitted.
  */
 function mollom_clean_state($form_id, $form_state) {
-  if (!empty($form_state['values']['mollom']['mollom_id'])) {
-    cache_clear_all($form_state['values']['mollom']['mollom_id'], 'cache_mollom');
+  if (!empty($form_state['values']['mollom']['session_id'])) {
+    cache_clear_all($form_state['values']['mollom']['session_id'], 'cache_mollom');
   }
 }
 
diff --git tests/mollom.test tests/mollom.test
index bd63747..2d49bca 100644
--- tests/mollom.test
+++ tests/mollom.test
@@ -348,15 +348,15 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
     $this->drupalGet('comment/reply/'. $node->nid);
     $this->assertFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is present on the comment form before submission.'));
 
-    // Retrieve the Mollom ID. After each attempted submission below,
+    // Retrieve the Mollom Session ID. After each attempted submission below,
     // we will verify that this remains the same.
-    $mollom_id = $this->_testGetMollomID();
-        
+    $mollom_session_id = $this->getFieldValue("edit-mollom-session-id");
+
     // Try to submit an incorrect answer for the CAPTCHA.
     $this->drupalPost(NULL, array('mollom[captcha]' => 'incorrect'), t('Preview'));
     $this->assertText(t('The CAPTCHA was not completed correctly. Please complete this new CAPTCHA and try again.'), t('The comment cannot be submitted when the CAPTCHA is incorrect.'));
     $this->assertFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is present on the comment form after an incorrect CAPTCHA is submitted.'));
-    $this->assertFieldByID('edit-mollom-mollom-id', $mollom_id, 'The Mollom ID remains unchanged.');
+    $this->assertFieldByID('edit-mollom-session-id', $mollom_session_id, t('The Mollom Session ID is present on the comment form after a possible spam comment is previewed.'));
     
     // Try to submit a correct answer for the CAPTCHA, but without one of the required field.
     $this->drupalPost(NULL, array('comment' => '', 'mollom[captcha]' => 'correct'), t('Preview'));
@@ -387,12 +387,12 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
     // and a Mollom ID.
     $this->drupalPost(NULL, array('comment' => 'unsure'), t('Preview'));
     $this->drupalPost(NULL, array(), t('Save'));
-    $this->assertFieldByID('edit-mollom-mollom-id', '', t('The Mollom ID is present on the comment form after a possible spam comment is previewed.'));
+    $this->assertFieldByID('edit-mollom-session-id', '', t('The Mollom Session ID is present on the comment form after a possible spam comment is previewed.'));
     $this->assertFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is present on the comment form after a possible spam comment is previewed.'));
 
-    // Retrieve the Mollom ID. After each attempted submission below,
+    // Retrieve the Mollom Session ID. After each attempted submission below,
     // we will verify that this remains the same.
-    $mollom_id = $this->_testGetMollomID();
+    $mollom_session_id = $this->getFieldValue("edit-mollom-session-id");
 
     // Try to submit the form by using an invalid CAPTCHA. At this point,
     // the submission should be rejected and a new CAPTCHA generated (even
@@ -404,7 +404,7 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText(t('The CAPTCHA was not completed correctly. Please complete this new CAPTCHA and try again.'), t('The comment cannot be submitted when the CAPTCHA is incorrect.'));
     $this->assertFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is present on the comment form after an incorrect CAPTCHA is submitted.'));
-    $this->assertFieldByID('edit-mollom-mollom-id', $mollom_id, 'The Mollom ID remains unchanged.');
+    $this->assertFieldByID('edit-mollom-session-id', $mollom_session_id, 'The Mollom Session ID remains unchanged.');
 
     // Now try using a valid CAPTCHA. The CAPTCHA form should no longer
     // be present.
@@ -422,7 +422,7 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
     $this->assertText(t('Your submission has triggered the spam filter and will not be accepted.'), t('When a comment that is known to be spam is submitted, the user is warned that it will be blocked.'));
     $this->assertEqual($this->_testCommentNumAll($node->nid), $original_number_of_comments, t('A comment that is known to be spam does not appear in the database.'));
     $this->assertNoFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is absent on the comment form when a comment that is known to be spam is submitted.'));
-    $this->assertFieldByID('edit-mollom-mollom-id', $mollom_id, 'The Mollom ID remains unchanged.');
+    $this->assertFieldByID('edit-mollom-session-id', $mollom_session_id, 'The Mollom Session ID remains unchanged.');
 
     // Try to save again the above 'spam' comment. It should also be
     // rejected with no CAPTCHA.
@@ -430,7 +430,7 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
     $this->assertText(t('Your submission has triggered the spam filter and will not be accepted.'), t('When a comment that is known to be spam is previewed, the user is warned that it will be blocked.'));
     $this->assertEqual($this->_testCommentNumAll($node->nid), $original_number_of_comments, t('A comment that is known to be spam does not appear in the database.'));
     $this->assertNoFieldByID('edit-mollom-captcha', '', t('The CAPTCHA is absent on the comment form when a comment that is known to be spam is previewed.'));
-    $this->assertFieldByID('edit-mollom-mollom-id', $mollom_id, 'The Mollom ID remains unchanged.');
+    $this->assertFieldByID('edit-mollom-session-id', $mollom_session_id, 'The Mollom Session ID remains unchanged.');
 
     // Preview and save a comment that is 'ham'. Make sure that the submitted
     // comment appears on the screen and in the database.
@@ -443,17 +443,17 @@ class MollomCommentFormTestCase extends MollomWebTestCase {
   }
 
   /**
-   * Retrieve the Mollom ID. There has got to be a better way to
-   * do this, but getFieldById() simply doesn't work here.
+   * Retrieve a field value by ID.
    */
-  function _testGetMollomID() {
-    $mollom_id = '';
+  function getFieldValue($field_id) {
+    $value = '';
     if ($this->parse()) {
-      $fields = $this->elements->xpath($this->_constructFieldXpath('id', 'edit-mollom-mollom-id'));
-      foreach ($fields as $field) {
-        $mollom_id = $field['value'];
+      $fields = $this->elements->xpath($this->_constructFieldXpath('id', $field_id));
+      if (isset($fields[0])) {
+        $value = $fields[0]->value;
       }
     }
+    return $value;
   }
 
   /**
