Skip to content

Commit 315cd6d

Browse files
committed
General: Fix force_ssl_admin() to always return bool.
Props pbearne, costdev, autotutorial, debarghyabanerjee, swissspidy. Fixes #60023. git-svn-id: https://develop.svn.wordpress.org/trunk@59830 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0754890 commit 315cd6d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/wp-includes/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6273,15 +6273,15 @@ function validate_file( $file, $allowed_files = array() ) {
62736273
*
62746274
* @since 2.6.0
62756275
*
6276-
* @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
6276+
* @param string|bool|null $force Optional. Whether to force SSL in admin screens. Default null.
62776277
* @return bool True if forced, false if not forced.
62786278
*/
62796279
function force_ssl_admin( $force = null ) {
62806280
static $forced = false;
62816281

62826282
if ( ! is_null( $force ) ) {
62836283
$old_forced = $forced;
6284-
$forced = $force;
6284+
$forced = (bool) $force;
62856285
return $old_forced;
62866286
}
62876287

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Test cases for the `force_ssl_admin()` function.
4+
*
5+
* @package WordPress\UnitTests
6+
*
7+
* @since 6.8.0
8+
*
9+
* @group functions
10+
*
11+
* @covers ::force_ssl_admin
12+
*/
13+
class Tests_Functions_ForceSslAdmin extends WP_UnitTestCase {
14+
15+
public function set_up(): void {
16+
parent::set_up();
17+
// Reset the static variable before each test
18+
force_ssl_admin( false );
19+
}
20+
21+
/**
22+
* Data provider for testing force_ssl_admin.
23+
*
24+
* Provides various inputs and expected outcomes for the function.
25+
*
26+
* @return array[]
27+
*/
28+
public function data_should_return_expected_value_when_various_inputs_are_passed() {
29+
return array(
30+
'default' => array( null, false, false ),
31+
'first_call_true' => array( true, false, true ),
32+
'first_call_false' => array( false, false, false ),
33+
'first_call_non_empty_string' => array( 'some string', false, true ),
34+
'empty_string' => array( '', false, false ),
35+
'first_call_integer_1' => array( 1, false, true ),
36+
'integer_0' => array( 0, false, false ),
37+
);
38+
}
39+
40+
/**
41+
* Tests that force_ssl_admin returns expected values based on various inputs.
42+
*
43+
* @dataProvider data_should_return_expected_value_when_various_inputs_are_passed
44+
*
45+
* @param mixed $input The input value to test.
46+
* @param bool $expected_first_call The expected result for the first call.
47+
* @param bool $expected_subsequent_call The expected result for subsequent calls.
48+
*/
49+
public function test_should_return_expected_value_when_various_inputs_are_passed( $input, $expected_first_call, $expected_subsequent_call ) {
50+
$this->assertSame( $expected_first_call, force_ssl_admin( $input ), 'First call did not return expected value' );
51+
52+
// Call again to check subsequent behavior
53+
$this->assertSame( $expected_subsequent_call, force_ssl_admin( $input ), 'Subsequent call did not return expected value' );
54+
}
55+
}

0 commit comments

Comments
 (0)