Make WordPress Core

Changeset 65122


Ignore:
Timestamp:
10/31/2026 04:41:03 PM (12 days ago)
Author:
lancewillett
Message:

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: ​https://github.com/WordPress/wordpress-develop/pull/12642, ​https://github.com/WordPress/wordpress-develop/pull/13226

Props jorgelopezrepiso, sainathpoojary.
See #65693, #49799.

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/cache.php

    r64746 r65122  
    459459                wp_cache_set( 'foo1', 'bar', 'group1' );
    460460                wp_cache_set( 'foo2', 'bar', 'group1' );
    461                 wp_cache_set( 'foo1', 'bar', 'group2' );
    462 
    463                 $found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' );
     461                wp_cache_set( 'foo3', 'bar', 'group2' );
     462                wp_cache_set( 'foo1', 'bar2', 'group2' );
     463
     464                $found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3', 'foo4' ), 'group1' );
    464465
    465466                $expected = array(
    … …  
    467468                        'foo2' => 'bar',
    468469                        'foo3' => false,
     470                        'foo4' => false,
    469471                );
    470472
    … …  
    481483
    482484                $found = wp_cache_delete_multiple(
    483                         array( 'foo1', 'foo2', 'foo3' ),
     485                        array( 'foo1', 'foo2', 'foo3', 'foo4' ),
    484486                        'group1'
    485487                );
    … …  
    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}
  • trunk/tests/phpunit/tests/term/getTheTerms.php

    r65014 r65122  
    226226        /**
    227227         * @ticket 40306
    228          */
    229         public function test_term_cache_should_be_invalidated_on_set_object_terms() {
     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.
     233         */
     234        public function test_term_cache_should_be_invalidated_on_set_object_terms( $priming_function ) {
    230235                register_taxonomy( 'wptests_tax', 'post' );
    231236
    … …  
    242247                $post_id = self::factory()->post->create();
    243248
    244                 // Prime cache.
    245                 get_the_terms( $post_id, 'wptests_tax' );
    246 
    247                 wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
    248 
    249                 $terms = get_the_terms( $post_id, 'wptests_tax' );
    250 
    251                 // Re-activate term counting so this doesn't affect other tests.
    252                 wp_defer_term_counting( false );
    253 
    254                 $this->assertIsArray( $terms );
    255                 $this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
     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                        }
     256
     257                        $cached = wp_cache_get( $post_id, 'wptests_tax_relationships' );
     258                        $this->assertIsArray( $cached );
     259                        $this->assertEmpty( $cached );
     260
     261                        wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
     262
     263                        $this->assertFalse( wp_cache_get( $post_id, 'wptests_tax_relationships' ) );
     264
     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
    … …  
    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}
Note: See TracChangeset for help on using the changeset viewer.