Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[lhc/web/wiklou.git] / tests / phpunit / unit / languages / LanguageCodeTest.php
1 <?php
2
3 /**
4 * @covers LanguageCode
5 * @group Language
6 *
7 * @author Thiemo Kreuz
8 */
9 class LanguageCodeTest extends MediaWikiUnitTestCase {
10
11 public function testConstructor() {
12 $instance = new LanguageCode();
13
14 $this->assertInstanceOf( LanguageCode::class, $instance );
15 }
16
17 public function testGetDeprecatedCodeMapping() {
18 $map = LanguageCode::getDeprecatedCodeMapping();
19
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 );
25
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' );
31
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 );
36 }
37
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 ) );
42 }
43
44 /**
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.
50 *
51 * @see https://tools.ietf.org/html/bcp47
52 * @dataProvider provideLanguageCodes()
53 */
54 public function testBcp47( $code, $expected ) {
55 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
56 "Applying BCP 47 standard to '$code'"
57 );
58
59 $code = strtolower( $code );
60 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
61 "Applying BCP 47 standard to lower case '$code'"
62 );
63
64 $code = strtoupper( $code );
65 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
66 "Applying BCP 47 standard to upper case '$code'"
67 );
68 }
69
70 /**
71 * Array format is ($code, $expected)
72 */
73 public static function provideLanguageCodes() {
74 return [
75 // Extracted from BCP 47 (list not exhaustive)
76 # 2.1.1
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' ],
80 # 2.2
81 [ 'sr-Latn-RS', 'sr-Latn-RS' ],
82 [ 'az-arab-ir', 'az-Arab-IR' ],
83
84 # 2.2.5
85 [ 'sl-nedis', 'sl-nedis' ],
86 [ 'de-ch-1996', 'de-CH-1996' ],
87
88 # 2.2.6
89 [
90 'en-latn-gb-boont-r-extended-sequence-x-private',
91 'en-Latn-GB-boont-r-extended-sequence-x-private'
92 ],
93
94 // Examples from BCP 47 Appendix A
95 # Simple language subtag:
96 [ 'DE', 'de' ],
97 [ 'fR', 'fr' ],
98 [ 'ja', 'ja' ],
99
100 # Language subtag plus script subtag:
101 [ 'zh-hans', 'zh-Hans' ],
102 [ 'sr-cyrl', 'sr-Cyrl' ],
103 [ 'sr-latn', 'sr-Latn' ],
104
105 # Extended language subtags and their primary language subtag
106 # counterparts:
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' ],
111
112 # Language-Script-Region:
113 [ 'zh-hans-cn', 'zh-Hans-CN' ],
114 [ 'sr-latn-RS', 'sr-Latn-RS' ],
115
116 # Language-Variant:
117 [ 'sl-rozaj', 'sl-rozaj' ],
118 [ 'sl-rozaj-biske', 'sl-rozaj-biske' ],
119 [ 'sl-nedis', 'sl-nedis' ],
120
121 # Language-Region-Variant:
122 [ 'de-ch-1901', 'de-CH-1901' ],
123 [ 'sl-it-nedis', 'sl-IT-nedis' ],
124
125 # Language-Script-Region-Variant:
126 [ 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ],
127
128 # Language-Region:
129 [ 'de-de', 'de-DE' ],
130 [ 'en-us', 'en-US' ],
131 [ 'es-419', 'es-419' ],
132
133 # Private use subtags:
134 [ 'de-ch-x-phonebk', 'de-CH-x-phonebk' ],
135 [ 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ],
136 /**
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' ],
142 */
143
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' ],
150
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' ],
155
156 # Invalid:
157 // de-419-DE
158 // a-DE
159 // ar-a-aaa-b-bbb-a-ccc
160
161 # Non-standard and deprecated language codes used by MediaWiki
162 [ 'als', 'gsw' ],
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' ],
169 [ 'zh-yue', 'yue' ],
170 [ 'cbk-zam', 'cbk' ],
171 [ 'de-formal', 'de-x-formal' ],
172 [ 'eml', 'egl' ],
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' ],
181 [ 'nrm', 'nrf' ],
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' ],
195 ];
196 }
197
198 }