| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group formatting |
| 5 | */ |
| 6 | class Tests_Formatting_WpNaturalTime extends WP_UnitTestCase { |
| 7 | private $ts; |
| 8 | |
| 9 | function setUp() { |
| 10 | $this->ts = array( |
| 11 | 'now' => gmmktime(10, 0, 0, 7, 1, 2015), |
| 12 | 'now-1s' => gmmktime(9, 59, 59, 7, 1, 2015), |
| 13 | 'now+1s' => gmmktime(10, 0, 1, 7, 1, 2015), |
| 14 | 'now-30s' => gmmktime(9, 59, 30, 7, 1, 2015), |
| 15 | 'now+30s' => gmmktime(10, 0, 30, 7, 1, 2015), |
| 16 | 'now-1m' => gmmktime(9, 59, 0, 7, 1, 2015), |
| 17 | 'now-1m30s' => gmmktime(9, 58, 30, 7, 1, 2015), |
| 18 | 'now+1m' => gmmktime(10, 1, 0, 7, 1, 2015), |
| 19 | 'now+1m30s' => gmmktime(10, 1, 30, 7, 1, 2015), |
| 20 | 'now-1h' => gmmktime(9, 0, 0, 7, 1, 2015), |
| 21 | 'now+1h' => gmmktime(11, 0, 0, 7, 1, 2015), |
| 22 | 'now-1d' => gmmktime(10, 0, 0, 6, 30, 2015), |
| 23 | 'now+1d' => gmmktime(10, 0, 0, 7, 2, 2015), |
| 24 | 'now-1w' => gmmktime(10, 0, 0, 6, 24, 2015), |
| 25 | 'now+1w' => gmmktime(10, 0, 0, 7, 8, 2015), |
| 26 | 'now-1M' => gmmktime(10, 0, 0, 6, 1, 2015), |
| 27 | 'now+1M' => gmmktime(10, 0, 0, 8, 1, 2015), |
| 28 | 'now-1y' => gmmktime(10, 0, 0, 7, 1, 2014), |
| 29 | 'now+1y' => gmmktime(10, 0, 0, 7, 1, 2016), |
| 30 | 'now+1y1m' => gmmktime(10, 0, 1, 7, 1, 2016), |
| 31 | 'now+1y1d' => gmmktime(10, 0, 0, 7, 2, 2016), |
| 32 | 'now+1y1M' => gmmktime(10, 0, 0, 8, 1, 2016), |
| 33 | 'now+1y1M1d' => gmmktime(10, 0, 0, 8, 2, 2016), |
| 34 | 'now+1y1M1w' => gmmktime(10, 0, 0, 8, 6, 2016), |
| 35 | 'now+1y1M1w1d' => gmmktime(10, 0, 0, 8, 7, 2016), |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | function test_seconds_ago() { |
| 40 | $this->assertEquals( '1 second ago', wp_natural_time( $this->ts['now-1s'], $this->ts['now'] ) ); |
| 41 | $this->assertEquals( '30 seconds ago', wp_natural_time( $this->ts['now-30s'], $this->ts['now'] ) ); |
| 42 | } |
| 43 | |
| 44 | function test_seconds_from_now() { |
| 45 | $this->assertEquals( '1 second from now', wp_natural_time( $this->ts['now+1s'], $this->ts['now'] ) ); |
| 46 | $this->assertEquals( '30 seconds from now', wp_natural_time( $this->ts['now+30s'], $this->ts['now'] ) ); |
| 47 | } |
| 48 | |
| 49 | function test_minutes_ago() { |
| 50 | $this->assertEquals( '1 minute ago', wp_natural_time( $this->ts['now-1m'], $this->ts['now'] ) ); |
| 51 | $this->assertEquals( '2 minutes ago', wp_natural_time( $this->ts['now-1m'], $this->ts['now+1m'] ) ); |
| 52 | $this->assertEquals( '2 minutes ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now+1m'] ) ); |
| 53 | $this->assertEquals( '3 minutes ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now+1m30s'] ) ); |
| 54 | } |
| 55 | |
| 56 | function test_minutes_from_now() { |
| 57 | $this->assertEquals( '1 minute from now', wp_natural_time( $this->ts['now+1m'], $this->ts['now'] ) ); |
| 58 | $this->assertEquals( '2 minutes from now', wp_natural_time( $this->ts['now+1m'], $this->ts['now-1m'] ) ); |
| 59 | $this->assertEquals( '2 minutes from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now-1m'] ) ); |
| 60 | $this->assertEquals( '3 minutes from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now-1m30s'] ) ); |
| 61 | } |
| 62 | |
| 63 | function test_hours_ago() { |
| 64 | $this->assertEquals( '1 hour ago', wp_natural_time( $this->ts['now-1h'], $this->ts['now'] ) ); |
| 65 | $this->assertEquals( '2 hours ago', wp_natural_time( $this->ts['now-1h'], $this->ts['now+1h'] ) ); |
| 66 | } |
| 67 | |
| 68 | function test_hours_from_now() { |
| 69 | $this->assertEquals( '1 hour from now', wp_natural_time( $this->ts['now+1h'], $this->ts['now'] ) ); |
| 70 | $this->assertEquals( '2 hours from now', wp_natural_time( $this->ts['now+1h'], $this->ts['now-1h'] ) ); |
| 71 | } |
| 72 | |
| 73 | function test_days_ago() { |
| 74 | $this->assertEquals( '1 day ago', wp_natural_time( $this->ts['now-1d'], $this->ts['now'] ) ); |
| 75 | $this->assertEquals( '2 days ago', wp_natural_time( $this->ts['now-1d'], $this->ts['now+1d'] ) ); |
| 76 | } |
| 77 | |
| 78 | function test_days_from_now() { |
| 79 | $this->assertEquals( '1 day from now', wp_natural_time( $this->ts['now+1d'], $this->ts['now'] ) ); |
| 80 | $this->assertEquals( '2 days from now', wp_natural_time( $this->ts['now+1d'], $this->ts['now-1d'] ) ); |
| 81 | } |
| 82 | |
| 83 | function test_weeks_ago() { |
| 84 | $this->assertEquals( '1 week ago', wp_natural_time( $this->ts['now-1w'], $this->ts['now'] ) ); |
| 85 | $this->assertEquals( '2 weeks ago', wp_natural_time( $this->ts['now-1w'], $this->ts['now+1w'] ) ); |
| 86 | } |
| 87 | |
| 88 | function test_weeks_from_now() { |
| 89 | $this->assertEquals( '1 week from now', wp_natural_time( $this->ts['now+1w'], $this->ts['now'] ) ); |
| 90 | $this->assertEquals( '2 weeks from now', wp_natural_time( $this->ts['now+1w'], $this->ts['now-1w'] ) ); |
| 91 | } |
| 92 | |
| 93 | function test_months_ago() { |
| 94 | $this->assertEquals( '1 month ago', wp_natural_time( $this->ts['now-1M'], $this->ts['now'] ) ); |
| 95 | $this->assertEquals( '2 months ago', wp_natural_time( $this->ts['now-1M'], $this->ts['now+1M'] ) ); |
| 96 | } |
| 97 | |
| 98 | function test_months_from_now() { |
| 99 | $this->assertEquals( '1 month from now', wp_natural_time( $this->ts['now+1M'], $this->ts['now'] ) ); |
| 100 | $this->assertEquals( '2 months from now', wp_natural_time( $this->ts['now+1M'], $this->ts['now-1M'] ) ); |
| 101 | } |
| 102 | |
| 103 | function test_years_ago() { |
| 104 | $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now-1y'], $this->ts['now'] ) ); |
| 105 | $this->assertEquals( '2 years ago', wp_natural_time( $this->ts['now-1y'], $this->ts['now+1y'] ) ); |
| 106 | } |
| 107 | |
| 108 | function test_years_from_now() { |
| 109 | $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y'], $this->ts['now'] ) ); |
| 110 | $this->assertEquals( '2 years from now', wp_natural_time( $this->ts['now+1y'], $this->ts['now-1y'] ) ); |
| 111 | } |
| 112 | |
| 113 | function test_now() { |
| 114 | $this->assertEquals( 'now', wp_natural_time( $this->ts['now'], $this->ts['now'] ) ); |
| 115 | } |
| 116 | |
| 117 | function test_composed_from_now() { |
| 118 | $this->assertEquals( '1 minute, 30 seconds from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now'], 2 ) ); |
| 119 | $this->assertEquals( '1 minute, 30 seconds from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now'], 3 ) ); |
| 120 | |
| 121 | $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y1m'], $this->ts['now'], 2 ) ); |
| 122 | $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y1d'], $this->ts['now'], 2 ) ); |
| 123 | |
| 124 | $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M'], $this->ts['now'], 3 ) ); |
| 125 | $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M'], $this->ts['now'], 3 ) ); |
| 126 | |
| 127 | $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M1d'], $this->ts['now'], 2 ) ); |
| 128 | |
| 129 | $this->assertEquals( '1 year, 1 month, 1 week from now', wp_natural_time( $this->ts['now+1y1M1w'], $this->ts['now'], 3 ) ); |
| 130 | |
| 131 | $this->assertEquals( '1 year, 1 month, 1 week, 1 day from now', wp_natural_time( $this->ts['now+1y1M1w1d'], $this->ts['now'], 4 ) ); |
| 132 | } |
| 133 | |
| 134 | function test_composed_ago() { |
| 135 | $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now'], 2 ) ); |
| 136 | $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now'], $this->ts['now+1m30s'], 2 ) ); |
| 137 | $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now'], $this->ts['now+1m30s'], 3 ) ); |
| 138 | |
| 139 | $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1m'], 2 ) ); |
| 140 | $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1d'], 2 ) ); |
| 141 | |
| 142 | $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M'], 3 ) ); |
| 143 | $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M'], 3 ) ); |
| 144 | |
| 145 | $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1d'], 2 ) ); |
| 146 | |
| 147 | $this->assertEquals( '1 year, 1 month, 1 week ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1w'], 3 ) ); |
| 148 | |
| 149 | $this->assertEquals( '1 year, 1 month, 1 week, 1 day ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1w1d'], 4 ) ); |
| 150 | } |
| 151 | |
| 152 | function test_composed_mysqldate() { |
| 153 | $one_hr_ago = date( 'Y-m-d H:i:s', $this->ts['now-1h'] ); |
| 154 | $now = date( 'Y-m-d H:i:s', $this->ts['now'] ); |
| 155 | |
| 156 | $this->assertEquals( '1 hour ago', wp_natural_time( $one_hr_ago, $this->ts['now'] ) ); |
| 157 | $this->assertEquals( '1 hour ago', wp_natural_time( $one_hr_ago, $now ) ); |
| 158 | $this->assertEquals( '1 hour ago', wp_natural_time( $this->ts['now-1h'], $now ) ); |
| 159 | } |
| 160 | |
| 161 | } |