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