4 * Tests timestamp parsing and output.
6 class MWTimestampTest
extends MediaWikiLangTestCase
{
8 protected function setUp() {
11 // Avoid 'GetHumanTimestamp' hook and others
12 $this->setMwGlobals( 'wgHooks', [] );
16 * @covers MWTimestamp::__construct
18 public function testConstructWithNoTimestamp() {
19 $timestamp = new MWTimestamp();
20 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
21 $this->assertNotEmpty( $timestamp->getTimestamp() );
22 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW
) ) );
26 * @covers MWTimestamp::__toString
28 public function testToString() {
29 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
30 $this->assertEquals( '1406833268', $timestamp->__toString() );
33 public static function provideValidTimestampDifferences() {
35 [ '1406833268', '1406833269', '00 00 00 01' ],
36 [ '1406833268', '1406833329', '00 00 01 01' ],
37 [ '1406833268', '1406836929', '00 01 01 01' ],
38 [ '1406833268', '1406923329', '01 01 01 01' ],
43 * @dataProvider provideValidTimestampDifferences
44 * @covers MWTimestamp::diff
46 public function testDiff( $timestamp1, $timestamp2, $expected ) {
47 $timestamp1 = new MWTimestamp( $timestamp1 );
48 $timestamp2 = new MWTimestamp( $timestamp2 );
49 $diff = $timestamp1->diff( $timestamp2 );
50 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
54 * Test parsing of valid timestamps and outputing to MW format.
55 * @dataProvider provideValidTimestamps
56 * @covers MWTimestamp::getTimestamp
58 public function testValidParse( $format, $original, $expected ) {
59 $timestamp = new MWTimestamp( $original );
60 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
64 * Test outputting valid timestamps to different formats.
65 * @dataProvider provideValidTimestamps
66 * @covers MWTimestamp::getTimestamp
68 public function testValidOutput( $format, $expected, $original ) {
69 $timestamp = new MWTimestamp( $original );
70 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
74 * Test an invalid timestamp.
75 * @expectedException TimestampException
78 public function testInvalidParse() {
79 new MWTimestamp( "This is not a timestamp." );
83 * Test an out of range timestamp
84 * @dataProvider provideOutOfRangeTimestamps
85 * @expectedException TimestampException
88 public function testOutOfRangeTimestamps( $format, $input ) {
89 $timestamp = new MWTimestamp( $input );
90 $timestamp->getTimestamp( $format );
94 * Test requesting an invalid output format.
95 * @expectedException TimestampException
96 * @covers MWTimestamp::getTimestamp
98 public function testInvalidOutput() {
99 $timestamp = new MWTimestamp( '1343761268' );
100 $timestamp->getTimestamp( 98 );
104 * Returns a list of valid timestamps in the format:
105 * array( type, timestamp_of_type, timestamp_in_MW )
107 public static function provideValidTimestamps() {
110 [ TS_UNIX
, '1343761268', '20120731190108' ],
111 [ TS_MW
, '20120731190108', '20120731190108' ],
112 [ TS_DB
, '2012-07-31 19:01:08', '20120731190108' ],
113 [ TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ],
114 [ TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ],
115 [ TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ],
116 [ TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ],
117 [ TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ],
118 [ TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ],
119 // Some extremes and weird values
120 [ TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ],
121 [ TS_UNIX
, '-62135596801', '00001231235959' ]
126 * Returns a list of out of range timestamps in the format:
127 * array( type, timestamp_of_type )
129 public static function provideOutOfRangeTimestamps() {
132 [ TS_MW
, '-62167219201' ], // -0001-12-31T23:59:59Z
133 [ TS_MW
, '253402300800' ], // 10000-01-01T00:00:00Z
138 * @dataProvider provideHumanTimestampTests
139 * @covers MWTimestamp::getHumanTimestamp
141 public function testHumanTimestamp(
142 $tsTime, // The timestamp to format
143 $currentTime, // The time to consider "now"
144 $timeCorrection, // The time offset to use
145 $dateFormat, // The date preference to use
146 $expectedOutput, // The expected output
149 $user = $this->getMock( 'User' );
150 $user->expects( $this->any() )
151 ->method( 'getOption' )
152 ->with( 'timecorrection' )
153 ->will( $this->returnValue( $timeCorrection ) );
155 $user->expects( $this->any() )
156 ->method( 'getDatePreference' )
157 ->will( $this->returnValue( $dateFormat ) );
159 $tsTime = new MWTimestamp( $tsTime );
160 $currentTime = new MWTimestamp( $currentTime );
164 $tsTime->getHumanTimestamp( $currentTime, $user ),
169 public static function provideHumanTimestampTests() {
176 'Yesterday at 17:00',
177 '"Yesterday" across years',
216 '15:15, January 30, 1991',
224 'Yesterday at 23:00',
225 '"Yesterday" across years with time correction',
233 'Recent weekday with time correction',
241 'Today at another time with time correction',
249 'Another month with dmy'
257 'Another month with ISO-8601'
264 '1991-01-30T15:15:00',
265 'Different year with ISO-8601',
271 * @dataProvider provideRelativeTimestampTests
272 * @covers MWTimestamp::getRelativeTimestamp
274 public function testRelativeTimestamp(
275 $tsTime, // The timestamp to format
276 $currentTime, // The time to consider "now"
277 $timeCorrection, // The time offset to use
278 $dateFormat, // The date preference to use
279 $expectedOutput, // The expected output
282 $user = $this->getMock( 'User' );
283 $user->expects( $this->any() )
284 ->method( 'getOption' )
285 ->with( 'timecorrection' )
286 ->will( $this->returnValue( $timeCorrection ) );
288 $tsTime = new MWTimestamp( $tsTime );
289 $currentTime = new MWTimestamp( $currentTime );
293 $tsTime->getRelativeTimestamp( $currentTime, $user ),
298 public static function provideRelativeTimestampTests() {
306 '"Yesterday" across years',
321 '6 minutes and 30 seconds ago',
322 'Combination of multiple units',
337 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
346 '"Yesterday" across years with time correction',
354 'Recent weekday with time correction',
361 '9 hours and 17 minutes ago',
362 'Today at another time with time correction',