MediaWiki  REL1_24
wfTimestampTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class WfTimestampTest extends MediaWikiTestCase {
00011     public function testNormalTimestamps( $input, $format, $output, $desc ) {
00012         $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
00013     }
00014 
00015     public static function provideNormalTimestamps() {
00016         $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
00017 
00018         return array(
00019             // TS_UNIX
00020             array( $t, TS_MW, '20010115123456', 'TS_UNIX to TS_MW' ),
00021             array( -30281104, TS_MW, '19690115123456', 'Negative TS_UNIX to TS_MW' ),
00022             array( $t, TS_UNIX, 979562096, 'TS_UNIX to TS_UNIX' ),
00023             array( $t, TS_DB, '2001-01-15 12:34:56', 'TS_UNIX to TS_DB' ),
00024 
00025             array( $t, TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_ISO_8601_BASIC to TS_DB' ),
00026 
00027             // TS_MW
00028             array( '20010115123456', TS_MW, '20010115123456', 'TS_MW to TS_MW' ),
00029             array( '20010115123456', TS_UNIX, 979562096, 'TS_MW to TS_UNIX' ),
00030             array( '20010115123456', TS_DB, '2001-01-15 12:34:56', 'TS_MW to TS_DB' ),
00031             array( '20010115123456', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_MW to TS_ISO_8601_BASIC' ),
00032 
00033             // TS_DB
00034             array( '2001-01-15 12:34:56', TS_MW, '20010115123456', 'TS_DB to TS_MW' ),
00035             array( '2001-01-15 12:34:56', TS_UNIX, 979562096, 'TS_DB to TS_UNIX' ),
00036             array( '2001-01-15 12:34:56', TS_DB, '2001-01-15 12:34:56', 'TS_DB to TS_DB' ),
00037             array(
00038                 '2001-01-15 12:34:56',
00039                 TS_ISO_8601_BASIC,
00040                 '20010115T123456Z',
00041                 'TS_DB to TS_ISO_8601_BASIC'
00042             ),
00043 
00044             # rfc2822 section 3.3
00045             array( '20010115123456', TS_RFC2822, 'Mon, 15 Jan 2001 12:34:56 GMT', 'TS_MW to TS_RFC2822' ),
00046             array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
00047             array(
00048                 ' Mon, 15 Jan 2001 12:34:56 GMT',
00049                 TS_MW,
00050                 '20010115123456',
00051                 'TS_RFC2822 with leading space to TS_MW'
00052             ),
00053             array(
00054                 '15 Jan 2001 12:34:56 GMT',
00055                 TS_MW,
00056                 '20010115123456',
00057                 'TS_RFC2822 without optional day-of-week to TS_MW'
00058             ),
00059 
00060             # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
00061             # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
00062             array( 'Mon, 15         Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
00063 
00064             # WSP = SP / HTAB ; rfc2234
00065             array(
00066                 "Mon, 15 Jan\x092001 12:34:56 GMT",
00067                 TS_MW,
00068                 '20010115123456',
00069                 'TS_RFC2822 with HTAB to TS_MW'
00070             ),
00071             array(
00072                 "Mon, 15 Jan\x09 \x09  2001 12:34:56 GMT",
00073                 TS_MW,
00074                 '20010115123456',
00075                 'TS_RFC2822 with HTAB and SP to TS_MW'
00076             ),
00077             array(
00078                 'Sun, 6 Nov 94 08:49:37 GMT',
00079                 TS_MW,
00080                 '19941106084937',
00081                 'TS_RFC2822 with obsolete year to TS_MW'
00082             ),
00083         );
00084     }
00085 
00092     public function testOldTimestamps( $input, $outputType, $output, $message ) {
00093         $timestamp = wfTimestamp( $outputType, $input );
00094         if ( substr( $output, 0, 1 ) === '/' ) {
00095             // Bug 64946: Day of the week calculations for very old
00096             // timestamps varies from system to system.
00097             $this->assertRegExp( $output, $timestamp, $message );
00098         } else {
00099             $this->assertEquals( $output, $timestamp, $message );
00100         }
00101     }
00102 
00103     public static function provideOldTimestamps() {
00104         return array(
00105             array(
00106                 '19011213204554',
00107                 TS_RFC2822,
00108                 'Fri, 13 Dec 1901 20:45:54 GMT',
00109                 'Earliest time according to PHP documentation'
00110             ),
00111             array( '20380119031407', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:07 GMT', 'Latest 32 bit time' ),
00112             array( '19011213204552', TS_UNIX, '-2147483648', 'Earliest 32 bit unix time' ),
00113             array( '20380119031407', TS_UNIX, '2147483647', 'Latest 32 bit unix time' ),
00114             array( '19011213204552', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:52 GMT', 'Earliest 32 bit time' ),
00115             array(
00116                 '19011213204551',
00117                 TS_RFC2822,
00118                 'Fri, 13 Dec 1901 20:45:51 GMT', 'Earliest 32 bit time - 1'
00119             ),
00120             array( '20380119031408', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:08 GMT', 'Latest 32 bit time + 1' ),
00121             array( '19011212000000', TS_MW, '19011212000000', 'Convert to itself r74778#c10645' ),
00122             array( '19011213204551', TS_UNIX, '-2147483649', 'Earliest 32 bit unix time - 1' ),
00123             array( '20380119031408', TS_UNIX, '2147483648', 'Latest 32 bit unix time + 1' ),
00124             array( '-2147483649', TS_MW, '19011213204551', '1901 negative unix time to MediaWiki' ),
00125             array( '-5331871504', TS_MW, '18010115123456', '1801 negative unix time to MediaWiki' ),
00126             array(
00127                 '0117-08-09 12:34:56',
00128                 TS_RFC2822,
00129                 '/, 09 Aug 0117 12:34:56 GMT$/',
00130                 'Death of Roman Emperor [[Trajan]]'
00131             ),
00132 
00133             /* @todo FIXME: 00 to 101 years are taken as being in [1970-2069] */
00134             array( '-58979923200', TS_RFC2822, '/, 01 Jan 0101 00:00:00 GMT$/', '1/1/101' ),
00135             array( '-62135596800', TS_RFC2822, 'Mon, 01 Jan 0001 00:00:00 GMT', 'Year 1' ),
00136 
00137             /* It is not clear if we should generate a year 0 or not
00138              * We are completely off RFC2822 requirement of year being
00139              * 1900 or later.
00140              */
00141             array(
00142                 '-62142076800',
00143                 TS_RFC2822,
00144                 'Wed, 18 Oct 0000 00:00:00 GMT',
00145                 'ISO 8601:2004 [[year 0]], also called [[1 BC]]'
00146             ),
00147         );
00148     }
00149 
00157     public function testHttpDate( $input, $output, $desc ) {
00158         $this->assertEquals( $output, wfTimestamp( TS_MW, $input ), $desc );
00159     }
00160 
00161     public static function provideHttpDates() {
00162         return array(
00163             array( 'Sun, 06 Nov 1994 08:49:37 GMT', '19941106084937', 'RFC 822 date' ),
00164             array( 'Sunday, 06-Nov-94 08:49:37 GMT', '19941106084937', 'RFC 850 date' ),
00165             array( 'Sun Nov  6 08:49:37 1994', '19941106084937', "ANSI C's asctime() format" ),
00166             // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
00167             array(
00168                 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626',
00169                 '20101122141242',
00170                 'Netscape extension to HTTP/1.0'
00171             ),
00172         );
00173     }
00174 
00179     public function testTimestampParameter() {
00180         $now = wfTimestamp( TS_UNIX );
00181         // We check that wfTimestamp doesn't return false (error) and use a LessThan assert
00182         // for the cases where the test is run in a second boundary.
00183 
00184         $zero = wfTimestamp( TS_UNIX, 0 );
00185         $this->assertNotEquals( false, $zero );
00186         $this->assertLessThan( 5, $zero - $now );
00187 
00188         $empty = wfTimestamp( TS_UNIX, '' );
00189         $this->assertNotEquals( false, $empty );
00190         $this->assertLessThan( 5, $empty - $now );
00191 
00192         $null = wfTimestamp( TS_UNIX, null );
00193         $this->assertNotEquals( false, $null );
00194         $this->assertLessThan( 5, $null - $now );
00195     }
00196 }