Skip to content

Commit 6b3cdb8

Browse files
committed
Tests: Strengthen object-cache assertions.
Verify cache-group separation, missing entries, and bulk deletion. Cover empty and populated term relationship caches, including invalidation during deferred term counting. Developed in: #12642, #13226 Props jorgelopezrepiso, sainathpoojary. See #65693, #49799. git-svn-id: https://develop.svn.wordpress.org/trunk@65122 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 78ec0a7 commit 6b3cdb8

2 files changed

Lines changed: 106 additions & 12 deletions

File tree

‎tests/phpunit/tests/cache.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,16 @@ public function test_wp_cache_set_multiple() {
458458
public function test_wp_cache_get_multiple() {
459459
wp_cache_set( 'foo1', 'bar', 'group1' );
460460
wp_cache_set( 'foo2', 'bar', 'group1' );
461-
wp_cache_set( 'foo1', 'bar', 'group2' );
461+
wp_cache_set( 'foo3', 'bar', 'group2' );
462+
wp_cache_set( 'foo1', 'bar2', 'group2' );
462463

463-
$found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' );
464+
$found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3', 'foo4' ), 'group1' );
464465

465466
$expected = array(
466467
'foo1' => 'bar',
467468
'foo2' => 'bar',
468469
'foo3' => false,
470+
'foo4' => false,
469471
);
470472

471473
$this->assertSame( $expected, $found );
@@ -480,16 +482,24 @@ public function test_wp_cache_delete_multiple() {
480482
wp_cache_set( 'foo3', 'bar', 'group2' );
481483

482484
$found = wp_cache_delete_multiple(
483-
array( 'foo1', 'foo2', 'foo3' ),
485+
array( 'foo1', 'foo2', 'foo3', 'foo4' ),
484486
'group1'
485487
);
486488

487489
$expected = array(
488490
'foo1' => true,
489491
'foo2' => true,
490492
'foo3' => false,
493+
'foo4' => false,
491494
);
492495

493496
$this->assertSame( $expected, $found );
497+
498+
// Verify that foo1 and foo2 were actually deleted from group1.
499+
$this->assertFalse( wp_cache_get( 'foo1', 'group1' ) );
500+
$this->assertFalse( wp_cache_get( 'foo2', 'group1' ) );
501+
502+
// Verify that foo3 in group2 was untouched.
503+
$this->assertSame( 'bar', wp_cache_get( 'foo3', 'group2' ) );
494504
}
495505
}

‎tests/phpunit/tests/term/getTheTerms.php‎

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,13 @@ public function test_uncached_terms_should_not_be_primed_with_a_single_query_by_
225225

226226
/**
227227
* @ticket 40306
228+
* @ticket 49799
229+
*
230+
* @dataProvider data_term_cache_priming_functions
231+
*
232+
* @param string $priming_function Function used to prime the empty relationship cache.
228233
*/
229-
public function test_term_cache_should_be_invalidated_on_set_object_terms() {
234+
public function test_term_cache_should_be_invalidated_on_set_object_terms( $priming_function ) {
230235
register_taxonomy( 'wptests_tax', 'post' );
231236

232237
// Temporarily disable term counting, which performs its own cache invalidation.
@@ -241,18 +246,42 @@ public function test_term_cache_should_be_invalidated_on_set_object_terms() {
241246

242247
$post_id = self::factory()->post->create();
243248

244-
// Prime cache.
245-
get_the_terms( $post_id, 'wptests_tax' );
249+
try {
250+
// Prime cache with an empty array.
251+
if ( 'update_object_term_cache' === $priming_function ) {
252+
update_object_term_cache( array( $post_id ), array( 'post' ) );
253+
} else {
254+
get_the_terms( $post_id, 'wptests_tax' );
255+
}
246256

247-
wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
257+
$cached = wp_cache_get( $post_id, 'wptests_tax_relationships' );
258+
$this->assertIsArray( $cached );
259+
$this->assertEmpty( $cached );
248260

249-
$terms = get_the_terms( $post_id, 'wptests_tax' );
261+
wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
250262

251-
// Re-activate term counting so this doesn't affect other tests.
252-
wp_defer_term_counting( false );
263+
$this->assertFalse( wp_cache_get( $post_id, 'wptests_tax_relationships' ) );
253264

254-
$this->assertIsArray( $terms );
255-
$this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
265+
$terms = get_the_terms( $post_id, 'wptests_tax' );
266+
267+
$this->assertIsArray( $terms );
268+
$this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
269+
} finally {
270+
// Re-activate term counting so this doesn't affect other tests.
271+
wp_defer_term_counting( false );
272+
}
273+
}
274+
275+
/**
276+
* Data provider for test_term_cache_should_be_invalidated_on_set_object_terms().
277+
*
278+
* @return array[]
279+
*/
280+
public static function data_term_cache_priming_functions() {
281+
return array(
282+
'get_the_terms' => array( 'get_the_terms' ),
283+
'update_object_term_cache' => array( 'update_object_term_cache' ),
284+
);
256285
}
257286

258287
/**
@@ -289,4 +318,59 @@ public function test_term_cache_should_be_invalidated_on_remove_object_terms() {
289318
$this->assertIsArray( $terms );
290319
$this->assertSame( array( $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
291320
}
321+
322+
/**
323+
* Tests that get_the_terms() returns false for a post with no terms even when
324+
* update_object_term_cache() has primed the cache with an empty array.
325+
*
326+
* @ticket 49799
327+
*/
328+
public function test_get_the_terms_returns_false_for_post_with_no_terms_when_cache_primed() {
329+
register_taxonomy( 'wptests_tax', 'post' );
330+
331+
$post_id = self::factory()->post->create();
332+
333+
// Prime cache with an empty array.
334+
update_object_term_cache( array( $post_id ), array( 'post' ) );
335+
336+
$cached = wp_cache_get( $post_id, 'wptests_tax_relationships' );
337+
$this->assertIsArray( $cached );
338+
$this->assertEmpty( $cached );
339+
340+
$num_queries_before = get_num_queries();
341+
$terms = get_the_terms( $post_id, 'wptests_tax' );
342+
343+
$this->assertSame( 0, get_num_queries() - $num_queries_before );
344+
$this->assertFalse( $terms );
345+
}
346+
347+
/**
348+
* Tests that get_the_terms() returns correct terms after update_object_term_cache()
349+
* is called following term assignment.
350+
*
351+
* @ticket 49799
352+
*/
353+
public function test_get_the_terms_returns_correct_terms_when_cache_primed_after_assignment() {
354+
register_taxonomy( 'wptests_tax', 'post' );
355+
356+
$post_id = self::factory()->post->create();
357+
$term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
358+
359+
wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
360+
361+
// Prime cache after assignment.
362+
update_object_term_cache( array( $post_id ), array( 'post' ) );
363+
364+
$cached = wp_cache_get( $post_id, 'wptests_tax_relationships' );
365+
$this->assertIsArray( $cached );
366+
$this->assertNotEmpty( $cached );
367+
368+
$num_queries_before = get_num_queries();
369+
$terms = get_the_terms( $post_id, 'wptests_tax' );
370+
371+
$this->assertSame( 0, get_num_queries() - $num_queries_before );
372+
$this->assertIsArray( $terms );
373+
$this->assertCount( 1, $terms );
374+
$this->assertSame( $term_id, (int) $terms[0]->term_id );
375+
}
292376
}

0 commit comments

Comments
 (0)