Make WordPress Core

Ticket #36635: 36635.2.diff

File 36635.2.diff, 3.4 KB (added by swissspidy, 7 years ago)
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index a3d1222..c4ca2fe 100644
    function size_format( $bytes, $decimals = 0 ) { 
    263263                'B'  => 1,
    264264        );
    265265
     266        if ( 0 === $bytes ) {
     267                return number_format_i18n( 0, $decimals ) . ' B';
     268        }
     269
    266270        foreach ( $quant as $unit => $mag ) {
    267271                if ( doubleval( $bytes ) >= $mag ) {
    268272                        return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 7d3beb3..4781670 100644
    class Tests_Functions extends WP_UnitTestCase { 
    5151                $this->assertInternalType( 'string', $args['bar'] );
    5252        }
    5353
    54         function test_size_format() {
    55                 $b  = 1;
    56                 $kb = 1024;
    57                 $mb = $kb*1024;
    58                 $gb = $mb*1024;
    59                 $tb = $gb*1024;
    60                 // test if boundaries are correct
    61                 $this->assertEquals('1 GB', size_format($gb, 0));
    62                 $this->assertEquals('1 MB', size_format($mb, 0));
    63                 $this->assertEquals('1 kB', size_format($kb, 0));
    64                 $this->assertEquals('1 B',  size_format($b, 0));
    65                 // now some values around
    66                 // add some bytes to make sure the result isn't 1.4999999
    67                 $this->assertEquals('1.5 TB', size_format($tb + $tb/2 + $mb, 1));
    68                 $this->assertEquals('1,023.999 GB', size_format($tb-$mb-$kb, 3));
    69                 // edge
    70                 $this->assertFalse(size_format(-1));
    71                 $this->assertFalse(size_format(0));
    72                 $this->assertFalse(size_format('baba'));
    73                 $this->assertFalse(size_format(array()));
    74         }
    75 
    7654        /**
    7755         * @ticket 35972
    7856         */
  • new file tests/phpunit/tests/functions/sizeFormat.php

    diff --git tests/phpunit/tests/functions/sizeFormat.php tests/phpunit/tests/functions/sizeFormat.php
    new file mode 100644
    index 0000000..8fbbd1e
    - +  
     1<?php
     2
     3/**
     4 * Tests for size_format()
     5 *
     6 * @group functions.php
     7 * @ticket 36635
     8 */
     9class Tests_Functions_Size_Format extends WP_UnitTestCase {
     10        public function _data_size_format() {
     11                return array(
     12                        array( array(), 0, false ),
     13                        array( 'baba', 0, false ),
     14                        array( '', 0, false ),
     15                        array( '-1', 0, false ),
     16                        array( -1, 0, false ),
     17                        array( 0, 0, '0 B' ),
     18                        array( 1, 0, '1 B' ),
     19                        array( 1023, 0, '1,023 B' ),
     20                        array( KB_IN_BYTES, 0, '1 kB' ),
     21                        array( KB_IN_BYTES, 2, '1.00 kB' ),
     22                        array( 2.5 * KB_IN_BYTES, 0, '3 kB' ),
     23                        array( 2.5 * KB_IN_BYTES, 2, '2.50 kB' ),
     24                        array( 10 * KB_IN_BYTES, 0, '10 kB' ),
     25                        array( (string) 1024 * KB_IN_BYTES, 2, '1.00 MB' ),
     26                        array( MB_IN_BYTES, 0, '1 MB' ),
     27                        array( 2.5 * MB_IN_BYTES, 0, '3 MB' ),
     28                        array( 2.5 * MB_IN_BYTES, 2, '2.50 MB' ),
     29                        array( (string) 1024 * MB_IN_BYTES, 2, '1.00 GB' ),
     30                        array( GB_IN_BYTES, 0, '1 GB' ),
     31                        array( 2.5 * GB_IN_BYTES, 0, '3 GB' ),
     32                        array( 2.5 * GB_IN_BYTES, 2, '2.50 GB' ),
     33                        array( (string) 1024 * GB_IN_BYTES, 2, '1.00 TB' ),
     34                        array( TB_IN_BYTES, 0, '1 TB' ),
     35                        array( 2.5 * TB_IN_BYTES, 0, '3 TB' ),
     36                        array( 2.5 * TB_IN_BYTES, 2, '2.50 TB' ),
     37                        array( TB_IN_BYTES + (TB_IN_BYTES/2) + MB_IN_BYTES, 1, '1.5 TB' ),
     38                        array( TB_IN_BYTES - MB_IN_BYTES - KB_IN_BYTES, 3, '1,023.999 GB' ),
     39                );
     40        }
     41
     42        /**
     43         * @dataProvider _data_size_format
     44         *
     45         * @param $bytes
     46         * @param $decimals
     47         * @param $expected
     48         */
     49        public function test_size_format( $bytes, $decimals, $expected ) {
     50                $this->assertSame( $expected, size_format( $bytes, $decimals ) );
     51        }
     52}