General: Ensure wp_rand() returns 0 when $min and $max values are equal to 0.

This changeset ensures `wp_rand()` returns zero instead of a random number when both `$min` and `$max` values are equal to zero.

Fixes #55194.


git-svn-id: https://develop.svn.wordpress.org/trunk@53473 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2022-06-06 22:48:21 +00:00
parent d9c7980834
commit a147212892
2 changed files with 113 additions and 11 deletions
+96 -2
View File
@@ -224,8 +224,8 @@ class Tests_Pluggable extends WP_UnitTestCase {
'extra_special_chars' => false,
),
'wp_rand' => array(
'min' => 0,
'max' => 0,
'min' => null,
'max' => null,
),
'wp_set_password' => array( 'password', 'user_id' ),
'get_avatar' => array(
@@ -354,4 +354,98 @@ class Tests_Pluggable extends WP_UnitTestCase {
$this->assertSame( $current_user, $from_get_user_by );
}
/**
* Tests that wp_rand() returns zero.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_zero
* @covers ::wp_rand
*
* @param mixed $min Lower limit for the generated number.
* @param mixed $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_zero( $min, $max ) {
$this->assertSame( 0, wp_rand( $min, $max ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_zero() {
return array(
'min and max as 0' => array(
'min' => 0,
'max' => 0,
),
'min and max as 0.0' => array(
'min' => 0.0,
'max' => 0.0,
),
'min as null, max as 0' => array(
'min' => null,
'max' => 0,
),
);
}
/**
* Tests that wp_rand() returns a value between 1 and 99.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_between_1_and_99
* @covers ::wp_rand
*
* @param int $min Lower limit for the generated number.
* @param int $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_between_1_and_99( $min, $max ) {
$this->assertGreaterThan(
0,
wp_rand( $min, $max ),
'The value was not greater than 0'
);
$this->assertLessThan(
100,
wp_rand( $min, $max ),
'The value was not less than 100'
);
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_between_1_and_99() {
return array(
'1 and 99' => array(
'min' => 1,
'max' => 99,
),
'-1 and 99' => array(
'min' => -1,
'max' => 99,
),
'1 and -99' => array(
'min' => 1,
'max' => -99,
),
'-1 and -99' => array(
'min' => -1,
'max' => -99,
),
'1.0 and 99.0' => array(
'min' => 1.0,
'max' => 99.0,
),
'-1.0 and -99.0' => array(
'min' => -1.0,
'max' => -99.0,
),
);
}
}