Remove superfluous re- from confirmemail_body_set
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / GlobalTest.php
blob57f8c199359fb9275fabf29ebc9c5c8acb4732a3
1 <?php
3 class GlobalTest extends MediaWikiTestCase {
4 protected function setUp() {
5 parent::setUp();
7 $readOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
8 unlink( $readOnlyFile );
10 $this->setMwGlobals( array(
11 'wgReadOnlyFile' => $readOnlyFile,
12 'wgUrlProtocols' => array(
13 'http://',
14 'https://',
15 'mailto:',
16 '//',
17 'file://', # Non-default
19 ) );
22 protected function tearDown() {
23 global $wgReadOnlyFile;
25 if ( file_exists( $wgReadOnlyFile ) ) {
26 unlink( $wgReadOnlyFile );
29 parent::tearDown();
32 /** @dataProvider provideForWfArrayDiff2 */
33 public function testWfArrayDiff2( $a, $b, $expected ) {
34 $this->assertEquals(
35 wfArrayDiff2( $a, $b ), $expected
39 // @todo Provide more tests
40 public static function provideForWfArrayDiff2() {
41 // $a $b $expected
42 return array(
43 array(
44 array( 'a', 'b' ),
45 array( 'a', 'b' ),
46 array(),
48 array(
49 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
50 array( array( 'a' ), array( 'a', 'b' ) ),
51 array( 1 => array( 'a', 'b', 'c' ) ),
56 function testRandom() {
57 # This could hypothetically fail, but it shouldn't ;)
58 $this->assertFalse(
59 wfRandom() == wfRandom() );
62 function testUrlencode() {
63 $this->assertEquals(
64 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
65 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
68 function testExpandIRI() {
69 $this->assertEquals(
70 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
71 wfExpandIRI( "https://te.wikibooks.org/wiki/%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
74 function testReadOnlyEmpty() {
75 global $wgReadOnly;
76 $wgReadOnly = null;
78 $this->assertFalse( wfReadOnly() );
79 $this->assertFalse( wfReadOnly() );
82 function testReadOnlySet() {
83 global $wgReadOnly, $wgReadOnlyFile;
85 $f = fopen( $wgReadOnlyFile, "wt" );
86 fwrite( $f, 'Message' );
87 fclose( $f );
88 $wgReadOnly = null; # Check on $wgReadOnlyFile
90 $this->assertTrue( wfReadOnly() );
91 $this->assertTrue( wfReadOnly() ); # Check cached
93 unlink( $wgReadOnlyFile );
94 $wgReadOnly = null; # Clean cache
96 $this->assertFalse( wfReadOnly() );
97 $this->assertFalse( wfReadOnly() );
100 function testQuotedPrintable() {
101 $this->assertEquals(
102 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
103 UserMailer::quotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
106 public static function provideArrayToCGI() {
107 return array(
108 array( array(), '' ), // empty
109 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
110 array( array( 'foo' => '' ), 'foo=' ), // empty string test
111 array( array( 'foo' => 1 ), 'foo=1' ), // number test
112 array( array( 'foo' => true ), 'foo=1' ), // true test
113 array( array( 'foo' => false ), '' ), // false test
114 array( array( 'foo' => null ), '' ), // null test
115 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
116 array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test
117 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
118 array( array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ), 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' ),
119 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
120 array( array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ), 'foo%5Bbar%5D%5Bbar%5D=baz' ),
125 * @dataProvider provideArrayToCGI
127 function testArrayToCGI( $array, $result ) {
128 $this->assertEquals( $result, wfArrayToCgi( $array ) );
132 function testArrayToCGI2() {
133 $this->assertEquals(
134 "baz=bar&foo=bar",
135 wfArrayToCgi(
136 array( 'baz' => 'bar' ),
137 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
140 public static function provideCgiToArray() {
141 return array(
142 array( '', array() ), // empty
143 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
144 array( 'foo=', array( 'foo' => '' ) ), // empty string
145 array( 'foo', array( 'foo' => '' ) ), // missing =
146 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
147 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
148 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
149 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ),
150 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
151 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ),
156 * @dataProvider provideCgiToArray
158 function testCgiToArray( $cgi, $result ) {
159 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
162 public static function provideCgiRoundTrip() {
163 return array(
164 array( '' ),
165 array( 'foo=bar' ),
166 array( 'foo=' ),
167 array( 'foo=bar&baz=biz' ),
168 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
169 array( 'foo%5Bbar%5D=baz' ),
170 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
171 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
176 * @dataProvider provideCgiRoundTrip
178 function testCgiRoundTrip( $cgi ) {
179 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
182 function testMimeTypeMatch() {
183 $this->assertEquals(
184 'text/html',
185 mimeTypeMatch( 'text/html',
186 array( 'application/xhtml+xml' => 1.0,
187 'text/html' => 0.7,
188 'text/plain' => 0.3 ) ) );
189 $this->assertEquals(
190 'text/*',
191 mimeTypeMatch( 'text/html',
192 array( 'image/*' => 1.0,
193 'text/*' => 0.5 ) ) );
194 $this->assertEquals(
195 '*/*',
196 mimeTypeMatch( 'text/html',
197 array( '*/*' => 1.0 ) ) );
198 $this->assertNull(
199 mimeTypeMatch( 'text/html',
200 array( 'image/png' => 1.0,
201 'image/svg+xml' => 0.5 ) ) );
204 function testNegotiateType() {
205 $this->assertEquals(
206 'text/html',
207 wfNegotiateType(
208 array( 'application/xhtml+xml' => 1.0,
209 'text/html' => 0.7,
210 'text/plain' => 0.5,
211 'text/*' => 0.2 ),
212 array( 'text/html' => 1.0 ) ) );
213 $this->assertEquals(
214 'application/xhtml+xml',
215 wfNegotiateType(
216 array( 'application/xhtml+xml' => 1.0,
217 'text/html' => 0.7,
218 'text/plain' => 0.5,
219 'text/*' => 0.2 ),
220 array( 'application/xhtml+xml' => 1.0,
221 'text/html' => 0.5 ) ) );
222 $this->assertEquals(
223 'text/html',
224 wfNegotiateType(
225 array( 'text/html' => 1.0,
226 'text/plain' => 0.5,
227 'text/*' => 0.5,
228 'application/xhtml+xml' => 0.2 ),
229 array( 'application/xhtml+xml' => 1.0,
230 'text/html' => 0.5 ) ) );
231 $this->assertEquals(
232 'text/html',
233 wfNegotiateType(
234 array( 'text/*' => 1.0,
235 'image/*' => 0.7,
236 '*/*' => 0.3 ),
237 array( 'application/xhtml+xml' => 1.0,
238 'text/html' => 0.5 ) ) );
239 $this->assertNull(
240 wfNegotiateType(
241 array( 'text/*' => 1.0 ),
242 array( 'application/xhtml+xml' => 1.0 ) ) );
245 function testFallbackMbstringFunctions() {
247 if ( !extension_loaded( 'mbstring' ) ) {
248 $this->markTestSkipped( "The mb_string functions must be installed to test the fallback functions" );
251 $sampleUTF = "Östergötland_coat_of_arms.png";
253 //mb_substr
254 $substr_params = array(
255 array( 0, 0 ),
256 array( 5, -4 ),
257 array( 33 ),
258 array( 100, -5 ),
259 array( -8, 10 ),
260 array( 1, 1 ),
261 array( 2, -1 )
264 foreach ( $substr_params as $param_set ) {
265 $old_param_set = $param_set;
266 array_unshift( $param_set, $sampleUTF );
268 $this->assertEquals(
269 call_user_func_array( 'mb_substr', $param_set ),
270 call_user_func_array( 'Fallback::mb_substr', $param_set ),
271 'Fallback mb_substr with params ' . implode( ', ', $old_param_set )
275 //mb_strlen
276 $this->assertEquals(
277 mb_strlen( $sampleUTF ),
278 Fallback::mb_strlen( $sampleUTF ),
279 'Fallback mb_strlen'
282 //mb_str(r?)pos
283 $strpos_params = array(
284 //array( 'ter' ),
285 //array( 'Ö' ),
286 //array( 'Ö', 3 ),
287 //array( 'oat_', 100 ),
288 //array( 'c', -10 ),
289 //Broken for now
292 foreach ( $strpos_params as $param_set ) {
293 $old_param_set = $param_set;
294 array_unshift( $param_set, $sampleUTF );
296 $this->assertEquals(
297 call_user_func_array( 'mb_strpos', $param_set ),
298 call_user_func_array( 'Fallback::mb_strpos', $param_set ),
299 'Fallback mb_strpos with params ' . implode( ', ', $old_param_set )
302 $this->assertEquals(
303 call_user_func_array( 'mb_strrpos', $param_set ),
304 call_user_func_array( 'Fallback::mb_strrpos', $param_set ),
305 'Fallback mb_strrpos with params ' . implode( ', ', $old_param_set )
311 function testDebugFunctionTest() {
313 global $wgDebugLogFile, $wgDebugTimestamps;
315 $old_log_file = $wgDebugLogFile;
316 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
317 # @todo FIXME: $wgDebugTimestamps should be tested
318 $old_wgDebugTimestamps = $wgDebugTimestamps;
319 $wgDebugTimestamps = false;
321 wfDebug( "This is a normal string" );
322 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
323 unlink( $wgDebugLogFile );
325 wfDebug( "This is nöt an ASCII string" );
326 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
327 unlink( $wgDebugLogFile );
329 wfDebug( "\00305This has böth UTF and control chars\003" );
330 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
331 unlink( $wgDebugLogFile );
333 wfDebugMem();
334 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
335 unlink( $wgDebugLogFile );
337 wfDebugMem( true );
338 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
339 unlink( $wgDebugLogFile );
341 $wgDebugLogFile = $old_log_file;
342 $wgDebugTimestamps = $old_wgDebugTimestamps;
345 function testClientAcceptsGzipTest() {
347 $settings = array(
348 'gzip' => true,
349 'bzip' => false,
350 '*' => false,
351 'compress, gzip' => true,
352 'gzip;q=1.0' => true,
353 'foozip' => false,
354 'foo*zip' => false,
355 'gzip;q=abcde' => true, //is this REALLY valid?
356 'gzip;q=12345678.9' => true,
357 ' gzip' => true,
360 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
361 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
364 foreach ( $settings as $encoding => $expect ) {
365 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
367 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
368 "'$encoding' => " . wfBoolToStr( $expect ) );
371 if ( isset( $old_server_setting ) ) {
372 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
376 function testSwapVarsTest() {
377 $var1 = 1;
378 $var2 = 2;
380 $this->assertEquals( $var1, 1, 'var1 is set originally' );
381 $this->assertEquals( $var2, 2, 'var1 is set originally' );
383 swap( $var1, $var2 );
385 $this->assertEquals( $var1, 2, 'var1 is swapped' );
386 $this->assertEquals( $var2, 1, 'var2 is swapped' );
389 function testWfPercentTest() {
391 $pcts = array(
392 array( 6 / 7, '0.86%', 2, false ),
393 array( 3 / 3, '1%' ),
394 array( 22 / 7, '3.14286%', 5 ),
395 array( 3 / 6, '0.5%' ),
396 array( 1 / 3, '0%', 0 ),
397 array( 10 / 3, '0%', -1 ),
398 array( 3 / 4 / 5, '0.1%', 1 ),
399 array( 6 / 7 * 8, '6.8571428571%', 10 ),
402 foreach ( $pcts as $pct ) {
403 if ( !isset( $pct[2] ) ) {
404 $pct[2] = 2;
406 if ( !isset( $pct[3] ) ) {
407 $pct[3] = true;
410 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
415 * test @see wfShorthandToInteger()
416 * @dataProvider provideShorthand
418 public function testWfShorthandToInteger( $shorthand, $expected ) {
419 $this->assertEquals( $expected,
420 wfShorthandToInteger( $shorthand )
424 /** array( shorthand, expected integer ) */
425 public static function provideShorthand() {
426 return array(
427 # Null, empty ...
428 array( '', -1 ),
429 array( ' ', -1 ),
430 array( null, -1 ),
432 # Failures returns 0 :(
433 array( 'ABCDEFG', 0 ),
434 array( 'Ak', 0 ),
436 # Int, strings with spaces
437 array( 1, 1 ),
438 array( ' 1 ', 1 ),
439 array( 1023, 1023 ),
440 array( ' 1023 ', 1023 ),
442 # kilo, Mega, Giga
443 array( '1k', 1024 ),
444 array( '1K', 1024 ),
445 array( '1m', 1024 * 1024 ),
446 array( '1M', 1024 * 1024 ),
447 array( '1g', 1024 * 1024 * 1024 ),
448 array( '1G', 1024 * 1024 * 1024 ),
450 # Negatives
451 array( -1, -1 ),
452 array( -500, -500 ),
453 array( '-500', -500 ),
454 array( '-1k', -1024 ),
456 # Zeroes
457 array( '0', 0 ),
458 array( '0k', 0 ),
459 array( '0M', 0 ),
460 array( '0G', 0 ),
461 array( '-0', 0 ),
462 array( '-0k', 0 ),
463 array( '-0M', 0 ),
464 array( '-0G', 0 ),
469 * @param String $old: Text as it was in the database
470 * @param String $mine: Text submitted while user was editing
471 * @param String $yours: Text submitted by the user
472 * @param Boolean $expectedMergeResult Whether the merge should be a success
473 * @param String $expectedText: Text after merge has been completed
475 * @dataProvider provideMerge()
476 * @group medium
478 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
479 $this->checkHasDiff3();
481 $mergedText = null;
482 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
484 $msg = 'Merge should be a ';
485 $msg .= $expectedMergeResult ? 'success' : 'failure';
486 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
488 if ( $isMerged ) {
489 // Verify the merged text
490 $this->assertEquals( $expectedText, $mergedText,
491 'is merged text as expected?' );
495 public static function provideMerge() {
496 $EXPECT_MERGE_SUCCESS = true;
497 $EXPECT_MERGE_FAILURE = false;
499 return array(
500 // #0: clean merge
501 array(
502 // old:
503 "one one one\n" . // trimmed
504 "\n" .
505 "two two two",
507 // mine:
508 "one one one ONE ONE\n" .
509 "\n" .
510 "two two two\n", // with tailing whitespace
512 // yours:
513 "one one one\n" .
514 "\n" .
515 "two two TWO TWO", // trimmed
517 // ok:
518 $EXPECT_MERGE_SUCCESS,
520 // result:
521 "one one one ONE ONE\n" .
522 "\n" .
523 "two two TWO TWO\n", // note: will always end in a newline
526 // #1: conflict, fail
527 array(
528 // old:
529 "one one one", // trimmed
531 // mine:
532 "one one one ONE ONE\n" .
533 "\n" .
534 "bla bla\n" .
535 "\n", // with tailing whitespace
537 // yours:
538 "one one one\n" .
539 "\n" .
540 "two two", // trimmed
542 $EXPECT_MERGE_FAILURE,
544 // result:
545 null,
551 * @dataProvider provideMakeUrlIndexes()
553 function testMakeUrlIndexes( $url, $expected ) {
554 $index = wfMakeUrlIndexes( $url );
555 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
558 public static function provideMakeUrlIndexes() {
559 return array(
560 array(
561 // just a regular :)
562 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
563 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
565 array(
566 // mailtos are handled special
567 // is this really right though? that final . probably belongs earlier?
568 'mailto:wiki@wikimedia.org',
569 array( 'mailto:org.wikimedia@wiki.' )
572 // file URL cases per bug 28627...
573 array(
574 // three slashes: local filesystem path Unix-style
575 'file:///whatever/you/like.txt',
576 array( 'file://./whatever/you/like.txt' )
578 array(
579 // three slashes: local filesystem path Windows-style
580 'file:///c:/whatever/you/like.txt',
581 array( 'file://./c:/whatever/you/like.txt' )
583 array(
584 // two slashes: UNC filesystem path Windows-style
585 'file://intranet/whatever/you/like.txt',
586 array( 'file://intranet./whatever/you/like.txt' )
588 // Multiple-slash cases that can sorta work on Mozilla
589 // if you hack it just right are kinda pathological,
590 // and unreliable cross-platform or on IE which means they're
591 // unlikely to appear on intranets.
593 // Those will survive the algorithm but with results that
594 // are less consistent.
596 // protocol-relative URL cases per bug 29854...
597 array(
598 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
599 array(
600 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
601 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
608 * @dataProvider provideWfMatchesDomainList
610 function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
611 $actual = wfMatchesDomainList( $url, $domains );
612 $this->assertEquals( $expected, $actual, $description );
615 public static function provideWfMatchesDomainList() {
616 $a = array();
617 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
618 foreach ( $protocols as $pDesc => $p ) {
619 $a = array_merge( $a, array(
620 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ),
621 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ),
622 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ),
623 array( "$p//www.example2.com", array( 'www.example.com', 'www.example2.com', 'www.example3.com' ), true, "Exact match with other domains in array, $pDesc URL" ),
624 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ),
625 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ),
626 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), false, "Non-matching substring of domain, $pDesc URL" ),
627 ) );
630 return $a;
634 * @dataProvider provideWfShellMaintenanceCmdList
636 function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) {
637 if ( wfIsWindows() ) {
638 // Approximation that's good enough for our purposes just now
639 $expected = str_replace( "'", '"', $expected );
641 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
642 $this->assertEquals( $expected, $actual, $description );
645 public static function provideWfShellMaintenanceCmdList() {
646 global $wgPhpCli;
648 return array(
649 array( 'eval.php', array( '--help', '--test' ), array(),
650 "'$wgPhpCli' 'eval.php' '--help' '--test'",
651 "Called eval.php --help --test" ),
652 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
653 "'php5' 'eval.php' '--help' '--test space'",
654 "Called eval.php --help --test with php option" ),
655 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
656 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
657 "Called eval.php --help --test with wrapper option" ),
658 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
659 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
660 "Called eval.php --help --test with wrapper and php option" ),
663 /* TODO: many more! */