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
This commit is contained in:
parent
ce7b53fb3f
commit
c259f4c35d
@ -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;
|
||||
|
@ -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
|
||||
*/
|
||||
|
52
tests/phpunit/tests/functions/sizeFormat.php
Normal file
52
tests/phpunit/tests/functions/sizeFormat.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests for size_format()
|
||||
*
|
||||
* @group functions.php
|
||||
* @ticket 36635
|
||||
*/
|
||||
class Tests_Functions_Size_Format extends WP_UnitTestCase {
|
||||
public function _data_size_format() {
|
||||
return array(
|
||||
array( array(), 0, false ),
|
||||
array( 'baba', 0, false ),
|
||||
array( '', 0, false ),
|
||||
array( '-1', 0, false ),
|
||||
array( -1, 0, false ),
|
||||
array( 0, 0, '0 B' ),
|
||||
array( 1, 0, '1 B' ),
|
||||
array( 1023, 0, '1,023 B' ),
|
||||
array( KB_IN_BYTES, 0, '1 KB' ),
|
||||
array( KB_IN_BYTES, 2, '1.00 KB' ),
|
||||
array( 2.5 * KB_IN_BYTES, 0, '3 KB' ),
|
||||
array( 2.5 * KB_IN_BYTES, 2, '2.50 KB' ),
|
||||
array( 10 * KB_IN_BYTES, 0, '10 KB' ),
|
||||
array( (string) 1024 * KB_IN_BYTES, 2, '1.00 MB' ),
|
||||
array( MB_IN_BYTES, 0, '1 MB' ),
|
||||
array( 2.5 * MB_IN_BYTES, 0, '3 MB' ),
|
||||
array( 2.5 * MB_IN_BYTES, 2, '2.50 MB' ),
|
||||
array( (string) 1024 * MB_IN_BYTES, 2, '1.00 GB' ),
|
||||
array( GB_IN_BYTES, 0, '1 GB' ),
|
||||
array( 2.5 * GB_IN_BYTES, 0, '3 GB' ),
|
||||
array( 2.5 * GB_IN_BYTES, 2, '2.50 GB' ),
|
||||
array( (string) 1024 * GB_IN_BYTES, 2, '1.00 TB' ),
|
||||
array( TB_IN_BYTES, 0, '1 TB' ),
|
||||
array( 2.5 * TB_IN_BYTES, 0, '3 TB' ),
|
||||
array( 2.5 * TB_IN_BYTES, 2, '2.50 TB' ),
|
||||
array( TB_IN_BYTES + (TB_IN_BYTES/2) + MB_IN_BYTES, 1, '1.5 TB' ),
|
||||
array( TB_IN_BYTES - MB_IN_BYTES - KB_IN_BYTES, 3, '1,023.999 GB' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider _data_size_format
|
||||
*
|
||||
* @param $bytes
|
||||
* @param $decimals
|
||||
* @param $expected
|
||||
*/
|
||||
public function test_size_format( $bytes, $decimals, $expected ) {
|
||||
$this->assertSame( $expected, size_format( $bytes, $decimals ) );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user