Bootstrap: Clean up `wp_convert_hr_to_bytes()`.

* Don't return a value higher than `PHP_INT_MAX`.
* Add unit tests.

Props jrf.
See #32075.

git-svn-id: https://develop.svn.wordpress.org/trunk@38013 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling 2016-07-08 12:53:08 +00:00
parent df78fb4ef3
commit 629c6e36b5
2 changed files with 86 additions and 13 deletions

View File

@ -956,7 +956,7 @@ function wp_installing( $is_installing = null ) {
* Determines if SSL is used.
*
* @since 2.6.0
* @since 4.6.0 Moved from functions.php to load.php
* @since 4.6.0 Moved from functions.php to load.php.
*
* @return bool True if SSL, otherwise false.
*/
@ -979,19 +979,26 @@ function is_ssl() {
* Converts a shorthand byte value to an integer byte value.
*
* @since 2.3.0
* @since 4.6.0 Moved from media.php to load.php
* @since 4.6.0 Moved from media.php to load.php.
*
* @param string $size A shorthand byte value.
* @link http://php.net/manual/en/function.ini-get.php
* @link http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes
*
* @param string $value A (PHP ini) byte value, either shorthand or ordinary.
* @return int An integer byte value.
*/
function wp_convert_hr_to_bytes( $size ) {
$size = strtolower( $size );
$bytes = (int) $size;
if ( strpos( $size, 'k' ) !== false )
$bytes = intval( $size ) * KB_IN_BYTES;
elseif ( strpos( $size, 'm' ) !== false )
$bytes = intval($size) * MB_IN_BYTES;
elseif ( strpos( $size, 'g' ) !== false )
$bytes = intval( $size ) * GB_IN_BYTES;
return $bytes;
function wp_convert_hr_to_bytes( $value ) {
$value = strtolower( trim( $value ) );
$bytes = (int) $value;
if ( false !== strpos( $value, 'g' ) ) {
$bytes *= GB_IN_BYTES;
} elseif ( false !== strpos( $value, 'm' ) ) {
$bytes *= MB_IN_BYTES;
} elseif ( false !== strpos( $value, 'k' ) ) {
$bytes *= KB_IN_BYTES;
}
// Deal with large (float) values which run into the maximum integer size.
return min( $bytes, PHP_INT_MAX );
}

View File

@ -0,0 +1,66 @@
<?php
/**
* Tests for wp_convert_hr_to_bytes().
*
* @group load.php
*/
class Tests_Functions_Convert_Hr_To_Bytes extends WP_UnitTestCase {
/**
* Tests converting (PHP ini) byte values to integer byte values.
*
* @ticket 32075
*
* @dataProvider data_wp_convert_hr_to_bytes
*
* @param int|string $value The value passed to wp_convert_hr_to_bytes().
* @param int $expected The expected output of wp_convert_hr_to_bytes().
*/
function test_wp_convert_hr_to_bytes( $value, $expected ) {
$this->assertSame( $expected, wp_convert_hr_to_bytes( $value ) );
}
/**
* Data provider for test_wp_convert_hr_to_bytes().
*
* @return array {
* @type array {
* @type int|string $value The value passed to wp_convert_hr_to_bytes().
* @type int $expected The expected output of wp_convert_hr_to_bytes().
* }
* }
*/
function data_wp_convert_hr_to_bytes() {
$array = array(
// Integer input.
array( -1, -1 ), // = no memory limit.
array( 8388608, 8388608 ), // 8M.
// String input (memory limit shorthand values).
array( '32k', 32768 ),
array( '64K', 65536 ),
array( '128m', 134217728 ),
array( '256M', 268435456 ),
array( '1g', 1073741824 ),
array( '128m ', 134217728 ), // Leading/trailing whitespace gets trimmed.
array( '1024', 1024 ), // No letter will be interpreted as integer value.
// Edge cases.
array( 'g', 0 ),
array( 'g1', 0 ),
array( 'null', 0 ),
array( 'off', 0 ),
);
// Test for running into maximum integer size limit on 32bit systems.
if ( 2147483647 === PHP_INT_MAX ) {
$array[] = array( '2G', 2147483647 );
$array[] = array( '4G', 2147483647 );
} else {
$array[] = array( '2G', 2147483648 );
$array[] = array( '4G', 4294967296 );
}
return $array;
}
}