diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 3c94200..064f6d2 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -1587,6 +1587,9 @@ function comment_save($comment) { if ($comment->status == COMMENT_PUBLISHED) { module_invoke_all('comment_publish', $comment); } + else { + module_invoke_all('comment_unpublish', $comment); + } unset($comment->original); } catch (Exception $e) { diff --git a/modules/comment/comment.test b/modules/comment/comment.test index 9e69ba6..ecea77e 100644 --- a/modules/comment/comment.test +++ b/modules/comment/comment.test @@ -11,7 +11,13 @@ class CommentHelperCase extends DrupalWebTestCase { protected $node; function setUp() { - parent::setUp('comment', 'search'); + $modules = func_get_args(); + if (isset($modules[0]) && is_array($modules[0])) { + $modules = $modules[0]; + } + $modules[] = 'comment'; + parent::setUp($modules); + // Create users and test node. $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks')); $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'edit own comments')); @@ -115,6 +121,32 @@ class CommentHelperCase extends DrupalWebTestCase { } /** + * Publish comment. + * + * @param object $comment + * Comment to publish. + */ + function publishComment($comment) { + $edit = array(); + $edit['status'] = COMMENT_PUBLISHED; + + $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Save')); + } + + /** + * Unpublish comment. + * + * @param object $comment + * Comment to unpublish. + */ + function unpublishComment($comment) { + $edit = array(); + $edit['status'] = COMMENT_NOT_PUBLISHED; + + $this->drupalPost('comment/' . $comment->id . '/edit', $edit, t('Save')); + } + + /** * Delete comment. * * @param object $comment @@ -1664,6 +1696,43 @@ class CommentApprovalTest extends CommentHelperCase { } /** + * Tests comment publishing and unpublishing. + */ +class CommentPublishTest extends CommentHelperCase { + public static function getInfo() { + return array( + 'name' => 'hook_comment_publish() and hook_comment_unpublish()', + 'description' => 'Tests comment publishing and unpublishing.', + 'group' => 'Comment', + ); + } + + function setUp() { + parent::setUp('comment_test'); + } + + /** + * Test that publish and unpublish hooks are invoked correctly. + */ + function testCommentPublishHooks() { + $this->drupalLogin($this->admin_user); + + // Post comment. + $subject = $this->randomName(); + $body = $this->randomName(); + $comment = $this->postComment($this->node, $body, $subject); + + // Publish comment. + $this->publishComment($comment); + $this->assertRaw(t('Comment @subject has been published', array('@subject' => $subject)), 'hook_comment_publish() fired correctly.'); + + // Unpublish comment. + $this->unpublishComment($comment); + $this->assertRaw(t('Comment @subject has been unpublished', array('@subject' => $subject)), 'hook_comment_unpublish() fired correctly.'); + } +} + +/** * Functional tests for the comment module blocks. */ class CommentBlockFunctionalTest extends CommentHelperCase { diff --git a/modules/comment/tests/comment_test.info b/modules/comment/tests/comment_test.info new file mode 100644 index 0000000..b6d9426 --- /dev/null +++ b/modules/comment/tests/comment_test.info @@ -0,0 +1,6 @@ +name = "Comment module tests" +description = "Support module for comment related testing." +package = Testing +version = VERSION +core = 7.x +hidden = TRUE diff --git a/modules/comment/tests/comment_test.module b/modules/comment/tests/comment_test.module new file mode 100644 index 0000000..a2701da --- /dev/null +++ b/modules/comment/tests/comment_test.module @@ -0,0 +1,25 @@ + $comment->subject))); +} + +/** + * Implements hook_comment_unpublish(). + */ +function comment_test_comment_unpublish($comment) { + // Display a message on unpublish. + drupal_set_message(t('Comment @subject has been unpublished', array('@subject' => $comment->subject))); +} diff --git a/modules/forum/forum.module b/modules/forum/forum.module index 575de36..badafbe 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -494,20 +494,6 @@ function forum_comment_publish($comment) { } /** - * Implements hook_comment_update(). - * - * The Comment module doesn't call hook_comment_unpublish() when saving - * individual comments, so we need to check for those here. - */ -function forum_comment_update($comment) { - // comment_save() calls hook_comment_publish() for all published comments, - // so we need to handle all other values here. - if (!$comment->status) { - _forum_update_forum_index($comment->nid); - } -} - -/** * Implements hook_comment_unpublish(). */ function forum_comment_unpublish($comment) { diff --git a/modules/tracker/tracker.module b/modules/tracker/tracker.module index 8694222..38f9c1d 100644 --- a/modules/tracker/tracker.module +++ b/modules/tracker/tracker.module @@ -200,20 +200,6 @@ function tracker_node_delete($node, $arg = 0) { } /** - * Implements hook_comment_update(). - * - * Comment module doesn't call hook_comment_unpublish() when saving individual - * comments so we need to check for those here. - */ -function tracker_comment_update($comment) { - // comment_save() calls hook_comment_publish() for all published comments - // so we need to handle all other values here. - if ($comment->status != COMMENT_PUBLISHED) { - _tracker_remove($comment->nid, $comment->uid, $comment->changed); - } -} - -/** * Implements hook_comment_publish(). * * This actually handles the insert and update of published nodes since