11 class LanguageCodeTest
extends PHPUnit_Framework_TestCase
{
13 use MediaWikiCoversValidator
;
15 public function testConstructor() {
16 $instance = new LanguageCode();
18 $this->assertInstanceOf( LanguageCode
::class, $instance );
21 public function testGetDeprecatedCodeMapping() {
22 $map = LanguageCode
::getDeprecatedCodeMapping();
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 );
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' );
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 );
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 ) );
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.
55 * @see https://tools.ietf.org/html/bcp47
56 * @dataProvider provideLanguageCodes()
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'"
64 $code = strtoupper( $code );
65 $this->assertEquals( $expected, LanguageCode
::bcp47( $code ),
66 "Applying BCP47 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