4 * Tests timestamp parsing and output.
6 class TimestampTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( array(
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language
::factory( 'en' ),
14 'wgLang' => Language
::factory( 'en' ),
19 * Test parsing of valid timestamps and outputing to MW format.
20 * @dataProvider provideValidTimestamps
22 function testValidParse( $format, $original, $expected ) {
23 $timestamp = new MWTimestamp( $original );
24 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
28 * Test outputting valid timestamps to different formats.
29 * @dataProvider provideValidTimestamps
31 function testValidOutput( $format, $expected, $original ) {
32 $timestamp = new MWTimestamp( $original );
33 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
37 * Test an invalid timestamp.
38 * @expectedException TimestampException
40 function testInvalidParse() {
41 $timestamp = new MWTimestamp( "This is not a timestamp." );
45 * Test requesting an invalid output format.
46 * @expectedException TimestampException
48 function testInvalidOutput() {
49 $timestamp = new MWTimestamp( '1343761268' );
50 $timestamp->getTimestamp( 98 );
54 * Returns a list of valid timestamps in the format:
55 * array( type, timestamp_of_type, timestamp_in_MW )
57 public static function provideValidTimestamps() {
60 array( TS_UNIX
, '1343761268', '20120731190108' ),
61 array( TS_MW
, '20120731190108', '20120731190108' ),
62 array( TS_DB
, '2012-07-31 19:01:08', '20120731190108' ),
63 array( TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ),
64 array( TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ),
65 array( TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ),
66 array( TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
67 array( TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ),
68 array( TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ),
69 // Some extremes and weird values
70 array( TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ),
71 array( TS_UNIX
, '-62135596801', '00001231235959' )
77 * @dataProvider provideHumanTimestampTests
79 public function testHumanTimestamp(
80 $tsTime, // The timestamp to format
81 $currentTime, // The time to consider "now"
82 $timeCorrection, // The time offset to use
83 $dateFormat, // The date preference to use
84 $expectedOutput, // The expected output
87 $user = $this->getMock( 'User' );
88 $user->expects( $this->any() )
89 ->method( 'getOption' )
90 ->with( 'timecorrection' )
91 ->will( $this->returnValue( $timeCorrection ) );
93 $user->expects( $this->any() )
94 ->method( 'getDatePreference' )
95 ->will( $this->returnValue( $dateFormat ) );
97 $tsTime = new MWTimestamp( $tsTime );
98 $currentTime = new MWTimestamp( $currentTime );
102 $tsTime->getHumanTimestamp( $currentTime, $user ),
107 public static function provideHumanTimestampTests() {
114 'Yesterday at 17:00',
115 '"Yesterday" across years',
154 '15:15, January 30, 1991',
162 'Yesterday at 23:00',
163 '"Yesterday" across years with time correction',
171 'Recent weekday with time correction',
179 'Today at another time with time correction',
187 'Another month with dmy'
195 'Another month with ISO-8601'
202 '1991-01-30T15:15:00',
203 'Different year with ISO-8601',