From eb4d356e293466fc78e4d75dcc756d9b41c724cc Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 5 Jan 2015 13:12:06 +0000 Subject: [PATCH] `size_format()` incorrectly included a trailing space for `B` values: less than 1024 bytes. Also add a unit test to check for this, so we don't do it again. Fixes #30908. Props tillkruess. git-svn-id: https://develop.svn.wordpress.org/trunk@31052 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 11 +++++++---- tests/phpunit/tests/functions.php | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 00480d6848..3b69570bf3 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -182,7 +182,7 @@ function number_format_i18n( $number, $decimals = 0 ) { /** * Convert number of bytes largest unit bytes will fit into. * - * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts + * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts * number of bytes to human readable number by taking the number of that unit * that the bytes will go into it. Supports TB value. * @@ -206,11 +206,14 @@ function size_format( $bytes, $decimals = 0 ) { 'GB' => 1073741824, // pow( 1024, 3) 'MB' => 1048576, // pow( 1024, 2) 'kB' => 1024, // pow( 1024, 1) - 'B ' => 1, // pow( 1024, 0) + 'B' => 1, // pow( 1024, 0) ); - foreach ( $quant as $unit => $mag ) - if ( doubleval($bytes) >= $mag ) + + foreach ( $quant as $unit => $mag ) { + if ( doubleval( $bytes ) >= $mag ) { return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; + } + } return false; } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 1edd143fbc..61f9c243a6 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -39,6 +39,7 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals($ss, wp_parse_args($q)); } function test_size_format() { + $b = 1; $kb = 1024; $mb = $kb*1024; $gb = $mb*1024; @@ -47,6 +48,7 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals('1 GB', size_format($gb, 0)); $this->assertEquals('1 MB', size_format($mb, 0)); $this->assertEquals('1 kB', size_format($kb, 0)); + $this->assertEquals('1 B', size_format($b, 0)); // now some values around // add some bytes to make sure the result isn't 1.4999999 $this->assertEquals('1.5 TB', size_format($tb + $tb/2 + $mb, 1));