Invalidate comment query cache when modifying comment meta.

Comment queries are sensitive to comment meta due to the `meta_query`
parameter, so the cache must be invalidated when comment meta is changed,
added, or deleted.

Props spacedmonkey.
See #40669.

git-svn-id: https://develop.svn.wordpress.org/trunk@41848 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2017-10-12 14:58:58 +00:00
parent ee44859ee4
commit 6ce4e413ce
2 changed files with 150 additions and 3 deletions

View File

@@ -3025,4 +3025,139 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEquals( $number_of_queries, $wpdb->num_queries );
}
/**
* @ticket 40669
*/
public function test_add_comment_meta_should_invalidate_query_cache() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$c1 = self::factory()->comment->create_post_comments( $p, 1 );
$c2 = self::factory()->comment->create_post_comments( $p, 1 );
foreach ( $c1 as $cid ) {
add_comment_meta( $cid, 'sauce', 'fire' );
}
$cached = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( $c1, $cached );
foreach ( $c2 as $cid ) {
add_comment_meta( $cid, 'sauce', 'fire' );
}
$found = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( array_merge( $c1, $c2 ), $found );
}
/**
* @ticket 40669
*/
public function test_update_comment_meta_should_invalidate_query_cache() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$c1 = self::factory()->comment->create_post_comments( $p, 1 );
$c2 = self::factory()->comment->create_post_comments( $p, 1 );
foreach ( array_merge( $c1, $c2 ) as $cid ) {
add_comment_meta( $cid, 'sauce', 'fire' );
}
$cached = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( array_merge( $c1, $c2 ), $cached );
foreach ( $c2 as $cid ) {
update_comment_meta( $cid, 'sauce', 'foo' );
}
$found = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( $c1, $found );
}
/**
* @ticket 40669
*/
public function test_delete_comment_meta_should_invalidate_query_cache() {
global $wpdb;
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$c1 = self::factory()->comment->create_post_comments( $p, 1 );
$c2 = self::factory()->comment->create_post_comments( $p, 1 );
foreach ( array_merge( $c1, $c2 ) as $cid ) {
add_comment_meta( $cid, 'sauce', 'fire' );
}
$cached = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( array_merge( $c1, $c2 ), $cached );
foreach ( $c2 as $cid ) {
delete_comment_meta( $cid, 'sauce' );
}
$found = get_comments( array(
'post_id' => $p,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'sauce',
'value' => 'fire',
),
)
) );
$this->assertEqualSets( $c1, $found );
}
}