MediaWiki
REL1_22
|
00001 <?php 00002 00006 class TimestampTest extends MediaWikiLangTestCase { 00007 00008 protected function setUp() { 00009 parent::setUp(); 00010 00011 RequestContext::getMain()->setLanguage( Language::factory( 'en' ) ); 00012 } 00013 00019 public function testValidParse( $format, $original, $expected ) { 00020 $timestamp = new MWTimestamp( $original ); 00021 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) ); 00022 } 00023 00029 public function testValidOutput( $format, $expected, $original ) { 00030 $timestamp = new MWTimestamp( $original ); 00031 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) ); 00032 } 00033 00039 public function testInvalidParse() { 00040 new MWTimestamp( "This is not a timestamp." ); 00041 } 00042 00048 public function testInvalidOutput() { 00049 $timestamp = new MWTimestamp( '1343761268' ); 00050 $timestamp->getTimestamp( 98 ); 00051 } 00052 00057 public static function provideValidTimestamps() { 00058 return array( 00059 // Various formats 00060 array( TS_UNIX, '1343761268', '20120731190108' ), 00061 array( TS_MW, '20120731190108', '20120731190108' ), 00062 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ), 00063 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ), 00064 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ), 00065 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ), 00066 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ), 00067 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ), 00068 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ), 00069 // Some extremes and weird values 00070 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ), 00071 array( TS_UNIX, '-62135596801', '00001231235959' ) 00072 ); 00073 } 00074 00079 public function testHumanTimestamp( 00080 $tsTime, // The timestamp to format 00081 $currentTime, // The time to consider "now" 00082 $timeCorrection, // The time offset to use 00083 $dateFormat, // The date preference to use 00084 $expectedOutput, // The expected output 00085 $desc // Description 00086 ) { 00087 $user = $this->getMock( 'User' ); 00088 $user->expects( $this->any() ) 00089 ->method( 'getOption' ) 00090 ->with( 'timecorrection' ) 00091 ->will( $this->returnValue( $timeCorrection ) ); 00092 00093 $user->expects( $this->any() ) 00094 ->method( 'getDatePreference' ) 00095 ->will( $this->returnValue( $dateFormat ) ); 00096 00097 $tsTime = new MWTimestamp( $tsTime ); 00098 $currentTime = new MWTimestamp( $currentTime ); 00099 00100 $this->assertEquals( 00101 $expectedOutput, 00102 $tsTime->getHumanTimestamp( $currentTime, $user ), 00103 $desc 00104 ); 00105 } 00106 00107 public static function provideHumanTimestampTests() { 00108 return array( 00109 array( 00110 '20111231170000', 00111 '20120101000000', 00112 'Offset|0', 00113 'mdy', 00114 'Yesterday at 17:00', 00115 '"Yesterday" across years', 00116 ), 00117 array( 00118 '20120717190900', 00119 '20120717190929', 00120 'Offset|0', 00121 'mdy', 00122 'just now', 00123 '"Just now"', 00124 ), 00125 array( 00126 '20120717190900', 00127 '20120717191530', 00128 'Offset|0', 00129 'mdy', 00130 '6 minutes ago', 00131 'X minutes ago', 00132 ), 00133 array( 00134 '20121006173100', 00135 '20121006173200', 00136 'Offset|0', 00137 'mdy', 00138 '1 minute ago', 00139 '"1 minute ago"', 00140 ), 00141 array( 00142 '20120617190900', 00143 '20120717190900', 00144 'Offset|0', 00145 'mdy', 00146 'June 17', 00147 'Another month' 00148 ), 00149 array( 00150 '19910130151500', 00151 '20120716193700', 00152 'Offset|0', 00153 'mdy', 00154 '15:15, January 30, 1991', 00155 'Different year', 00156 ), 00157 array( 00158 '20120101050000', 00159 '20120101080000', 00160 'Offset|-360', 00161 'mdy', 00162 'Yesterday at 23:00', 00163 '"Yesterday" across years with time correction', 00164 ), 00165 array( 00166 '20120714184300', 00167 '20120716184300', 00168 'Offset|-420', 00169 'mdy', 00170 'Saturday at 11:43', 00171 'Recent weekday with time correction', 00172 ), 00173 array( 00174 '20120714184300', 00175 '20120715040000', 00176 'Offset|-420', 00177 'mdy', 00178 '11:43', 00179 'Today at another time with time correction', 00180 ), 00181 array( 00182 '20120617190900', 00183 '20120717190900', 00184 'Offset|0', 00185 'dmy', 00186 '17 June', 00187 'Another month with dmy' 00188 ), 00189 array( 00190 '20120617190900', 00191 '20120717190900', 00192 'Offset|0', 00193 'ISO 8601', 00194 '06-17', 00195 'Another month with ISO-8601' 00196 ), 00197 array( 00198 '19910130151500', 00199 '20120716193700', 00200 'Offset|0', 00201 'ISO 8601', 00202 '1991-01-30T15:15:00', 00203 'Different year with ISO-8601', 00204 ), 00205 ); 00206 } 00207 00212 public function testRelativeTimestamp( 00213 $tsTime, // The timestamp to format 00214 $currentTime, // The time to consider "now" 00215 $timeCorrection, // The time offset to use 00216 $dateFormat, // The date preference to use 00217 $expectedOutput, // The expected output 00218 $desc // Description 00219 ) { 00220 $user = $this->getMock( 'User' ); 00221 $user->expects( $this->any() ) 00222 ->method( 'getOption' ) 00223 ->with( 'timecorrection' ) 00224 ->will( $this->returnValue( $timeCorrection ) ); 00225 00226 $tsTime = new MWTimestamp( $tsTime ); 00227 $currentTime = new MWTimestamp( $currentTime ); 00228 00229 $this->assertEquals( 00230 $expectedOutput, 00231 $tsTime->getRelativeTimestamp( $currentTime, $user ), 00232 $desc 00233 ); 00234 } 00235 00236 public static function provideRelativeTimestampTests() { 00237 return array( 00238 array( 00239 '20111231170000', 00240 '20120101000000', 00241 'Offset|0', 00242 'mdy', 00243 '7 hours ago', 00244 '"Yesterday" across years', 00245 ), 00246 array( 00247 '20120717190900', 00248 '20120717190929', 00249 'Offset|0', 00250 'mdy', 00251 '29 seconds ago', 00252 '"Just now"', 00253 ), 00254 array( 00255 '20120717190900', 00256 '20120717191530', 00257 'Offset|0', 00258 'mdy', 00259 '6 minutes and 30 seconds ago', 00260 'Combination of multiple units', 00261 ), 00262 array( 00263 '20121006173100', 00264 '20121006173200', 00265 'Offset|0', 00266 'mdy', 00267 '1 minute ago', 00268 '"1 minute ago"', 00269 ), 00270 array( 00271 '19910130151500', 00272 '20120716193700', 00273 'Offset|0', 00274 'mdy', 00275 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago', 00276 'A long time ago', 00277 ), 00278 array( 00279 '20120101050000', 00280 '20120101080000', 00281 'Offset|-360', 00282 'mdy', 00283 '3 hours ago', 00284 '"Yesterday" across years with time correction', 00285 ), 00286 array( 00287 '20120714184300', 00288 '20120716184300', 00289 'Offset|-420', 00290 'mdy', 00291 '2 days ago', 00292 'Recent weekday with time correction', 00293 ), 00294 array( 00295 '20120714184300', 00296 '20120715040000', 00297 'Offset|-420', 00298 'mdy', 00299 '9 hours and 17 minutes ago', 00300 'Today at another time with time correction', 00301 ), 00302 ); 00303 } 00304 }