From 464046178d7446c5daf3cfd4c53530dc55b69104 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 5 Nov 2026 19:11:14 -0300 Subject: [PATCH 1/2] Options: Add constants for the option autoload values. Introduces the `WP_Option_Autoload` class, which holds the values stored in the `autoload` column of the options table, and uses it in the Option API instead of repeating the strings. There is no change in behavior: the stored values, return values and filters stay the same strings. Follow-up to [57920], [58105]. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MKNCpEmZxEKHTbyKtmgpzZ --- src/wp-includes/class-wp-option-autoload.php | 86 +++++++++++++++++++ src/wp-includes/functions.php | 1 + src/wp-includes/option.php | 47 +++++----- .../phpunit/tests/option/wpOptionAutoload.php | 34 ++++++++ 4 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 src/wp-includes/class-wp-option-autoload.php create mode 100644 tests/phpunit/tests/option/wpOptionAutoload.php diff --git a/src/wp-includes/class-wp-option-autoload.php b/src/wp-includes/class-wp-option-autoload.php new file mode 100644 index 0000000000000..e706a577210c7 --- /dev/null +++ b/src/wp-includes/class-wp-option-autoload.php @@ -0,0 +1,86 @@ + array(), - 'off' => array(), + WP_Option_Autoload::ON => array(), + WP_Option_Autoload::OFF => array(), ); $results = array(); foreach ( $options as $option => $autoload ) { @@ -413,10 +413,10 @@ function wp_set_option_autoload_values( array $options ) { * Sanitize autoload value and categorize accordingly. * The values 'yes', 'no', 'on', and 'off' are supported for backward compatibility. */ - if ( 'off' === $autoload || 'no' === $autoload || false === $autoload ) { - $grouped_options['off'][] = $option; + if ( WP_Option_Autoload::OFF === $autoload || WP_Option_Autoload::LEGACY_NO === $autoload || false === $autoload ) { + $grouped_options[ WP_Option_Autoload::OFF ][] = $option; } else { - $grouped_options['on'][] = $option; + $grouped_options[ WP_Option_Autoload::ON ][] = $option; } $results[ $option ] = false; // Initialize result value. } @@ -446,7 +446,7 @@ function wp_set_option_autoload_values( array $options ) { return $results; } - // Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'yes' or 'no'. + // Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'on' or 'off'. foreach ( $grouped_options as $autoload => $options ) { if ( ! $options ) { continue; @@ -486,13 +486,13 @@ function wp_set_option_autoload_values( array $options ) { * cache. This is not necessary when options were changed to 'on', since in that situation the entire cache is * deleted anyway. */ - if ( $grouped_options['on'] ) { - wp_cache_delete_multiple( $grouped_options['on'], 'options' ); + if ( $grouped_options[ WP_Option_Autoload::ON ] ) { + wp_cache_delete_multiple( $grouped_options[ WP_Option_Autoload::ON ], 'options' ); wp_cache_delete( 'alloptions', 'options' ); - } elseif ( $grouped_options['off'] ) { + } elseif ( $grouped_options[ WP_Option_Autoload::OFF ] ) { $alloptions = wp_load_alloptions( true ); - foreach ( $grouped_options['off'] as $option ) { + foreach ( $grouped_options[ WP_Option_Autoload::OFF ] as $option ) { if ( isset( $alloptions[ $option ] ) ) { unset( $alloptions[ $option ] ); } @@ -951,7 +951,7 @@ function update_option( $option, $value, $autoload = null ) { } else { // Retrieve the current autoload value to reevaluate it in case it was set automatically. $raw_autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); - $allow_values = array( 'auto-on', 'auto-off', 'auto' ); + $allow_values = array( WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO_OFF, WP_Option_Autoload::AUTO ); if ( in_array( $raw_autoload, $allow_values, true ) ) { $autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ); if ( $autoload !== $raw_autoload ) { @@ -1309,16 +1309,16 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value, // Check if autoload is a boolean. if ( is_bool( $autoload ) ) { - return $autoload ? 'on' : 'off'; + return $autoload ? WP_Option_Autoload::ON : WP_Option_Autoload::OFF; } switch ( $autoload ) { - case 'on': - case 'yes': - return 'on'; - case 'off': - case 'no': - return 'off'; + case WP_Option_Autoload::ON: + case WP_Option_Autoload::LEGACY_YES: + return WP_Option_Autoload::ON; + case WP_Option_Autoload::OFF: + case WP_Option_Autoload::LEGACY_NO: + return WP_Option_Autoload::OFF; } /** @@ -1334,10 +1334,10 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value, */ $autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value ); if ( is_bool( $autoload ) ) { - return $autoload ? 'auto-on' : 'auto-off'; + return $autoload ? WP_Option_Autoload::AUTO_ON : WP_Option_Autoload::AUTO_OFF; } - return 'auto'; + return WP_Option_Autoload::AUTO; } /** @@ -3348,7 +3348,12 @@ function filter_default_option( $default_value, $option, $passed_default ) { * @return string[] The values that trigger autoloading. */ function wp_autoload_values_to_autoload() { - $autoload_values = array( 'yes', 'on', 'auto-on', 'auto' ); + $autoload_values = array( + WP_Option_Autoload::LEGACY_YES, + WP_Option_Autoload::ON, + WP_Option_Autoload::AUTO_ON, + WP_Option_Autoload::AUTO, + ); /** * Filters the autoload values that should be considered for autoloading from the options table. diff --git a/tests/phpunit/tests/option/wpOptionAutoload.php b/tests/phpunit/tests/option/wpOptionAutoload.php new file mode 100644 index 0000000000000..8e55af124756f --- /dev/null +++ b/tests/phpunit/tests/option/wpOptionAutoload.php @@ -0,0 +1,34 @@ +assertSame( + array( + 'ON' => 'on', + 'OFF' => 'off', + 'AUTO' => 'auto', + 'AUTO_ON' => 'auto-on', + 'AUTO_OFF' => 'auto-off', + 'LEGACY_YES' => 'yes', + 'LEGACY_NO' => 'no', + ), + ( new ReflectionClass( 'WP_Option_Autoload' ) )->getConstants() + ); + } +} From 8394920e28c1a6f42a802783bb764d30dd571873 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 6 Nov 2026 07:53:34 -0300 Subject: [PATCH 2/2] Tests: Use the autoload constants in the option tests. Uses the `WP_Option_Autoload` constants for expected values and for current input values. The legacy 'yes' and 'no' inputs stay as plain strings, since those tests check backward compatibility. Follow-up to [58105]. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MKNCpEmZxEKHTbyKtmgpzZ --- tests/phpunit/tests/option/option.php | 36 +++++++++--------- .../option/wpAutoloadValuesToAutoload.php | 14 +++---- .../option/wpDetermineOptionAutoloadValue.php | 38 +++++++++---------- .../tests/option/wpSetOptionAutoload.php | 10 ++--- .../option/wpSetOptionAutoloadValues.php | 14 +++---- .../tests/option/wpSetOptionsAutoload.php | 12 +++--- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/tests/phpunit/tests/option/option.php b/tests/phpunit/tests/option/option.php index cdad4b042c911..461d84574c742 100644 --- a/tests/phpunit/tests/option/option.php +++ b/tests/phpunit/tests/option/option.php @@ -344,18 +344,18 @@ public function test_option_autoloading( $name, $autoload_value, $expected ) { public static function data_option_autoloading() { return array( // Supported values. - array( 'autoload_true', true, 'on' ), - array( 'autoload_false', false, 'off' ), - array( 'autoload_null', null, 'auto' ), + array( 'autoload_true', true, WP_Option_Autoload::ON ), + array( 'autoload_false', false, WP_Option_Autoload::OFF ), + array( 'autoload_null', null, WP_Option_Autoload::AUTO ), // Values supported for backward compatibility. - array( 'autoload_yes', 'yes', 'on' ), - array( 'autoload_no', 'no', 'off' ), + array( 'autoload_yes', 'yes', WP_Option_Autoload::ON ), + array( 'autoload_no', 'no', WP_Option_Autoload::OFF ), // Technically unsupported values. - array( 'autoload_string', 'foo', 'auto' ), - array( 'autoload_int', 123456, 'auto' ), - array( 'autoload_array', array(), 'auto' ), + array( 'autoload_string', 'foo', WP_Option_Autoload::AUTO ), + array( 'autoload_int', 123456, WP_Option_Autoload::AUTO ), + array( 'autoload_array', array(), WP_Option_Autoload::AUTO ), ); } @@ -382,32 +382,32 @@ public function test_update_option_autoloading_large_option( $autoload, $expecte public static function data_option_autoloading_large_option() { return array( 'on' => array( - 'autoload' => 'on', - 'expected' => 'on', + 'autoload' => WP_Option_Autoload::ON, + 'expected' => WP_Option_Autoload::ON, ), 'off' => array( - 'autoload' => 'off', - 'expected' => 'off', + 'autoload' => WP_Option_Autoload::OFF, + 'expected' => WP_Option_Autoload::OFF, ), 'yes' => array( 'autoload' => 'yes', - 'expected' => 'on', + 'expected' => WP_Option_Autoload::ON, ), 'true' => array( 'autoload' => true, - 'expected' => 'on', + 'expected' => WP_Option_Autoload::ON, ), 'no' => array( 'autoload' => 'no', - 'expected' => 'off', + 'expected' => WP_Option_Autoload::OFF, ), 'false' => array( 'autoload' => false, - 'expected' => 'off', + 'expected' => WP_Option_Autoload::OFF, ), 'null' => array( 'autoload' => null, - 'expected' => 'auto-off', + 'expected' => WP_Option_Autoload::AUTO_OFF, ), ); } @@ -430,7 +430,7 @@ public function test_update_option_autoloading_small_option_auto() { $this->assertTrue( $updated ); $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) ); - $this->assertSame( 'auto', $actual->autoload ); + $this->assertSame( WP_Option_Autoload::AUTO, $actual->autoload ); } /** diff --git a/tests/phpunit/tests/option/wpAutoloadValuesToAutoload.php b/tests/phpunit/tests/option/wpAutoloadValuesToAutoload.php index e679f481eed46..d539d5010aed4 100644 --- a/tests/phpunit/tests/option/wpAutoloadValuesToAutoload.php +++ b/tests/phpunit/tests/option/wpAutoloadValuesToAutoload.php @@ -13,7 +13,7 @@ class Tests_Option_wpAutoloadValuesToAutoload extends WP_UnitTestCase { * @ticket 42441 */ public function test_wp_autoload_values_to_autoload() { - $this->assertSameSets( array( 'yes', 'on', 'auto-on', 'auto' ), wp_autoload_values_to_autoload() ); + $this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO ), wp_autoload_values_to_autoload() ); } /** @@ -24,11 +24,11 @@ public function test_wp_autoload_values_to_autoload_filter_remove() { add_filter( 'wp_autoload_values_to_autoload', static function () { - return array( 'yes' ); + return array( WP_Option_Autoload::LEGACY_YES ); } ); - $this->assertSameSets( array( 'yes' ), wp_autoload_values_to_autoload() ); + $this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES ), wp_autoload_values_to_autoload() ); } /** @@ -39,11 +39,11 @@ public function test_wp_autoload_values_to_autoload_filter_extra() { add_filter( 'wp_autoload_values_to_autoload', static function () { - return array( 'yes', 'on', 'auto-on', 'auto', 'extra' ); + return array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO, 'extra' ); } ); - $this->assertSameSets( array( 'yes', 'on', 'auto-on', 'auto' ), wp_autoload_values_to_autoload() ); + $this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO ), wp_autoload_values_to_autoload() ); } /** @@ -54,10 +54,10 @@ public function test_wp_autoload_values_to_autoload_filter_replace() { add_filter( 'wp_autoload_values_to_autoload', static function () { - return array( 'yes', 'on', 'auto-on', 'extra' ); + return array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, 'extra' ); } ); - $this->assertSameSets( array( 'yes', 'on', 'auto-on' ), wp_autoload_values_to_autoload() ); + $this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON ), wp_autoload_values_to_autoload() ); } } diff --git a/tests/phpunit/tests/option/wpDetermineOptionAutoloadValue.php b/tests/phpunit/tests/option/wpDetermineOptionAutoloadValue.php index 7927827063499..d001f6c930d36 100644 --- a/tests/phpunit/tests/option/wpDetermineOptionAutoloadValue.php +++ b/tests/phpunit/tests/option/wpDetermineOptionAutoloadValue.php @@ -28,47 +28,47 @@ public static function data_values() { return array( 'yes' => array( 'autoload' => 'yes', - 'expected' => 'on', + 'expected' => WP_Option_Autoload::ON, ), 'on' => array( - 'autoload' => 'on', - 'expected' => 'on', + 'autoload' => WP_Option_Autoload::ON, + 'expected' => WP_Option_Autoload::ON, ), 'true' => array( 'autoload' => true, - 'expected' => 'on', + 'expected' => WP_Option_Autoload::ON, ), 'no' => array( 'autoload' => 'no', - 'expected' => 'off', + 'expected' => WP_Option_Autoload::OFF, ), 'off' => array( - 'autoload' => 'off', - 'expected' => 'off', + 'autoload' => WP_Option_Autoload::OFF, + 'expected' => WP_Option_Autoload::OFF, ), 'false' => array( 'autoload' => false, - 'expected' => 'off', + 'expected' => WP_Option_Autoload::OFF, ), 'invalid' => array( 'autoload' => 'foo', - 'expected' => 'auto', + 'expected' => WP_Option_Autoload::AUTO, ), 'null' => array( 'autoload' => null, - 'expected' => 'auto', + 'expected' => WP_Option_Autoload::AUTO, ), 'auto' => array( - 'autoload' => 'auto', - 'expected' => 'auto', + 'autoload' => WP_Option_Autoload::AUTO, + 'expected' => WP_Option_Autoload::AUTO, ), 'auto-on' => array( - 'autoload' => 'auto-on', - 'expected' => 'auto', + 'autoload' => WP_Option_Autoload::AUTO_ON, + 'expected' => WP_Option_Autoload::AUTO, ), 'auto-off' => array( - 'autoload' => 'auto-off', - 'expected' => 'auto', + 'autoload' => WP_Option_Autoload::AUTO_OFF, + 'expected' => WP_Option_Autoload::AUTO, ), ); } @@ -78,7 +78,7 @@ public static function data_values() { */ public function test_small_option() { $test = wp_determine_option_autoload_value( 'foo', 'bar', 'bar', null ); - $this->assertSame( 'auto', $test ); + $this->assertSame( WP_Option_Autoload::AUTO, $test ); } /** @@ -88,7 +88,7 @@ public function test_large_option() { $value = file( DIR_TESTDATA . '/formatting/entities.txt' ); $serialized_value = maybe_serialize( $value ); $test = wp_determine_option_autoload_value( 'foo', $value, $serialized_value, null ); - $this->assertSame( 'auto-off', $test ); + $this->assertSame( WP_Option_Autoload::AUTO_OFF, $test ); } /** @@ -98,7 +98,7 @@ public function test_large_option_json() { $value = file( DIR_TESTDATA . '/themedir1/block-theme/theme.json' ); $serialized_value = maybe_serialize( $value ); $test = wp_determine_option_autoload_value( 'foo', $value, $serialized_value, null ); - $this->assertSame( 'auto-off', $test ); + $this->assertSame( WP_Option_Autoload::AUTO_OFF, $test ); } public function filter_max_option_size( $current ) { diff --git a/tests/phpunit/tests/option/wpSetOptionAutoload.php b/tests/phpunit/tests/option/wpSetOptionAutoload.php index b6eeb4026374e..9d1fabed890cb 100644 --- a/tests/phpunit/tests/option/wpSetOptionAutoload.php +++ b/tests/phpunit/tests/option/wpSetOptionAutoload.php @@ -24,7 +24,7 @@ public function test_wp_set_option_autoload_yes() { add_option( $option, $value, '', 'no' ); $this->assertTrue( wp_set_option_autoload( $option, 'yes' ), 'Function did not succeed' ); - $this->assertSame( 'on', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); + $this->assertSame( WP_Option_Autoload::ON, $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' ); } @@ -45,7 +45,7 @@ public function test_wp_set_option_autoload_no() { add_option( $option, $value, '', 'yes' ); $this->assertTrue( wp_set_option_autoload( $option, 'no' ), 'Function did not succeed' ); - $this->assertSame( 'off', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); + $this->assertSame( WP_Option_Autoload::OFF, $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' ); } @@ -63,7 +63,7 @@ public function test_wp_set_option_autoload_same() { add_option( $option, $value, '', true ); $this->assertFalse( wp_set_option_autoload( $option, true ), 'Function did unexpectedly succeed' ); - $this->assertSame( 'on', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value unexpectedly updated in database' ); + $this->assertSame( WP_Option_Autoload::ON, $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value unexpectedly updated in database' ); } /** @@ -96,7 +96,7 @@ public function test_wp_set_option_autoload_true() { add_option( $option, $value, '', false ); $this->assertTrue( wp_set_option_autoload( $option, true ), 'Function did not succeed' ); - $this->assertSame( 'on', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); + $this->assertSame( WP_Option_Autoload::ON, $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); } /** @@ -113,6 +113,6 @@ public function test_wp_set_option_autoload_false() { add_option( $option, $value, '', true ); $this->assertTrue( wp_set_option_autoload( $option, false ), 'Function did not succeed' ); - $this->assertSame( 'off', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); + $this->assertSame( WP_Option_Autoload::OFF, $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' ); } } diff --git a/tests/phpunit/tests/option/wpSetOptionAutoloadValues.php b/tests/phpunit/tests/option/wpSetOptionAutoloadValues.php index 7214beedfe8e9..ea7cacae4c174 100644 --- a/tests/phpunit/tests/option/wpSetOptionAutoloadValues.php +++ b/tests/phpunit/tests/option/wpSetOptionAutoloadValues.php @@ -32,7 +32,7 @@ public function test_wp_set_option_autoload_values_all_yes_partial_update() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 2, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $autoload ) { $this->assertFalse( wp_cache_get( $option, 'options' ), sprintf( 'Option %s not deleted from individual cache', $option ) ); } @@ -65,7 +65,7 @@ public function test_wp_set_option_autoload_values_all_no_partial_update() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 2, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSame( array( 'off', 'off' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::OFF, WP_Option_Autoload::OFF ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $autoload ) { $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), sprintf( 'Option %s not deleted from alloptions cache', $option ) ); } @@ -95,7 +95,7 @@ public function test_wp_set_option_autoload_values_all_yes_no_update() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 1, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $autoload ) { $this->assertArrayHasKey( $option, wp_cache_get( 'alloptions', 'options' ), sprintf( 'Option %s unexpectedly deleted from alloptions cache', $option ) ); } @@ -130,7 +130,7 @@ public function test_wp_set_option_autoload_values_mixed_partial_update() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 3, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSameSets( array( 'on', 'off', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSameSets( array( WP_Option_Autoload::ON, WP_Option_Autoload::OFF, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $autoload ) { $this->assertFalse( wp_cache_get( $option, 'options' ), sprintf( 'Option %s not deleted from individual cache', $option ) ); } @@ -164,7 +164,7 @@ public function test_wp_set_option_autoload_values_mixed_only_update_no() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 2, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSameSets( array( 'on', 'off', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSameSets( array( WP_Option_Autoload::ON, WP_Option_Autoload::OFF, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $autoload ) { if ( false === $autoload ) { $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), sprintf( 'Option %s not deleted from alloptions cache', $option ) ); @@ -205,7 +205,7 @@ static function ( $query ) { ); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); - $this->assertSame( array( 'off', 'off' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::OFF, WP_Option_Autoload::OFF ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); } /** @@ -230,7 +230,7 @@ public function test_wp_set_option_autoload_values_with_bool() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_option_autoload_values( $options ), 'Function produced unexpected result' ); $this->assertSame( $num_queries + 3, get_num_queries(), 'Function made unexpected amount of database queries' ); - $this->assertSameSets( array( 'on', 'off' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSameSets( array( WP_Option_Autoload::ON, WP_Option_Autoload::OFF ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); } /** diff --git a/tests/phpunit/tests/option/wpSetOptionsAutoload.php b/tests/phpunit/tests/option/wpSetOptionsAutoload.php index eb8358f533527..285812c5de4c3 100644 --- a/tests/phpunit/tests/option/wpSetOptionsAutoload.php +++ b/tests/phpunit/tests/option/wpSetOptionsAutoload.php @@ -32,7 +32,7 @@ public function test_wp_set_options_autoload_yes() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), 'yes' ), 'Function did not succeed' ); $this->assertSame( $num_queries + 2, get_num_queries(), 'Updating options autoload value ran too many queries' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $value ) { $this->assertFalse( wp_cache_get( $option, 'options' ), sprintf( 'Option %s not deleted from individual cache', $option ) ); } @@ -63,7 +63,7 @@ public function test_wp_set_options_autoload_no() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), 'no' ), 'Function did not succeed' ); $this->assertSame( $num_queries + 2, get_num_queries(), 'Updating options autoload value ran too many queries' ); - $this->assertSame( array( 'off', 'off' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::OFF, WP_Option_Autoload::OFF ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $value ) { $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), sprintf( 'Option %s not deleted from alloptions cache', $option ) ); } @@ -91,7 +91,7 @@ public function test_wp_set_options_autoload_same() { $num_queries = get_num_queries(); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), true ), 'Function did unexpectedly succeed' ); $this->assertSame( $num_queries + 1, get_num_queries(), 'Function attempted to update options autoload value in database' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Options autoload value unexpectedly updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Options autoload value unexpectedly updated in database' ); } /** @@ -137,7 +137,7 @@ public function test_wp_set_options_autoload_mixed() { ); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), true ), 'Function produced unexpected result' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); foreach ( $options as $option => $value ) { $this->assertFalse( wp_cache_get( $option, 'options' ), sprintf( 'Option %s not deleted from individual cache', $option ) ); } @@ -165,7 +165,7 @@ public function test_wp_set_options_autoload_true() { ); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), true ), 'Function produced unexpected result' ); - $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::ON, WP_Option_Autoload::ON ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); } /** @@ -189,6 +189,6 @@ public function test_wp_set_options_autoload_false() { ); $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), false ), 'Function produced unexpected result' ); - $this->assertSame( array( 'off', 'off' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); + $this->assertSame( array( WP_Option_Autoload::OFF, WP_Option_Autoload::OFF ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' ); } }