diff --git a/sync/class.jetpack-sync-module-comments.php b/sync/class.jetpack-sync-module-comments.php index d19f16deaa354..992f191a57d7d 100644 --- a/sync/class.jetpack-sync-module-comments.php +++ b/sync/class.jetpack-sync-module-comments.php @@ -22,6 +22,8 @@ public function init_listeners( $callable ) { add_action( 'spammed_comment', $callable ); add_action( 'trashed_post_comments', $callable, 10, 2 ); add_action( 'untrash_post_comments', $callable ); + add_action( 'comment_approved_to_unapproved', $callable ); + add_action( 'comment_unapproved_to_approved', $callable ); // even though it's messy, we implement these hooks because // the edit_comment hook doesn't include the data diff --git a/tests/php/sync/test_class.jetpack-sync-comments.php b/tests/php/sync/test_class.jetpack-sync-comments.php index 049d20b5780c3..b1b721260839a 100644 --- a/tests/php/sync/test_class.jetpack-sync-comments.php +++ b/tests/php/sync/test_class.jetpack-sync-comments.php @@ -47,6 +47,38 @@ public function test_update_comment() { $this->assertEquals( "foo bar baz", $remote_comment->comment_content ); } + public function test_unapprove_comment() { + + $this->assertEquals( 1, $this->server_replica_storage->comment_count( 'approve' ) ); + $this->comment->comment_approved = 0; + wp_update_comment( (array) $this->comment ); + + $this->sender->do_sync(); + + //Test both sync actions we're expecting + $this->assertEquals( 0, $this->server_replica_storage->comment_count( 'approve' ) ); + $remote_comment = $this->server_replica_storage->get_comment( $this->comment->comment_ID ); + $this->assertEquals( 0, $remote_comment->comment_approved ); + $comment_unapproved_event = $this->server_event_storage->get_most_recent_event( 'comment_unapproved_' ); + $this->assertTrue( (bool) $comment_unapproved_event ); + + $comment_approved_to_unapproved_event = $this->server_event_storage->get_most_recent_event( 'comment_approved_to_unapproved' ); + $this->assertTrue( (bool) $comment_approved_to_unapproved_event ); + + //Test both sync actions again, this time without causing a change in state (comment_unapproved_ remains true despite no state change, while comment_approved_to_unapproved does not) + + $this->server_event_storage->reset(); + + wp_update_comment( (array) $this->comment ); + $this->sender->do_sync(); + + $comment_unapproved_event = $this->server_event_storage->get_most_recent_event( 'comment_unapproved_' ); + $this->assertTrue( (bool) $comment_unapproved_event ); + + $comment_approved_to_unapproved_event = $this->server_event_storage->get_most_recent_event( 'comment_approved_to_unapproved' ); + $this->assertFalse( (bool) $comment_approved_to_unapproved_event ); + } + public function test_trash_comment_trashes_data() { $this->assertEquals( 1, $this->server_replica_storage->comment_count( 'approve' ) ); wp_delete_comment( $this->comment->comment_ID ); @@ -75,6 +107,13 @@ public function test_wp_trash_comment() { $this->assertEquals( 0, $this->server_replica_storage->comment_count( 'approve' ) ); $this->assertEquals( 1, $this->server_replica_storage->comment_count( 'trash' ) ); + + //Test that you don't get an event back when you try to trash the same comment again + $this->server_event_storage->reset(); + wp_trash_comment( $this->comment->comment_ID ); + $this->sender->do_sync(); + $event = $this->server_event_storage->get_most_recent_event( 'trashed_comment' ); + $this->assertFalse( $event ); } public function test_wp_untrash_comment() {