General: Fix force_ssl_admin() to always return bool.

Props pbearne, costdev, autotutorial, debarghyabanerjee, swissspidy.
Fixes .

git-svn-id: https://develop.svn.wordpress.org/trunk@59830 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2025-02-17 15:30:29 +00:00
parent 0754890f0e
commit 315cd6de21
2 changed files with 57 additions and 2 deletions
src/wp-includes
tests/phpunit/tests/functions

@ -6273,7 +6273,7 @@ function validate_file( $file, $allowed_files = array() ) {
*
* @since 2.6.0
*
* @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
* @param string|bool|null $force Optional. Whether to force SSL in admin screens. Default null.
* @return bool True if forced, false if not forced.
*/
function force_ssl_admin( $force = null ) {
@ -6281,7 +6281,7 @@ function force_ssl_admin( $force = null ) {
if ( ! is_null( $force ) ) {
$old_forced = $forced;
$forced = $force;
$forced = (bool) $force;
return $old_forced;
}

@ -0,0 +1,55 @@
<?php
/**
* Test cases for the `force_ssl_admin()` function.
*
* @package WordPress\UnitTests
*
* @since 6.8.0
*
* @group functions
*
* @covers ::force_ssl_admin
*/
class Tests_Functions_ForceSslAdmin extends WP_UnitTestCase {
public function set_up(): void {
parent::set_up();
// Reset the static variable before each test
force_ssl_admin( false );
}
/**
* Data provider for testing force_ssl_admin.
*
* Provides various inputs and expected outcomes for the function.
*
* @return array[]
*/
public function data_should_return_expected_value_when_various_inputs_are_passed() {
return array(
'default' => array( null, false, false ),
'first_call_true' => array( true, false, true ),
'first_call_false' => array( false, false, false ),
'first_call_non_empty_string' => array( 'some string', false, true ),
'empty_string' => array( '', false, false ),
'first_call_integer_1' => array( 1, false, true ),
'integer_0' => array( 0, false, false ),
);
}
/**
* Tests that force_ssl_admin returns expected values based on various inputs.
*
* @dataProvider data_should_return_expected_value_when_various_inputs_are_passed
*
* @param mixed $input The input value to test.
* @param bool $expected_first_call The expected result for the first call.
* @param bool $expected_subsequent_call The expected result for subsequent calls.
*/
public function test_should_return_expected_value_when_various_inputs_are_passed( $input, $expected_first_call, $expected_subsequent_call ) {
$this->assertSame( $expected_first_call, force_ssl_admin( $input ), 'First call did not return expected value' );
// Call again to check subsequent behavior
$this->assertSame( $expected_subsequent_call, force_ssl_admin( $input ), 'Subsequent call did not return expected value' );
}
}