|
| 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