Skip to content

Commit 01c7ce9

Browse files
committed
Add the test case for the updated comment count scenario
1 parent 1da3ad1 commit 01c7ce9

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

tests/phpunit/tests/comment/wpUpdateCommentCountNow.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,94 @@ public function test_using_filter_adjusts_comment_count_without_an_additional_da
4949
public function _return_100() {
5050
return 100;
5151
}
52+
53+
/**
54+
* Test case where a trashed parent comment causes its child comments to be excluded from the comment count.
55+
*
56+
* @return void
57+
*/
58+
public function test_trashed_parent_comment_excludes_child_comments_from_count() {
59+
$post_id = self::factory()->post->create();
60+
61+
/**
62+
* Create 2 parent comment, and 2 child comment for parent 1.
63+
*/
64+
$parent_comment_id = self::factory()->comment->create( array(
65+
'comment_post_ID' => $post_id,
66+
'comment_approved' => 1,
67+
) );
68+
69+
self::factory()->comment->create( array(
70+
'comment_post_ID' => $post_id,
71+
'comment_approved' => 1,
72+
) );
73+
74+
self::factory()->comment->create( array(
75+
'comment_post_ID' => $post_id,
76+
'comment_parent' => $parent_comment_id,
77+
'comment_approved' => 1,
78+
) );
79+
80+
self::factory()->comment->create( array(
81+
'comment_post_ID' => $post_id,
82+
'comment_parent' => $parent_comment_id,
83+
'comment_approved' => 1,
84+
) );
85+
86+
wp_update_comment_count_now( $post_id );
87+
$this->assertSame( '4', get_comments_number( $post_id ) );
88+
89+
wp_update_comment( array(
90+
'comment_ID' => $parent_comment_id,
91+
'comment_approved' => 'trash',
92+
) );
93+
94+
wp_update_comment_count_now( $post_id );
95+
$this->assertSame( '1', get_comments_number( $post_id ) );
96+
}
97+
98+
/**
99+
* Test case for unapproved parent comment causing its child comments to be excluded from the comment count.
100+
*
101+
* @return void
102+
*/
103+
public function test_unapproved_parent_comment_excludes_child_comments_from_count() {
104+
$post_id = self::factory()->post->create();
105+
106+
/**
107+
* Create 2 parent comment, and 2 child comment for parent 1.
108+
*/
109+
$parent_comment_id = self::factory()->comment->create( array(
110+
'comment_post_ID' => $post_id,
111+
'comment_approved' => 1,
112+
) );
113+
114+
self::factory()->comment->create( array(
115+
'comment_post_ID' => $post_id,
116+
'comment_approved' => 1,
117+
) );
118+
119+
self::factory()->comment->create( array(
120+
'comment_post_ID' => $post_id,
121+
'comment_parent' => $parent_comment_id,
122+
'comment_approved' => 1,
123+
) );
124+
125+
self::factory()->comment->create( array(
126+
'comment_post_ID' => $post_id,
127+
'comment_parent' => $parent_comment_id,
128+
'comment_approved' => 1,
129+
) );
130+
131+
wp_update_comment_count_now( $post_id );
132+
$this->assertSame( '4', get_comments_number( $post_id ) );
133+
134+
wp_update_comment( array(
135+
'comment_ID' => $parent_comment_id,
136+
'comment_approved' => '0',
137+
) );
138+
139+
wp_update_comment_count_now( $post_id );
140+
$this->assertSame( '1', get_comments_number( $post_id ) );
141+
}
52142
}

0 commit comments

Comments
 (0)