4 * Tests timestamp parsing and output.
6 class MWTimestampTest
extends MediaWikiLangTestCase
{
7 protected function setUp() {
10 // Avoid 'GetHumanTimestamp' hook and others
11 $this->setMwGlobals( 'wgHooks', [] );
15 * @dataProvider provideHumanTimestampTests
16 * @covers MWTimestamp::getHumanTimestamp
18 public function testHumanTimestamp(
19 $tsTime, // The timestamp to format
20 $currentTime, // The time to consider "now"
21 $timeCorrection, // The time offset to use
22 $dateFormat, // The date preference to use
23 $expectedOutput, // The expected output
26 $user = $this->createMock( 'User' );
27 $user->expects( $this->any() )
28 ->method( 'getOption' )
29 ->with( 'timecorrection' )
30 ->will( $this->returnValue( $timeCorrection ) );
32 $user->expects( $this->any() )
33 ->method( 'getDatePreference' )
34 ->will( $this->returnValue( $dateFormat ) );
36 $tsTime = new MWTimestamp( $tsTime );
37 $currentTime = new MWTimestamp( $currentTime );
41 $tsTime->getHumanTimestamp( $currentTime, $user ),
46 public static function provideHumanTimestampTests() {
54 '"Yesterday" across years',
93 '15:15, January 30, 1991',
101 'Yesterday at 23:00',
102 '"Yesterday" across years with time correction',
110 'Recent weekday with time correction',
118 'Today at another time with time correction',
126 'Another month with dmy'
134 'Another month with ISO-8601'
141 '1991-01-30T15:15:00',
142 'Different year with ISO-8601',
148 * @dataProvider provideRelativeTimestampTests
149 * @covers MWTimestamp::getRelativeTimestamp
151 public function testRelativeTimestamp(
152 $tsTime, // The timestamp to format
153 $currentTime, // The time to consider "now"
154 $timeCorrection, // The time offset to use
155 $dateFormat, // The date preference to use
156 $expectedOutput, // The expected output
159 $user = $this->createMock( 'User' );
160 $user->expects( $this->any() )
161 ->method( 'getOption' )
162 ->with( 'timecorrection' )
163 ->will( $this->returnValue( $timeCorrection ) );
165 $tsTime = new MWTimestamp( $tsTime );
166 $currentTime = new MWTimestamp( $currentTime );
170 $tsTime->getRelativeTimestamp( $currentTime, $user ),
175 public static function provideRelativeTimestampTests() {
183 '"Yesterday" across years',
198 '6 minutes and 30 seconds ago',
199 'Combination of multiple units',
214 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
223 '"Yesterday" across years with time correction',
231 'Recent weekday with time correction',
238 '9 hours and 17 minutes ago',
239 'Today at another time with time correction',