Make WordPress Core

Ticket #39667: 39667.8.diff

File 39667.8.diff, 7.2 KB (added by adamsilverstein, 5 years ago)
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 59a728b593..861425c804 100644
    function size_format( $bytes, $decimals = 0 ) { 
    323323}
    324324
    325325/**
    326  * Convert a filelength to human readable format.
     326 * Convert a duration to human readable format.
    327327 *
    328  * @since 5.0
     328 * @since 5.1.0
    329329 *
    330  * @param string $filelength Duration will be in string format (HH:ii:ss) OR (ii:ss).
    331  * @return boolean|string A human readable filelength string, false on failure.
     330 * @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss),
     331 *                         with a possible prepended negative sign (-).
     332 * @return string|false A human readable duration string, false on failure.
    332333 */
    333 function human_readable_duration( $filelength = '' ) {
    334         // Return false if filelength is empty or not in format.
    335         if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) {
     334function human_readable_duration( $duration = '' ) {
     335        if ( ( empty( $duration ) || ! is_string( $duration ) ) ) {
    336336                return false;
    337337        }
    338338
    339         // Validate filelength format.
    340         if ( ! ( (bool) preg_match( '/^(([0-3]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/', $filelength ) ) ) {
    341                 return false;
     339        $duration = trim( $duration );
     340
     341        // Remove prepended negative sign.
     342        if ( '-' === substr( $duration, 0, 1 ) ) {
     343                $duration = substr( $duration, 1 );
    342344        }
    343345
    344         $human_readable_duration = array();
     346        // Extract duration parts.
     347        $duration_parts = array_reverse( explode( ':', $duration ) );
     348        $duration_count = count( $duration_parts );
    345349
    346         // Extract duration.
    347         $durations      = array_reverse( explode( ':', $filelength ) );
    348         $duration_count = count( $durations );
     350        $hour   = null;
     351        $minute = null;
     352        $second = null;
    349353
    350354        if ( 3 === $duration_count ) {
     355                // Validate HH:ii:ss duration format.
     356                if ( ! ( (bool) preg_match( '/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {
     357                        return false;
     358                }
    351359                // Three parts: hours, minutes & seconds.
    352                 list( $second, $minute, $hour ) = $durations;
     360                list( $second, $minute, $hour ) = $duration_parts;
    353361        } elseif ( 2 === $duration_count ) {
     362                // Validate ii:ss duration format.
     363                if ( ! ( (bool) preg_match( '/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {
     364                        return false;
     365                }
    354366                // Two parts: minutes & seconds.
    355                 list( $second, $minute ) = $durations;
     367                list( $second, $minute ) = $duration_parts;
    356368        } else {
    357369                return false;
    358370        }
    359371
     372        $human_readable_duration = array();
     373
    360374        // Add the hour part to the string.
    361         if ( ! empty( $hour ) && is_numeric( $hour ) ) {
     375        if ( is_numeric( $hour ) ) {
    362376                /* translators: Time duration in hour or hours. */
    363377                $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour );
    364378        }
    365379
    366380        // Add the minute part to the string.
    367         if ( ! empty( $minute ) && is_numeric( $minute ) ) {
     381        if ( is_numeric( $minute ) ) {
    368382                /* translators: Time duration in minute or minutes. */
    369383                $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute );
    370384        }
    371385
    372386        // Add the second part to the string.
    373         if ( ! empty( $second ) && is_numeric( $second ) ) {
     387        if ( is_numeric( $second ) ) {
    374388                /* translators: Time duration in second or seconds. */
    375389                $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second );
    376390        }
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 84d032dd95..efbc88a762 100644
    class Tests_Functions extends WP_UnitTestCase { 
    15791579        }
    15801580
    15811581        /**
    1582          * Test the human_readable_duration function.
     1582         * Test human_readable_duration().
    15831583         *
    15841584         * @ticket 39667
    1585          * @dataProvider _datahuman_readable_duration()
     1585         * @dataProvider data_test_human_readable_duration
    15861586         *
    1587          * @param $input
    1588          * @param $expected
     1587         * @param string $input    Duration.
     1588         * @param string $expected Expected human readable duration.
    15891589         */
    1590         public function test_duration_format( $input, $expected ) {
     1590        public function test_human_readable_duration( $input, $expected ) {
    15911591                $this->assertSame( $expected, human_readable_duration( $input ) );
    15921592        }
    15931593
    1594         public function _datahuman_readable_duration() {
     1594        /**
     1595         * Dataprovider for test_duration_format().
     1596         *
     1597         * @return array {
     1598         *     @type array {
     1599         *         @type string $input  Duration.
     1600         *         @type string $expect Expected human readable duration.
     1601         *     }
     1602         * }
     1603         */
     1604        public function data_test_human_readable_duration() {
    15951605                return array(
    1596                         array( array(), false ),
     1606                        // Valid ii:ss cases.
     1607                        array( '0:0', '0 minutes, 0 seconds' ),
     1608                        array( '00:00', '0 minutes, 0 seconds' ),
     1609                        array( '0:5', '0 minutes, 5 seconds' ),
     1610                        array( '0:05', '0 minutes, 5 seconds' ),
     1611                        array( '01:01', '1 minute, 1 second' ),
    15971612                        array( '30:00', '30 minutes, 0 seconds' ),
     1613                        array( ' 30:00 ', '30 minutes, 0 seconds' ),
     1614                        // Valid HH:ii:ss cases.
     1615                        array( '0:0:0', '0 hours, 0 minutes, 0 seconds' ),
     1616                        array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ),
     1617                        array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ),
     1618                        array( '01:01:01', '1 hour, 1 minute, 1 second' ),
     1619                        array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ),
     1620                        array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ),
     1621                        array( '1234567890:59:59', '1234567890 hours, 59 minutes, 59 seconds' ),
     1622                        // Valid ii:ss cases with negative sign.
     1623                        array( '-00:00', '0 minutes, 0 seconds' ),
     1624                        array( '-3:00', '3 minutes, 0 seconds' ),
     1625                        array( '-03:00', '3 minutes, 0 seconds' ),
     1626                        array( '-30:00', '30 minutes, 0 seconds' ),
     1627                        // Valid HH:ii:ss cases with negative sign.
     1628                        array( '-00:00:00', '0 hours, 0 minutes, 0 seconds' ),
     1629                        array( '-1:02:00', '1 hour, 2 minutes, 0 seconds' ),
     1630                        // Invalid cases.
     1631                        array( null, false ),
     1632                        array( '', false ),
     1633                        array( ':', false ),
     1634                        array( '::', false ),
     1635                        array( array(), false ),
    15981636                        array( 'Batman Begins !', false ),
    15991637                        array( '', false ),
    16001638                        array( '-1', false ),
    class Tests_Functions extends WP_UnitTestCase { 
    16021640                        array( 0, false ),
    16031641                        array( 1, false ),
    16041642                        array( '00', false ),
    1605                         array( '00:00', '0 minutes, 0 seconds' ),
    1606                         array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ),
    1607                         array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ),
    1608                         array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ),
    1609                         array( 'MM:30:00', false ),
    1610                         array( '30:MM', false ),
    1611                         array( 'MM:00', false ),
    1612                         array( 'MM:MM', false ),
    1613                         array( '01:01', '1 minute, 1 second' ),
    1614                         array( '01:01:01', '1 hour, 1 minute, 1 second' ),
    1615                         array( '0:05', '5 seconds' ),
    1616                         array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ),
     1643                        array( '30:-10', false ),
     1644                        array( ':30:00', false ), // Missing HH.
     1645                        array( 'MM:30:00', false ), // Invalid HH.
     1646                        array( '30:MM:00', false ), // Invalid ii.
     1647                        array( '30:30:MM', false ), // Invalid ss.
     1648                        array( '30:MM', false ), // Invalid ss.
     1649                        array( 'MM:00', false ), // Invalid ii.
     1650                        array( 'MM:MM', false ), // Invalid ii and ss.
     1651                        array( '10 :30', false ), // Containing a space.
     1652                        array( '59:61', false ), // Out of bound.
     1653                        array( '61:59', false ), // Out of bound.
     1654                        array( '3:59:61', false ), // Out of bound.
     1655                        array( '03:61:59', false ), // Out of bound.
    16171656                );
    16181657        }
    16191658}