MediaWiki  REL1_21
wfTimestampTest.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Tests for wfTimestamp()
00004  */
00005 class WfTimestampTest extends MediaWikiTestCase {
00009         function testNormalTimestamps( $input, $format, $output, $desc ) {
00010                 $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
00011         }
00012 
00013         function provideNormalTimestamps() {
00014                 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
00015                 return array(
00016                         // TS_UNIX
00017                         array( $t, TS_MW, '20010115123456', 'TS_UNIX to TS_MW' ),
00018                         array( -30281104, TS_MW, '19690115123456', 'Negative TS_UNIX to TS_MW' ),
00019                         array( $t, TS_UNIX, 979562096, 'TS_UNIX to TS_UNIX' ),
00020                         array( $t, TS_DB, '2001-01-15 12:34:56', 'TS_UNIX to TS_DB' ),
00021 
00022                         array( $t, TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_ISO_8601_BASIC to TS_DB' ),
00023 
00024                         // TS_MW
00025                         array( '20010115123456', TS_MW, '20010115123456', 'TS_MW to TS_MW' ),
00026                         array( '20010115123456', TS_UNIX, 979562096, 'TS_MW to TS_UNIX' ),
00027                         array( '20010115123456', TS_DB, '2001-01-15 12:34:56', 'TS_MW to TS_DB' ),
00028                         array( '20010115123456', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_MW to TS_ISO_8601_BASIC' ),
00029 
00030                         // TS_DB
00031                         array( '2001-01-15 12:34:56', TS_MW, '20010115123456', 'TS_DB to TS_MW' ),
00032                         array( '2001-01-15 12:34:56', TS_UNIX, 979562096, 'TS_DB to TS_UNIX' ),
00033                         array( '2001-01-15 12:34:56', TS_DB, '2001-01-15 12:34:56', 'TS_DB to TS_DB' ),
00034                         array( '2001-01-15 12:34:56', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_DB to TS_ISO_8601_BASIC' ),
00035 
00036                         # rfc2822 section 3.3
00037                         array( '20010115123456', TS_RFC2822, 'Mon, 15 Jan 2001 12:34:56 GMT', 'TS_MW to TS_RFC2822' ),
00038                         array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
00039                         array( ' Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 with leading space to TS_MW' ),
00040                         array( '15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 without optional day-of-week to TS_MW' ),
00041 
00042                         # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
00043                         # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
00044                         array( 'Mon, 15         Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
00045 
00046                         # WSP = SP / HTAB ; rfc2234
00047                         array( "Mon, 15 Jan\x092001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB to TS_MW' ),
00048                         array( "Mon, 15 Jan\x09 \x09  2001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB and SP to TS_MW' ),
00049                         array( 'Sun, 6 Nov 94 08:49:37 GMT', TS_MW, '19941106084937', 'TS_RFC2822 with obsolete year to TS_MW' ),
00050                 );
00051         }
00052 
00059         function testOldTimestamps( $input, $format, $output, $desc ) {
00060                 $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
00061         }
00062 
00063         function provideOldTimestamps() {
00064                 return array(
00065                         array( '19011213204554', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:54 GMT', 'Earliest time according to php documentation' ),
00066                         array( '20380119031407', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:07 GMT', 'Latest 32 bit time' ),
00067                         array( '19011213204552', TS_UNIX, '-2147483648', 'Earliest 32 bit unix time' ),
00068                         array( '20380119031407', TS_UNIX, '2147483647', 'Latest 32 bit unix time' ),
00069                         array( '19011213204552', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:52 GMT', 'Earliest 32 bit time' ),
00070                         array( '19011213204551', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:51 GMT', 'Earliest 32 bit time - 1' ),
00071                         array( '20380119031408', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:08 GMT', 'Latest 32 bit time + 1' ),
00072                         array( '19011212000000', TS_MW, '19011212000000', 'Convert to itself r74778#c10645' ),
00073                         array( '19011213204551', TS_UNIX, '-2147483649', 'Earliest 32 bit unix time - 1' ),
00074                         array( '20380119031408', TS_UNIX, '2147483648', 'Latest 32 bit unix time + 1' ),
00075                         array( '-2147483649', TS_MW, '19011213204551', '1901 negative unix time to MediaWiki' ),
00076                         array( '-5331871504', TS_MW, '18010115123456', '1801 negative unix time to MediaWiki' ),
00077                         array( '0117-08-09 12:34:56', TS_RFC2822, 'Tue, 09 Aug 0117 12:34:56 GMT', 'Death of Roman Emperor [[Trajan]]' ),
00078 
00079                         /* @todo FIXME: 00 to 101 years are taken as being in [1970-2069] */
00080                         array( '-58979923200', TS_RFC2822, 'Sun, 01 Jan 0101 00:00:00 GMT', '1/1/101' ),
00081                         array( '-62135596800', TS_RFC2822, 'Mon, 01 Jan 0001 00:00:00 GMT', 'Year 1' ),
00082 
00083                         /* It is not clear if we should generate a year 0 or not
00084                          * We are completely off RFC2822 requirement of year being
00085                          * 1900 or later.
00086                          */
00087                         array( '-62142076800', TS_RFC2822, 'Wed, 18 Oct 0000 00:00:00 GMT', 'ISO 8601:2004 [[year 0]], also called [[1 BC]]' ),
00088                 );
00089         }
00090 
00098         function testHttpDate( $input, $output, $desc ) {
00099                 $this->assertEquals( $output, wfTimestamp( TS_MW, $input ), $desc );
00100         }
00101 
00102         function provideHttpDates() {
00103                 return array(
00104                         array( 'Sun, 06 Nov 1994 08:49:37 GMT', '19941106084937', 'RFC 822 date' ),
00105                         array( 'Sunday, 06-Nov-94 08:49:37 GMT', '19941106084937', 'RFC 850 date' ),
00106                         array( 'Sun Nov  6 08:49:37 1994', '19941106084937', "ANSI C's asctime() format" ),
00107                         // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
00108                         array( 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626', '20101122141242', 'Netscape extension to HTTP/1.0' ),
00109                 );
00110         }
00111 
00116         function testTimestampParameter() {
00117                 $now = wfTimestamp( TS_UNIX );
00118                 // We check that wfTimestamp doesn't return false (error) and use a LessThan assert 
00119                 // for the cases where the test is run in a second boundary.
00120 
00121                 $zero = wfTimestamp( TS_UNIX, 0 );
00122                 $this->assertNotEquals( false, $zero );
00123                 $this->assertLessThan( 5, $zero - $now );
00124 
00125                 $empty = wfTimestamp( TS_UNIX, '' );
00126                 $this->assertNotEquals( false, $empty );
00127                 $this->assertLessThan( 5, $empty - $now );
00128 
00129                 $null = wfTimestamp( TS_UNIX, null );
00130                 $this->assertNotEquals( false, $null );
00131                 $this->assertLessThan( 5, $null - $now );
00132         }
00133 }