MediaWiki  master
TimestampTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class TimestampTest extends MediaWikiTestCase {
00007 
00008         protected function setUp() {
00009                 parent::setUp();
00010 
00011                 $this->setMwGlobals( array(
00012                         'wgLanguageCode' => 'en',
00013                         'wgContLang' => Language::factory( 'en' ),
00014                         'wgLang' => Language::factory( 'en' ),
00015                 ) );
00016         }
00021         function testValidParse( $format, $original, $expected ) {
00022                 $timestamp = new MWTimestamp( $original );
00023                 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
00024         }
00025 
00030         function testValidOutput( $format, $expected, $original ) {
00031                 $timestamp = new MWTimestamp( $original );
00032                 $this->assertEquals( $expected, (string) $timestamp->getTimestamp( $format ) );
00033         }
00034 
00039         function testInvalidParse() {
00040                 $timestamp = new MWTimestamp( "This is not a timestamp." );
00041         }
00042 
00047         function testInvalidOutput() {
00048                 $timestamp = new MWTimestamp( '1343761268' );
00049                 $timestamp->getTimestamp( 98 );
00050         }
00051 
00055         function testHumanOutput() {
00056                 $timestamp = new MWTimestamp( time() - 3600 );
00057                 $this->assertEquals( "1 hour ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
00058         }
00059 
00064         public static function provideValidTimestamps() {
00065                 return array(
00066                         // Various formats
00067                         array( TS_UNIX, '1343761268', '20120731190108' ),
00068                         array( TS_MW, '20120731190108', '20120731190108' ),
00069                         array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
00070                         array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
00071                         array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
00072                         array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
00073                         array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
00074                         array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
00075                         array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
00076                         array( TS_DB2, '2012-07-31 19:01:08', '20120731190108' ),
00077                         // Some extremes and weird values
00078                         array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
00079                         array( TS_UNIX, '-62135596801', '00001231235959' )
00080                 );
00081         }
00082 }