1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
<?php
/**
* @group Title
*/
class TitleTest extends MediaWikiTestCase {
protected function setUp() {
parent::setUp();
$this->setMwGlobals( array(
'wgLanguageCode' => 'en',
'wgContLang' => Language::factory( 'en' ),
// User language
'wgLang' => Language::factory( 'en' ),
'wgAllowUserJs' => false,
'wgDefaultLanguageVariant' => false,
'wgMetaNamespace' => 'Project',
) );
}
/**
* @covers Title::legalChars
*/
public function testLegalChars() {
$titlechars = Title::legalChars();
foreach ( range( 1, 255 ) as $num ) {
$chr = chr( $num );
if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
$this->assertFalse(
(bool)preg_match( "/[$titlechars]/", $chr ),
"chr($num) = $chr is not a valid titlechar"
);
} else {
$this->assertTrue(
(bool)preg_match( "/[$titlechars]/", $chr ),
"chr($num) = $chr is a valid titlechar"
);
}
}
}
public static function provideValidSecureAndSplit() {
return array(
array( 'Sandbox' ),
array( 'A "B"' ),
array( 'A \'B\'' ),
array( '.com' ),
array( '~' ),
array( '#' ),
array( '"' ),
array( '\'' ),
array( 'Talk:Sandbox' ),
array( 'Talk:Foo:Sandbox' ),
array( 'File:Example.svg' ),
array( 'File_talk:Example.svg' ),
array( 'Foo/.../Sandbox' ),
array( 'Sandbox/...' ),
array( 'A~~' ),
array( ':A' ),
// Length is 256 total, but only title part matters
array( 'Category:' . str_repeat( 'x', 248 ) ),
array( str_repeat( 'x', 252 ) ),
// interwiki prefix
array( 'localtestiw: #anchor' ),
array( 'localtestiw:' ),
array( 'localtestiw:foo' ),
array( 'localtestiw: foo # anchor' ),
array( 'localtestiw: Talk: Sandbox # anchor' ),
array( 'remotetestiw:' ),
array( 'remotetestiw: Talk: # anchor' ),
array( 'remotetestiw: #bar' ),
array( 'remotetestiw: Talk:' ),
array( 'remotetestiw: Talk: Foo' ),
array( 'localtestiw:remotetestiw:' ),
array( 'localtestiw:remotetestiw:foo' )
);
}
public static function provideInvalidSecureAndSplit() {
return array(
array( '' ),
array( ':' ),
array( '__ __' ),
array( ' __ ' ),
// Bad characters forbidden regardless of wgLegalTitleChars
array( 'A [ B' ),
array( 'A ] B' ),
array( 'A { B' ),
array( 'A } B' ),
array( 'A < B' ),
array( 'A > B' ),
array( 'A | B' ),
// URL encoding
array( 'A%20B' ),
array( 'A%23B' ),
array( 'A%2523B' ),
// XML/HTML character entity references
// Note: Commented out because they are not marked invalid by the PHP test as
// Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
//'A é B',
//'A é B',
//'A é B',
// Subject of NS_TALK does not roundtrip to NS_MAIN
array( 'Talk:File:Example.svg' ),
// Directory navigation
array( '.' ),
array( '..' ),
array( './Sandbox' ),
array( '../Sandbox' ),
array( 'Foo/./Sandbox' ),
array( 'Foo/../Sandbox' ),
array( 'Sandbox/.' ),
array( 'Sandbox/..' ),
// Tilde
array( 'A ~~~ Name' ),
array( 'A ~~~~ Signature' ),
array( 'A ~~~~~ Timestamp' ),
array( str_repeat( 'x', 256 ) ),
// Namespace prefix without actual title
array( 'Talk:' ),
array( 'Talk:#' ),
array( 'Category: ' ),
array( 'Category: #bar' ),
// interwiki prefix
array( 'localtestiw: Talk: # anchor' ),
array( 'localtestiw: Talk:' )
);
}
private function secureAndSplitGlobals() {
$this->setMwGlobals( array(
'wgLocalInterwikis' => array( 'localtestiw' ),
'wgHooks' => array(
'InterwikiLoadPrefix' => array(
function ( $prefix, &$data ) {
if ( $prefix === 'localtestiw' ) {
$data = array( 'iw_url' => 'localtestiw' );
} elseif ( $prefix === 'remotetestiw' ) {
$data = array( 'iw_url' => 'remotetestiw' );
}
return false;
}
)
)
));
}
/**
* See also mediawiki.Title.test.js
* @covers Title::secureAndSplit
* @dataProvider provideValidSecureAndSplit
* @note This mainly tests MediaWikiTitleCodec::parseTitle().
*/
public function testSecureAndSplitValid( $text ) {
$this->secureAndSplitGlobals();
$this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
}
/**
* See also mediawiki.Title.test.js
* @covers Title::secureAndSplit
* @dataProvider provideInvalidSecureAndSplit
* @note This mainly tests MediaWikiTitleCodec::parseTitle().
*/
public function testSecureAndSplitInvalid( $text ) {
$this->secureAndSplitGlobals();
$this->assertNull( Title::newFromText( $text ), "Invalid: $text" );
}
public static function provideConvertByteClassToUnicodeClass() {
return array(
array(
' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
),
array(
'QWERTYf-\\xFF+',
'QWERTYf-\\x7F+\\u0080-\\uFFFF',
),
array(
'QWERTY\\x66-\\xFD+',
'QWERTYf-\\x7F+\\u0080-\\uFFFF',
),
array(
'QWERTYf-y+',
'QWERTYf-y+',
),
array(
'QWERTYf-\\x80+',
'QWERTYf-\\x7F+\\u0080-\\uFFFF',
),
array(
'QWERTY\\x66-\\x80+\\x23',
'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
),
array(
'QWERTY\\x66-\\x80+\\xD3',
'QWERTYf-\\x7F+\\u0080-\\uFFFF',
),
array(
'\\\\\\x99',
'\\\\\\u0080-\\uFFFF',
),
array(
'-\\x99',
'\\-\\u0080-\\uFFFF',
),
array(
'QWERTY\\-\\x99',
'QWERTY\\-\\u0080-\\uFFFF',
),
array(
'\\\\x99',
'\\\\x99',
),
array(
'A-\\x9F',
'A-\\x7F\\u0080-\\uFFFF',
),
array(
'\\x66-\\x77QWERTY\\x88-\\x91FXZ',
'f-wQWERTYFXZ\\u0080-\\uFFFF',
),
array(
'\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
),
);
}
/**
* @dataProvider provideConvertByteClassToUnicodeClass
* @covers Title::convertByteClassToUnicodeClass
*/
public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
$this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
}
/**
* @dataProvider provideSpecialNamesWithAndWithoutParameter
* @covers Title::fixSpecialName
*/
public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
$title = Title::newFromText( $text );
$fixed = $title->fixSpecialName();
$stuff = explode( '/', $fixed->getDBkey(), 2 );
if ( count( $stuff ) == 2 ) {
$par = $stuff[1];
} else {
$par = null;
}
$this->assertEquals(
$expectedParam,
$par,
"Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
);
}
public static function provideSpecialNamesWithAndWithoutParameter() {
return array(
array( 'Special:Version', null ),
array( 'Special:Version/', '' ),
array( 'Special:Version/param', 'param' ),
);
}
/**
* Auth-less test of Title::isValidMoveOperation
*
* @group Database
* @param string $source
* @param string $target
* @param array|string|bool $expected Required error
* @dataProvider provideTestIsValidMoveOperation
* @covers Title::isValidMoveOperation
* @covers Title::validateFileMoveOperation
*/
public function testIsValidMoveOperation( $source, $target, $expected ) {
$this->setMwGlobals( 'wgContentHandlerUseDB', false );
$title = Title::newFromText( $source );
$nt = Title::newFromText( $target );
$errors = $title->isValidMoveOperation( $nt, false );
if ( $expected === true ) {
$this->assertTrue( $errors );
} else {
$errors = $this->flattenErrorsArray( $errors );
foreach ( (array)$expected as $error ) {
$this->assertContains( $error, $errors );
}
}
}
public static function provideTestIsValidMoveOperation() {
return array(
// for Title::isValidMoveOperation
array( 'Some page', '', 'badtitletext' ),
array( 'Test', 'Test', 'selfmove' ),
array( 'Special:FooBar', 'Test', 'immobile-source-namespace' ),
array( 'Test', 'Special:FooBar', 'immobile-target-namespace' ),
array( 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ),
array( 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ),
// for Title::validateFileMoveOperation
array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ),
);
}
/**
* Auth-less test of Title::userCan
*
* @param array $whitelistRegexp
* @param string $source
* @param string $action
* @param array|string|bool $expected Required error
*
* @covers Title::checkReadPermissions
* @dataProvider dataWgWhitelistReadRegexp
*/
public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
// $wgWhitelistReadRegexp must be an array. Since the provided test cases
// usually have only one regex, it is more concise to write the lonely regex
// as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
// type requisite.
if ( is_string( $whitelistRegexp ) ) {
$whitelistRegexp = array( $whitelistRegexp );
}
$this->setMwGlobals( array(
// So User::isEveryoneAllowed( 'read' ) === false
'wgGroupPermissions' => array( '*' => array( 'read' => false ) ),
'wgWhitelistRead' => array( 'some random non sense title' ),
'wgWhitelistReadRegexp' => $whitelistRegexp,
) );
$title = Title::newFromDBkey( $source );
// New anonymous user with no rights
$user = new User;
$user->mRights = array();
$errors = $title->userCan( $action, $user );
if ( is_bool( $expected ) ) {
# Forge the assertion message depending on the assertion expectation
$allowableness = $expected
? " should be allowed"
: " should NOT be allowed";
$this->assertEquals(
$expected,
$errors,
"User action '$action' on [[$source]] $allowableness."
);
} else {
$errors = $this->flattenErrorsArray( $errors );
foreach ( (array)$expected as $error ) {
$this->assertContains( $error, $errors );
}
}
}
/**
* Provides test parameter values for testWgWhitelistReadRegexp()
*/
public function dataWgWhitelistReadRegexp() {
$ALLOWED = true;
$DISALLOWED = false;
return array(
// Everything, if this doesn't work, we're really in trouble
array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
// We validate against the title name, not the db key
array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
// Main page
array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
// With spaces
array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
// Unicode multibyte
// ...without unicode modifier
array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
// ...with unicode modifier
array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
// Case insensitive
array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
// From DefaultSettings.php:
array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
// With namespaces:
array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
array( null, 'Special:Newpages', 'read', $DISALLOWED ),
);
}
public function flattenErrorsArray( $errors ) {
$result = array();
foreach ( $errors as $error ) {
$result[] = $error[0];
}
return $result;
}
/**
* @dataProvider provideGetPageViewLanguage
* @covers Title::getPageViewLanguage
*/
public function testGetPageViewLanguage( $expected, $titleText, $contLang,
$lang, $variant, $msg = ''
) {
// Setup environnement for this test
$this->setMwGlobals( array(
'wgLanguageCode' => $contLang,
'wgContLang' => Language::factory( $contLang ),
'wgLang' => Language::factory( $lang ),
'wgDefaultLanguageVariant' => $variant,
'wgAllowUserJs' => true,
) );
$title = Title::newFromText( $titleText );
$this->assertInstanceOf( 'Title', $title,
"Test must be passed a valid title text, you gave '$titleText'"
);
$this->assertEquals( $expected,
$title->getPageViewLanguage()->getCode(),
$msg
);
}
public static function provideGetPageViewLanguage() {
# Format:
# - expected
# - Title name
# - wgContLang (expected in most case)
# - wgLang (on some specific pages)
# - wgDefaultLanguageVariant
# - Optional message
return array(
array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
);
}
/**
* @dataProvider provideBaseTitleCases
* @covers Title::getBaseText
*/
public function testGetBaseText( $title, $expected, $msg = '' ) {
$title = Title::newFromText( $title );
$this->assertEquals( $expected,
$title->getBaseText(),
$msg
);
}
public static function provideBaseTitleCases() {
return array(
# Title, expected base, optional message
array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
);
}
/**
* @dataProvider provideRootTitleCases
* @covers Title::getRootText
*/
public function testGetRootText( $title, $expected, $msg = '' ) {
$title = Title::newFromText( $title );
$this->assertEquals( $expected,
$title->getRootText(),
$msg
);
}
public static function provideRootTitleCases() {
return array(
# Title, expected base, optional message
array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
array( 'User:Foo/Bar/Baz', 'Foo' ),
);
}
/**
* @todo Handle $wgNamespacesWithSubpages cases
* @dataProvider provideSubpageTitleCases
* @covers Title::getSubpageText
*/
public function testGetSubpageText( $title, $expected, $msg = '' ) {
$title = Title::newFromText( $title );
$this->assertEquals( $expected,
$title->getSubpageText(),
$msg
);
}
public static function provideSubpageTitleCases() {
return array(
# Title, expected base, optional message
array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
array( 'User:John_Doe/subOne', 'subOne' ),
);
}
public static function provideNewFromTitleValue() {
return array(
array( new TitleValue( NS_MAIN, 'Foo' ) ),
array( new TitleValue( NS_MAIN, 'Foo', 'bar' ) ),
array( new TitleValue( NS_USER, 'Hansi_Maier' ) ),
);
}
/**
* @dataProvider provideNewFromTitleValue
*/
public function testNewFromTitleValue( TitleValue $value ) {
$title = Title::newFromTitleValue( $value );
$dbkey = str_replace( ' ', '_', $value->getText() );
$this->assertEquals( $dbkey, $title->getDBkey() );
$this->assertEquals( $value->getNamespace(), $title->getNamespace() );
$this->assertEquals( $value->getFragment(), $title->getFragment() );
}
public static function provideGetTitleValue() {
return array(
array( 'Foo' ),
array( 'Foo#bar' ),
array( 'User:Hansi_Maier' ),
);
}
/**
* @dataProvider provideGetTitleValue
*/
public function testGetTitleValue( $text ) {
$title = Title::newFromText( $text );
$value = $title->getTitleValue();
$dbkey = str_replace( ' ', '_', $value->getText() );
$this->assertEquals( $title->getDBkey(), $dbkey );
$this->assertEquals( $title->getNamespace(), $value->getNamespace() );
$this->assertEquals( $title->getFragment(), $value->getFragment() );
}
public static function provideGetFragment() {
return array(
array( 'Foo', '' ),
array( 'Foo#bar', 'bar' ),
array( 'Foo#bär', 'bär' ),
// Inner whitespace is normalized
array( 'Foo#bar_bar', 'bar bar' ),
array( 'Foo#bar bar', 'bar bar' ),
array( 'Foo#bar bar', 'bar bar' ),
// Leading whitespace is kept, trailing whitespace is trimmed.
// XXX: Is this really want we want?
array( 'Foo#_bar_bar_', ' bar bar' ),
array( 'Foo# bar bar ', ' bar bar' ),
);
}
/**
* @dataProvider provideGetFragment
*
* @param string $full
* @param string $fragment
*/
public function testGetFragment( $full, $fragment ) {
$title = Title::newFromText( $full );
$this->assertEquals( $fragment, $title->getFragment() );
}
/**
* @covers Title::isAlwaysKnown
* @dataProvider provideIsAlwaysKnown
* @param string $page
* @param bool $isKnown
*/
public function testIsAlwaysKnown( $page, $isKnown ) {
$title = Title::newFromText( $page );
$this->assertEquals( $isKnown, $title->isAlwaysKnown() );
}
public static function provideIsAlwaysKnown() {
return array(
array( 'Some nonexistent page', false ),
array( 'UTPage', false ),
array( '#test', true ),
array( 'Special:BlankPage', true ),
array( 'Special:SomeNonexistentSpecialPage', false ),
array( 'MediaWiki:Parentheses', true ),
array( 'MediaWiki:Some nonexistent message', false ),
);
}
/**
* @covers Title::isAlwaysKnown
*/
public function testIsAlwaysKnownOnInterwiki() {
$title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
$this->assertTrue( $title->isAlwaysKnown() );
}
}
|