Make WordPress Core

Ticket #36635: 36635.diff

File 36635.diff, 2.1 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 1c4a445..6bb85eb 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;
  • 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..0222069
    - +  
     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( '', 0, false ),
     13                        array( '-1', 0, false ),
     14                        array( -1, 0, false ),
     15                        array( 0, 0, '0 B' ),
     16                        array( 1, 0, '1 B' ),
     17                        array( 1023, 0, '1,023 B' ),
     18                        array( KB_IN_BYTES, 0, '1 kB' ),
     19                        array( KB_IN_BYTES, 2, '1.00 kB' ),
     20                        array( 2.5 * KB_IN_BYTES, 0, '3 kB' ),
     21                        array( 2.5 * KB_IN_BYTES, 2, '2.50 kB' ),
     22                        array( 10 * KB_IN_BYTES, 0, '10 kB' ),
     23                        array( (string) 1024 * KB_IN_BYTES, 2, '1.00 MB' ),
     24                        array( MB_IN_BYTES, 0, '1 MB' ),
     25                        array( 2.5 * MB_IN_BYTES, 0, '3 MB' ),
     26                        array( 2.5 * MB_IN_BYTES, 2, '2.50 MB' ),
     27                        array( (string) 1024 * MB_IN_BYTES, 2, '1.00 GB' ),
     28                        array( GB_IN_BYTES, 0, '1 GB' ),
     29                        array( 2.5 * GB_IN_BYTES, 0, '3 GB' ),
     30                        array( 2.5 * GB_IN_BYTES, 2, '2.50 GB' ),
     31                        array( (string) 1024 * GB_IN_BYTES, 2, '1.00 TB' ),
     32                        array( TB_IN_BYTES, 0, '1 TB' ),
     33                        array( 2.5 * TB_IN_BYTES, 0, '3 TB' ),
     34                        array( 2.5 * TB_IN_BYTES, 2, '2.50 TB' ),
     35                );
     36        }
     37
     38        /**
     39         * @dataProvider _data_size_format
     40         *
     41         * @param $bytes
     42         * @param $decimals
     43         * @param $expected
     44         */
     45        public function test_size_format( $bytes, $decimals, $expected ) {
     46                $this->assertSame( $expected, size_format( $bytes, $decimals ) );
     47        }
     48}