Make WordPress Core


Ignore:
Timestamp:
07/23/2024 12:25:19 AM (2 years ago)
Author:
peterwilsoncc
Message:

Options, Meta APIs: Prime notoptions cache when deleting options.

Prime the notoptions cache within delete_option and delete_network_option to avoid the need for a database query if get_option or get_network_option is subsequently called.

Adds some associated tests to ensure that an option is cleared from the notoptions cache when an option is added either via add_option, update_option or their network option equivalent.

Props pbearne, mukesh27.
Fixes #61484.

File:
1 edited

Legend:

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

    r57920 r58782  
    479479                $this->assertFalse( get_option( 'foo' ) );
    480480        }
     481
     482        /**
     483         * Tests that calling delete_option() updates notoptions when option deleted.
     484         *
     485         * @ticket 61484
     486         *
     487         * @covers ::delete_option
     488         */
     489        public function test_check_delete_option_updates_notoptions() {
     490                add_option( 'foo', 'value1' );
     491
     492                delete_option( 'foo' );
     493                $notoptions = wp_cache_get( 'notoptions', 'options' );
     494                $this->assertIsArray( $notoptions, 'The notoptions cache is expected to be an array.' );
     495                $this->assertTrue( $notoptions['foo'], 'The deleted options is expected to be in notoptions.' );
     496
     497                $before = get_num_queries();
     498                get_option( 'foo' );
     499                $queries = get_num_queries() - $before;
     500
     501                $this->assertSame( 0, $queries, 'get_option should not make any database queries.' );
     502        }
     503
     504        /**
     505         * Tests that calling update_option() clears the notoptions cache.
     506         *
     507         * @ticket 61484
     508         *
     509         * @covers ::update_option
     510         */
     511        public function test_update_option_clears_the_notoptions_cache() {
     512                $option_name = 'ticket_61484_option_to_be_created';
     513                $notoptions  = wp_cache_get( 'notoptions', 'options' );
     514                if ( ! is_array( $notoptions ) ) {
     515                        $notoptions = array();
     516                }
     517                $notoptions[ $option_name ] = true;
     518                wp_cache_set( 'notoptions', $notoptions, 'options' );
     519                $this->assertArrayHasKey( $option_name, wp_cache_get( 'notoptions', 'options' ), 'The "foobar" option should be in the notoptions cache.' );
     520
     521                update_option( $option_name, 'baz' );
     522
     523                $updated_notoptions = wp_cache_get( 'notoptions', 'options' );
     524                $this->assertArrayNotHasKey( $option_name, $updated_notoptions, 'The "foobar" option should not be in the notoptions cache after updating it.' );
     525        }
     526
     527        /**
     528         * Tests that calling add_option() clears the notoptions cache.
     529         *
     530         * @ticket 61484
     531         *
     532         * @covers ::add_option
     533         */
     534        public function test_add_option_clears_the_notoptions_cache() {
     535                $option_name = 'ticket_61484_option_to_be_created';
     536                $notoptions  = wp_cache_get( 'notoptions', 'options' );
     537                if ( ! is_array( $notoptions ) ) {
     538                        $notoptions = array();
     539                }
     540                $notoptions[ $option_name ] = true;
     541                wp_cache_set( 'notoptions', $notoptions, 'options' );
     542                $this->assertArrayHasKey( $option_name, wp_cache_get( 'notoptions', 'options' ), 'The "foobar" option should be in the notoptions cache.' );
     543
     544                add_option( $option_name, 'baz' );
     545
     546                $updated_notoptions = wp_cache_get( 'notoptions', 'options' );
     547                $this->assertArrayNotHasKey( $option_name, $updated_notoptions, 'The "foobar" option should not be in the notoptions cache after adding it.' );
     548        }
    481549}
Note: See TracChangeset for help on using the changeset viewer.