Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / GlobalTest.php
blob1d48d08b1d397e0c495bc495d7b2c10f68536a53
1 <?php
3 /**
4 * @group GlobalFunctions
5 */
6 class GlobalTest extends MediaWikiTestCase {
7 protected function setUp() {
8 parent::setUp();
10 $readOnlyFile = $this->getNewTempFile();
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( [
14 'wgReadOnlyFile' => $readOnlyFile,
15 'wgUrlProtocols' => [
16 'http://',
17 'https://',
18 'mailto:',
19 '//',
20 'file://', # Non-default
22 ] );
25 /**
26 * @dataProvider provideForWfArrayDiff2
27 * @covers ::wfArrayDiff2
29 public function testWfArrayDiff2( $a, $b, $expected ) {
30 $this->assertEquals(
31 wfArrayDiff2( $a, $b ), $expected
35 // @todo Provide more tests
36 public static function provideForWfArrayDiff2() {
37 // $a $b $expected
38 return [
40 [ 'a', 'b' ],
41 [ 'a', 'b' ],
42 [],
45 [ [ 'a' ], [ 'a', 'b', 'c' ] ],
46 [ [ 'a' ], [ 'a', 'b' ] ],
47 [ 1 => [ 'a', 'b', 'c' ] ],
53 * Test cases for random functions could hypothetically fail,
54 * even though they shouldn't.
57 /**
58 * @covers ::wfRandom
60 public function testRandom() {
61 $this->assertFalse(
62 wfRandom() == wfRandom()
66 /**
67 * @covers ::wfRandomString
69 public function testRandomString() {
70 $this->assertFalse(
71 wfRandomString() == wfRandomString()
73 $this->assertEquals(
74 strlen( wfRandomString( 10 ) ), 10
76 $this->assertTrue(
77 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
81 /**
82 * @covers ::wfUrlencode
84 public function testUrlencode() {
85 $this->assertEquals(
86 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
87 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
90 /**
91 * @covers ::wfExpandIRI
93 public function testExpandIRI() {
94 $this->assertEquals(
95 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
96 wfExpandIRI( "https://te.wikibooks.org/wiki/"
97 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
98 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
99 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
100 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
104 * @covers ::wfReadOnly
106 public function testReadOnlyEmpty() {
107 global $wgReadOnly;
108 $wgReadOnly = null;
110 $this->assertFalse( wfReadOnly() );
111 $this->assertFalse( wfReadOnly() );
115 * @covers ::wfReadOnly
117 public function testReadOnlySet() {
118 global $wgReadOnly, $wgReadOnlyFile;
120 $f = fopen( $wgReadOnlyFile, "wt" );
121 fwrite( $f, 'Message' );
122 fclose( $f );
123 $wgReadOnly = null; # Check on $wgReadOnlyFile
125 $this->assertTrue( wfReadOnly() );
126 $this->assertTrue( wfReadOnly() ); # Check cached
128 unlink( $wgReadOnlyFile );
129 $wgReadOnly = null; # Clean cache
131 $this->assertFalse( wfReadOnly() );
132 $this->assertFalse( wfReadOnly() );
135 public static function provideArrayToCGI() {
136 return [
137 [ [], '' ], // empty
138 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
139 [ [ 'foo' => '' ], 'foo=' ], // empty string test
140 [ [ 'foo' => 1 ], 'foo=1' ], // number test
141 [ [ 'foo' => true ], 'foo=1' ], // true test
142 [ [ 'foo' => false ], '' ], // false test
143 [ [ 'foo' => null ], '' ], // null test
144 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
146 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
147 'foo=bar&baz=is&asdf=qwerty'
148 ], // multi-item test
149 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
151 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
152 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
154 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
156 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ],
157 'foo%5Bbar%5D%5Bbar%5D=baz'
163 * @dataProvider provideArrayToCGI
164 * @covers ::wfArrayToCgi
166 public function testArrayToCGI( $array, $result ) {
167 $this->assertEquals( $result, wfArrayToCgi( $array ) );
171 * @covers ::wfArrayToCgi
173 public function testArrayToCGI2() {
174 $this->assertEquals(
175 "baz=bar&foo=bar",
176 wfArrayToCgi(
177 [ 'baz' => 'bar' ],
178 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
181 public static function provideCgiToArray() {
182 return [
183 [ '', [] ], // empty
184 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
185 [ 'foo=', [ 'foo' => '' ] ], // empty string
186 [ 'foo', [ 'foo' => '' ] ], // missing =
187 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
188 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
189 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
191 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
192 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
194 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
196 'foo%5Bbar%5D%5Bbar%5D=baz',
197 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ]
203 * @dataProvider provideCgiToArray
204 * @covers ::wfCgiToArray
206 public function testCgiToArray( $cgi, $result ) {
207 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
210 public static function provideCgiRoundTrip() {
211 return [
212 [ '' ],
213 [ 'foo=bar' ],
214 [ 'foo=' ],
215 [ 'foo=bar&baz=biz' ],
216 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
217 [ 'foo%5Bbar%5D=baz' ],
218 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
219 [ 'foo%5Bbar%5D%5Bbar%5D=baz' ],
224 * @dataProvider provideCgiRoundTrip
225 * @covers ::wfArrayToCgi
227 public function testCgiRoundTrip( $cgi ) {
228 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
232 * @covers ::mimeTypeMatch
234 public function testMimeTypeMatch() {
235 $this->assertEquals(
236 'text/html',
237 mimeTypeMatch( 'text/html',
238 [ 'application/xhtml+xml' => 1.0,
239 'text/html' => 0.7,
240 'text/plain' => 0.3 ] ) );
241 $this->assertEquals(
242 'text/*',
243 mimeTypeMatch( 'text/html',
244 [ 'image/*' => 1.0,
245 'text/*' => 0.5 ] ) );
246 $this->assertEquals(
247 '*/*',
248 mimeTypeMatch( 'text/html',
249 [ '*/*' => 1.0 ] ) );
250 $this->assertNull(
251 mimeTypeMatch( 'text/html',
252 [ 'image/png' => 1.0,
253 'image/svg+xml' => 0.5 ] ) );
257 * @covers ::wfNegotiateType
259 public function testNegotiateType() {
260 $this->assertEquals(
261 'text/html',
262 wfNegotiateType(
263 [ 'application/xhtml+xml' => 1.0,
264 'text/html' => 0.7,
265 'text/plain' => 0.5,
266 'text/*' => 0.2 ],
267 [ 'text/html' => 1.0 ] ) );
268 $this->assertEquals(
269 'application/xhtml+xml',
270 wfNegotiateType(
271 [ 'application/xhtml+xml' => 1.0,
272 'text/html' => 0.7,
273 'text/plain' => 0.5,
274 'text/*' => 0.2 ],
275 [ 'application/xhtml+xml' => 1.0,
276 'text/html' => 0.5 ] ) );
277 $this->assertEquals(
278 'text/html',
279 wfNegotiateType(
280 [ 'text/html' => 1.0,
281 'text/plain' => 0.5,
282 'text/*' => 0.5,
283 'application/xhtml+xml' => 0.2 ],
284 [ 'application/xhtml+xml' => 1.0,
285 'text/html' => 0.5 ] ) );
286 $this->assertEquals(
287 'text/html',
288 wfNegotiateType(
289 [ 'text/*' => 1.0,
290 'image/*' => 0.7,
291 '*/*' => 0.3 ],
292 [ 'application/xhtml+xml' => 1.0,
293 'text/html' => 0.5 ] ) );
294 $this->assertNull(
295 wfNegotiateType(
296 [ 'text/*' => 1.0 ],
297 [ 'application/xhtml+xml' => 1.0 ] ) );
301 * @covers ::wfDebug
302 * @covers ::wfDebugMem
304 public function testDebugFunctionTest() {
305 $debugLogFile = $this->getNewTempFile();
307 $this->setMwGlobals( [
308 'wgDebugLogFile' => $debugLogFile,
309 #  @todo FIXME: $wgDebugTimestamps should be tested
310 'wgDebugTimestamps' => false
311 ] );
313 wfDebug( "This is a normal string" );
314 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
315 unlink( $debugLogFile );
317 wfDebug( "This is nöt an ASCII string" );
318 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
319 unlink( $debugLogFile );
321 wfDebug( "\00305This has böth UTF and control chars\003" );
322 $this->assertEquals(
323 " 05This has böth UTF and control chars \n",
324 file_get_contents( $debugLogFile )
326 unlink( $debugLogFile );
328 wfDebugMem();
329 $this->assertGreaterThan(
330 1000,
331 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
333 unlink( $debugLogFile );
335 wfDebugMem( true );
336 $this->assertGreaterThan(
337 1000000,
338 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
340 unlink( $debugLogFile );
344 * @covers ::wfClientAcceptsGzip
346 public function testClientAcceptsGzipTest() {
348 $settings = [
349 'gzip' => true,
350 'bzip' => false,
351 '*' => false,
352 'compress, gzip' => true,
353 'gzip;q=1.0' => true,
354 'foozip' => false,
355 'foo*zip' => false,
356 'gzip;q=abcde' => true, // is this REALLY valid?
357 'gzip;q=12345678.9' => true,
358 ' gzip' => true,
361 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
362 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
365 foreach ( $settings as $encoding => $expect ) {
366 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
368 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
369 "'$encoding' => " . wfBoolToStr( $expect ) );
372 if ( isset( $old_server_setting ) ) {
373 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
378 * @covers ::wfPercent
380 public function testWfPercentTest() {
382 $pcts = [
383 [ 6 / 7, '0.86%', 2, false ],
384 [ 3 / 3, '1%' ],
385 [ 22 / 7, '3.14286%', 5 ],
386 [ 3 / 6, '0.5%' ],
387 [ 1 / 3, '0%', 0 ],
388 [ 10 / 3, '0%', -1 ],
389 [ 3 / 4 / 5, '0.1%', 1 ],
390 [ 6 / 7 * 8, '6.8571428571%', 10 ],
393 foreach ( $pcts as $pct ) {
394 if ( !isset( $pct[2] ) ) {
395 $pct[2] = 2;
397 if ( !isset( $pct[3] ) ) {
398 $pct[3] = true;
401 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
406 * test @see wfShorthandToInteger()
407 * @dataProvider provideShorthand
408 * @covers ::wfShorthandToInteger
410 public function testWfShorthandToInteger( $shorthand, $expected ) {
411 $this->assertEquals( $expected,
412 wfShorthandToInteger( $shorthand )
416 public static function provideShorthand() {
417 // Syntax: [ shorthand, expected integer ]
418 return [
419 # Null, empty ...
420 [ '', -1 ],
421 [ ' ', -1 ],
422 [ null, -1 ],
424 # Failures returns 0 :(
425 [ 'ABCDEFG', 0 ],
426 [ 'Ak', 0 ],
428 # Int, strings with spaces
429 [ 1, 1 ],
430 [ ' 1 ', 1 ],
431 [ 1023, 1023 ],
432 [ ' 1023 ', 1023 ],
434 # kilo, Mega, Giga
435 [ '1k', 1024 ],
436 [ '1K', 1024 ],
437 [ '1m', 1024 * 1024 ],
438 [ '1M', 1024 * 1024 ],
439 [ '1g', 1024 * 1024 * 1024 ],
440 [ '1G', 1024 * 1024 * 1024 ],
442 # Negatives
443 [ -1, -1 ],
444 [ -500, -500 ],
445 [ '-500', -500 ],
446 [ '-1k', -1024 ],
448 # Zeroes
449 [ '0', 0 ],
450 [ '0k', 0 ],
451 [ '0M', 0 ],
452 [ '0G', 0 ],
453 [ '-0', 0 ],
454 [ '-0k', 0 ],
455 [ '-0M', 0 ],
456 [ '-0G', 0 ],
461 * @param string $old Text as it was in the database
462 * @param string $mine Text submitted while user was editing
463 * @param string $yours Text submitted by the user
464 * @param bool $expectedMergeResult Whether the merge should be a success
465 * @param string $expectedText Text after merge has been completed
467 * @dataProvider provideMerge()
468 * @group medium
469 * @covers ::wfMerge
471 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
472 $this->markTestSkippedIfNoDiff3();
474 $mergedText = null;
475 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
477 $msg = 'Merge should be a ';
478 $msg .= $expectedMergeResult ? 'success' : 'failure';
479 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
481 if ( $isMerged ) {
482 // Verify the merged text
483 $this->assertEquals( $expectedText, $mergedText,
484 'is merged text as expected?' );
488 public static function provideMerge() {
489 $EXPECT_MERGE_SUCCESS = true;
490 $EXPECT_MERGE_FAILURE = false;
492 return [
493 // #0: clean merge
495 // old:
496 "one one one\n" . // trimmed
497 "\n" .
498 "two two two",
500 // mine:
501 "one one one ONE ONE\n" .
502 "\n" .
503 "two two two\n", // with tailing whitespace
505 // yours:
506 "one one one\n" .
507 "\n" .
508 "two two TWO TWO", // trimmed
510 // ok:
511 $EXPECT_MERGE_SUCCESS,
513 // result:
514 "one one one ONE ONE\n" .
515 "\n" .
516 "two two TWO TWO\n", // note: will always end in a newline
519 // #1: conflict, fail
521 // old:
522 "one one one", // trimmed
524 // mine:
525 "one one one ONE ONE\n" .
526 "\n" .
527 "bla bla\n" .
528 "\n", // with tailing whitespace
530 // yours:
531 "one one one\n" .
532 "\n" .
533 "two two", // trimmed
535 $EXPECT_MERGE_FAILURE,
537 // result:
538 null,
544 * @dataProvider provideMakeUrlIndexes()
545 * @covers ::wfMakeUrlIndexes
547 public function testMakeUrlIndexes( $url, $expected ) {
548 $index = wfMakeUrlIndexes( $url );
549 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
552 public static function provideMakeUrlIndexes() {
553 return [
554 // Testcase for T30627
556 'https://example.org/test.cgi?id=12345',
557 [ 'https://org.example./test.cgi?id=12345' ]
560 // mailtos are handled special
561 // is this really right though? that final . probably belongs earlier?
562 'mailto:wiki@wikimedia.org',
563 [ 'mailto:org.wikimedia@wiki.' ]
566 // file URL cases per T30627...
568 // three slashes: local filesystem path Unix-style
569 'file:///whatever/you/like.txt',
570 [ 'file://./whatever/you/like.txt' ]
573 // three slashes: local filesystem path Windows-style
574 'file:///c:/whatever/you/like.txt',
575 [ 'file://./c:/whatever/you/like.txt' ]
578 // two slashes: UNC filesystem path Windows-style
579 'file://intranet/whatever/you/like.txt',
580 [ 'file://intranet./whatever/you/like.txt' ]
582 // Multiple-slash cases that can sorta work on Mozilla
583 // if you hack it just right are kinda pathological,
584 // and unreliable cross-platform or on IE which means they're
585 // unlikely to appear on intranets.
586 // Those will survive the algorithm but with results that
587 // are less consistent.
589 // protocol-relative URL cases per T31854...
591 '//example.org/test.cgi?id=12345',
593 'http://org.example./test.cgi?id=12345',
594 'https://org.example./test.cgi?id=12345'
601 * @dataProvider provideWfMatchesDomainList
602 * @covers ::wfMatchesDomainList
604 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
605 $actual = wfMatchesDomainList( $url, $domains );
606 $this->assertEquals( $expected, $actual, $description );
609 public static function provideWfMatchesDomainList() {
610 $a = [];
611 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
612 foreach ( $protocols as $pDesc => $p ) {
613 $a = array_merge( $a, [
615 "$p//www.example.com",
617 false,
618 "No matches for empty domains array, $pDesc URL"
621 "$p//www.example.com",
622 [ 'www.example.com' ],
623 true,
624 "Exact match in domains array, $pDesc URL"
627 "$p//www.example.com",
628 [ 'example.com' ],
629 true,
630 "Match without subdomain in domains array, $pDesc URL"
633 "$p//www.example2.com",
634 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
635 true,
636 "Exact match with other domains in array, $pDesc URL"
639 "$p//www.example2.com",
640 [ 'example.com', 'example2.com', 'example3,com' ],
641 true,
642 "Match without subdomain with other domains in array, $pDesc URL"
645 "$p//www.example4.com",
646 [ 'example.com', 'example2.com', 'example3,com' ],
647 false,
648 "Domain not in array, $pDesc URL"
651 "$p//nds-nl.wikipedia.org",
652 [ 'nl.wikipedia.org' ],
653 false,
654 "Non-matching substring of domain, $pDesc URL"
656 ] );
659 return $a;
663 * @covers ::wfMkdirParents
665 public function testWfMkdirParents() {
666 // Should not return true if file exists instead of directory
667 $fname = $this->getNewTempFile();
668 MediaWiki\suppressWarnings();
669 $ok = wfMkdirParents( $fname );
670 MediaWiki\restoreWarnings();
671 $this->assertFalse( $ok );
675 * @dataProvider provideWfShellWikiCmdList
676 * @covers ::wfShellWikiCmd
678 public function testWfShellWikiCmd( $script, $parameters, $options,
679 $expected, $description
681 if ( wfIsWindows() ) {
682 // Approximation that's good enough for our purposes just now
683 $expected = str_replace( "'", '"', $expected );
685 $actual = wfShellWikiCmd( $script, $parameters, $options );
686 $this->assertEquals( $expected, $actual, $description );
689 public function wfWikiID() {
690 $this->setMwGlobals( [
691 'wgDBname' => 'example',
692 'wgDBprefix' => '',
693 ] );
694 $this->assertEquals(
695 wfWikiID(),
696 'example'
699 $this->setMwGlobals( [
700 'wgDBname' => 'example',
701 'wgDBprefix' => 'mw_',
702 ] );
703 $this->assertEquals(
704 wfWikiID(),
705 'example-mw_'
709 public function testWfMemcKey() {
710 $cache = ObjectCache::getLocalClusterInstance();
711 $this->assertEquals(
712 $cache->makeKey( 'foo', 123, 'bar' ),
713 wfMemcKey( 'foo', 123, 'bar' )
717 public function testWfForeignMemcKey() {
718 $cache = ObjectCache::getLocalClusterInstance();
719 $keyspace = $this->readAttribute( $cache, 'keyspace' );
720 $this->assertEquals(
721 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
722 $cache->makeKey( 'foo', 'bar' )
726 public function testWfGlobalCacheKey() {
727 $cache = ObjectCache::getLocalClusterInstance();
728 $this->assertEquals(
729 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
730 wfGlobalCacheKey( 'foo', 123, 'bar' )
734 public static function provideWfShellWikiCmdList() {
735 global $wgPhpCli;
737 return [
738 [ 'eval.php', [ '--help', '--test' ], [],
739 "'$wgPhpCli' 'eval.php' '--help' '--test'",
740 "Called eval.php --help --test" ],
741 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
742 "'php5' 'eval.php' '--help' '--test space'",
743 "Called eval.php --help --test with php option" ],
744 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
745 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
746 "Called eval.php --help --test with wrapper option" ],
748 'eval.php',
749 [ '--help', '--test', 'y' ],
750 [ 'php' => 'php5', 'wrapper' => 'MWScript.php' ],
751 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
752 "Called eval.php --help --test with wrapper and php option"
756 /* @todo many more! */