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