MediaWiki  REL1_21
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         }
00017 
00022         function testValidParse( $format, $original, $expected ) {
00023                 $timestamp = new MWTimestamp( $original );
00024                 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
00025         }
00026 
00031         function testValidOutput( $format, $expected, $original ) {
00032                 $timestamp = new MWTimestamp( $original );
00033                 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
00034         }
00035 
00040         function testInvalidParse() {
00041                 $timestamp = new MWTimestamp( "This is not a timestamp." );
00042         }
00043 
00048         function testInvalidOutput() {
00049                 $timestamp = new MWTimestamp( '1343761268' );
00050                 $timestamp->getTimestamp( 98 );
00051         }
00052 
00056         function testHumanOutput() {
00057                 $timestamp = new MWTimestamp( time() - 3600 );
00058                 $this->assertEquals( "1 hour ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
00059                 $timestamp = new MWTimestamp( time() - 5184000 );
00060                 $this->assertEquals( "2 months ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
00061                 $timestamp = new MWTimestamp( time() - 31536000 );
00062                 $this->assertEquals( "1 year ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
00063         }
00064 
00069         public static function provideValidTimestamps() {
00070                 return array(
00071                         // Various formats
00072                         array( TS_UNIX, '1343761268', '20120731190108' ),
00073                         array( TS_MW, '20120731190108', '20120731190108' ),
00074                         array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
00075                         array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
00076                         array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
00077                         array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
00078                         array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
00079                         array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
00080                         array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
00081                         // Some extremes and weird values
00082                         array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
00083                         array( TS_UNIX, '-62135596801', '00001231235959' )
00084                 );
00085         }
00086 }