diff --git a/modules/comment.rules.inc b/modules/comment.rules.inc index f9846fd..199a604 100644 --- a/modules/comment.rules.inc +++ b/modules/comment.rules.inc @@ -8,6 +8,13 @@ */ /** + * Implements hook_rules_file_info() on behalf of the comment module. + */ +function rules_comment_file_info() { + return array('modules/comment.eval'); +} + +/** * Implementation of hook_rules_event_info(). */ function rules_comment_event_info() { @@ -54,6 +61,51 @@ function rules_comment_event_info() { } /** + * Implements hook_rules_condition_info() on behalf of the comment module. + */ +function rules_comment_condition_info() { + $defaults = array( + 'parameter' => array( + 'node' => array('type' => 'comment', 'label' => t('Comment')), + ), + 'group' => t('Comment'), + 'access callback' => 'rules_comment_integration_access', + ); + $items['comment_is_published'] = $defaults + array( + 'label' => t('Comment is published'), + 'base' => 'rules_condition_comment_is_published', + ); + return $items; +} + +/** + * Implements hook_rules_action_info() on behalf of the comment module. + */ +function rules_comment_action_info() { + $defaults = array( + 'parameter' => array( + 'node' => array('type' => 'comment', 'label' => t('Comment'), 'save' => TRUE), + ), + 'group' => t('Comment'), + 'access callback' => 'rules_comment_admin_access', + ); + // Add support for hand-picked core actions. + $core_actions = comment_action_info(); + $actions = array( + 'comment_publish_action', + 'comment_unpublish_action', + ); + foreach ($actions as $base) { + $action_name = str_replace('_action', '', $base); + $items[$action_name] = $defaults + array( + 'label' => $core_actions[$base]['label'], + 'base' => $base, + ); + } + return $items; +} + +/** * Comment integration access callback. */ function rules_comment_integration_access($type, $name) { @@ -63,5 +115,12 @@ function rules_comment_integration_access($type, $name) { } /** + * Comment integration admin access callback. + */ +function rules_comment_admin_access() { + return user_access('administer comments'); +} + +/** * @} */ diff --git a/tests/rules.test b/tests/rules.test index 1b3246c..e0bc96c 100644 --- a/tests/rules.test +++ b/tests/rules.test @@ -1273,6 +1273,24 @@ class RulesIntegrationTestCase extends DrupalWebTestCase { } /** + * Helper function: Post a comment to node. + */ + private function postComment($nid, $comment_body, $subject = '', $contact = NULL) { + // Post a comment. + $this->drupalGet('comment/reply/' . $nid); + $edit = array(); + $edit['subject'] = $subject; + $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment_body; + if ($contact !== NULL && is_array($contact)) { + $edit += $contact; + } + $this->drupalPost(NULL, $edit, t('Save')); + // Get comment ID. + preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); + return comment_load($match[1]); + } + + /** * Just make sure the access callback run without errors. */ function testAccessCallbacks() { @@ -1679,6 +1697,60 @@ class RulesIntegrationTestCase extends DrupalWebTestCase { } /** + * Test integration for the comment module. + */ + function testCommentIntegration() { + // Create a user that can post comments. + $user = $this->drupalCreateUser(array( + 'administer nodes', + 'post comments', + )); + $this->drupalLogin($user); + $tests = array( + array('comment_unpublish', 'comment_is_published', 'comment_publish', 'status'), + ); + $node = $this->drupalCreateNode(array( + 'type' => 'page', + 'status' => 1, + 'sticky' => 1, + 'promote' => 1, + )); + // Post a comment. + $subject= $this->randomName(); + $comment_body = $this->randomName(32); + $comment = $this->postComment($node->nid, $comment_body, $subject); + foreach ($tests as $info) { + list($action1, $condition, $action2, $property) = $info; + + rules_action($action1)->execute($comment); + $comment = comment_load($comment->cid, TRUE); + $this->assertFalse($comment->$property, 'Action has permanently disabled comment '. $property); + $return = rules_condition($condition)->execute($comment); + $this->assertFalse($return, 'Condition determines comment '. $property . ' is disabled.'); + + rules_action($action2)->execute($comment); + $comment = comment_load($comment->cid, TRUE); + $this->assertTrue($comment->$property, 'Action has permanently enabled comment '. $property); + $return = rules_condition($condition)->execute($comment); + $this->assertTrue($return, 'Condition determines comment '. $property . ' is enabled.'); + } + + // Test auto saving of a new comment after it has been inserted into the DB. + $rule = rules_reaction_rule(); + $rand = $this->randomName(); + $rule->event('comment_insert') + ->action('data_set', array('data:select' => 'comment:subject', 'value' => $rand)); + $rule->save('test'); + $node = $this->drupalCreateNode(); + // Post a comment. + $subject= $this->randomName(); + $comment_body = $this->randomName(32); + $comment = $this->postComment($node->nid, $comment_body, $subject); + $this->assertEqual($comment->subject, $rand, 'Comment subject is correct.'); + RulesLog::logger()->checkLog(); + } + + /** * Test integration for the user module. */ function testUserIntegration() {