Debian Codegraph @

tests/phpunit/unit/includes/language/LanguageCodeTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Language\Language;
4 use MediaWiki\Language\LanguageCode;
5 use MediaWiki\Languages\LanguageNameUtils;
6 use MediaWiki\Tests\Unit\DummyServicesTrait;
7 use Wikimedia\Bcp47Code\Bcp47CodeValue;
8
9 /**
10 * @group Language
11 * @covers \MediaWiki\Language\LanguageCode
12 *
13 * @author Thiemo Kreuz
14 */
15 class LanguageCodeTest extends MediaWikiUnitTestCase {
16 use DummyServicesTrait;
17
18 public function testConstructor() {
19 $instance = new LanguageCode( 'fa' );
20
21 $this->assertInstanceOf( LanguageCode::class, $instance );
22 $this->assertSame( 'fa', $instance->toString() );
23 }
24
25 public function testGetDeprecatedCodeMapping() {
26 $map = LanguageCode::getDeprecatedCodeMapping();
27
28 $this->assertIsArray( $map );
29 $this->assertContainsOnly( 'string', array_keys( $map ) );
30 $this->assertArrayNotHasKey( '', $map );
31 $this->assertContainsOnly( 'string', $map );
32 $this->assertNotContains( '', $map );
33
34 // Codes special to MediaWiki should never appear in a map of "deprecated" codes
35 $this->assertArrayNotHasKey( 'qqq', $map, 'documentation' );
36 $this->assertNotContains( 'qqq', $map, 'documentation' );
37 $this->assertArrayNotHasKey( 'qqx', $map, 'debug code' );
38 $this->assertNotContains( 'qqx', $map, 'debug code' );
39
40 // Valid language codes that are currently not "deprecated"
41 $this->assertArrayNotHasKey( 'bh', $map, 'family of Bihari languages' );
42 $this->assertArrayNotHasKey( 'no', $map, 'family of Norwegian languages' );
43 $this->assertArrayNotHasKey( 'simple', $map );
44 }
45
46 public function testReplaceDeprecatedCodes() {
47 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'als' ) );
48 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'gsw' ) );
49 $this->assertEquals( 'ja', LanguageCode::replaceDeprecatedCodes( 'ja' ) );
50 }
51
52 /**
53 * test @see LanguageCode::bcp47().
54 * Please note the BCP 47 explicitly state that language codes are case
55 * insensitive, there are some exceptions to the rule :)
56 * This test is used to verify our formatting against all lower and
57 * all upper cases language code.
58 *
59 * @see https://tools.ietf.org/html/bcp47
60 * @dataProvider provideLanguageCodes()
61 */
62 public function testBcp47( $code, $expected ) {
63 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
64 "Applying BCP 47 standard to '$code'"
65 );
66
67 $code = strtolower( $code );
68 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
69 "Applying BCP 47 standard to lower case '$code'"
70 );
71
72 $code = strtoupper( $code );
73 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
74 "Applying BCP 47 standard to upper case '$code'"
75 );
76 }
77
78 /**
79 * Array format is ($code, $expected)
80 */
81 public static function provideLanguageCodes() {
82 return [
83 // Extracted from BCP 47 (list not exhaustive)
84 # 2.1.1
85 [ 'en-ca-x-ca', 'en-CA-x-ca' ],
86 [ 'sgn-be-fr', 'sgn-BE-FR' ],
87 [ 'az-latn-x-latn', 'az-Latn-x-latn' ],
88 # 2.2
89 [ 'sr-Latn-RS', 'sr-Latn-RS' ],
90 [ 'az-arab-ir', 'az-Arab-IR' ],
91
92 # 2.2.5
93 [ 'sl-nedis', 'sl-nedis' ],
94 [ 'de-ch-1996', 'de-CH-1996' ],
95
96 # 2.2.6
97 [
98 'en-latn-gb-boont-r-extended-sequence-x-private',
99 'en-Latn-GB-boont-r-extended-sequence-x-private'
100 ],
101
102 // Examples from BCP 47 Appendix A
103 # Simple language subtag:
104 [ 'DE', 'de' ],
105 [ 'fR', 'fr' ],
106 [ 'ja', 'ja' ],
107
108 # Language subtag plus script subtag:
109 [ 'zh-hans', 'zh-Hans' ],
110 [ 'sr-cyrl', 'sr-Cyrl' ],
111 [ 'sr-latn', 'sr-Latn' ],
112
113 # Extended language subtags and their primary language subtag
114 # counterparts:
115 [ 'zh-cmn-hans-cn', 'zh-cmn-Hans-CN' ],
116 [ 'cmn-hans-cn', 'cmn-Hans-CN' ],
117 [ 'zh-yue-hk', 'zh-yue-HK' ],
118 [ 'yue-hk', 'yue-HK' ],
119
120 # Language-Script-Region:
121 [ 'zh-hans-cn', 'zh-Hans-CN' ],
122 [ 'sr-latn-RS', 'sr-Latn-RS' ],
123
124 # Language-Variant:
125 [ 'sl-rozaj', 'sl-rozaj' ],
126 [ 'sl-rozaj-biske', 'sl-rozaj-biske' ],
127 [ 'sl-nedis', 'sl-nedis' ],
128
129 # Language-Region-Variant:
130 [ 'de-ch-1901', 'de-CH-1901' ],
131 [ 'sl-it-nedis', 'sl-IT-nedis' ],
132
133 # Language-Script-Region-Variant:
134 [ 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ],
135
136 # Language-Region:
137 [ 'de-de', 'de-DE' ],
138 [ 'en-us', 'en-US' ],
139 [ 'es-419', 'es-419' ],
140
141 # Private use subtags:
142 [ 'de-ch-x-phonebk', 'de-CH-x-phonebk' ],
143 [ 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ],
144 /**
145 * Previous test does not reflect the BCP 47 which states:
146 * az-Arab-x-AZE-derbend
147 * AZE being private, it should be lower case, hence the test above
148 * should probably be:
149 * [ 'az-arab-x-aze-derbend', 'az-Arab-x-AZE-derbend' ],
150 */
151
152 # Private use registry values:
153 [ 'x-whatever', 'x-whatever' ],
154 [ 'qaa-qaaa-qm-x-southern', 'qaa-Qaaa-QM-x-southern' ],
155 [ 'de-qaaa', 'de-Qaaa' ],
156 [ 'sr-latn-qm', 'sr-Latn-QM' ],
157 [ 'sr-qaaa-rs', 'sr-Qaaa-RS' ],
158
159 # Tags that use extensions
160 [ 'en-us-u-islamcal', 'en-US-u-islamcal' ],
161 [ 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ],
162 [ 'en-a-myext-b-another', 'en-a-myext-b-another' ],
163
164 # Invalid:
165 // de-419-DE
166 // a-DE
167 // ar-a-aaa-b-bbb-a-ccc
168
169 # Non-standard and deprecated language codes used by MediaWiki
170 [ 'als', 'gsw' ],
171 [ 'bat-smg', 'sgs' ],
172 [ 'be-x-old', 'be-tarask' ],
173 [ 'fiu-vro', 'vro' ],
174 [ 'roa-rup', 'rup' ],
175 [ 'zh-classical', 'lzh' ],
176 [ 'zh-min-nan', 'nan' ],
177 [ 'zh-yue', 'yue' ],
178 [ 'cbk-zam', 'cbk' ],
179 [ 'de-formal', 'de-x-formal' ],
180 [ 'eml', 'egl' ],
181 [ 'en-rtl', 'en-x-rtl' ],
182 [ 'es-formal', 'es-x-formal' ],
183 [ 'hu-formal', 'hu-x-formal' ],
184 [ 'kk-Arab', 'kk-Arab' ],
185 [ 'kk-Cyrl', 'kk-Cyrl' ],
186 [ 'kk-Latn', 'kk-Latn' ],
187 [ 'map-bms', 'jv-x-bms' ],
188 [ 'mo', 'ro-Cyrl-MD' ],
189 [ 'nrm', 'nrf' ],
190 [ 'nl-informal', 'nl-x-informal' ],
191 [ 'roa-tara', 'nap-x-tara' ],
192 [ 'simple', 'en-simple' ],
193 [ 'sr-ec', 'sr-Cyrl' ],
194 [ 'sr-el', 'sr-Latn' ],
195 [ 'zh-cn', 'zh-Hans-CN' ],
196 [ 'zh-sg', 'zh-Hans-SG' ],
197 [ 'zh-my', 'zh-Hans-MY' ],
198 [ 'zh-tw', 'zh-Hant-TW' ],
199 [ 'zh-hk', 'zh-Hant-HK' ],
200 [ 'zh-mo', 'zh-Hant-MO' ],
201 [ 'zh-hans', 'zh-Hans' ],
202 [ 'zh-hant', 'zh-Hant' ],
203 ];
204 }
205
206 /**
207 * @dataProvider provideBcp47ToInternal()
208 */
209 public function testBcp47ToInternal( $expected, $bcp47 ) {
210 $result = LanguageCode::bcp47ToInternal( $bcp47 );
211 $this->assertEquals( $expected, $result );
212 }
213
214 /**
215 * @dataProvider provideSupportedLanguageCodes()
216 */
217 public function testBcp47ToInternalLanguage( $internalCode ) {
218 if ( $internalCode === 'egl' ) {
219 # 'egl' was added as an internal code prematurely; 'eml' hasn't
220 # been added to the deprecated list yet (T36217) and so only
221 # 'eml' is a "real" internal code.
222 $internalCode = 'eml';
223 }
224 $lang = $this->createMock( Language::class );
225 $lang->method( 'getCode' )->willReturn( $internalCode );
226 $result = LanguageCode::bcp47ToInternal( $lang );
227 $this->assertEquals( $internalCode, $result );
228 }
229
230 public function provideSupportedLanguageCodes() {
231 $lnu = $this->getDummyLanguageNameUtils();
232 $languages = $lnu->getLanguageNames(
233 LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED
234 );
235 foreach ( $languages as $code => $autonym ) {
236 yield [ $code ];
237 }
238 }
239
240 public function provideBcp47ToInternal() {
241 foreach ( $this->provideSupportedLanguageCodes() as $args ) {
242 $code = $args[0];
243 if ( $code === 'egl' ) {
244 # 'egl' was added as an internal code prematurely; 'eml' hasn't
245 # been added to the deprecated list yet (T36217) and so only
246 # 'eml' is a "real" internal code.
247 continue;
248 }
249 $bcp47 = LanguageCode::bcp47( $code );
250 yield "$code as string" => [ $code, $bcp47 ];
251 yield "$code as Bcp47Code object" => [ $code, new Bcp47CodeValue( $bcp47 ) ];
252 // Verify case-insensitivity: lowercase
253 $bcp47 = strtolower( $bcp47 );
254 yield "$code as lowercase string" => [ $code, $bcp47 ];
255 yield "$code as lowercase Bcp47Code object" => [ $code, new Bcp47CodeValue( $bcp47 ) ];
256 // Verify case-insensitivity: uppercase
257 $bcp47 = strtoupper( $bcp47 );
258 yield "$code as uppercase string" => [ $code, $bcp47 ];
259 yield "$code as uppercase Bcp47Code object" => [ $code, new Bcp47CodeValue( $bcp47 ) ];
260 }
261 }
262
263 /**
264 * @dataProvider provideWellFormedLanguageTags
265 */
266 public function testWellFormedLanguageTag( $code, $message = '' ) {
267 $this->assertTrue(
268 LanguageCode::isWellFormedLanguageTag( $code ),
269 "validating code $code $message"
270 );
271 }
272
273 /**
274 * The test cases are based on the tests in the GaBuZoMeu parser
275 * written by Stéphane Bortzmeyer <[email protected]>
276 * and distributed as free software, under the GNU General Public Licence.
277 * https://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
278 */
279 public static function provideWellFormedLanguageTags() {
280 return [
281 [ 'fr', 'two-letter code' ],
282 [ 'fr-latn', 'two-letter code with lower case script code' ],
283 [ 'fr-Latn-FR', 'two-letter code with title case script code and uppercase country code' ],
284 [ 'fr-Latn-419', 'two-letter code with title case script code and region number' ],
285 [ 'fr-FR', 'two-letter code with uppercase' ],
286 [ 'ax-TZ', 'Not in the registry, but well-formed' ],
287 [ 'fr-shadok', 'two-letter code with variant' ],
288 [ 'fr-y-myext-myext2', 'non-x singleton' ],
289 [ 'fra-Latn', 'ISO 639 can be 3-letters' ],
290 [ 'fra', 'three-letter language code' ],
291 [ 'fra-FX', 'three-letter language code with country code' ],
292 [ 'i-klingon', 'grandfathered with singleton' ],
293 [ 'I-kLINgon', 'tags are case-insensitive...' ],
294 [ 'no-bok', 'grandfathered without singleton' ],
295 [ 'i-enochian', 'Grandfathered' ],
296 [ 'x-fr-CH', 'private use' ],
297 [ 'es-419', 'two-letter code with region number' ],
298 [ 'en-Latn-GB-boont-r-extended-sequence-x-private', 'weird, but well-formed' ],
299 [ 'ab-x-abc-x-abc', 'anything goes after x' ],
300 [ 'ab-x-abc-a-a', 'anything goes after x, including several non-x singletons' ],
301 [ 'i-default', 'grandfathered' ],
302 [ 'abcd-Latn', 'Language of 4 chars reserved for future use' ],
303 [ 'AaBbCcDd-x-y-any-x', 'Language of 5-8 chars, registered' ],
304 [ 'de-CH-1901', 'with country and year' ],
305 [ 'en-US-x-twain', 'with country and singleton' ],
306 [ 'zh-cmn', 'three-letter variant' ],
307 [ 'zh-cmn-Hant', 'three-letter variant and script' ],
308 [ 'zh-cmn-Hant-HK', 'three-letter variant, script and country' ],
309 [ 'xr-p-lze', 'Extension' ],
310 [ 'en-GB-oed', 'grandfathered language tag, mixed capitalisation' ],
311 [ 'en-gb-oed', 'grandfathered language tag, all lowercase' ],
312 ];
313 }
314
315 /**
316 * @dataProvider provideMalformedLanguageTags
317 */
318 public function testMalformedLanguageTag( $code, $message = '' ) {
319 $this->assertFalse(
320 LanguageCode::isWellFormedLanguageTag( $code ),
321 "validating that code $code is a malformed language tag - $message"
322 );
323 }
324
325 /**
326 * The test cases are based on the tests in the GaBuZoMeu parser
327 * written by Stéphane Bortzmeyer <[email protected]>
328 * and distributed as free software, under the GNU General Public Licence.
329 * https://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
330 */
331 public static function provideMalformedLanguageTags() {
332 return [
333 [ 'f', 'language too short' ],
334 [ 'f-Latn', 'language too short with script' ],
335 [ 'xr-lxs-qut', 'variants too short' ], # extlangS
336 [ 'fr-Latn-F', 'region too short' ],
337 [ 'a-value', 'language too short with region' ],
338 [ 'tlh-a-b-foo', 'valid three-letter with wrong variant' ],
339 [
340 'i-notexist',
341 'grandfathered but not registered: invalid, even if we only test well-formedness'
342 ],
343 [ 'abcdefghi-012345678', 'numbers too long' ],
344 [ 'ab-abc-abc-abc-abc', 'invalid extensions' ],
345 [ 'ab-abcd-abc', 'invalid extensions' ],
346 [ 'ab-ab-abc', 'invalid extensions' ],
347 [ 'ab-123-abc', 'invalid extensions' ],
348 [ 'a-Hant-ZH', 'short language with valid extensions' ],
349 [ 'a1-Hant-ZH', 'invalid character in language' ],
350 [ 'ab-abcde-abc', 'invalid extensions' ],
351 [ 'ab-1abc-abc', 'invalid characters in extensions' ],
352 [ 'ab-ab-abcd', 'invalid order of extensions' ],
353 [ 'ab-123-abcd', 'invalid order of extensions' ],
354 [ 'ab-abcde-abcd', 'invalid extensions' ],
355 [ 'ab-1abc-abcd', 'invalid characters in extensions' ],
356 [ 'ab-a-b', 'extensions too short' ],
357 [ 'ab-a-x', 'extensions too short, even with singleton' ],
358 [ 'ab--ab', 'two separators' ],
359 [ 'ab-abc-', 'separator in the end' ],
360 [ '-ab-abc', 'separator in the beginning' ],
361 [ 'abcd-efg', 'language too long' ],
362 [ 'aabbccddE', 'tag too long' ],
363 [ 'pa_guru', 'A tag with underscore is invalid in strict mode' ],
364 [ 'de-f', 'subtag too short' ],
365 [ 'zh-classical', 'internal language code zh-classical is not a well-formed language tag' ],
366 ];
367 }
368
369 public function testLenientLanguageTag() {
370 $this->assertTrue(
371 LanguageCode::isWellFormedLanguageTag( 'pa_guru', true ),
372 'pa_guru is a well-formed language tag in lenient mode'
373 );
374 }
375
376 }