WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/05/15 19:13:00 (2 years ago)
Author:
boonebgorges
Message:

Introduce $autoload parameter to update_option().

When creating an option via add_option(), the $autoload param allows you to
tell WP whether the option should be loaded as part of the 'alloptions' cache
during every pageload. update_option(), when used with a non-existent option
calls add_option() internally. The new $autoload param in update_option()
is passed along to add_option() in cases where the option does not yet exist.

The associated unit tests are skipped on multisite due to an issue that causes
WP_INSTALLING to force cache misses. See #31130.

Props codix, nofearinc, MikeHansenMe.
Fixes #26394.

File:
1 edited

Legend:

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

    r31473 r31628  
    150150        $this->assertEquals( $expected, $actual->autoload ); 
    151151    } 
     152 
     153    /** 
     154     * @ticket 26394 
     155     */ 
     156    public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() { 
     157        if ( is_multisite() ) { 
     158            $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' ); 
     159        } 
     160 
     161        global $wpdb; 
     162        wp_cache_flush(); 
     163        update_option( 'test_update_option_default', 'value' ); 
     164        wp_cache_flush(); 
     165 
     166        // Populate the alloptions cache, which includes autoload=yes options. 
     167        wp_load_alloptions(); 
     168 
     169        $before = $wpdb->num_queries; 
     170        $value = get_option( 'test_update_option_default' ); 
     171        $after = $wpdb->num_queries; 
     172 
     173        $this->assertEquals( $before, $after ); 
     174        $this->assertEquals( $value, 'value' ); 
     175    } 
     176 
     177    /** 
     178     * @ticket 26394 
     179     */ 
     180    public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() { 
     181        if ( is_multisite() ) { 
     182            $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' ); 
     183        } 
     184 
     185        global $wpdb; 
     186        wp_cache_flush(); 
     187        update_option( 'test_update_option_default', 'value', 'yes' ); 
     188        wp_cache_flush(); 
     189 
     190        // Populate the alloptions cache, which includes autoload=yes options. 
     191        wp_load_alloptions(); 
     192 
     193        $before = $wpdb->num_queries; 
     194        $value = get_option( 'test_update_option_default' ); 
     195        $after = $wpdb->num_queries; 
     196 
     197        $this->assertEquals( $before, $after ); 
     198        $this->assertEquals( $value, 'value' ); 
     199    } 
     200 
     201    /** 
     202     * @ticket 26394 
     203     */ 
     204    public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_no() { 
     205        if ( is_multisite() ) { 
     206            $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' ); 
     207        } 
     208 
     209        global $wpdb; 
     210        wp_cache_flush(); 
     211        update_option( 'test_update_option_default', 'value', 'no' ); 
     212        wp_cache_flush(); 
     213 
     214        // Populate the alloptions cache, which does not include autoload=no options. 
     215        wp_load_alloptions(); 
     216 
     217        $before = $wpdb->num_queries; 
     218        $value = get_option( 'test_update_option_default' ); 
     219        $after = $wpdb->num_queries; 
     220 
     221        // Database has been hit. 
     222        $this->assertEquals( $before + 1, $after ); 
     223        $this->assertEquals( $value, 'value' ); 
     224    } 
     225 
     226    /** 
     227     * @ticket 26394 
     228     */ 
     229    public function test_update_option_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_false() { 
     230        if ( is_multisite() ) { 
     231            $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' ); 
     232        } 
     233 
     234        global $wpdb; 
     235        wp_cache_flush(); 
     236        update_option( 'test_update_option_default', 'value', false ); 
     237        wp_cache_flush(); 
     238 
     239        // Populate the alloptions cache, which does not include autoload=no options. 
     240        wp_load_alloptions(); 
     241 
     242        $before = $wpdb->num_queries; 
     243        $value = get_option( 'test_update_option_default' ); 
     244        $after = $wpdb->num_queries; 
     245 
     246        // Database has been hit. 
     247        $this->assertEquals( $before + 1, $after ); 
     248        $this->assertEquals( $value, 'value' ); 
     249    } 
    152250} 
Note: See TracChangeset for help on using the changeset viewer.