950d2df36bb72c154694e4b5df2485e773485352
[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 public function testConstructor() {
14 $instance = new LanguageCode();
15
16 $this->assertInstanceOf( LanguageCode::class, $instance );
17 }
18
19 public function testGetDeprecatedCodeMapping() {
20 $map = LanguageCode::getDeprecatedCodeMapping();
21
22 $this->assertInternalType( 'array', $map );
23 $this->assertContainsOnly( 'string', array_keys( $map ) );
24 $this->assertArrayNotHasKey( '', $map );
25 $this->assertContainsOnly( 'string', $map );
26 $this->assertNotContains( '', $map );
27
28 // Codes special to MediaWiki should never appear in a map of "deprecated" codes
29 $this->assertArrayNotHasKey( 'qqq', $map, 'documentation' );
30 $this->assertNotContains( 'qqq', $map, 'documentation' );
31 $this->assertArrayNotHasKey( 'qqx', $map, 'debug code' );
32 $this->assertNotContains( 'qqx', $map, 'debug code' );
33
34 // Valid language codes that are currently not "deprecated"
35 $this->assertArrayNotHasKey( 'bh', $map, 'family of Bihari languages' );
36 $this->assertArrayNotHasKey( 'no', $map, 'family of Norwegian languages' );
37 $this->assertArrayNotHasKey( 'simple', $map );
38 }
39
40 public function testReplaceDeprecatedCodes() {
41 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'als' ) );
42 $this->assertEquals( 'gsw', LanguageCode::replaceDeprecatedCodes( 'gsw' ) );
43 $this->assertEquals( null, LanguageCode::replaceDeprecatedCodes( null ) );
44 }
45
46 /**
47 * test @see LanguageCode::bcp47().
48 * Please note the BCP 47 explicitly state that language codes are case
49 * insensitive, there are some exceptions to the rule :)
50 * This test is used to verify our formatting against all lower and
51 * all upper cases language code.
52 *
53 * @see https://tools.ietf.org/html/bcp47
54 * @dataProvider provideLanguageCodes()
55 */
56 public function testBcp47( $code, $expected ) {
57 $code = strtolower( $code );
58 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
59 "Applying BCP47 standard to lower case '$code'"
60 );
61
62 $code = strtoupper( $code );
63 $this->assertEquals( $expected, LanguageCode::bcp47( $code ),
64 "Applying BCP47 standard to upper case '$code'"
65 );
66 }
67
68 /**
69 * Array format is ($code, $expected)
70 */
71 public static function provideLanguageCodes() {
72 return [
73 // Extracted from BCP 47 (list not exhaustive)
74 # 2.1.1
75 [ 'en-ca-x-ca', 'en-CA-x-ca' ],
76 [ 'sgn-be-fr', 'sgn-BE-FR' ],
77 [ 'az-latn-x-latn', 'az-Latn-x-latn' ],
78 # 2.2
79 [ 'sr-Latn-RS', 'sr-Latn-RS' ],
80 [ 'az-arab-ir', 'az-Arab-IR' ],
81
82 # 2.2.5
83 [ 'sl-nedis', 'sl-nedis' ],
84 [ 'de-ch-1996', 'de-CH-1996' ],
85
86 # 2.2.6
87 [
88 'en-latn-gb-boont-r-extended-sequence-x-private',
89 'en-Latn-GB-boont-r-extended-sequence-x-private'
90 ],
91
92 // Examples from BCP 47 Appendix A
93 # Simple language subtag:
94 [ 'DE', 'de' ],
95 [ 'fR', 'fr' ],
96 [ 'ja', 'ja' ],
97
98 # Language subtag plus script subtag:
99 [ 'zh-hans', 'zh-Hans' ],
100 [ 'sr-cyrl', 'sr-Cyrl' ],
101 [ 'sr-latn', 'sr-Latn' ],
102
103 # Extended language subtags and their primary language subtag
104 # counterparts:
105 [ 'zh-cmn-hans-cn', 'zh-cmn-Hans-CN' ],
106 [ 'cmn-hans-cn', 'cmn-Hans-CN' ],
107 [ 'zh-yue-hk', 'zh-yue-HK' ],
108 [ 'yue-hk', 'yue-HK' ],
109
110 # Language-Script-Region:
111 [ 'zh-hans-cn', 'zh-Hans-CN' ],
112 [ 'sr-latn-RS', 'sr-Latn-RS' ],
113
114 # Language-Variant:
115 [ 'sl-rozaj', 'sl-rozaj' ],
116 [ 'sl-rozaj-biske', 'sl-rozaj-biske' ],
117 [ 'sl-nedis', 'sl-nedis' ],
118
119 # Language-Region-Variant:
120 [ 'de-ch-1901', 'de-CH-1901' ],
121 [ 'sl-it-nedis', 'sl-IT-nedis' ],
122
123 # Language-Script-Region-Variant:
124 [ 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ],
125
126 # Language-Region:
127 [ 'de-de', 'de-DE' ],
128 [ 'en-us', 'en-US' ],
129 [ 'es-419', 'es-419' ],
130
131 # Private use subtags:
132 [ 'de-ch-x-phonebk', 'de-CH-x-phonebk' ],
133 [ 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ],
134 /**
135 * Previous test does not reflect the BCP 47 which states:
136 * az-Arab-x-AZE-derbend
137 * AZE being private, it should be lower case, hence the test above
138 * should probably be:
139 * [ 'az-arab-x-aze-derbend', 'az-Arab-x-AZE-derbend' ],
140 */
141
142 # Private use registry values:
143 [ 'x-whatever', 'x-whatever' ],
144 [ 'qaa-qaaa-qm-x-southern', 'qaa-Qaaa-QM-x-southern' ],
145 [ 'de-qaaa', 'de-Qaaa' ],
146 [ 'sr-latn-qm', 'sr-Latn-QM' ],
147 [ 'sr-qaaa-rs', 'sr-Qaaa-RS' ],
148
149 # Tags that use extensions
150 [ 'en-us-u-islamcal', 'en-US-u-islamcal' ],
151 [ 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ],
152 [ 'en-a-myext-b-another', 'en-a-myext-b-another' ],
153
154 # Invalid:
155 // de-419-DE
156 // a-DE
157 // ar-a-aaa-b-bbb-a-ccc
158 ];
159 }
160
161 }