Index: mollom.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/mollom/mollom.module,v retrieving revision 1.92 diff -u -p -r1.92 mollom.module --- mollom.module 18 Oct 2010 12:31:06 -0000 1.92 +++ mollom.module 27 Oct 2010 01:48:51 -0000 @@ -410,7 +410,23 @@ function mollom_cron() { * The entity id to retrieve data for. */ function mollom_data_load($entity, $id) { - return db_query_range('SELECT * FROM {mollom} WHERE entity = :entity AND id = :id', 0, 1, array(':entity' => $entity, ':id' => $id))->fetchObject(); + $data = mollom_data_load_multiple($entity, array($id)); + return $data ? $data[$id] : FALSE; +} + +/** + * Load Mollom data records from the database. + * + * @param $entity + * The entity type to retrieve data for. + * @param $ids + * A list of entity ids to retrieve data for. + */ +function mollom_data_load_multiple($entity, array $ids) { + return db_query('SELECT * FROM {mollom} WHERE entity = :entity AND id IN (:ids)', array( + ':entity' => $entity, + ':ids' => $ids, + ))->fetchAllAssoc('id'); } /** @@ -2107,12 +2123,28 @@ function mollom_mail_add_report_link(&$m } /** + * Implements hook_entity_load(). + */ +function mollom_entity_load($entities, $type) { + // Attach Mollom session data to all loaded entities, if any. + $data = mollom_data_load_multiple($type, array_keys($entities)); + foreach ($entities as $id => $entity) { + if (isset($entity->status)) { + $entities[$id]->original_status = $entity->status; + } + if (isset($data[$id])) { + $entities[$id]->mollom = $data[$id]; + } + } +} + +/** * Implements hook_entity_update(). */ function mollom_entity_update($entity, $type) { // If an existing entity is published and we have session data stored for it, // mark the data as moderated. - if (!empty($entity->status)) { + if (!empty($entity->status) && isset($entity->original_status) && $entity->original_status != $entity->status) { $id = reset(entity_extract_ids($type, $entity)); mollom_data_moderate($type, $id); } Index: tests/mollom.test =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/mollom/tests/mollom.test,v retrieving revision 1.73 diff -u -p -r1.73 mollom.test --- tests/mollom.test 19 Oct 2010 21:47:03 -0000 1.73 +++ tests/mollom.test 26 Oct 2010 17:23:28 -0000 @@ -580,6 +580,20 @@ class MollomWebTestCase extends DrupalWe } /** + * Replaces the server list to point to the local fake server implementation. + */ + protected function enableTestServer() { + variable_set('mollom_servers', array($GLOBALS['base_url'] . '/xmlrpc.php?version=')); + } + + /** + * Reverts the server list to use real Mollom servers in following requests. + */ + protected function disableTestServer() { + variable_del('mollom_servers'); + } + + /** * Retrieve submitted XML-RPC values from testing server implementation. * * @param $method @@ -2888,6 +2902,99 @@ class MollomAnalysisTestCase extends Mol $this->assertSame('profanity', $data->profanity, $expected['profanity']); $this->assertSame('moderate', $data->moderate, 0); } + + // Verify that no feedback is sent for new, unpublished SPAM comments. + // @todo Entirely rethink which tests actually need to run against the real + // Mollom backend. MollomResponseTestCase might be the only one. + $this->enableTestServer(); + $edit = array( + 'title' => $this->randomString(), + 'body' => 'spam', + ); + $this->drupalPost('mollom-test/form', $edit, 'Submit'); + $mid = $this->assertTestSubmitData(); + $data = $this->assertMollomData('mollom_test', $mid); + $record = mollom_test_load($mid); + $this->assertEqual($record['status'], 0, t('Unpublished test post found.')); + $this->assertSame('spam', $data->spam, MOLLOM_ANALYSIS_SPAM); + $this->assertSame('profanity', $data->profanity, 0); + $this->assertSame('moderate', $data->moderate, 1); + $feedback = $this->getServerRecord('mollom.sendFeedback'); + $this->assertTrue(empty($feedback), t('No feedback sent for the published test post.')); + + // Verify that publishing the post sends feedback. + $this->drupalLogin($this->admin_user); + $edit = array( + 'status' => TRUE, + ); + $this->drupalPost('mollom-test/form/' . $mid, $edit, 'Submit'); + $mid = $this->assertTestSubmitData($mid); + $data = $this->assertMollomData('mollom_test', $mid); + $record = mollom_test_load($mid); + $this->assertEqual($record['status'], 1, t('Published test post found.')); + $this->assertSame('spam', $data->spam, MOLLOM_ANALYSIS_SPAM); + $this->assertSame('profanity', $data->profanity, 0); + $this->assertSame('moderate', $data->moderate, 0); + $feedback = $this->getServerRecord('mollom.sendFeedback'); + $this->assertSame('session_id', $feedback['session_id'], $data->session_id); + $this->assertSame('feedback', $feedback['feedback'], 'ham'); + + // Verify that no feedback is sent for new, published HAM comments. + $this->drupalLogout(); + $edit = array( + 'title' => $this->randomString(), + 'body' => 'ham', + ); + $this->drupalPost('mollom-test/form', $edit, 'Submit'); + $mid = $this->assertTestSubmitData(); + $data = $this->assertMollomData('mollom_test', $mid); + $record = mollom_test_load($mid); + $this->assertEqual($record['status'], 1, t('Published test post found.')); + $this->assertSame('spam', $data->spam, MOLLOM_ANALYSIS_HAM); + $this->assertSame('profanity', $data->profanity, 0); + $this->assertSame('moderate', $data->moderate, 0); + $feedback = $this->getServerRecord('mollom.sendFeedback'); + $this->assertTrue(empty($feedback), t('No feedback sent for the published test post.')); + + // Change the form protection discard bad posts. + $this->drupalLogin($this->admin_user); + $this->setProtection('mollom_test_form', MOLLOM_MODE_ANALYSIS, NULL, array( + 'mollom[discard]' => 1, + )); + $this->drupalLogout(); + + // Verify that no feedback is sent for new, published HAM comments. + $edit = array( + 'title' => $this->randomString(), + 'body' => 'ham', + ); + $this->drupalPost('mollom-test/form', $edit, 'Submit'); + $mid = $this->assertTestSubmitData(); + $data = $this->assertMollomData('mollom_test', $mid); + $record = mollom_test_load($mid); + $this->assertEqual($record['status'], 1, t('Published test post found.')); + $this->assertSame('spam', $data->spam, MOLLOM_ANALYSIS_HAM); + $this->assertSame('profanity', $data->profanity, 0); + $this->assertSame('moderate', $data->moderate, 0); + $feedback = $this->getServerRecord('mollom.sendFeedback'); + $this->assertTrue(empty($feedback), t('No feedback sent for the published test post.')); + + // Verify that no feedback is sent for new, published UNSURE comments. + $edit = array( + 'title' => $this->randomString(), + 'body' => 'unsure', + ); + $this->drupalPost('mollom-test/form', $edit, 'Submit'); + $this->postCorrectCaptcha(NULL, array(), 'Submit'); + $mid = $this->assertTestSubmitData(); + $data = $this->assertMollomData('mollom_test', $mid); + $record = mollom_test_load($mid); + $this->assertEqual($record['status'], 1, t('Published test post found.')); + $this->assertSame('spam', $data->spam, MOLLOM_ANALYSIS_UNSURE); + $this->assertSame('profanity', $data->profanity, 0); + $this->assertSame('moderate', $data->moderate, 0); + $feedback = $this->getServerRecord('mollom.sendFeedback'); + $this->assertTrue(empty($feedback), t('No feedback sent for the published test post.')); } /** Index: tests/mollom_test.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/mollom/tests/mollom_test.module,v retrieving revision 1.15 diff -u -p -r1.15 mollom_test.module --- tests/mollom_test.module 9 Oct 2010 23:13:01 -0000 1.15 +++ tests/mollom_test.module 27 Oct 2010 01:53:16 -0000 @@ -413,10 +413,29 @@ function mollom_mollom_test_presave($rec } /** + * Implements hook_entity_info(). + */ +function mollom_test_entity_info() { + $entities = array( + 'mollom_test' => array( + 'label' => 'Mollom test', + 'base table' => 'mollom_test', + 'entity keys' => array( + 'id' => 'mid', + 'label' => 'title', + ), + ), + ); + return $entities; +} + +/** * Loads a {mollom_test} data record by id. */ function mollom_test_load($mid) { - return db_query('SELECT * FROM {mollom_test} WHERE mid = :mid', array(':mid' => $mid))->fetchAssoc(); + $records = entity_load('mollom_test', array($mid), array()); + $record = reset($records); + return (array) $record; } /**