f3a7ae4d7d9161866fdef56f5fd679308aaad08b
9 class LanguageCodeTest
extends MediaWikiUnitTestCase
{
11 public function testConstructor() {
12 $instance = new LanguageCode();
14 $this->assertInstanceOf( LanguageCode
::class, $instance );
17 public function testGetDeprecatedCodeMapping() {
18 $map = LanguageCode
::getDeprecatedCodeMapping();
20 $this->assertInternalType( 'array', $map );
21 $this->assertContainsOnly( 'string', array_keys( $map ) );
22 $this->assertArrayNotHasKey( '', $map );
23 $this->assertContainsOnly( 'string', $map );
24 $this->assertNotContains( '', $map );
26 // Codes special to MediaWiki should never appear in a map of "deprecated" codes
27 $this->assertArrayNotHasKey( 'qqq', $map, 'documentation' );
28 $this->assertNotContains( 'qqq', $map, 'documentation' );
29 $this->assertArrayNotHasKey( 'qqx', $map, 'debug code' );
30 $this->assertNotContains( 'qqx', $map, 'debug code' );
32 // Valid language codes that are currently not "deprecated"
33 $this->assertArrayNotHasKey( 'bh', $map, 'family of Bihari languages' );
34 $this->assertArrayNotHasKey( 'no', $map, 'family of Norwegian languages' );
35 $this->assertArrayNotHasKey( 'simple', $map );
38 public function testReplaceDeprecatedCodes() {
39 $this->assertEquals( 'gsw', LanguageCode
::replaceDeprecatedCodes( 'als' ) );
40 $this->assertEquals( 'gsw', LanguageCode
::replaceDeprecatedCodes( 'gsw' ) );
41 $this->assertEquals( null, LanguageCode
::replaceDeprecatedCodes( null ) );
45 * test @see LanguageCode::bcp47().
46 * Please note the BCP 47 explicitly state that language codes are case
47 * insensitive, there are some exceptions to the rule :)
48 * This test is used to verify our formatting against all lower and
49 * all upper cases language code.
51 * @see https://tools.ietf.org/html/bcp47
52 * @dataProvider provideLanguageCodes()
54 public function testBcp47( $code, $expected ) {
55 $this->assertEquals( $expected, LanguageCode
::bcp47( $code ),
56 "Applying BCP 47 standard to '$code'"
59 $code = strtolower( $code );
60 $this->assertEquals( $expected, LanguageCode
::bcp47( $code ),
61 "Applying BCP 47 standard to lower case '$code'"
64 $code = strtoupper( $code );
65 $this->assertEquals( $expected, LanguageCode
::bcp47( $code ),
66 "Applying BCP 47 standard to upper case '$code'"
71 * Array format is ($code, $expected)
73 public static function provideLanguageCodes() {
75 // Extracted from BCP 47 (list not exhaustive)
77 [ 'en-ca-x-ca', 'en-CA-x-ca' ],
78 [ 'sgn-be-fr', 'sgn-BE-FR' ],
79 [ 'az-latn-x-latn', 'az-Latn-x-latn' ],
81 [ 'sr-Latn-RS', 'sr-Latn-RS' ],
82 [ 'az-arab-ir', 'az-Arab-IR' ],
85 [ 'sl-nedis', 'sl-nedis' ],
86 [ 'de-ch-1996', 'de-CH-1996' ],
90 'en-latn-gb-boont-r-extended-sequence-x-private',
91 'en-Latn-GB-boont-r-extended-sequence-x-private'
94 // Examples from BCP 47 Appendix A
95 # Simple language subtag:
100 # Language subtag plus script subtag:
101 [ 'zh-hans', 'zh-Hans' ],
102 [ 'sr-cyrl', 'sr-Cyrl' ],
103 [ 'sr-latn', 'sr-Latn' ],
105 # Extended language subtags and their primary language subtag
107 [ 'zh-cmn-hans-cn', 'zh-cmn-Hans-CN' ],
108 [ 'cmn-hans-cn', 'cmn-Hans-CN' ],
109 [ 'zh-yue-hk', 'zh-yue-HK' ],
110 [ 'yue-hk', 'yue-HK' ],
112 # Language-Script-Region:
113 [ 'zh-hans-cn', 'zh-Hans-CN' ],
114 [ 'sr-latn-RS', 'sr-Latn-RS' ],
117 [ 'sl-rozaj', 'sl-rozaj' ],
118 [ 'sl-rozaj-biske', 'sl-rozaj-biske' ],
119 [ 'sl-nedis', 'sl-nedis' ],
121 # Language-Region-Variant:
122 [ 'de-ch-1901', 'de-CH-1901' ],
123 [ 'sl-it-nedis', 'sl-IT-nedis' ],
125 # Language-Script-Region-Variant:
126 [ 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ],
129 [ 'de-de', 'de-DE' ],
130 [ 'en-us', 'en-US' ],
131 [ 'es-419', 'es-419' ],
133 # Private use subtags:
134 [ 'de-ch-x-phonebk', 'de-CH-x-phonebk' ],
135 [ 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ],
137 * Previous test does not reflect the BCP 47 which states:
138 * az-Arab-x-AZE-derbend
139 * AZE being private, it should be lower case, hence the test above
140 * should probably be:
141 * [ 'az-arab-x-aze-derbend', 'az-Arab-x-AZE-derbend' ],
144 # Private use registry values:
145 [ 'x-whatever', 'x-whatever' ],
146 [ 'qaa-qaaa-qm-x-southern', 'qaa-Qaaa-QM-x-southern' ],
147 [ 'de-qaaa', 'de-Qaaa' ],
148 [ 'sr-latn-qm', 'sr-Latn-QM' ],
149 [ 'sr-qaaa-rs', 'sr-Qaaa-RS' ],
151 # Tags that use extensions
152 [ 'en-us-u-islamcal', 'en-US-u-islamcal' ],
153 [ 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ],
154 [ 'en-a-myext-b-another', 'en-a-myext-b-another' ],
159 // ar-a-aaa-b-bbb-a-ccc
161 # Non-standard and deprecated language codes used by MediaWiki
163 [ 'bat-smg', 'sgs' ],
164 [ 'be-x-old', 'be-tarask' ],
165 [ 'fiu-vro', 'vro' ],
166 [ 'roa-rup', 'rup' ],
167 [ 'zh-classical', 'lzh' ],
168 [ 'zh-min-nan', 'nan' ],
170 [ 'cbk-zam', 'cbk' ],
171 [ 'de-formal', 'de-x-formal' ],
173 [ 'en-rtl', 'en-x-rtl' ],
174 [ 'es-formal', 'es-x-formal' ],
175 [ 'hu-formal', 'hu-x-formal' ],
176 [ 'kk-Arab', 'kk-Arab' ],
177 [ 'kk-Cyrl', 'kk-Cyrl' ],
178 [ 'kk-Latn', 'kk-Latn' ],
179 [ 'map-bms', 'jv-x-bms' ],
180 [ 'mo', 'ro-Cyrl-MD' ],
182 [ 'nl-informal', 'nl-x-informal' ],
183 [ 'roa-tara', 'nap-x-tara' ],
184 [ 'simple', 'en-simple' ],
185 [ 'sr-ec', 'sr-Cyrl' ],
186 [ 'sr-el', 'sr-Latn' ],
187 [ 'zh-cn', 'zh-Hans-CN' ],
188 [ 'zh-sg', 'zh-Hans-SG' ],
189 [ 'zh-my', 'zh-Hans-MY' ],
190 [ 'zh-tw', 'zh-Hant-TW' ],
191 [ 'zh-hk', 'zh-Hant-HK' ],
192 [ 'zh-mo', 'zh-Hant-MO' ],
193 [ 'zh-hans', 'zh-Hans' ],
194 [ 'zh-hant', 'zh-Hant' ],