4 * Tests timestamp parsing and output.
6 class MWTimestampTest
extends MediaWikiLangTestCase
{
8 protected function setUp() {
11 RequestContext
::getMain()->setLanguage( Language
::factory( 'en' ) );
15 * @covers MWTimestamp::__construct
17 public function testConstructWithNoTimestamp() {
18 $timestamp = new MWTimestamp();
19 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
20 $this->assertNotEmpty( $timestamp->getTimestamp() );
21 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW
) ) );
25 * @covers MWTimestamp::__toString
27 public function testToString() {
28 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
29 $this->assertEquals( '1406833268', $timestamp->__toString() );
32 public function provideValidTimestampDifferences() {
34 array( '1406833268', '1406833269', '00 00 00 01' ),
35 array( '1406833268', '1406833329', '00 00 01 01' ),
36 array( '1406833268', '1406836929', '00 01 01 01' ),
37 array( '1406833268', '1406923329', '01 01 01 01' ),
42 * @dataProvider provideValidTimestampDifferences
43 * @covers MWTimestamp::diff
45 public function testDiff( $timestamp1, $timestamp2, $expected ) {
46 $timestamp1 = new MWTimestamp( $timestamp1 );
47 $timestamp2 = new MWTimestamp( $timestamp2 );
48 $diff = $timestamp1->diff( $timestamp2 );
49 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
53 * Test parsing of valid timestamps and outputing to MW format.
54 * @dataProvider provideValidTimestamps
55 * @covers MWTimestamp::getTimestamp
57 public function testValidParse( $format, $original, $expected ) {
58 $timestamp = new MWTimestamp( $original );
59 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
63 * Test outputting valid timestamps to different formats.
64 * @dataProvider provideValidTimestamps
65 * @covers MWTimestamp::getTimestamp
67 public function testValidOutput( $format, $expected, $original ) {
68 $timestamp = new MWTimestamp( $original );
69 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
73 * Test an invalid timestamp.
74 * @expectedException TimestampException
77 public function testInvalidParse() {
78 new MWTimestamp( "This is not a timestamp." );
82 * Test requesting an invalid output format.
83 * @expectedException TimestampException
84 * @covers MWTimestamp::getTimestamp
86 public function testInvalidOutput() {
87 $timestamp = new MWTimestamp( '1343761268' );
88 $timestamp->getTimestamp( 98 );
92 * Returns a list of valid timestamps in the format:
93 * array( type, timestamp_of_type, timestamp_in_MW )
95 public static function provideValidTimestamps() {
98 array( TS_UNIX
, '1343761268', '20120731190108' ),
99 array( TS_MW
, '20120731190108', '20120731190108' ),
100 array( TS_DB
, '2012-07-31 19:01:08', '20120731190108' ),
101 array( TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ),
102 array( TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ),
103 array( TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ),
104 array( TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
105 array( TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ),
106 array( TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ),
107 // Some extremes and weird values
108 array( TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ),
109 array( TS_UNIX
, '-62135596801', '00001231235959' )
114 * @dataProvider provideHumanTimestampTests
115 * @covers MWTimestamp::getHumanTimestamp
117 public function testHumanTimestamp(
118 $tsTime, // The timestamp to format
119 $currentTime, // The time to consider "now"
120 $timeCorrection, // The time offset to use
121 $dateFormat, // The date preference to use
122 $expectedOutput, // The expected output
125 $user = $this->getMock( 'User' );
126 $user->expects( $this->any() )
127 ->method( 'getOption' )
128 ->with( 'timecorrection' )
129 ->will( $this->returnValue( $timeCorrection ) );
131 $user->expects( $this->any() )
132 ->method( 'getDatePreference' )
133 ->will( $this->returnValue( $dateFormat ) );
135 $tsTime = new MWTimestamp( $tsTime );
136 $currentTime = new MWTimestamp( $currentTime );
140 $tsTime->getHumanTimestamp( $currentTime, $user ),
145 public static function provideHumanTimestampTests() {
152 'Yesterday at 17:00',
153 '"Yesterday" across years',
192 '15:15, January 30, 1991',
200 'Yesterday at 23:00',
201 '"Yesterday" across years with time correction',
209 'Recent weekday with time correction',
217 'Today at another time with time correction',
225 'Another month with dmy'
233 'Another month with ISO-8601'
240 '1991-01-30T15:15:00',
241 'Different year with ISO-8601',
247 * @dataProvider provideRelativeTimestampTests
248 * @covers MWTimestamp::getRelativeTimestamp
250 public function testRelativeTimestamp(
251 $tsTime, // The timestamp to format
252 $currentTime, // The time to consider "now"
253 $timeCorrection, // The time offset to use
254 $dateFormat, // The date preference to use
255 $expectedOutput, // The expected output
258 $user = $this->getMock( 'User' );
259 $user->expects( $this->any() )
260 ->method( 'getOption' )
261 ->with( 'timecorrection' )
262 ->will( $this->returnValue( $timeCorrection ) );
264 $tsTime = new MWTimestamp( $tsTime );
265 $currentTime = new MWTimestamp( $currentTime );
269 $tsTime->getRelativeTimestamp( $currentTime, $user ),
274 public static function provideRelativeTimestampTests() {
282 '"Yesterday" across years',
297 '6 minutes and 30 seconds ago',
298 'Combination of multiple units',
313 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
322 '"Yesterday" across years with time correction',
330 'Recent weekday with time correction',
337 '9 hours and 17 minutes ago',
338 'Today at another time with time correction',