Ticket #39667: 39667.7.diff
File 39667.7.diff, 7.2 KB (added by , 6 years ago) |
---|
-
src/wp-includes/functions.php
diff --git src/wp-includes/functions.php src/wp-includes/functions.php index 1433ca9..cf1bf22 100644
function size_format( $bytes, $decimals = 0 ) { 323 323 } 324 324 325 325 /** 326 * Convert a filelengthto human readable format.326 * Convert a duration to human readable format. 327 327 * 328 * @since 5.0 328 * @since 5.0.0 329 329 * 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. 332 333 */ 333 function human_readable_duration( $filelength = '' ) { 334 // Return false if filelength is empty or not in format. 335 if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) { 334 function human_readable_duration( $duration = '' ) { 335 if ( ( empty( $duration ) || ! is_string( $duration ) ) ) { 336 336 return false; 337 337 } 338 338 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 ); 342 344 } 343 345 344 $human_readable_duration = array(); 346 // Extract duration parts. 347 $duration_parts = array_reverse( explode( ':', $duration ) ); 348 $duration_count = count( $duration_parts ); 345 349 346 // Extract duration.347 $ durations = array_reverse( explode( ':', $filelength ) );348 $ duration_count = count( $durations );350 $hour = null; 351 $minute = null; 352 $second = null; 349 353 350 354 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 } 351 359 // Three parts: hours, minutes & seconds. 352 list( $second, $minute, $hour ) = $duration s;360 list( $second, $minute, $hour ) = $duration_parts; 353 361 } 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 } 354 366 // Two parts: minutes & seconds. 355 list( $second, $minute ) = $duration s;367 list( $second, $minute ) = $duration_parts; 356 368 } else { 357 369 return false; 358 370 } 359 371 372 $human_readable_duration = array(); 373 360 374 // Add the hour part to the string. 361 if ( ! empty( $hour ) &&is_numeric( $hour ) ) {375 if ( is_numeric( $hour ) ) { 362 376 /* translators: Time duration in hour or hours. */ 363 377 $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour ); 364 378 } 365 379 366 380 // Add the minute part to the string. 367 if ( ! empty( $minute ) &&is_numeric( $minute ) ) {381 if ( is_numeric( $minute ) ) { 368 382 /* translators: Time duration in minute or minutes. */ 369 383 $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute ); 370 384 } 371 385 372 386 // Add the second part to the string. 373 if ( ! empty( $second ) &&is_numeric( $second ) ) {387 if ( is_numeric( $second ) ) { 374 388 /* translators: Time duration in second or seconds. */ 375 389 $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second ); 376 390 } -
tests/phpunit/tests/functions.php
diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php index d6d5d06..e715e0b 100644
class Tests_Functions extends WP_UnitTestCase { 1520 1520 } 1521 1521 1522 1522 /** 1523 * Test the human_readable_duration function.1523 * Test the human_readable_duration() function. 1524 1524 * 1525 1525 * @ticket 39667 1526 * @dataProvider _datahuman_readable_duration()1526 * @dataProvider data_test_human_readable_duration() 1527 1527 * 1528 * @param $input1529 * @param $expected1528 * @param string $input Duration. 1529 * @param string $expected Expected human readable duration. 1530 1530 */ 1531 public function test_ duration_format( $input, $expected ) {1531 public function test_human_readable_duration( $input, $expected ) { 1532 1532 $this->assertSame( $expected, human_readable_duration( $input ) ); 1533 1533 } 1534 1534 1535 public function _datahuman_readable_duration() { 1535 /** 1536 * Dataprovider for test_duration_format(). 1537 * 1538 * @return array { 1539 * @type array { 1540 * @type string $input Duration. 1541 * @type string $expect Expected human readable duration. 1542 * } 1543 * } 1544 */ 1545 public function data_test_human_readable_duration() { 1536 1546 return array( 1537 array( array(), false ), 1547 // Valid ii:ss cases. 1548 array( '0:0', '0 minutes, 0 seconds' ), 1549 array( '00:00', '0 minutes, 0 seconds' ), 1550 array( '0:5', '0 minutes, 5 seconds' ), 1551 array( '0:05', '0 minutes, 5 seconds' ), 1552 array( '01:01', '1 minute, 1 second' ), 1538 1553 array( '30:00', '30 minutes, 0 seconds' ), 1554 array( ' 30:00 ', '30 minutes, 0 seconds' ), 1555 // Valid HH:ii:ss cases. 1556 array( '0:0:0', '0 hours, 0 minutes, 0 seconds' ), 1557 array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1558 array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ), 1559 array( '01:01:01', '1 hour, 1 minute, 1 second' ), 1560 array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1561 array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ), 1562 array( '1234567890:59:59', '1234567890 hours, 59 minutes, 59 seconds' ), 1563 // Valid ii:ss cases with negative sign. 1564 array( '-00:00', '0 minutes, 0 seconds' ), 1565 array( '-3:00', '3 minutes, 0 seconds' ), 1566 array( '-03:00', '3 minutes, 0 seconds' ), 1567 array( '-30:00', '30 minutes, 0 seconds' ), 1568 // Valid HH:ii:ss cases with negative sign. 1569 array( '-00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1570 array( '-1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1571 // Invalid cases. 1572 array( null, false ), 1573 array( '', false ), 1574 array( ':', false ), 1575 array( '::', false ), 1576 array( array(), false ), 1539 1577 array( 'Batman Begins !', false ), 1540 1578 array( '', false ), 1541 1579 array( '-1', false ), … … class Tests_Functions extends WP_UnitTestCase { 1543 1581 array( 0, false ), 1544 1582 array( 1, false ), 1545 1583 array( '00', false ), 1546 array( '00:00', '0 minutes, 0 seconds' ), 1547 array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1548 array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ), 1549 array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ), 1550 array( 'MM:30:00', false ), 1551 array( '30:MM', false ), 1552 array( 'MM:00', false ), 1553 array( 'MM:MM', false ), 1554 array( '01:01', '1 minute, 1 second' ), 1555 array( '01:01:01', '1 hour, 1 minute, 1 second' ), 1556 array( '0:05', '5 seconds' ), 1557 array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1584 array( '30:-10', false ), 1585 array( ':30:00', false ), // Missing HH. 1586 array( 'MM:30:00', false ), // Invalid HH. 1587 array( '30:MM:00', false ), // Invalid ii. 1588 array( '30:30:MM', false ), // Invalid ss. 1589 array( '30:MM', false ), // Invalid ss. 1590 array( 'MM:00', false ), // Invalid ii. 1591 array( 'MM:MM', false ), // Invalid ii and ss. 1592 array( '10 :30', false ), // Containing a space. 1593 array( '59:61', false ), // Out of bound. 1594 array( '61:59', false ), // Out of bound. 1595 array( '3:59:61', false ), // Out of bound. 1596 array( '03:61:59', false ), // Out of bound. 1558 1597 ); 1559 1598 } 1560 1599 }