Make WordPress Core


Ignore:
Timestamp:
09/25/2015 05:10:40 AM (10 years ago)
Author:
boonebgorges
Message:

Use WP_Comment_Query in get_page_of_comment().

This change allows get_page_of_comment() to use WP_Comment_Query's native
caching mechanisms.

Props boonebgorges, Viper007Bond, wmertens, jeremyfelt.
Fixes #11334.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r31289 r34535  
    66 */
    77class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
    8 
    9     public function setUp() {
    10         parent::setUp();
    11     }
    128
    139    public function test_last_comment() {
     
    3935        $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
    4036    }
     37
     38    public function test_type_pings() {
     39        $p = $this->factory->post->create();
     40        $now = time();
     41
     42        $trackbacks = array();
     43        for ( $i = 0; $i <= 3; $i++ ) {
     44            $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     45            $now -= 10 * $i;
     46        }
     47
     48        $pingbacks = array();
     49        for ( $i = 0; $i <= 6; $i++ ) {
     50            $pingbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     51            $now -= 10 * $i;
     52        }
     53
     54        $this->assertEquals( 2, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'trackback' ) ) );
     55        $this->assertEquals( 3, get_page_of_comment( $pingbacks[0], array( 'per_page' => 2, 'type' => 'pingback' ) ) );
     56        $this->assertEquals( 5, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'pings' ) ) );
     57    }
     58
     59    /**
     60     * @ticket 11334
     61     */
     62    public function test_subsequent_calls_should_hit_cache() {
     63        global $wpdb;
     64
     65        $p = $this->factory->post->create();
     66        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     67
     68        // Prime cache.
     69        $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     70
     71        $num_queries = $wpdb->num_queries;
     72        $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     73
     74        $this->assertSame( $page_1, $page_2 );
     75        $this->assertSame( $num_queries, $wpdb->num_queries );
     76    }
     77
     78    /**
     79     * @ticket 11334
     80     */
     81    public function test_cache_hits_should_be_sensitive_to_comment_type() {
     82        global $wpdb;
     83
     84        $p = $this->factory->post->create();
     85        $comment = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
     86
     87        $now = time();
     88        $trackbacks = array();
     89        for ( $i = 0; $i <= 5; $i++ ) {
     90            $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
     91        }
     92
     93        // Prime cache for trackbacks.
     94        $page_trackbacks = get_page_of_comment( $trackbacks[1], array( 'per_page' => 3, 'type' => 'trackback' ) );
     95        $this->assertEquals( 2, $page_trackbacks );
     96
     97        $num_queries = $wpdb->num_queries;
     98        $page_comments = get_page_of_comment( $comment, array( 'per_page' => 3, 'type' => 'comment' ) );
     99        $this->assertEquals( 1, $page_comments );
     100
     101        $this->assertNotEquals( $num_queries, $wpdb->num_queries );
     102    }
     103
     104    /**
     105     * @ticket 11334
     106     */
     107    public function test_cache_should_be_invalidated_when_comment_is_approved() {
     108        $p = $this->factory->post->create();
     109        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
     110
     111        // Prime cache.
     112        $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     113
     114        // Approve comment.
     115        wp_set_comment_status( $c, 'approve' );
     116
     117        $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     118    }
     119
     120    /**
     121     * @ticket 11334
     122     */
     123    public function test_cache_should_be_invalidated_when_comment_is_deleted() {
     124        $p = $this->factory->post->create();
     125        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     126
     127        // Prime cache.
     128        $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     129
     130        // Trash comment.
     131        wp_trash_comment( $c );
     132
     133        $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     134    }
     135
     136    /**
     137     * @ticket 11334
     138     */
     139    public function test_cache_should_be_invalidated_when_comment_is_spammed() {
     140        $p = $this->factory->post->create();
     141        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     142
     143        // Prime cache.
     144        $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
     145
     146        // Spam comment.
     147        wp_spam_comment( $c );
     148
     149        $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
     150    }
     151
     152    /**
     153     * @ticket 11334
     154     */
     155    public function test_cache_should_be_invalidated_when_older_comment_is_published() {
     156        $now = time();
     157
     158        $p = $this->factory->post->create();
     159        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     160        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
     161        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
     162
     163        $this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
     164
     165        wp_set_comment_status( $c3, '1' );
     166
     167        $this->assertEquals( 2, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
     168    }
    41169}
Note: See TracChangeset for help on using the changeset viewer.