fccd53b189d742dc42a446a18191bc7f0cbd61bc
4 * Tests timestamp parsing and output.
6 class MWTimestampTest
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
17 * @covers MWTimestamp::getTimestamp
19 public function testValidParse( $format, $original, $expected ) {
20 $timestamp = new MWTimestamp( $original );
21 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW
) );
25 * Test outputting valid timestamps to different formats.
26 * @dataProvider provideValidTimestamps
27 * @covers MWTimestamp::getTimestamp
29 public function testValidOutput( $format, $expected, $original ) {
30 $timestamp = new MWTimestamp( $original );
31 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
35 * Test an invalid timestamp.
36 * @expectedException TimestampException
39 public function testInvalidParse() {
40 new MWTimestamp( "This is not a timestamp." );
44 * Test requesting an invalid output format.
45 * @expectedException TimestampException
46 * @covers MWTimestamp::getTimestamp
48 public 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' )
76 * @dataProvider provideHumanTimestampTests
77 * @covers MWTimestamp::getHumanTimestamp
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',
209 * @dataProvider provideRelativeTimestampTests
210 * @covers MWTimestamp::getRelativeTimestamp
212 public function testRelativeTimestamp(
213 $tsTime, // The timestamp to format
214 $currentTime, // The time to consider "now"
215 $timeCorrection, // The time offset to use
216 $dateFormat, // The date preference to use
217 $expectedOutput, // The expected output
220 $user = $this->getMock( 'User' );
221 $user->expects( $this->any() )
222 ->method( 'getOption' )
223 ->with( 'timecorrection' )
224 ->will( $this->returnValue( $timeCorrection ) );
226 $tsTime = new MWTimestamp( $tsTime );
227 $currentTime = new MWTimestamp( $currentTime );
231 $tsTime->getRelativeTimestamp( $currentTime, $user ),
236 public static function provideRelativeTimestampTests() {
244 '"Yesterday" across years',
259 '6 minutes and 30 seconds ago',
260 'Combination of multiple units',
275 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
284 '"Yesterday" across years with time correction',
292 'Recent weekday with time correction',
299 '9 hours and 17 minutes ago',
300 'Today at another time with time correction',