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', array() );
14 RequestContext
::getMain()->setLanguage( Language
::factory( 'en' ) );
18 * @covers MWTimestamp::__construct
20 public function testConstructWithNoTimestamp() {
21 $timestamp = new MWTimestamp();
22 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
23 $this->assertNotEmpty( $timestamp->getTimestamp() );
24 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW
) ) );
28 * @covers MWTimestamp::__toString
30 public function testToString() {
31 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
32 $this->assertEquals( '1406833268', $timestamp->__toString() );
35 public static function provideValidTimestampDifferences() {
37 array( '1406833268', '1406833269', '00 00 00 01' ),
38 array( '1406833268', '1406833329', '00 00 01 01' ),
39 array( '1406833268', '1406836929', '00 01 01 01' ),
40 array( '1406833268', '1406923329', '01 01 01 01' ),
45 * @dataProvider provideValidTimestampDifferences
46 * @covers MWTimestamp::diff
48 public function testDiff( $timestamp1, $timestamp2, $expected ) {
49 $timestamp1 = new MWTimestamp( $timestamp1 );
50 $timestamp2 = new MWTimestamp( $timestamp2 );
51 $diff = $timestamp1->diff( $timestamp2 );
52 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
56 * Test parsing of valid timestamps and outputing to MW format.
57 * @dataProvider provideValidTimestamps
58 * @covers MWTimestamp::getTimestamp
60 public function testValidParse( $format, $original, $expected ) {
61 $timestamp = new MWTimestamp( $original );
62 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
66 * Test outputting valid timestamps to different formats.
67 * @dataProvider provideValidTimestamps
68 * @covers MWTimestamp::getTimestamp
70 public function testValidOutput( $format, $expected, $original ) {
71 $timestamp = new MWTimestamp( $original );
72 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
76 * Test an invalid timestamp.
77 * @expectedException TimestampException
80 public function testInvalidParse() {
81 new MWTimestamp( "This is not a timestamp." );
85 * Test an out of range timestamp
86 * @dataProvider provideOutOfRangeTimestamps
87 * @expectedException TimestampException
90 public function testOutOfRangeTimestamps( $format, $input ) {
91 $timestamp = new MWTimestamp( $input );
92 $timestamp->getTimestamp( $format );
96 * Test requesting an invalid output format.
97 * @expectedException TimestampException
98 * @covers MWTimestamp::getTimestamp
100 public function testInvalidOutput() {
101 $timestamp = new MWTimestamp( '1343761268' );
102 $timestamp->getTimestamp( 98 );
106 * Returns a list of valid timestamps in the format:
107 * array( type, timestamp_of_type, timestamp_in_MW )
109 public static function provideValidTimestamps() {
112 array( TS_UNIX
, '1343761268', '20120731190108' ),
113 array( TS_MW
, '20120731190108', '20120731190108' ),
114 array( TS_DB
, '2012-07-31 19:01:08', '20120731190108' ),
115 array( TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ),
116 array( TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ),
117 array( TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ),
118 array( TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
119 array( TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ),
120 array( TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ),
121 // Some extremes and weird values
122 array( TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ),
123 array( TS_UNIX
, '-62135596801', '00001231235959' )
128 * Returns a list of out of range timestamps in the format:
129 * array( type, timestamp_of_type )
131 public static function provideOutOfRangeTimestamps() {
134 array( TS_MW
, '-62167219201' ), // -0001-12-31T23:59:59Z
135 array( TS_MW
, '253402300800' ), // 10000-01-01T00:00:00Z
140 * @dataProvider provideHumanTimestampTests
141 * @covers MWTimestamp::getHumanTimestamp
143 public function testHumanTimestamp(
144 $tsTime, // The timestamp to format
145 $currentTime, // The time to consider "now"
146 $timeCorrection, // The time offset to use
147 $dateFormat, // The date preference to use
148 $expectedOutput, // The expected output
151 $user = $this->getMock( 'User' );
152 $user->expects( $this->any() )
153 ->method( 'getOption' )
154 ->with( 'timecorrection' )
155 ->will( $this->returnValue( $timeCorrection ) );
157 $user->expects( $this->any() )
158 ->method( 'getDatePreference' )
159 ->will( $this->returnValue( $dateFormat ) );
161 $tsTime = new MWTimestamp( $tsTime );
162 $currentTime = new MWTimestamp( $currentTime );
166 $tsTime->getHumanTimestamp( $currentTime, $user ),
171 public static function provideHumanTimestampTests() {
178 'Yesterday at 17:00',
179 '"Yesterday" across years',
218 '15:15, January 30, 1991',
226 'Yesterday at 23:00',
227 '"Yesterday" across years with time correction',
235 'Recent weekday with time correction',
243 'Today at another time with time correction',
251 'Another month with dmy'
259 'Another month with ISO-8601'
266 '1991-01-30T15:15:00',
267 'Different year with ISO-8601',
273 * @dataProvider provideRelativeTimestampTests
274 * @covers MWTimestamp::getRelativeTimestamp
276 public function testRelativeTimestamp(
277 $tsTime, // The timestamp to format
278 $currentTime, // The time to consider "now"
279 $timeCorrection, // The time offset to use
280 $dateFormat, // The date preference to use
281 $expectedOutput, // The expected output
284 $user = $this->getMock( 'User' );
285 $user->expects( $this->any() )
286 ->method( 'getOption' )
287 ->with( 'timecorrection' )
288 ->will( $this->returnValue( $timeCorrection ) );
290 $tsTime = new MWTimestamp( $tsTime );
291 $currentTime = new MWTimestamp( $currentTime );
295 $tsTime->getRelativeTimestamp( $currentTime, $user ),
300 public static function provideRelativeTimestampTests() {
308 '"Yesterday" across years',
323 '6 minutes and 30 seconds ago',
324 'Combination of multiple units',
339 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
348 '"Yesterday" across years with time correction',
356 'Recent weekday with time correction',
363 '9 hours and 17 minutes ago',
364 'Today at another time with time correction',