Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / languages / LanguageCodeTest.php
1 <?php
2
3 /**
4 * @covers LanguageCode
5 *
6 * @group Language
7 *
8 * @license GPL-2.0+
9 * @author Thiemo Kreuz
10 */
11 class LanguageCodeTest extends PHPUnit_Framework_TestCase {
12
13 use MediaWikiCoversValidator;
14
15 public function testConstructor() {
16 $instance = new LanguageCode();
17
18 $this->assertInstanceOf( LanguageCode::class, $instance );
19 }
20
21 public function testGetDeprecatedCodeMapping() {
22 $map = LanguageCode::getDeprecatedCodeMapping();
23
24 $this->assertInternalType( 'array', $map );
25 $this->assertContainsOnly( 'string', array_keys( $map ) );
26 $this->assertArrayNotHasKey( '', $map );
27 $this->assertContainsOnly( 'string', $map );
28 $this->assertNotContains( '', $map );
29
30 // Codes special to MediaWiki should never appear in a map of "deprecated" codes
31 $this->assertArrayNotHasKey( 'qqq', $map, 'documentation' );
32 $this->assertNotContains( 'qqq', $map, 'documentation' );
33 $this->assertArrayNotHasKey( 'qqx', $map, 'debug code' );
34 $this->assertNotContains( 'qqx', $map, 'debug code' );
35
36 // Valid language codes that are currently not "deprecated"
37 $this->assertArrayNotHasKey( 'bh', $map, 'family of Bihari languages' );
38 $this->assertArrayNotHasKey( 'no', $map, 'family of Norwegian languages' );
39 $this->assertArrayNotHasKey( 'simple', $map );
40 }
41
42 public function testReplaceDeprecatedCodes() {
43 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'als' ) );
44 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'gsw' ) );
45 $this->assertEquals( null, LanguageCode::replaceDeprecatedCodes( null ) );
46 }
47
48 /**
49 * test @see LanguageCode::bcp47().
50 * Please note the BCP 47 explicitly state that language codes are case
51 * insensitive, there are some exceptions to the rule :)
52 * This test is used to verify our formatting against all lower and
53 * all upper cases language code.
54 *
55 * @see https://tools.ietf.org/html/bcp47
56 * @dataProvider provideLanguageCodes()
57 */
58 public function testBcp47( $code, $expected ) {
59 $code = strtolower( $code );
60 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
61 "Applying BCP47 standard to lower case '$code'"
62 );
63
64 $code = strtoupper( $code );
65 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
66 "Applying BCP47 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 }
162
163 }