Debian Codegraph @

tests/phpunit/unit/includes/language/LanguageFactoryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Languages;
4
5 use LocalisationCache;
6 use MediaWiki\Config\HashConfig;
7 use MediaWiki\Config\ServiceOptions;
8 use MediaWiki\HookContainer\HookContainer;
9 use MediaWiki\Languages\LanguageConverterFactory;
10 use MediaWiki\Languages\LanguageFactory;
11 use MediaWiki\Languages\LanguageFallback;
12 use MediaWiki\Languages\LanguageNameUtils;
13 use MediaWiki\Title\NamespaceInfo;
14 use MediaWikiUnitTestCase;
15 use Wikimedia\Bcp47Code\Bcp47CodeValue;
16
17 /**
18 * @group Language
19 * @covers \MediaWiki\Languages\LanguageFactory
20 */
21 class LanguageFactoryTest extends MediaWikiUnitTestCase {
22 private function createFactory() {
23 $options = new ServiceOptions(
24 LanguageFactory::CONSTRUCTOR_OPTIONS,
25 array_fill_keys( LanguageFactory::CONSTRUCTOR_OPTIONS, null )
26 );
27 $languageNameUtils = $this->createMock( LanguageNameUtils::class );
28 $languageNameUtils
29 ->method( 'isValidCode' )
30 ->willReturn( true );
31 $factory = new LanguageFactory(
32 $options,
33 $this->createNoOpMock( NamespaceInfo::class ),
34 $this->createNoOpMock( LocalisationCache::class ),
35 $languageNameUtils,
36 $this->createNoOpMock( LanguageFallback::class ),
37 $this->createNoOpMock( LanguageConverterFactory::class ),
38 $this->createNoOpMock( HookContainer::class ),
39 new HashConfig()
40 );
41 return $factory;
42 }
43
44 /**
45 * @dataProvider provideCodes
46 * @dataProvider provideDeprecatedCodes
47 */
48 public function testGetLanguage( $code, $bcp47code ) {
49 $factory = $this->createFactory();
50 $lang = $factory->getLanguage( $code );
51 $this->assertSame( $bcp47code, $lang->toBcp47Code() );
52 }
53
54 /**
55 * @dataProvider provideCodes
56 */
57 public function testGetLanguageBcp47Code( $code, $bcp47code ) {
58 $factory = $this->createFactory();
59 $bcp47obj = new Bcp47CodeValue( $bcp47code );
60 $lang = $factory->getLanguage( $bcp47obj );
61 $this->assertSame( $code, $lang->getCode() );
62 }
63
64 public function testGetLanguageAmbig() {
65 // At this moment, `egl` is a valid internal code, and should *not*
66 // be remapped. Its BCP-47 equivalent is also `egl`.
67 $factory = $this->createFactory();
68 $lang = $factory->getLanguage( 'egl' );
69 $this->assertSame( 'egl', $lang->getCode() );
70 $this->assertSame( 'egl', $lang->toBcp47Code() );
71 // `eml` is *also* a valid internal code, and in fact is used for
72 // (eg) `eml.wikipedia.org`. But it is not the recommended BCP-47
73 // code, and should be mapped to `egl` in that context.
74 $lang = $factory->getLanguage( 'eml' );
75 $this->assertSame( 'eml', $lang->getCode() );
76 $this->assertSame( 'egl', $lang->toBcp47Code() );
77 // When converting *from* BCP47, `egl` should get mapped to `eml`,
78 // since that's what is currently used internally in production.
79 $lang = $factory->getLanguage( new Bcp47CodeValue( 'egl' ) );
80 $this->assertSame( 'eml', $lang->getCode() );
81 $this->assertSame( 'egl', $lang->toBcp47Code() );
82 // See T36217; eventually `eml` should be deprecated and added to
83 // DEPRECATED_LANGUAGE_CODE_MAPPING; alternatively `egl` could
84 // be removed as a valid internal code. Either way would remove
85 // the ambiguity and this test case would need to be updated
86 // appropriately.
87 }
88
89 // These are codes which should *not* be used internally: they can
90 // be given as inputs to LanguageFactory::getLanguage() (for backward
91 // compatibility) but should never be returned from Language::getCode()
92 public static function provideDeprecatedCodes() {
93 return [
94 [ 'als', 'gsw' ],
95 [ 'bat-smg', 'sgs' ],
96 [ 'be-x-old', 'be-tarask' ],
97 [ 'fiu-vro', 'vro' ],
98 [ 'roa-rup', 'rup' ],
99 [ 'zh-classical', 'lzh' ],
100 [ 'zh-min-nan', 'nan' ],
101 [ 'zh-yue', 'yue' ],
102 ];
103 }
104
105 public static function provideCodes() {
106 return [
107 # Basic codes
108 [ 'en', 'en' ],
109 [ 'de', 'de' ],
110 [ 'fr', 'fr' ],
111 [ 'ja', 'ja' ],
112 # Variant codes
113 [ 'zh-hans', 'zh-Hans' ],
114 [ 'zh-yue-hk', 'zh-yue-HK' ],
115 # Non standard codes
116 # Unlike deprecated codes, this *are* valid internal codes and
117 # will be returned from Language::getCode()
118 [ 'cbk-zam', 'cbk' ],
119 [ 'de-formal', 'de-x-formal' ],
120 [ 'eml', 'egl' ],
121 [ 'en-rtl', 'en-x-rtl' ],
122 [ 'es-formal', 'es-x-formal' ],
123 [ 'hu-formal', 'hu-x-formal' ],
124 [ 'kk-arab', 'kk-Arab' ],
125 [ 'kk-cyrl', 'kk-Cyrl' ],
126 [ 'kk-latn', 'kk-Latn' ],
127 [ 'map-bms', 'jv-x-bms' ],
128 [ 'mo', 'ro-Cyrl-MD' ],
129 [ 'nrm', 'nrf' ],
130 [ 'nl-informal', 'nl-x-informal' ],
131 [ 'roa-tara', 'nap-x-tara' ],
132 [ 'simple', 'en-simple' ],
133 [ 'sr-ec', 'sr-Cyrl' ],
134 [ 'sr-el', 'sr-Latn' ],
135 [ 'zh-cn', 'zh-Hans-CN' ],
136 [ 'zh-sg', 'zh-Hans-SG' ],
137 [ 'zh-my', 'zh-Hans-MY' ],
138 [ 'zh-tw', 'zh-Hant-TW' ],
139 [ 'zh-hk', 'zh-Hant-HK' ],
140 [ 'zh-mo', 'zh-Hant-MO' ],
141 [ 'zh-hans', 'zh-Hans' ],
142 [ 'zh-hant', 'zh-Hant' ],
143 ];
144 }
145 }