4 * Tests timestamp parsing and output.
6 class TimestampTest
extends MediaWikiLangTestCase
{
8 protected function setUp() {
11 RequestContext
::getMain()->setLanguage( Language
::factory( 'en' ) );
15 * Test parsing of valid timestamps and outputing to MW format.
16 * @dataProvider provideValidTimestamps
18 function testValidParse( $format, $original, $expected ) {
19 $timestamp = new MWTimestamp( $original );
20 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
24 * Test outputting valid timestamps to different formats.
25 * @dataProvider provideValidTimestamps
27 function testValidOutput( $format, $expected, $original ) {
28 $timestamp = new MWTimestamp( $original );
29 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
33 * Test an invalid timestamp.
34 * @expectedException TimestampException
36 function testInvalidParse() {
37 new MWTimestamp( "This is not a timestamp." );
41 * Test requesting an invalid output format.
42 * @expectedException TimestampException
44 function testInvalidOutput() {
45 $timestamp = new MWTimestamp( '1343761268' );
46 $timestamp->getTimestamp( 98 );
50 * Returns a list of valid timestamps in the format:
51 * array( type, timestamp_of_type, timestamp_in_MW )
53 public static function provideValidTimestamps() {
56 array( TS_UNIX
, '1343761268', '20120731190108' ),
57 array( TS_MW
, '20120731190108', '20120731190108' ),
58 array( TS_DB
, '2012-07-31 19:01:08', '20120731190108' ),
59 array( TS_ISO_8601
, '2012-07-31T19:01:08Z', '20120731190108' ),
60 array( TS_ISO_8601_BASIC
, '20120731T190108Z', '20120731190108' ),
61 array( TS_EXIF
, '2012:07:31 19:01:08', '20120731190108' ),
62 array( TS_RFC2822
, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
63 array( TS_ORACLE
, '31-07-2012 19:01:08.000000', '20120731190108' ),
64 array( TS_POSTGRES
, '2012-07-31 19:01:08 GMT', '20120731190108' ),
65 // Some extremes and weird values
66 array( TS_ISO_8601
, '9999-12-31T23:59:59Z', '99991231235959' ),
67 array( TS_UNIX
, '-62135596801', '00001231235959' )
73 * @dataProvider provideHumanTimestampTests
75 public function testHumanTimestamp(
76 $tsTime, // The timestamp to format
77 $currentTime, // The time to consider "now"
78 $timeCorrection, // The time offset to use
79 $dateFormat, // The date preference to use
80 $expectedOutput, // The expected output
83 $user = $this->getMock( 'User' );
84 $user->expects( $this->any() )
85 ->method( 'getOption' )
86 ->with( 'timecorrection' )
87 ->will( $this->returnValue( $timeCorrection ) );
89 $user->expects( $this->any() )
90 ->method( 'getDatePreference' )
91 ->will( $this->returnValue( $dateFormat ) );
93 $tsTime = new MWTimestamp( $tsTime );
94 $currentTime = new MWTimestamp( $currentTime );
98 $tsTime->getHumanTimestamp( $currentTime, $user ),
103 public static function provideHumanTimestampTests() {
110 'Yesterday at 17:00',
111 '"Yesterday" across years',
150 '15:15, January 30, 1991',
158 'Yesterday at 23:00',
159 '"Yesterday" across years with time correction',
167 'Recent weekday with time correction',
175 'Today at another time with time correction',
183 'Another month with dmy'
191 'Another month with ISO-8601'
198 '1991-01-30T15:15:00',
199 'Different year with ISO-8601',
206 * @dataProvider provideRelativeTimestampTests
208 public function testRelativeTimestamp(
209 $tsTime, // The timestamp to format
210 $currentTime, // The time to consider "now"
211 $timeCorrection, // The time offset to use
212 $dateFormat, // The date preference to use
213 $expectedOutput, // The expected output
216 $user = $this->getMock( 'User' );
217 $user->expects( $this->any() )
218 ->method( 'getOption' )
219 ->with( 'timecorrection' )
220 ->will( $this->returnValue( $timeCorrection ) );
222 $tsTime = new MWTimestamp( $tsTime );
223 $currentTime = new MWTimestamp( $currentTime );
227 $tsTime->getRelativeTimestamp( $currentTime, $user ),
232 public static function provideRelativeTimestampTests() {
240 '"Yesterday" across years',
255 '6 minutes and 30 seconds ago',
256 'Combination of multiple units',
271 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
280 '"Yesterday" across years with time correction',
288 'Recent weekday with time correction',
295 '9 hours and 17 minutes ago',
296 'Today at another time with time correction',