From da0a823129c286471950b95d196525a69580a66e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 30 Aug 2015 02:18:18 +0000 Subject: [PATCH] `get_comment_count()` currently increments `awaiting_moderation` when comments are in the trash. This occurs because `case 0:` will match any value passed to `switch` that is a string that isn't specified in the list of cases. This is terrifying. * Cases for `0` and `1` should be `'1'` and `'0'` * Add unit tests for `get_comment_count()`. Currently, there are none. See #33414. git-svn-id: https://develop.svn.wordpress.org/trunk@33806 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-functions.php | 4 +- .../phpunit/tests/comment/getCommentCount.php | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/comment/getCommentCount.php diff --git a/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index 26d66cd2fa..930d1897f7 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/wp-includes/comment-functions.php @@ -375,11 +375,11 @@ function get_comment_count( $post_id = 0 ) { $comment_count['spam'] = $row['total']; $comment_count["total_comments"] += $row['total']; break; - case 1: + case '1': $comment_count['approved'] = $row['total']; $comment_count['total_comments'] += $row['total']; break; - case 0: + case '0': $comment_count['awaiting_moderation'] = $row['total']; $comment_count['total_comments'] += $row['total']; break; diff --git a/tests/phpunit/tests/comment/getCommentCount.php b/tests/phpunit/tests/comment/getCommentCount.php new file mode 100644 index 0000000000..fa78784f06 --- /dev/null +++ b/tests/phpunit/tests/comment/getCommentCount.php @@ -0,0 +1,65 @@ +assertEquals( 0, $count['approved'] ); + $this->assertEquals( 0, $count['awaiting_moderation'] ); + $this->assertEquals( 0, $count['spam'] ); + $this->assertEquals( 0, $count['total_comments'] ); + } + + public function test_get_comment_count_approved() { + $this->factory->comment->create( array( + 'comment_approved' => 1 + ) ); + + $count = get_comment_count(); + + $this->assertEquals( 1, $count['approved'] ); + $this->assertEquals( 0, $count['awaiting_moderation'] ); + $this->assertEquals( 0, $count['spam'] ); + $this->assertEquals( 1, $count['total_comments'] ); + } + + public function test_get_comment_count_awaiting() { + $this->factory->comment->create( array( + 'comment_approved' => 0 + ) ); + + $count = get_comment_count(); + + $this->assertEquals( 0, $count['approved'] ); + $this->assertEquals( 1, $count['awaiting_moderation'] ); + $this->assertEquals( 0, $count['spam'] ); + $this->assertEquals( 1, $count['total_comments'] ); + } + + public function test_get_comment_count_spam() { + $this->factory->comment->create( array( + 'comment_approved' => 'spam' + ) ); + + $count = get_comment_count(); + + $this->assertEquals( 0, $count['approved'] ); + $this->assertEquals( 0, $count['awaiting_moderation'] ); + $this->assertEquals( 1, $count['spam'] ); + $this->assertEquals( 1, $count['total_comments'] ); + } + + public function test_get_comment_count_trash() { + $this->factory->comment->create( array( + 'comment_approved' => 'trash' + ) ); + + $count = get_comment_count(); + + $this->assertEquals( 0, $count['approved'] ); + $this->assertEquals( 0, $count['awaiting_moderation'] ); + $this->assertEquals( 0, $count['spam'] ); + $this->assertEquals( 0, $count['total_comments'] ); + } +}