Skip to content

Commit de1fd79

Browse files
committed
Fix wp_using_ext_object_cache() return type and add early-load guard.
1 parent 051cae3 commit de1fd79

2 files changed

Lines changed: 84 additions & 11 deletions

File tree

src/wp-includes/load.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -808,20 +808,21 @@ function wp_set_wpdb_vars() {
808808
* @return bool The current 'using' setting.
809809
*/
810810
function wp_using_ext_object_cache( $using = null ) {
811-
global $_wp_using_ext_object_cache;
811+
global $_wp_using_ext_object_cache;
812812

813-
// Save the current state to return later.
814-
$current_using = $_wp_using_ext_object_cache;
813+
// Save the current state to return later.
814+
$current_using = $_wp_using_ext_object_cache;
815815

816-
if ( null !== $using ) {
817-
$_wp_using_ext_object_cache = (bool) $using;
818-
}
816+
if ( null !== $using ) {
817+
$_wp_using_ext_object_cache = (bool) $using;
818+
}
819+
820+
if ( null === $_wp_using_ext_object_cache ) {
821+
// If the global is uninitialized, the value would be null, which violates the type signature.
822+
return false;
823+
}
819824

820-
/**
821-
* Ensure the returned value is always a boolean.
822-
* If the global is uninitialized, it could be null, which violates the type signature.
823-
*/
824-
return (bool) $current_using;
825+
return (bool) $current_using;
825826
}
826827

827828
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_using_ext_object_cache().
5+
*
6+
* @group load
7+
*
8+
* @covers ::wp_using_ext_object_cache
9+
*/
10+
class Tests_Load_wpUsingExtObjectCache extends WP_UnitTestCase {
11+
12+
/**
13+
* Resets the global variable before each test to ensure isolation.
14+
*/
15+
public function set_up() {
16+
parent::set_up();
17+
global $_wp_using_ext_object_cache;
18+
$_wp_using_ext_object_cache = null;
19+
}
20+
21+
/**
22+
* Tests the toggling and retrieval of the external object cache state.
23+
*
24+
* @ticket #64950 wp_using_ext_object_cache can return null
25+
*
26+
* @dataProvider data_wp_using_ext_object_cache
27+
*
28+
* @param mixed $initial_global The initial value of the global variable.
29+
* @param mixed $input_param The value passed to the function.
30+
* @param bool $expected_return The expected return value (previous state).
31+
* @param bool $final_global The expected state of the global after execution.
32+
*/
33+
public function test_wp_using_ext_object_cache( $initial_global, $input_param, $expected_return, $final_global ) {
34+
global $_wp_using_ext_object_cache;
35+
36+
// Set the environment to the initial state.
37+
$_wp_using_ext_object_cache = $initial_global;
38+
39+
// Verify the function return value matches the expected previous state.
40+
$this->assertSame( $expected_return, wp_using_ext_object_cache( $input_param ) );
41+
42+
// Verify the global variable has been updated to the new state.
43+
$this->assertSame( $final_global, $_wp_using_ext_object_cache );
44+
}
45+
46+
/**
47+
* Data provider for test_wp_using_ext_object_cache().
48+
*
49+
* Format: array( Initial Global, Function Input, Expected Return, Expected Final Global )
50+
*
51+
* @return array
52+
*/
53+
public function data_wp_using_ext_object_cache() {
54+
return array(
55+
// Scenario 1: Read current state without changing it.
56+
array( true, null, true, true ),
57+
array( false, null, false, false ),
58+
59+
// Scenario 2: Handle uninitialized global state.
60+
array( null, null, false, null ),
61+
62+
// Scenario 3: Change state and return the previous value.
63+
array( false, true, false, true ),
64+
array( true, false, true, false ),
65+
66+
// Scenario 4: Ensure type casting to boolean.
67+
array( false, 1, false, true ), // Integer 1 becomes true.
68+
array( true, 0, true, false ), // Integer 0 becomes false.
69+
array( false, '1', false, true ), // String '1' becomes true.
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)