3 class GlobalTest
extends PHPUnit_Framework_TestCase
{
5 global $wgReadOnlyFile, $wgContLang, $wgLang;
6 $this->originals
['wgReadOnlyFile'] = $wgReadOnlyFile;
7 $wgReadOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
8 unlink( $wgReadOnlyFile );
9 $wgContLang = $wgLang = Language
::factory( 'en' );
13 global $wgReadOnlyFile;
14 if ( file_exists( $wgReadOnlyFile ) ) {
15 unlink( $wgReadOnlyFile );
17 $wgReadOnlyFile = $this->originals
['wgReadOnlyFile'];
20 function testRandom() {
21 # This could hypothetically fail, but it shouldn't ;)
23 wfRandom() == wfRandom() );
26 function testUrlencode() {
28 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
29 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
32 function testReadOnlyEmpty() {
36 $this->assertFalse( wfReadOnly() );
37 $this->assertFalse( wfReadOnly() );
40 function testReadOnlySet() {
41 global $wgReadOnly, $wgReadOnlyFile;
43 $f = fopen( $wgReadOnlyFile, "wt" );
44 fwrite( $f, 'Message' );
48 $this->assertTrue( wfReadOnly() );
49 $this->assertTrue( wfReadOnly() );
51 unlink( $wgReadOnlyFile );
54 $this->assertFalse( wfReadOnly() );
55 $this->assertFalse( wfReadOnly() );
58 function testQuotedPrintable() {
60 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
61 wfQuotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
66 $this->assertType( 'float', $start );
68 $this->assertTrue( $end > $start, "Time is running backwards!" );
71 function testArrayToCGI() {
75 array( 'baz' => 'AT&T', 'ignore' => '' ),
76 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
79 function testMimeTypeMatch() {
82 mimeTypeMatch( 'text/html',
83 array( 'application/xhtml+xml' => 1.0,
85 'text/plain' => 0.3 ) ) );
88 mimeTypeMatch( 'text/html',
89 array( 'image/*' => 1.0,
90 'text/*' => 0.5 ) ) );
93 mimeTypeMatch( 'text/html',
94 array( '*/*' => 1.0 ) ) );
96 mimeTypeMatch( 'text/html',
97 array( 'image/png' => 1.0,
98 'image/svg+xml' => 0.5 ) ) );
101 function testNegotiateType() {
105 array( 'application/xhtml+xml' => 1.0,
109 array( 'text/html' => 1.0 ) ) );
111 'application/xhtml+xml',
113 array( 'application/xhtml+xml' => 1.0,
117 array( 'application/xhtml+xml' => 1.0,
118 'text/html' => 0.5 ) ) );
122 array( 'text/html' => 1.0,
125 'application/xhtml+xml' => 0.2 ),
126 array( 'application/xhtml+xml' => 1.0,
127 'text/html' => 0.5 ) ) );
131 array( 'text/*' => 1.0,
134 array( 'application/xhtml+xml' => 1.0,
135 'text/html' => 0.5 ) ) );
138 array( 'text/*' => 1.0 ),
139 array( 'application/xhtml+xml' => 1.0 ) ) );
142 function testTimestamp() {
143 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
146 wfTimestamp( TS_MW
, $t ),
147 'TS_UNIX to TS_MW' );
150 wfTimestamp( TS_MW
, -30281104 ),
151 'Negative TS_UNIX to TS_MW' );
154 wfTimestamp( TS_UNIX
, $t ),
155 'TS_UNIX to TS_UNIX' );
157 '2001-01-15 12:34:56',
158 wfTimestamp( TS_DB
, $t ),
159 'TS_UNIX to TS_DB' );
162 wfTimestamp( TS_ISO_8601_BASIC
, $t ),
163 'TS_ISO_8601_BASIC to TS_DB' );
167 wfTimestamp( TS_MW
, '20010115123456' ),
171 wfTimestamp( TS_UNIX
, '20010115123456' ),
172 'TS_MW to TS_UNIX' );
174 '2001-01-15 12:34:56',
175 wfTimestamp( TS_DB
, '20010115123456' ),
179 wfTimestamp( TS_ISO_8601_BASIC
, '20010115123456' ),
180 'TS_MW to TS_ISO_8601_BASIC' );
184 wfTimestamp( TS_MW
, '2001-01-15 12:34:56' ),
188 wfTimestamp( TS_UNIX
, '2001-01-15 12:34:56' ),
189 'TS_DB to TS_UNIX' );
191 '2001-01-15 12:34:56',
192 wfTimestamp( TS_DB
, '2001-01-15 12:34:56' ),
196 wfTimestamp( TS_ISO_8601_BASIC
, '2001-01-15 12:34:56' ),
197 'TS_DB to TS_ISO_8601_BASIC' );
199 # rfc2822 section 3.3
202 'Mon, 15 Jan 2001 12:34:56 GMT',
203 wfTimestamp( TS_RFC2822
, '20010115123456' ),
204 'TS_MW to TS_RFC2822' );
208 wfTimestamp( TS_MW
, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
209 'TS_RFC2822 to TS_MW' );
213 wfTimestamp( TS_MW
, ' Mon, 15 Jan 2001 12:34:56 GMT' ),
214 'TS_RFC2822 with leading space to TS_MW' );
218 wfTimestamp( TS_MW
, '15 Jan 2001 12:34:56 GMT' ),
219 'TS_RFC2822 without optional day-of-week to TS_MW' );
221 # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
222 # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
225 wfTimestamp( TS_MW
, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
226 'TS_RFC2822 to TS_MW' );
228 # WSP = SP / HTAB ; rfc2234
231 wfTimestamp( TS_MW
, "Mon, 15 Jan\x092001 12:34:56 GMT" ),
232 'TS_RFC2822 with HTAB to TS_MW' );
236 wfTimestamp( TS_MW
, "Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT" ),
237 'TS_RFC2822 with HTAB and SP to TS_MW' );
241 wfTimestamp( TS_MW
, "Sun, 6 Nov 94 08:49:37 GMT" ),
242 'TS_RFC2822 with obsolete year to TS_MW' );
246 * This test checks wfTimestamp() with values outside.
247 * It needs PHP 64 bits or PHP > 5.1.
248 * See r74778 and bug 25451
250 function testOldTimestamps() {
251 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:54 GMT',
252 wfTimestamp( TS_RFC2822
, '19011213204554' ),
253 'Earliest time according to php documentation' );
255 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:07 GMT',
256 wfTimestamp( TS_RFC2822
, '20380119031407' ),
257 'Latest 32 bit time' );
259 $this->assertEquals( '-2147483648',
260 wfTimestamp( TS_UNIX
, '19011213204552' ),
261 'Earliest 32 bit unix time' );
263 $this->assertEquals( '2147483647',
264 wfTimestamp( TS_UNIX
, '20380119031407' ),
265 'Latest 32 bit unix time' );
267 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:52 GMT',
268 wfTimestamp( TS_RFC2822
, '19011213204552' ),
269 'Earliest 32 bit time' );
271 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:51 GMT',
272 wfTimestamp( TS_RFC2822
, '19011213204551' ),
273 'Earliest 32 bit time - 1' );
275 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:08 GMT',
276 wfTimestamp( TS_RFC2822
, '20380119031408' ),
277 'Latest 32 bit time + 1' );
279 $this->assertEquals( '19011212000000',
280 wfTimestamp(TS_MW
, '19011212000000'),
281 'Convert to itself r74778#c10645' );
283 $this->assertEquals( '-2147483649',
284 wfTimestamp( TS_UNIX
, '19011213204551' ),
285 'Earliest 32 bit unix time - 1' );
287 $this->assertEquals( '2147483648',
288 wfTimestamp( TS_UNIX
, '20380119031408' ),
289 'Latest 32 bit unix time + 1' );
291 $this->assertEquals( '19011213204551',
292 wfTimestamp( TS_MW
, '-2147483649' ),
293 '1901 negative unix time to MediaWiki' );
295 $this->assertEquals( '18010115123456',
296 wfTimestamp( TS_MW
, '-5331871504' ),
297 '1801 negative unix time to MediaWiki' );
299 $this->assertEquals( 'Tue, 09 Aug 0117 12:34:56 GMT',
300 wfTimestamp( TS_RFC2822
, '0117-08-09 12:34:56'),
301 'Death of Roman Emperor [[Trajan]]');
303 /* FIXME: 00 to 101 years are taken as being in [1970-2069] */
305 $this->assertEquals( 'Sun, 01 Jan 0101 00:00:00 GMT',
306 wfTimestamp( TS_RFC2822
, '-58979923200'),
309 $this->assertEquals( 'Mon, 01 Jan 0001 00:00:00 GMT',
310 wfTimestamp( TS_RFC2822
, '-62135596800'),
313 /* It is not clear if we should generate a year 0 or not
314 * We are completely off RFC2822 requirement of year being
317 $this->assertEquals( 'Wed, 18 Oct 0000 00:00:00 GMT',
318 wfTimestamp( TS_RFC2822
, '-62142076800'),
319 'ISO 8601:2004 [[year 0]], also called [[1 BC]]');
322 function testHttpDate() {
323 # The Resource Loader uses wfTimestamp() to convert timestamps
324 # from If-Modified-Since header.
325 # Thus it must be able to parse all rfc2616 date formats
326 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
330 wfTimestamp( TS_MW
, 'Sun, 06 Nov 1994 08:49:37 GMT' ),
335 wfTimestamp( TS_MW
, 'Sunday, 06-Nov-94 08:49:37 GMT' ),
340 wfTimestamp( TS_MW
, 'Sun Nov 6 08:49:37 1994' ),
341 "ANSI C's asctime() format" );
343 // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
346 wfTimestamp( TS_MW
, 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626' ),
347 "Netscape extension to HTTP/1.0" );
351 function testBasename() {
365 '\\aaaa\\' => 'aaaa',
366 '\\aaaa\\' => 'aaaa',
367 '/mnt/upload3/wikipedia/en/thumb/8/8b/Zork_Grand_Inquisitor_box_cover.jpg/93px-Zork_Grand_Inquisitor_box_cover.jpg' => '93px-Zork_Grand_Inquisitor_box_cover.jpg',
368 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
369 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
371 foreach ( $sets as $from => $to ) {
372 $this->assertEquals( $to, wfBaseName( $from ),
373 "wfBaseName('$from') => '$to'" );
377 /* TODO: many more! */