MediaWiki
REL1_24
|
00001 <?php 00002 00006 class MWTimestampTest extends MediaWikiLangTestCase { 00007 00008 protected function setUp() { 00009 parent::setUp(); 00010 00011 RequestContext::getMain()->setLanguage( Language::factory( 'en' ) ); 00012 } 00013 00017 public function testConstructWithNoTimestamp() { 00018 $timestamp = new MWTimestamp(); 00019 $this->assertInternalType( 'string', $timestamp->getTimestamp() ); 00020 $this->assertNotEmpty( $timestamp->getTimestamp() ); 00021 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) ); 00022 } 00023 00027 public function testToString() { 00028 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108 00029 $this->assertEquals( '1406833268', $timestamp->__toString() ); 00030 } 00031 00032 public static function provideValidTimestampDifferences() { 00033 return array( 00034 array( '1406833268', '1406833269', '00 00 00 01' ), 00035 array( '1406833268', '1406833329', '00 00 01 01' ), 00036 array( '1406833268', '1406836929', '00 01 01 01' ), 00037 array( '1406833268', '1406923329', '01 01 01 01' ), 00038 ); 00039 } 00040 00045 public function testDiff( $timestamp1, $timestamp2, $expected ) { 00046 $timestamp1 = new MWTimestamp( $timestamp1 ); 00047 $timestamp2 = new MWTimestamp( $timestamp2 ); 00048 $diff = $timestamp1->diff( $timestamp2 ); 00049 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) ); 00050 } 00051 00057 public function testValidParse( $format, $original, $expected ) { 00058 $timestamp = new MWTimestamp( $original ); 00059 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) ); 00060 } 00061 00067 public function testValidOutput( $format, $expected, $original ) { 00068 $timestamp = new MWTimestamp( $original ); 00069 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) ); 00070 } 00071 00077 public function testInvalidParse() { 00078 new MWTimestamp( "This is not a timestamp." ); 00079 } 00080 00086 public function testInvalidOutput() { 00087 $timestamp = new MWTimestamp( '1343761268' ); 00088 $timestamp->getTimestamp( 98 ); 00089 } 00090 00095 public static function provideValidTimestamps() { 00096 return array( 00097 // Various formats 00098 array( TS_UNIX, '1343761268', '20120731190108' ), 00099 array( TS_MW, '20120731190108', '20120731190108' ), 00100 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ), 00101 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ), 00102 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ), 00103 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ), 00104 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ), 00105 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ), 00106 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ), 00107 // Some extremes and weird values 00108 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ), 00109 array( TS_UNIX, '-62135596801', '00001231235959' ) 00110 ); 00111 } 00112 00117 public function testHumanTimestamp( 00118 $tsTime, // The timestamp to format 00119 $currentTime, // The time to consider "now" 00120 $timeCorrection, // The time offset to use 00121 $dateFormat, // The date preference to use 00122 $expectedOutput, // The expected output 00123 $desc // Description 00124 ) { 00125 $user = $this->getMock( 'User' ); 00126 $user->expects( $this->any() ) 00127 ->method( 'getOption' ) 00128 ->with( 'timecorrection' ) 00129 ->will( $this->returnValue( $timeCorrection ) ); 00130 00131 $user->expects( $this->any() ) 00132 ->method( 'getDatePreference' ) 00133 ->will( $this->returnValue( $dateFormat ) ); 00134 00135 $tsTime = new MWTimestamp( $tsTime ); 00136 $currentTime = new MWTimestamp( $currentTime ); 00137 00138 $this->assertEquals( 00139 $expectedOutput, 00140 $tsTime->getHumanTimestamp( $currentTime, $user ), 00141 $desc 00142 ); 00143 } 00144 00145 public static function provideHumanTimestampTests() { 00146 return array( 00147 array( 00148 '20111231170000', 00149 '20120101000000', 00150 'Offset|0', 00151 'mdy', 00152 'Yesterday at 17:00', 00153 '"Yesterday" across years', 00154 ), 00155 array( 00156 '20120717190900', 00157 '20120717190929', 00158 'Offset|0', 00159 'mdy', 00160 'just now', 00161 '"Just now"', 00162 ), 00163 array( 00164 '20120717190900', 00165 '20120717191530', 00166 'Offset|0', 00167 'mdy', 00168 '6 minutes ago', 00169 'X minutes ago', 00170 ), 00171 array( 00172 '20121006173100', 00173 '20121006173200', 00174 'Offset|0', 00175 'mdy', 00176 '1 minute ago', 00177 '"1 minute ago"', 00178 ), 00179 array( 00180 '20120617190900', 00181 '20120717190900', 00182 'Offset|0', 00183 'mdy', 00184 'June 17', 00185 'Another month' 00186 ), 00187 array( 00188 '19910130151500', 00189 '20120716193700', 00190 'Offset|0', 00191 'mdy', 00192 '15:15, January 30, 1991', 00193 'Different year', 00194 ), 00195 array( 00196 '20120101050000', 00197 '20120101080000', 00198 'Offset|-360', 00199 'mdy', 00200 'Yesterday at 23:00', 00201 '"Yesterday" across years with time correction', 00202 ), 00203 array( 00204 '20120714184300', 00205 '20120716184300', 00206 'Offset|-420', 00207 'mdy', 00208 'Saturday at 11:43', 00209 'Recent weekday with time correction', 00210 ), 00211 array( 00212 '20120714184300', 00213 '20120715040000', 00214 'Offset|-420', 00215 'mdy', 00216 '11:43', 00217 'Today at another time with time correction', 00218 ), 00219 array( 00220 '20120617190900', 00221 '20120717190900', 00222 'Offset|0', 00223 'dmy', 00224 '17 June', 00225 'Another month with dmy' 00226 ), 00227 array( 00228 '20120617190900', 00229 '20120717190900', 00230 'Offset|0', 00231 'ISO 8601', 00232 '06-17', 00233 'Another month with ISO-8601' 00234 ), 00235 array( 00236 '19910130151500', 00237 '20120716193700', 00238 'Offset|0', 00239 'ISO 8601', 00240 '1991-01-30T15:15:00', 00241 'Different year with ISO-8601', 00242 ), 00243 ); 00244 } 00245 00250 public function testRelativeTimestamp( 00251 $tsTime, // The timestamp to format 00252 $currentTime, // The time to consider "now" 00253 $timeCorrection, // The time offset to use 00254 $dateFormat, // The date preference to use 00255 $expectedOutput, // The expected output 00256 $desc // Description 00257 ) { 00258 $user = $this->getMock( 'User' ); 00259 $user->expects( $this->any() ) 00260 ->method( 'getOption' ) 00261 ->with( 'timecorrection' ) 00262 ->will( $this->returnValue( $timeCorrection ) ); 00263 00264 $tsTime = new MWTimestamp( $tsTime ); 00265 $currentTime = new MWTimestamp( $currentTime ); 00266 00267 $this->assertEquals( 00268 $expectedOutput, 00269 $tsTime->getRelativeTimestamp( $currentTime, $user ), 00270 $desc 00271 ); 00272 } 00273 00274 public static function provideRelativeTimestampTests() { 00275 return array( 00276 array( 00277 '20111231170000', 00278 '20120101000000', 00279 'Offset|0', 00280 'mdy', 00281 '7 hours ago', 00282 '"Yesterday" across years', 00283 ), 00284 array( 00285 '20120717190900', 00286 '20120717190929', 00287 'Offset|0', 00288 'mdy', 00289 '29 seconds ago', 00290 '"Just now"', 00291 ), 00292 array( 00293 '20120717190900', 00294 '20120717191530', 00295 'Offset|0', 00296 'mdy', 00297 '6 minutes and 30 seconds ago', 00298 'Combination of multiple units', 00299 ), 00300 array( 00301 '20121006173100', 00302 '20121006173200', 00303 'Offset|0', 00304 'mdy', 00305 '1 minute ago', 00306 '"1 minute ago"', 00307 ), 00308 array( 00309 '19910130151500', 00310 '20120716193700', 00311 'Offset|0', 00312 'mdy', 00313 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago', 00314 'A long time ago', 00315 ), 00316 array( 00317 '20120101050000', 00318 '20120101080000', 00319 'Offset|-360', 00320 'mdy', 00321 '3 hours ago', 00322 '"Yesterday" across years with time correction', 00323 ), 00324 array( 00325 '20120714184300', 00326 '20120716184300', 00327 'Offset|-420', 00328 'mdy', 00329 '2 days ago', 00330 'Recent weekday with time correction', 00331 ), 00332 array( 00333 '20120714184300', 00334 '20120715040000', 00335 'Offset|-420', 00336 'mdy', 00337 '9 hours and 17 minutes ago', 00338 'Today at another time with time correction', 00339 ), 00340 ); 00341 } 00342 }