Skip to content

Commit 562c593

Browse files
Tests: Add tests to verify alloptions cache no longer contains the option.
1 parent e644188 commit 562c593

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/phpunit/tests/option/updateOption.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,41 @@ public function test_update_option_array_with_object() {
225225
public function __return_foo() {
226226
return 'foo';
227227
}
228+
229+
/**
230+
* @ticket 26402
231+
*
232+
* @covers ::update_option
233+
*/
234+
public function test_should_invalidate_cache_when_db_update_fails() {
235+
global $wpdb;
236+
237+
$option_name = 'test_option_update_fail';
238+
$option_value = 'initial';
239+
240+
add_option( $option_name, $option_value );
241+
$this->assertSame( $option_value, get_option( $option_name ) );
242+
243+
$wpdb->delete( $wpdb->options, array( 'option_name' => $option_name ) );
244+
245+
tests_add_filter(
246+
'query',
247+
function ( $query ) use ( $wpdb ) {
248+
if ( stripos( $query, "update `$wpdb->options`" ) === 0 ) {
249+
return false; // Simulate DB update failure.
250+
}
251+
return $query;
252+
}
253+
);
254+
255+
$result = update_option( $option_name, 'new_value' );
256+
257+
$this->assertFalse( $result, 'Expected update_option() to fail when DB update fails.' );
258+
$this->assertFalse( wp_cache_get( $option_name, 'options' ) );
259+
260+
// Verify alloptions cache no longer contains the option.
261+
$alloptions = wp_cache_get( 'alloptions', 'options' );
262+
$this->assertIsArray( $alloptions );
263+
$this->assertArrayNotHasKey( $option_name, $alloptions );
264+
}
228265
}

0 commit comments

Comments
 (0)