| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group comment |
| | 5 | * @covers wp_update_comment_count_now |
| | 6 | */ |
| | 7 | class Tests_Update_Comment_Count_Now extends WP_UnitTestCase { |
| | 8 | public function _return_100() { |
| | 9 | return 100; |
| | 10 | } |
| | 11 | |
| | 12 | public function test_invalid_post_bails_early() { |
| | 13 | $this->assertFalse( wp_update_comment_count_now( 100 ) ); |
| | 14 | $this->assertFalse( wp_update_comment_count_now( null ) ); |
| | 15 | $this->assertFalse( wp_update_comment_count_now( 0 ) ); |
| | 16 | } |
| | 17 | |
| | 18 | public function test_regular_post_updates_comment_count() { |
| | 19 | global $wpdb; |
| | 20 | |
| | 21 | $post_id = self::factory()->post->create(); |
| | 22 | |
| | 23 | self::factory()->comment->create_post_comments( $post_id, 1 ); |
| | 24 | $this->assertSame( '1', get_comments_number( $post_id ) ); |
| | 25 | |
| | 26 | $num_queries = $wpdb->num_queries; |
| | 27 | $this->assertTrue( wp_update_comment_count_now( $post_id ) ); |
| | 28 | $this->assertSame( $num_queries + 2, $wpdb->num_queries ); |
| | 29 | |
| | 30 | $this->assertSame( '1', get_comments_number( $post_id ) ); |
| | 31 | } |
| | 32 | |
| | 33 | public function test_using_filter_adjusts_comment_count_without_an_additional_database_query() { |
| | 34 | global $wpdb; |
| | 35 | |
| | 36 | add_filter( 'wp_update_comment_count_now', array( $this, '_return_100' ) ); |
| | 37 | |
| | 38 | $post_id = self::factory()->post->create(); |
| | 39 | |
| | 40 | self::factory()->comment->create_post_comments( $post_id, 1 ); |
| | 41 | $this->assertSame( '100', get_comments_number( $post_id ) ); |
| | 42 | |
| | 43 | $num_queries = $wpdb->num_queries; |
| | 44 | $this->assertTrue( wp_update_comment_count_now( $post_id ) ); |
| | 45 | // Only one query is made instead of two. |
| | 46 | $this->assertSame( $num_queries + 1, $wpdb->num_queries ); |
| | 47 | |
| | 48 | $this->assertSame( '100', get_comments_number( $post_id ) ); |
| | 49 | |
| | 50 | remove_filter( 'wp_update_comment_count_now', array( $this, '_return_100' ) ); |
| | 51 | } |
| | 52 | } |