@@ -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