Options, Meta APIs: Fix follow up bug when comparing values for options using the pre_option_{$option} filter.

This fix is relevant for options such as `gmt_offset` that use a filter to force a specific value regardless of what is stored in the database.

Props mamaduka, flixos90, mukesh27, spacedmonkey.
See #22192.


git-svn-id: https://develop.svn.wordpress.org/trunk@56717 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2023-09-26 15:53:39 +00:00
parent 6e635242ec
commit 319e6b8857
2 changed files with 74 additions and 3 deletions
+54 -1
View File
@@ -439,7 +439,6 @@ class Tests_Option_Option extends WP_UnitTestCase {
}
}
/**
* Data provider.
*
@@ -581,4 +580,58 @@ class Tests_Option_Option extends WP_UnitTestCase {
$this->assertSame( 1, $actual );
}
/**
* Tests that a non-existing option is added even when its pre filter returns a value.
*
* @ticket 22192
*
* @covers ::update_option
*/
public function test_update_option_with_pre_filter_adds_missing_option() {
// Force a return value of integer 0.
add_filter( 'pre_option_foo', '__return_zero' );
/*
* This should succeed, since the 'foo' option does not exist in the database.
* The default value is false, so it differs from 0.
*/
$this->assertTrue( update_option( 'foo', 0 ) );
}
/**
* Tests that an existing option is updated even when its pre filter returns the same value.
*
* @ticket 22192
*
* @covers ::update_option
*/
public function test_update_option_with_pre_filter_updates_option_with_different_value() {
// Add the option with a value of 1 to the database.
add_option( 'foo', 1 );
// Force a return value of integer 0.
add_filter( 'pre_option_foo', '__return_zero' );
/*
* This should succeed, since the 'foo' option has a value of 1 in the database.
* Therefore it differs from 0 and should be updated.
*/
$this->assertTrue( update_option( 'foo', 0 ) );
}
/**
* Tests that calling update_option() does not permanently remove pre filters.
*
* @ticket 22192
*
* @covers ::update_option
*/
public function test_update_option_maintains_pre_filters() {
add_filter( 'pre_option_foo', '__return_zero' );
update_option( 'foo', 0 );
// Assert that the filter is still present.
$this->assertSame( 10, has_filter( 'pre_option_foo', '__return_zero' ) );
}
}