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