Make WordPress Core

Changeset 31052


Ignore:
Timestamp:
01/05/2015 01:12:06 PM (10 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r31044 r31052  
    183183 * Convert number of bytes largest unit bytes will fit into.
    184184 *
    185  * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
     185 * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts
    186186 * number of bytes to human readable number by taking the number of that unit
    187187 * that the bytes will go into it. Supports TB value.
     
    207207        'MB' => 1048576,        // pow( 1024, 2)
    208208        'kB' => 1024,           // pow( 1024, 1)
    209         'B ' => 1,              // pow( 1024, 0)
     209        'B' => 1,              // pow( 1024, 0)
    210210    );
    211     foreach ( $quant as $unit => $mag )
    212         if ( doubleval($bytes) >= $mag )
     211
     212    foreach ( $quant as $unit => $mag ) {
     213        if ( doubleval( $bytes ) >= $mag ) {
    213214            return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
     215        }
     216    }
    214217
    215218    return false;
  • trunk/tests/phpunit/tests/functions.php

    r30561 r31052  
    4040    }
    4141    function test_size_format() {
     42        $b  = 1;
    4243        $kb = 1024;
    4344        $mb = $kb*1024;
     
    4849        $this->assertEquals('1 MB', size_format($mb, 0));
    4950        $this->assertEquals('1 kB', size_format($kb, 0));
     51        $this->assertEquals('1 B',  size_format($b, 0));
    5052        // now some values around
    5153        // add some bytes to make sure the result isn't 1.4999999
Note: See TracChangeset for help on using the changeset viewer.