From c259f4c35d8d116080eddb92fbe6690d206570ba Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Tue, 5 Jul 2016 10:35:24 +0000 Subject: [PATCH] General: Return "O B" when passing 0 to `size_format()`. Props swissspidy. Fixes #36635. git-svn-id: https://develop.svn.wordpress.org/trunk@37962 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 4 ++ tests/phpunit/tests/functions.php | 22 --------- tests/phpunit/tests/functions/sizeFormat.php | 52 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 tests/phpunit/tests/functions/sizeFormat.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index d8cfa324f9..6075f09b51 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -263,6 +263,10 @@ function size_format( $bytes, $decimals = 0 ) { 'B' => 1, ); + if ( 0 === $bytes ) { + return number_format_i18n( 0, $decimals ) . ' B'; + } + foreach ( $quant as $unit => $mag ) { if ( doubleval( $bytes ) >= $mag ) { return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index e28e65f752..ce5f6579f2 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -51,28 +51,6 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertInternalType( 'string', $args['bar'] ); } - function test_size_format() { - $b = 1; - $kb = 1024; - $mb = $kb*1024; - $gb = $mb*1024; - $tb = $gb*1024; - // test if boundaries are correct - $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)); - $this->assertEquals('1,023.999 GB', size_format($tb-$mb-$kb, 3)); - // edge - $this->assertFalse(size_format(-1)); - $this->assertFalse(size_format(0)); - $this->assertFalse(size_format('baba')); - $this->assertFalse(size_format(array())); - } - /** * @ticket 35972 */ diff --git a/tests/phpunit/tests/functions/sizeFormat.php b/tests/phpunit/tests/functions/sizeFormat.php new file mode 100644 index 0000000000..bc01cb740e --- /dev/null +++ b/tests/phpunit/tests/functions/sizeFormat.php @@ -0,0 +1,52 @@ +assertSame( $expected, size_format( $bytes, $decimals ) ); + } +}