diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php
index fdbfdea250..feec2ea408 100644
a
|
b
|
if ( ! function_exists( 'wp_rand' ) ) : |
2537 | 2537 | * |
2538 | 2538 | * @global string $rnd_value |
2539 | 2539 | * |
2540 | | * @param int $min Lower limit for the generated number |
2541 | | * @param int $max Upper limit for the generated number |
2542 | | * @return int A random number between min and max |
| 2540 | * @param int $min Optional. Lower limit for the generated number. |
| 2541 | * Accepts positive integers or zero. Defaults to 0. |
| 2542 | * @param int $max Optional. Upper limit for the generated number. |
| 2543 | * Accepts positive integers. Defaults to 4294967295. |
| 2544 | * @return int A random number between min and max. |
2543 | 2545 | */ |
2544 | | function wp_rand( $min = 0, $max = 0 ) { |
| 2546 | function wp_rand( $min = null, $max = null ) { |
2545 | 2547 | global $rnd_value; |
2546 | 2548 | |
2547 | 2549 | // Some misconfigured 32-bit environments (Entropy PHP, for example) |
2548 | 2550 | // truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. |
2549 | 2551 | $max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff |
2550 | 2552 | |
| 2553 | if ( null === $min ) { |
| 2554 | $min = 0; |
| 2555 | } |
| 2556 | |
| 2557 | if ( null === $max ) { |
| 2558 | $max = $max_random_number; |
| 2559 | } |
| 2560 | |
2551 | 2561 | // We only handle ints, floats are truncated to their integer value. |
2552 | 2562 | $min = (int) $min; |
2553 | 2563 | $max = (int) $max; |
… |
… |
if ( ! function_exists( 'wp_rand' ) ) : |
2556 | 2566 | static $use_random_int_functionality = true; |
2557 | 2567 | if ( $use_random_int_functionality ) { |
2558 | 2568 | try { |
2559 | | $_max = ( 0 != $max ) ? $max : $max_random_number; |
2560 | 2569 | // wp_rand() can accept arguments in either order, PHP cannot. |
2561 | | $_max = max( $min, $_max ); |
2562 | | $_min = min( $min, $_max ); |
| 2570 | $_max = max( $min, $max ); |
| 2571 | $_min = min( $min, $max ); |
2563 | 2572 | $val = random_int( $_min, $_max ); |
2564 | 2573 | if ( false !== $val ) { |
2565 | 2574 | return absint( $val ); |
… |
… |
if ( ! function_exists( 'wp_rand' ) ) : |
2599 | 2608 | $value = abs( hexdec( $value ) ); |
2600 | 2609 | |
2601 | 2610 | // Reduce the value to be within the min - max range. |
2602 | | if ( 0 != $max ) { |
2603 | | $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); |
2604 | | } |
| 2611 | $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); |
2605 | 2612 | |
2606 | 2613 | return abs( (int) $value ); |
2607 | 2614 | } |
diff --git a/tests/phpunit/tests/pluggable.php b/tests/phpunit/tests/pluggable.php
index c7b0225987..c924229c2e 100644
a
|
b
|
class Tests_Pluggable extends WP_UnitTestCase { |
354 | 354 | |
355 | 355 | $this->assertSame( $current_user, $from_get_user_by ); |
356 | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @ticket 55194 |
| 360 | * @covers ::wp_rand |
| 361 | */ |
| 362 | function test_wp_rand_range() { |
| 363 | // Range of 0, 0 must return 0 |
| 364 | $this->assertEquals( 0, wp_rand( 0, 0 ) ); |
| 365 | |
| 366 | // Random between 1 and 99 must be between these numbers |
| 367 | $min = 1; |
| 368 | $max = 99; |
| 369 | |
| 370 | $this->assertLessThan( 100, wp_rand( $min, $max ) ); |
| 371 | $this->assertGreaterThan( 0, wp_rand( $min, $max ) ); |
| 372 | |
| 373 | // Range with minimum number must return a positive number |
| 374 | $min = -99; |
| 375 | $max = -1; |
| 376 | |
| 377 | $this->assertLessThan( 100, wp_rand( $min, $max ) ); |
| 378 | $this->assertGreaterThan( 0, wp_rand( $min, $max ) ); |
| 379 | |
| 380 | // Range with numbers greater than PHP_INT_MAX must not overflow |
| 381 | $min = PHP_INT_MAX + 1; |
| 382 | $max = PHP_INT_MAX + 1; |
| 383 | |
| 384 | $this->assertEquals( PHP_INT_MAX, wp_rand( $min, $max ) ); |
| 385 | } |
357 | 386 | } |