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 requesting an invalid output format.
86 * @expectedException TimestampException
87 * @covers MWTimestamp::getTimestamp
89 public function testInvalidOutput() {
90 $timestamp = new MWTimestamp( '1343761268' );
91 $timestamp->getTimestamp( 98 );
95 * Returns a list of valid timestamps in the format:
96 * array( type, timestamp_of_type, timestamp_in_MW )
98 public static function provideValidTimestamps() {
101 array( TS_UNIX
, '1343761268', '20120731190108' ),
102 array( TS_MW
, '20120731190108', '20120731190108' ),
103 array( TS_DB
, '2012-07-31 19:01:08', '20120731190108' ),
104 array( TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ),
105 array( TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ),
106 array( TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ),
107 array( TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
108 array( TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ),
109 array( TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ),
110 // Some extremes and weird values
111 array( TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ),
112 array( TS_UNIX
, '-62135596801', '00001231235959' )
117 * @dataProvider provideHumanTimestampTests
118 * @covers MWTimestamp::getHumanTimestamp
120 public function testHumanTimestamp(
121 $tsTime, // The timestamp to format
122 $currentTime, // The time to consider "now"
123 $timeCorrection, // The time offset to use
124 $dateFormat, // The date preference to use
125 $expectedOutput, // The expected output
128 $user = $this->getMock( 'User' );
129 $user->expects( $this->any() )
130 ->method( 'getOption' )
131 ->with( 'timecorrection' )
132 ->will( $this->returnValue( $timeCorrection ) );
134 $user->expects( $this->any() )
135 ->method( 'getDatePreference' )
136 ->will( $this->returnValue( $dateFormat ) );
138 $tsTime = new MWTimestamp( $tsTime );
139 $currentTime = new MWTimestamp( $currentTime );
143 $tsTime->getHumanTimestamp( $currentTime, $user ),
148 public static function provideHumanTimestampTests() {
155 'Yesterday at 17:00',
156 '"Yesterday" across years',
195 '15:15, January 30, 1991',
203 'Yesterday at 23:00',
204 '"Yesterday" across years with time correction',
212 'Recent weekday with time correction',
220 'Today at another time with time correction',
228 'Another month with dmy'
236 'Another month with ISO-8601'
243 '1991-01-30T15:15:00',
244 'Different year with ISO-8601',
250 * @dataProvider provideRelativeTimestampTests
251 * @covers MWTimestamp::getRelativeTimestamp
253 public function testRelativeTimestamp(
254 $tsTime, // The timestamp to format
255 $currentTime, // The time to consider "now"
256 $timeCorrection, // The time offset to use
257 $dateFormat, // The date preference to use
258 $expectedOutput, // The expected output
261 $user = $this->getMock( 'User' );
262 $user->expects( $this->any() )
263 ->method( 'getOption' )
264 ->with( 'timecorrection' )
265 ->will( $this->returnValue( $timeCorrection ) );
267 $tsTime = new MWTimestamp( $tsTime );
268 $currentTime = new MWTimestamp( $currentTime );
272 $tsTime->getRelativeTimestamp( $currentTime, $user ),
277 public static function provideRelativeTimestampTests() {
285 '"Yesterday" across years',
300 '6 minutes and 30 seconds ago',
301 'Combination of multiple units',
316 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
325 '"Yesterday" across years with time correction',
333 'Recent weekday with time correction',
340 '9 hours and 17 minutes ago',
341 'Today at another time with time correction',