mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
This function accepts an associative array of option names and their autoload values to set, and it will update those values in the database in bulk, only for those options where the autoload field is not already set to the given value. Two wrapper functions for ease of use accompany the new main function: * `wp_set_options_autoload( $options, $autoload )` can be used to set multiple options to the same autoload value. * `wp_set_option_autoload( $option, $autoload )` can be used to set the autoload value for a single option. All of these functions allow changing the autoload value of an option, which previously has only been possible in combination with updating the value. This limitation prevented some relevant use-cases: For example, a plugin deactivation hook could set all of its options to not autoload, as a cleanup routine, while not actually deleting any data. The plugin's activation hook could then implement the reverse, resetting those options' autoload values to the originally intended ones for when using the plugin. Props boonebgorges, joemcgill, costdev, mukesh27, SergeyBiryukov, tabrisrp, flixos90. Fixes #58964. git-svn-id: https://develop.svn.wordpress.org/trunk@56508 602fd350-edb4-49c9-b593-d223f7449a82
115 lines
3.9 KiB
PHP
115 lines
3.9 KiB
PHP
<?php
|
|
/**
|
|
* Test wp_set_option_autoload().
|
|
*
|
|
* @group option
|
|
*
|
|
* @covers ::wp_set_option_autoload
|
|
*/
|
|
class Tests_Option_WpSetOptionAutoload extends WP_UnitTestCase {
|
|
|
|
/**
|
|
* Tests that setting an option's autoload value to 'yes' works as expected.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_yes() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
$value = 'value';
|
|
|
|
add_option( $option, $value, '', 'no' );
|
|
|
|
$this->assertTrue( wp_set_option_autoload( $option, 'yes' ), 'Function did not succeed' );
|
|
$this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
|
|
$this->assertFalse( wp_cache_get( $option, 'options' ), 'Option not deleted from individual cache' );
|
|
$this->assertFalse( wp_cache_get( 'alloptions', 'options' ), 'Alloptions cache not cleared' );
|
|
}
|
|
|
|
/**
|
|
* Tests that setting an option's autoload value to 'no' works as expected.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_no() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
$value = 'value';
|
|
|
|
add_option( $option, $value, '', 'yes' );
|
|
|
|
$this->assertTrue( wp_set_option_autoload( $option, 'no' ), 'Function did not succeed' );
|
|
$this->assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
|
|
$this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), 'Option not deleted from alloptions cache' );
|
|
}
|
|
|
|
/**
|
|
* Tests that setting an option's autoload value to the same value as prior works as expected.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_same() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
$value = 'value';
|
|
|
|
add_option( $option, $value, '', 'yes' );
|
|
|
|
$this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
|
|
$this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value unexpectedly updated in database' );
|
|
}
|
|
|
|
/**
|
|
* Tests that setting a missing option's autoload value does not do anything.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_missing() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
|
|
$this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
|
|
$this->assertNull( $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Missing option autoload value was set in database' );
|
|
$this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), 'Missing option found in alloptions cache' );
|
|
$this->assertFalse( wp_cache_get( $option, 'options' ), 'Missing option found in individual cache' );
|
|
}
|
|
|
|
/**
|
|
* Tests setting an option's autoload value to boolean true.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_true() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
$value = 'value';
|
|
|
|
add_option( $option, $value, '', false );
|
|
|
|
$this->assertTrue( wp_set_option_autoload( $option, true ), 'Function did not succeed' );
|
|
$this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
|
|
}
|
|
|
|
/**
|
|
* Tests setting an option's autoload value to boolean false.
|
|
*
|
|
* @ticket 58964
|
|
*/
|
|
public function test_wp_set_option_autoload_false() {
|
|
global $wpdb;
|
|
|
|
$option = 'test_option';
|
|
$value = 'value';
|
|
|
|
add_option( $option, $value, '', true );
|
|
|
|
$this->assertTrue( wp_set_option_autoload( $option, false ), 'Function did not succeed' );
|
|
$this->assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
|
|
}
|
|
}
|