| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group formatting |
| | 5 | * @ticket 38773 |
| | 6 | */ |
| | 7 | class Tests_Formatting_HiumanTimeDiff extends WP_UnitTestCase { |
| | 8 | function test_simple() { |
| | 9 | // Test some basic differences such as "1 hour", "5 mins", "2 days". |
| | 10 | $startdate = new DateTime( '2016-01-01 12:00:00' ); |
| | 11 | |
| | 12 | // Test a difference of 5 minutes. |
| | 13 | $stopdate = new DateTime( '2016-01-01 12:05:00' ); |
| | 14 | $this->assertEquals( '5 mins', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 15 | |
| | 16 | // Test a difference of 1 hour. |
| | 17 | $stopdate = new DateTime( '2016-01-01 13:00:00' ); |
| | 18 | $this->assertEquals( '1 hour', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 19 | |
| | 20 | // Test a difference of 2 days. |
| | 21 | $stopdate = new DateTime( '2016-01-03 12:00:00' ); |
| | 22 | $this->assertEquals( '2 days', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 23 | } |
| | 24 | function test_rounding() { |
| | 25 | // Test some close dates for rounding issues. |
| | 26 | $startdate = new DateTime( '2016-01-01 12:00:00' ); |
| | 27 | |
| | 28 | // Test a difference of 2 hours and 30 minutes and 1 second - should round up to 3 hours. |
| | 29 | $stopdate = new DateTime( '2016-01-01 14:30:01' ); |
| | 30 | $this->assertEquals( '3 hours', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 31 | |
| | 32 | // Test a difference of 1 month and 16 days - should round up to 2 months. |
| | 33 | $stopdate = new DateTime( '2016-02-17 12:00:00' ); |
| | 34 | $this->assertEquals( '2 months', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 35 | |
| | 36 | // Test a difference of 2 years 6 months and 1 day, should round up to 3 years. |
| | 37 | $stopdate = new DateTime( '2018-07-02 12:00:00' ); |
| | 38 | $this->assertEquals( '3 years', human_time_diff( $startdate->getTimestamp(), $stopdate->getTimestamp() ) ); |
| | 39 | } |
| | 40 | |
| | 41 | } |