test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / TimestampTest.php
blob07dcb7d81b7563156655d5c74ee05c72f6c6c467
1 <?php
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class TimestampTest extends MediaWikiLangTestCase {
8 protected function setUp() {
9 parent::setUp();
11 RequestContext::getMain()->setLanguage( Language::factory( 'en' ) );
14 /**
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 ) );
23 /**
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 ) );
32 /**
33 * Test an invalid timestamp.
34 * @expectedException TimestampException
36 function testInvalidParse() {
37 new MWTimestamp( "This is not a timestamp." );
40 /**
41 * Test requesting an invalid output format.
42 * @expectedException TimestampException
44 function testInvalidOutput() {
45 $timestamp = new MWTimestamp( '1343761268' );
46 $timestamp->getTimestamp( 98 );
49 /**
50 * Returns a list of valid timestamps in the format:
51 * array( type, timestamp_of_type, timestamp_in_MW )
53 public static function provideValidTimestamps() {
54 return array(
55 // Various formats
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' )
71 /**
72 * @test
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
81 $desc // Description
82 ) {
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 );
96 $this->assertEquals(
97 $expectedOutput,
98 $tsTime->getHumanTimestamp( $currentTime, $user ),
99 $desc
103 public static function provideHumanTimestampTests() {
104 return array(
105 array(
106 '20111231170000',
107 '20120101000000',
108 'Offset|0',
109 'mdy',
110 'Yesterday at 17:00',
111 '"Yesterday" across years',
113 array(
114 '20120717190900',
115 '20120717190929',
116 'Offset|0',
117 'mdy',
118 'just now',
119 '"Just now"',
121 array(
122 '20120717190900',
123 '20120717191530',
124 'Offset|0',
125 'mdy',
126 '6 minutes ago',
127 'X minutes ago',
129 array(
130 '20121006173100',
131 '20121006173200',
132 'Offset|0',
133 'mdy',
134 '1 minute ago',
135 '"1 minute ago"',
137 array(
138 '20120617190900',
139 '20120717190900',
140 'Offset|0',
141 'mdy',
142 'June 17',
143 'Another month'
145 array(
146 '19910130151500',
147 '20120716193700',
148 'Offset|0',
149 'mdy',
150 '15:15, January 30, 1991',
151 'Different year',
153 array(
154 '20120101050000',
155 '20120101080000',
156 'Offset|-360',
157 'mdy',
158 'Yesterday at 23:00',
159 '"Yesterday" across years with time correction',
161 array(
162 '20120714184300',
163 '20120716184300',
164 'Offset|-420',
165 'mdy',
166 'Saturday at 11:43',
167 'Recent weekday with time correction',
169 array(
170 '20120714184300',
171 '20120715040000',
172 'Offset|-420',
173 'mdy',
174 '11:43',
175 'Today at another time with time correction',
177 array(
178 '20120617190900',
179 '20120717190900',
180 'Offset|0',
181 'dmy',
182 '17 June',
183 'Another month with dmy'
185 array(
186 '20120617190900',
187 '20120717190900',
188 'Offset|0',
189 'ISO 8601',
190 '06-17',
191 'Another month with ISO-8601'
193 array(
194 '19910130151500',
195 '20120716193700',
196 'Offset|0',
197 'ISO 8601',
198 '1991-01-30T15:15:00',
199 'Different year with ISO-8601',