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
|
<?php
// phpcs:disable MediaWiki.Commenting.FunctionComment.MissingParamTag -- Traits are not excluded
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\MainConfigNames;
const AUTONYMS = LanguageNameUtils::AUTONYMS;
const ALL = LanguageNameUtils::ALL;
const DEFINED = LanguageNameUtils::DEFINED;
const SUPPORTED = LanguageNameUtils::SUPPORTED;
/**
* @internal For LanguageNameUtilsTest and LanguageIntegrationTest.
*/
trait LanguageNameUtilsTestTrait {
abstract protected function isSupportedLanguage( $code );
/**
* @dataProvider provideIsSupportedLanguage
*/
public function testIsSupportedLanguage( $code, $expected ) {
$this->assertSame( $expected, $this->isSupportedLanguage( $code ) );
}
public static function provideIsSupportedLanguage() {
return [
'en' => [ 'en', true ],
'fi' => [ 'fi', true ],
'bunny' => [ 'bunny', false ],
'qqq' => [ 'qqq', false ],
'uppercase is not considered supported' => [ 'FI', false ],
];
}
abstract protected function isValidCode( $code );
/**
* We don't test that the result is cached, because that should only be noticeable if the
* configuration changes in between calls, and 1) that should never happen in normal operation,
* 2) if you do it you deserve whatever you get, and 3) once the static Language method is
* dropped and the invalid title regex is moved to something injected instead of a static call,
* the cache will be undetectable.
*
* @todo Should we test changes to $wgLegalTitleChars here? Does anybody actually change that?
* Is it possible to change it usefully without breaking everything?
*
* @dataProvider provideIsValidCode
* @param string $code
* @param bool $expected
*/
public function testIsValidCode( $code, $expected ) {
$this->assertSame( $expected, $this->isValidCode( $code ) );
}
public static function provideIsValidCode() {
$ret = [
'en' => [ 'en', true ],
'en-GB' => [ 'en-GB', true ],
'Funny chars' => [ "%!$()*,-.;=?@^_`~\x80\xA2\xFF+", true ],
'Percent escape not allowed' => [ 'a%aF', false ],
'Percent with only one following char is okay' => [ '%a', true ],
'Percent with non-hex following chars is okay' => [ '%AG', true ],
'Named char reference "a"' => [ 'a&a', false ],
'Named char reference "A"' => [ 'a&A', false ],
'Named char reference "0"' => [ 'a&0', false ],
'Named char reference non-ASCII' => [ "a&\x92", false ],
'Numeric char reference' => [ "a�", false ],
'Hex char reference 0' => [ "a�", false ],
'Hex char reference A' => [ "a
", false ],
'Lone ampersand is valid for title but not lang code' => [ '&', false ],
'Ampersand followed by just # is valid for title but not lang code' => [ '&#', false ],
'Ampersand followed by # and non-x/digit is valid for title but not lang code' =>
[ '&#a', false ],
];
$disallowedChars = ":/\\\000&<>'\"";
foreach ( str_split( $disallowedChars ) as $char ) {
$ret["Disallowed character $char"] = [ "a{$char}a", false ];
}
return $ret;
}
abstract protected function isValidBuiltInCode( $code );
/**
* @dataProvider provideIsValidBuiltInCode
* @param string $code
* @param bool $expected
*/
public function testIsValidBuiltInCode( $code, $expected ) {
$this->assertSame( $expected, $this->isValidBuiltInCode( $code ) );
}
public static function provideIsValidBuiltInCode() {
return [
'Two letters, lowercase' => [ 'fr', true ],
'Two letters, uppercase' => [ 'EN', false ],
'Three letters' => [ 'tyv', true ],
'With dash' => [ 'be-tarask', true ],
'With extension (two dashes)' => [ 'be-x-old', true ],
'Reject underscores' => [ 'be_tarask', false ],
'One letter' => [ 'a', false ],
'Only digits' => [ '00', true ],
'Only dashes' => [ '--', true ],
'Long' => [ str_repeat( 'x', 100 ), true ],
'Too long' => [ str_repeat( 'x', 200 ), false ],
'qqq' => [ 'qqq', true ],
];
}
abstract protected function isKnownLanguageTag( $code );
/**
* @dataProvider provideIsKnownLanguageTag
* @param string $code
* @param bool $expected
*/
public function testIsKnownLanguageTag( $code, $expected ) {
$this->assertSame( $expected, $this->isKnownLanguageTag( $code ) );
}
public static function provideIsKnownLanguageTag() {
$invalidBuiltInCodes = array_filter( static::provideIsValidBuiltInCode(),
static function ( $arr ) {
// If isValidBuiltInCode() returns false, we want to also, but if it returns true,
// we could still return false from isKnownLanguageTag(), so skip those.
return !$arr[1];
}
);
return array_merge( $invalidBuiltInCodes, [
'Simple code' => [ 'fr', true ],
'An MW legacy tag' => [ 'bat-smg', true ],
'An internal standard MW name, for which a legacy tag is used externally' =>
[ 'sgs', true ],
'Non-existent two-letter code' => [ 'mw', false ],
'Very invalid language code' => [ 'foo"<bar', false ],
] );
}
abstract protected function assertGetLanguageNames(
array $options, $expected, $code, ...$otherArgs
);
abstract protected function getLanguageNames( ...$args );
abstract protected function getLanguageName( ...$args );
/**
* @dataProvider provideGetLanguageNames
*/
public function testGetLanguageNames( $expected, $code, ...$otherArgs ) {
$this->clearLanguageHook( 'LanguageGetTranslatedLanguageNames' );
$this->assertGetLanguageNames( [], $expected, $code, ...$otherArgs );
}
public static function provideGetLanguageNames() {
// @todo There are probably lots of interesting tests to add here.
return [
'Simple code' => [ 'Deutsch', 'de' ],
'Simple code in a different language' => [ 'Deutsch', 'de', 'fr' ],
'Invalid code' => [ '', '&' ],
'Pig Latin' => [ 'Igpay Atinlay', 'en-x-piglatin', AUTONYMS, ALL ],
'qqq doesn\'t have a name' => [ '', 'qqq', AUTONYMS, ALL ],
'An MW legacy tag is recognized' => [ 'žemaitėška', 'bat-smg' ],
'An internal standard name, for which a legacy tag is used externally, is supported' =>
[ 'žemaitėška', 'sgs', AUTONYMS, SUPPORTED ],
];
}
/**
* Set hook for current test.
* @param string $hookName
* @param mixed $handler Value suitable for a hook handler
*/
abstract protected function setLanguageTemporaryHook( string $hookName, $handler ): void;
/**
* Clear hook for the current test.
*/
abstract protected function clearLanguageHook( string $hookName ): void;
/**
* @dataProvider provideGetLanguageNames_withHook
*/
public function testGetLanguageNames_withHook( $expected, $code, ...$otherArgs ) {
$this->setLanguageTemporaryHook( 'LanguageGetTranslatedLanguageNames',
static function ( &$names, $inLanguage ) {
switch ( $inLanguage ) {
case 'de':
$names = [
'de' => 'Deutsch',
'en' => 'Englisch',
'fr' => 'Französisch',
];
break;
case 'en':
$names = [
'de' => 'German',
'en' => 'English',
'fr' => 'French',
'sqsqsqsq' => '!!?!',
'bat-smg' => 'Samogitian',
];
break;
case 'fr':
$names = [
'de' => 'allemand',
'en' => 'anglais',
// Deliberate mistake (no cedilla)
'fr' => 'francais',
];
break;
}
}
);
// Really we could dispense with assertGetLanguageNames() and just call
// testGetLanguageNames() here, but it looks weird to call a test method from another test
// method.
$this->assertGetLanguageNames( [], $expected, $code, ...$otherArgs );
}
public static function provideGetLanguageNames_withHook() {
return [
'Simple code in a different language' => [ 'allemand', 'de', 'fr' ],
'Invalid inLanguage defaults to English' => [ 'German', 'de', '&' ],
'If inLanguage not provided, default to autonym' => [ 'Deutsch', 'de' ],
'Hooks ignored for explicitly-requested autonym' => [ 'français', 'fr', 'fr' ],
'Hooks don\'t make a language supported' => [ '', 'es-419', 'en', SUPPORTED ],
'Hooks don\'t make a language defined' => [ '', 'sqsqsqsq', 'en', DEFINED ],
'Hooks do make a language name returned with ALL' => [ '!!?!', 'sqsqsqsq', 'en', ALL ],
];
}
/**
* @dataProvider provideGetLanguageNames_ExtraLanguageNames
*/
public function testGetLanguageNames_ExtraLanguageNames( $expected, $code, ...$otherArgs ) {
$this->setLanguageTemporaryHook( 'LanguageGetTranslatedLanguageNames',
static function ( &$names ) {
$names['de'] = 'die deutsche Sprache';
}
);
$this->assertGetLanguageNames(
[ MainConfigNames::ExtraLanguageNames => [ 'de' => 'deutsche Sprache', 'sqsqsqsq' => '!!?!' ] ],
$expected, $code, ...$otherArgs
);
}
public static function provideGetLanguageNames_ExtraLanguageNames() {
return [
'Simple extra language name' => [ '!!?!', 'sqsqsqsq' ],
'Extra language is defined' => [ '!!?!', 'sqsqsqsq', AUTONYMS, DEFINED ],
'Extra language is not supported' => [ '', 'sqsqsqsq', AUTONYMS, SUPPORTED ],
'Extra language overrides default' => [ 'deutsche Sprache', 'de' ],
'Extra language overrides hook for explicitly requested autonym' =>
[ 'deutsche Sprache', 'de', 'de' ],
'Hook overrides extra language for non-autonym' =>
[ 'die deutsche Sprache', 'de', 'fr' ],
];
}
/**
* Test that getLanguageNames() defaults to DEFINED, and getLanguageName() defaults to ALL.
*/
public function testGetLanguageNames_parameterDefault() {
$this->setLanguageTemporaryHook( 'LanguageGetTranslatedLanguageNames',
static function ( &$names ) {
$names = [ 'sqsqsqsq' => '!!?!' ];
}
);
// We use 'en' here because the hook is not run if we're requesting autonyms, although in
// this case (language that isn't defined by MediaWiki itself) that behavior seems wrong.
$this->assertArrayNotHasKey( 'sqsqsqsq', $this->getLanguageNames(), 'en' );
$this->assertSame( '!!?!', $this->getLanguageName( 'sqsqsqsq', 'en' ) );
}
/**
* @dataProvider provideGetLanguageNames_sorted
*/
public function testGetLanguageNames_sorted( ...$args ) {
$names = $this->getLanguageNames( ...$args );
$sortedNames = $names;
ksort( $sortedNames );
$this->assertSame( $sortedNames, $names );
}
public static function provideGetLanguageNames_sorted() {
return [
[],
[ AUTONYMS ],
[ AUTONYMS, DEFINED ],
[ AUTONYMS, ALL ],
[ AUTONYMS, SUPPORTED ],
[ 'he', DEFINED ],
[ 'he', ALL ],
[ 'he', SUPPORTED ],
];
}
public function testGetLanguageNames_hookNotCalledForAutonyms() {
$count = 0;
$this->setLanguageTemporaryHook( 'LanguageGetTranslatedLanguageNames',
static function () use ( &$count ) {
$count++;
}
);
$this->getLanguageNames();
$this->assertSame( 0, $count, 'Hook must not be called for autonyms' );
// We test elsewhere that the hook works, but the following verifies that our test is
// working and $count isn't being incremented above only because we're checking autonyms.
$this->getLanguageNames( 'fr' );
$this->assertSame( 1, $count, 'Hook must be called for non-autonyms' );
}
/**
* @dataProvider provideGetLanguageNames_pigLatin
*/
public function testGetLanguageNames_pigLatin( $expected, ...$otherArgs ) {
$this->setLanguageTemporaryHook( 'LanguageGetTranslatedLanguageNames',
static function ( &$names, $inLanguage ) {
switch ( $inLanguage ) {
case 'fr':
$names = [ 'en-x-piglatin' => 'latin de cochons' ];
break;
case 'en-x-piglatin':
// Deliberately lowercase
$names = [ 'en-x-piglatin' => 'igpay atinlay' ];
break;
}
}
);
$this->assertGetLanguageNames(
[ MainConfigNames::UsePigLatinVariant => true ], $expected, 'en-x-piglatin', ...$otherArgs );
}
public static function provideGetLanguageNames_pigLatin() {
# Pig Latin is supported only if UsePigLatinVariant is true
# (which it is, for these tests)
return [
'Simple test' => [ 'Igpay Atinlay' ],
'Supported' => [ 'Igpay Atinlay', AUTONYMS, SUPPORTED ],
'Foreign language' => [ 'latin de cochons', 'fr' ],
'Hook doesn\'t override explicit autonym' =>
[ 'Igpay Atinlay', 'en-x-piglatin', 'en-x-piglatin' ],
];
}
public function testGetLanguageNames_pigLatinNotSupported() {
// Pig Latin is "not supported" when UsePigLatinVariant is false
$this->assertGetLanguageNames(
[ MainConfigNames::UsePigLatinVariant => false ],
'', 'en-x-piglatin', AUTONYMS, SUPPORTED
);
}
/**
* Just for the sake of completeness, test that ExtraLanguageNames
* can override the name for Pig Latin. Nobody actually cares
* about this, but once we're testing the whole file we may as
* well be comprehensive.
*/
public function testGetLanguageNames_pigLatinAndExtraLanguageNames() {
$this->assertGetLanguageNames(
[
MainConfigNames::UsePigLatinVariant => true,
MainConfigNames::ExtraLanguageNames => [ 'en-x-piglatin' => 'igpay atinlay' ]
],
'igpay atinlay',
'en-x-piglatin'
);
}
public function testGetLanguageNames_xss(): void {
// not supported if disabled
$this->assertGetLanguageNames(
[
MainConfigNames::UseXssLanguage => false,
],
'',
'x-xss'
);
// supported if enabled
$this->assertGetLanguageNames(
[
MainConfigNames::UseXssLanguage => true,
],
'fake xss language (see $wgUseXssLanguage)',
'x-xss'
);
}
abstract protected function getFileName( ...$args );
/**
* @dataProvider provideGetFileName
*/
public function testGetFileName( $expected, ...$args ) {
$this->assertSame( $expected, $this->getFileName( ...$args ) );
}
public static function provideGetFileName() {
return [
'Simple case' => [ 'MessagesXx.php', 'Messages', 'xx' ],
'With extension' => [ 'MessagesXx.ext', 'Messages', 'xx', '.ext' ],
'Replacing dashes' => [ '!__?', '!', '--', '?' ],
'Empty prefix and extension' => [ 'Xx', '', 'xx', '' ],
'Uppercase only first letter' => [ 'Messages_a.php', 'Messages', '-a' ],
];
}
abstract protected function getMessagesFileName( $code );
/**
* @dataProvider provideGetMessagesFileName
* @param string $code
* @param string $expected
*/
public function testGetMessagesFileName( $code, $expected ) {
$this->assertSame( $expected, $this->getMessagesFileName( $code ) );
}
public static function provideGetMessagesFileName() {
global $IP;
return [
'Simple case' => [ 'en', "$IP/languages/messages/MessagesEn.php" ],
'Replacing dashes' => [ '--', "$IP/languages/messages/Messages__.php" ],
'Uppercase only first letter' => [ '-a', "$IP/languages/messages/Messages_a.php" ],
];
}
public function testGetMessagesFileName_withHook() {
$called = 0;
$this->setLanguageTemporaryHook( 'Language::getMessagesFileName',
function ( $code, &$file ) use ( &$called ) {
global $IP;
$called++;
$this->assertSame( 'ab-cd', $code );
$this->assertSame( "$IP/languages/messages/MessagesAb_cd.php", $file );
$file = 'bye-bye';
}
);
$this->assertSame( 'bye-bye', $this->getMessagesFileName( 'ab-cd' ) );
$this->assertSame( 1, $called );
}
abstract protected function getJsonMessagesFileName( $code );
public function testGetJsonMessagesFileName() {
global $IP;
// Not so much to test here, one test seems to be enough
$expected = "$IP/languages/i18n/en--123.json";
$this->assertSame( $expected, $this->getJsonMessagesFileName( 'en--123' ) );
}
/**
* getFileName, getMessagesFileName, and getJsonMessagesFileName all throw if they get an
* invalid code. To save boilerplate, test them all in one method.
*
* @dataProvider provideExceptionFromInvalidCode
* @param callable $callback Will throw when passed $code
* @param string $code
*/
public function testExceptionFromInvalidCode( $callback, $code ) {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Invalid language code \"$code\"" );
$callback( $code );
}
public function provideExceptionFromInvalidCode() {
$ret = [];
foreach ( static::provideIsValidBuiltInCode() as $desc => [ $code, $valid ] ) {
if ( $valid ) {
// Won't get an exception from this one
continue;
}
// For getFileName, we define an anonymous function because of the extra first param
$ret["getFileName: $desc"] = [
function ( $code ) {
return $this->getFileName( 'Messages', $code );
},
$code
];
$ret["getMessagesFileName: $desc"] =
[ [ $this, 'getMessagesFileName' ], $code ];
$ret["getJsonMessagesFileName: $desc"] =
[ [ $this, 'getJsonMessagesFileName' ], $code ];
}
return $ret;
}
}
|