Merge "Commafy support for convertNumber"
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfTimestampTest.php
1 <?php
2 /*
3 * Tests for wfTimestamp()
4 */
5 class WfTimestampTest extends MediaWikiTestCase {
6 /**
7 * @dataProvider provideNormalTimestamps
8 */
9 function testNormalTimestamps( $input, $format, $output, $desc ) {
10 $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
11 }
12
13 function provideNormalTimestamps() {
14 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
15 return array(
16 // TS_UNIX
17 array( $t, TS_MW, '20010115123456', 'TS_UNIX to TS_MW' ),
18 array( -30281104, TS_MW, '19690115123456', 'Negative TS_UNIX to TS_MW' ),
19 array( $t, TS_UNIX, 979562096, 'TS_UNIX to TS_UNIX' ),
20 array( $t, TS_DB, '2001-01-15 12:34:56', 'TS_UNIX to TS_DB' ),
21
22 array( $t, TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_ISO_8601_BASIC to TS_DB' ),
23
24 // TS_MW
25 array( '20010115123456', TS_MW, '20010115123456', 'TS_MW to TS_MW' ),
26 array( '20010115123456', TS_UNIX, 979562096, 'TS_MW to TS_UNIX' ),
27 array( '20010115123456', TS_DB, '2001-01-15 12:34:56', 'TS_MW to TS_DB' ),
28 array( '20010115123456', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_MW to TS_ISO_8601_BASIC' ),
29
30 // TS_DB
31 array( '2001-01-15 12:34:56', TS_MW, '20010115123456', 'TS_DB to TS_MW' ),
32 array( '2001-01-15 12:34:56', TS_UNIX, 979562096, 'TS_DB to TS_UNIX' ),
33 array( '2001-01-15 12:34:56', TS_DB, '2001-01-15 12:34:56', 'TS_DB to TS_DB' ),
34 array( '2001-01-15 12:34:56', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_DB to TS_ISO_8601_BASIC' ),
35
36 # rfc2822 section 3.3
37 array( '20010115123456', TS_RFC2822, 'Mon, 15 Jan 2001 12:34:56 GMT', 'TS_MW to TS_RFC2822' ),
38 array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
39 array( ' Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 with leading space to TS_MW' ),
40 array( '15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 without optional day-of-week to TS_MW' ),
41
42 # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
43 # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
44 array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
45
46 # WSP = SP / HTAB ; rfc2234
47 array( "Mon, 15 Jan\x092001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB to TS_MW' ),
48 array( "Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB and SP to TS_MW' ),
49 array( 'Sun, 6 Nov 94 08:49:37 GMT', TS_MW, '19941106084937', 'TS_RFC2822 with obsolete year to TS_MW' ),
50 );
51 }
52
53 /**
54 * This test checks wfTimestamp() with values outside.
55 * It needs PHP 64 bits or PHP > 5.1.
56 * See r74778 and bug 25451
57 * @dataProvider provideOldTimestamps
58 */
59 function testOldTimestamps( $input, $format, $output, $desc ) {
60 $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
61 }
62
63 function provideOldTimestamps() {
64 return array(
65 array( '19011213204554', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:54 GMT', 'Earliest time according to php documentation' ),
66 array( '20380119031407', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:07 GMT', 'Latest 32 bit time' ),
67 array( '19011213204552', TS_UNIX, '-2147483648', 'Earliest 32 bit unix time' ),
68 array( '20380119031407', TS_UNIX, '2147483647', 'Latest 32 bit unix time' ),
69 array( '19011213204552', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:52 GMT', 'Earliest 32 bit time' ),
70 array( '19011213204551', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:51 GMT', 'Earliest 32 bit time - 1' ),
71 array( '20380119031408', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:08 GMT', 'Latest 32 bit time + 1' ),
72 array( '19011212000000', TS_MW, '19011212000000', 'Convert to itself r74778#c10645' ),
73 array( '19011213204551', TS_UNIX, '-2147483649', 'Earliest 32 bit unix time - 1' ),
74 array( '20380119031408', TS_UNIX, '2147483648', 'Latest 32 bit unix time + 1' ),
75 array( '-2147483649', TS_MW, '19011213204551', '1901 negative unix time to MediaWiki' ),
76 array( '-5331871504', TS_MW, '18010115123456', '1801 negative unix time to MediaWiki' ),
77 array( '0117-08-09 12:34:56', TS_RFC2822, 'Tue, 09 Aug 0117 12:34:56 GMT', 'Death of Roman Emperor [[Trajan]]' ),
78
79 /* @todo FIXME: 00 to 101 years are taken as being in [1970-2069] */
80 array( '-58979923200', TS_RFC2822, 'Sun, 01 Jan 0101 00:00:00 GMT', '1/1/101' ),
81 array( '-62135596800', TS_RFC2822, 'Mon, 01 Jan 0001 00:00:00 GMT', 'Year 1' ),
82
83 /* It is not clear if we should generate a year 0 or not
84 * We are completely off RFC2822 requirement of year being
85 * 1900 or later.
86 */
87 array( '-62142076800', TS_RFC2822, 'Wed, 18 Oct 0000 00:00:00 GMT', 'ISO 8601:2004 [[year 0]], also called [[1 BC]]' ),
88 );
89 }
90
91 /**
92 * The Resource Loader uses wfTimestamp() to convert timestamps
93 * from If-Modified-Since header. Thus it must be able to parse all
94 * rfc2616 date formats
95 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
96 * @dataProvider provideHttpDates
97 */
98 function testHttpDate( $input, $output, $desc ) {
99 $this->assertEquals( $output, wfTimestamp( TS_MW, $input ), $desc );
100 }
101
102 function provideHttpDates() {
103 return array(
104 array( 'Sun, 06 Nov 1994 08:49:37 GMT', '19941106084937', 'RFC 822 date' ),
105 array( 'Sunday, 06-Nov-94 08:49:37 GMT', '19941106084937', 'RFC 850 date' ),
106 array( 'Sun Nov 6 08:49:37 1994', '19941106084937', "ANSI C's asctime() format" ),
107 // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
108 array( 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626', '20101122141242', 'Netscape extension to HTTP/1.0' ),
109 );
110 }
111
112 /**
113 * There are a number of assumptions in our codebase where wfTimestamp()
114 * should give the current date but it is not given a 0 there. See r71751 CR
115 */
116 function testTimestampParameter() {
117 $now = wfTimestamp( TS_UNIX );
118 // We check that wfTimestamp doesn't return false (error) and use a LessThan assert
119 // for the cases where the test is run in a second boundary.
120
121 $zero = wfTimestamp( TS_UNIX, 0 );
122 $this->assertNotEquals( false, $zero );
123 $this->assertLessThan( 5, $zero - $now );
124
125 $empty = wfTimestamp( TS_UNIX, '' );
126 $this->assertNotEquals( false, $empty );
127 $this->assertLessThan( 5, $empty - $now );
128
129 $null = wfTimestamp( TS_UNIX, null );
130 $this->assertNotEquals( false, $null );
131 $this->assertLessThan( 5, $null - $now );
132 }
133 }