Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\Json\FormatJson;
5 use MediaWiki\Language\Language;
6 use MediaWiki\Languages\LanguageConverterFactory;
7 use MediaWiki\Languages\LanguageFallback;
8 use MediaWiki\Languages\LanguageNameUtils;
9 use MediaWiki\StubObject\StubObject;
10 use MediaWiki\Title\NamespaceInfo;
11 use Wikimedia\Bcp47Code\Bcp47CodeValue;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @group Language
16 * @covers \MediaWiki\Language\Language
17 */
18 class LanguageTest extends MediaWikiUnitTestCase {
19 /**
20 * @param array $options Valid keys:
21 * 'code'
22 * 'grammarTransformCache'
23 * @return Language
24 */
25 private function getObj( array $options = [] ) {
26 return new Language(
27 $options['code'] ?? 'en',
28 $this->createNoOpMock( NamespaceInfo::class ),
29 $this->createNoOpMock( LocalisationCache::class ),
30 $this->createNoOpMock( LanguageNameUtils::class ),
31 $this->createNoOpMock( LanguageFallback::class ),
32 $this->createNoOpMock( LanguageConverterFactory::class ),
33 $this->createHookContainer(),
34 new HashConfig( [] )
35 );
36 }
37
38 /**
39 * @dataProvider provideCodes
40 */
41 public function testGetCode( $code, $bcp47code ) {
42 $lang = $this->getObj( [ 'code' => $code ] );
43 $this->assertSame( $code, $lang->getCode() );
44 }
45
46 /**
47 * @dataProvider provideCodes
48 */
49 public function testGetHtmlCode( $code, $bcp47code ) {
50 $lang = $this->getObj( [ 'code' => $code ] );
51 $this->assertSame( $bcp47code, $lang->getHtmlCode() );
52 }
53
54 /**
55 * @dataProvider provideCodes
56 */
57 public function testToBcp47Code( $code, $bcp47code ) {
58 $lang = $this->getObj( [ 'code' => $code ] );
59 $this->assertSame( $bcp47code, $lang->toBcp47Code() );
60 }
61
62 /**
63 * @dataProvider provideCodes
64 */
65 public function testIsSameCodeAs( $code, $bcp47code ) {
66 // Commented out symmetric tests below can be enabled once
67 // we require wikimedia/bcp47-code ^2.0.0
68 $code1 = $this->getObj( [ 'code' => $code ] );
69 $code2 = new Bcp47CodeValue( $bcp47code );
70 $this->assertTrue( $code1->isSameCodeAs( $code2 ) );
71 // $this->assertTrue( $code2->isSameCodeAs( $code1 ) ); // ^2.0.0
72
73 // Should be case-insensitive.
74 $code2 = new Bcp47CodeValue( strtoupper( $bcp47code ) );
75 $this->assertTrue( $code1->isSameCodeAs( $code2 ) );
76 // $this->assertTrue( $code2->isSameCodeAs( $code1 ) ); // ^2.0.0
77
78 $code2 = new Bcp47CodeValue( strtolower( $bcp47code ) );
79 $this->assertTrue( $code1->isSameCodeAs( $code2 ) );
80 // $this->assertTrue( $code2->isSameCodeAs( $code1 ) ); // ^2.0.0
81 }
82
83 /**
84 * @dataProvider provideCodes
85 */
86 public function testIsNotSameCodeAs( $code, $bcp47code ) {
87 // Commented out symmetric tests below can be enabled once
88 // we require wikimedia/bcp47-code ^2.0.0
89 $code1 = $this->getObj( [ 'code' => $code ] );
90 $code2 = new Bcp47CodeValue( 'x-not-the-same-code' );
91 $this->assertFalse( $code1->isSameCodeAs( $code2 ) );
92 // $this->assertFalse( $code2->isSameCodeAs( $code1 ) ); // ^2.0.0
93 }
94
95 public static function provideCodes() {
96 return LanguageCodeTest::provideLanguageCodes();
97 }
98
99 /**
100 * @todo Test the exception case
101 */
102 public function testGetGrammarTransformations() {
103 global $IP;
104
105 // XXX Inject a value here instead of reading the filesystem (T231159)
106 $expected = FormatJson::decode(
107 file_get_contents( "$IP/languages/data/grammarTransformations/he.json" ), true );
108
109 $lang = $this->getObj( [ 'code' => 'he' ] );
110
111 $this->assertSame( $expected, $lang->getGrammarTransformations() );
112 $this->assertSame( $expected, $lang->getGrammarTransformations() );
113 }
114
115 public function testGetGrammarTransformations_empty() {
116 $lang = $this->getObj();
117
118 $this->assertSame( [], $lang->getGrammarTransformations() );
119 $this->assertSame( [], $lang->getGrammarTransformations() );
120 }
121
122 /**
123 * @dataProvider provideUcwords
124 */
125 public function testUcwords( string $input, string $expected ) {
126 $lang = $this->getObj();
127 $this->assertSame( $expected, $lang->ucwords( $input ) );
128 }
129
130 public static function provideUcwords() {
131 return [
132 'Empty string' => [ '', '' ],
133 'Non-alpha only' => [ '3212-353', '3212-353' ],
134 'Non-alpha only, multiple words' => [ '@%#, #@$%!', '@%#, #@$%!' ],
135 'Single ASCII word' => [ 'teSt', 'Test' ],
136 'ASCII words' => [ 'TeSt 123 test foo-bar', 'Test 123 Test Foo-bar' ],
137 'Single multibyte word' => [ 'теСт', 'Тест' ],
138 'Multibyte words' => [ 'ТесТ 123, тест 测试 test раз-два', 'Тест 123, Тест 测试 Test Раз-два' ],
139 ];
140 }
141
142 /**
143 * @dataProvider provideUcwordbreaks
144 */
145 public function testUcwordbreaks( string $input, string $expected ) {
146 $lang = $this->getObj();
147 $this->assertSame( $expected, $lang->ucwordbreaks( $input ) );
148 }
149
150 public static function provideUcwordbreaks() {
151 return [
152 'Empty string' => [ '', '' ],
153 'Non-alpha only' => [ '3212-353', '3212-353' ],
154 'Non-alpha only, multiple words' => [ '@%#, #@$%!', '@%#, #@$%!' ],
155 'Single ASCII word' => [ 'teSt', 'TeSt' ],
156 'Single ASCII word, prefixed' => [ '-teSt', '-TeSt' ],
157 'ASCII words' => [ 'TeSt 123 test foo-bar', 'TeSt 123 Test Foo-Bar' ],
158 'Single multibyte word' => [ 'теСт', 'Тест' ],
159 'Single multibyte word, prefixed' => [ '-теСт', '-Тест' ],
160 'Multibyte words' => [ 'ТесТ 123, тест 测试 test раз-два', 'Тест 123, Тест 测试 Test Раз-Два' ],
161 ];
162 }
163
164 /**
165 * @dataProvider provideFirstChar
166 */
167 public function testFirstChar( string $input, string $expected ) {
168 $lang = $this->getObj();
169 $this->assertSame( $expected, $lang->firstChar( $input ) );
170 }
171
172 public static function provideFirstChar() {
173 return [
174 'Empty string' => [ '', '' ],
175 'Single Latin' => [ 'T', 'T' ],
176 'Latin' => [ 'TEST', 'T' ],
177 'Digit' => [ '123', '1' ],
178 'Russian' => [ 'Ёпт', 'Ё' ],
179 'Emoji' => [ '😂💀☢️', '😂' ],
180 // Korean is special-cased to remove single letters from syllables
181 'Korean' => [ '위키백과', 'ㅇ' ],
182 ];
183 }
184
185 /**
186 * @dataProvider provideConvertDateFormatToJs
187 * @param string $input
188 * @param array $expected
189 */
190 public function testConvertDateFormatToJs( $input, $expected ) {
191 $lang = $this->getObj();
192 /** @var Language|StubObject $tlang */
193 $tlang = TestingAccessWrapper::newFromObject( $lang );
194 $tlang->localisationCache = $this->createMock( LocalisationCache::class );
195 $tlang->localisationCache
196 ->method( 'getItem' )
197 ->with( 'en', 'numberingSystem' )
198 ->willReturn( 'latn' );
199
200 $result = $tlang->convertDateFormatToJs( $input );
201 $this->assertArrayEquals( $expected, $result, false, true );
202 }
203
204 public static function provideConvertDateFormatToJs() {
205 return [
206 [
207 'j F Y',
208 [
209 'options' => [
210 'numberingSystem' => 'latn',
211 'day' => 'numeric',
212 'month' => 'long',
213 'year' => 'numeric'
214 ],
215 'pattern' => '{day} {mwMonth} {year}'
216 ]
217 ],
218 [
219 'xx xnY xx "quote" \Y\\',
220 [
221 'locale' => 'en',
222 'options' => [ 'year' => 'numeric' ],
223 'pattern' => 'x {year} x quote Y\\'
224 ]
225 ],
226 [
227 'xhY',
228 [
229 'options' => [
230 'numberingSystem' => 'hebr',
231 'year' => 'numeric'
232 ],
233 'pattern' => '{year}',
234 'error' => 'Unsupported format code(s): xh'
235 ]
236 ],
237 [
238 'xg',
239 [
240 'options' => [
241 'numberingSystem' => 'latn',
242 'month' => 'long',
243 ],
244 'pattern' => '{mwMonthGen}',
245 ]
246 ],
247 [
248 'xjx',
249 [
250 'options' => [
251 'numberingSystem' => 'latn',
252 'calendar' => 'hebrew',
253 'month' => 'long',
254 ],
255 'pattern' => '{mwMonthGen}',
256 ]
257 ],
258 [
259 'd D',
260 [
261 'options' => [
262 'numberingSystem' => 'latn',
263 'day' => '2-digit',
264 'weekday' => 'short',
265 ],
266 'pattern' => '{day} {weekday}'
267 ]
268 ],
269 [
270 'xij xiF xiY',
271 [
272 'options' => [
273 'numberingSystem' => 'latn',
274 'day' => 'numeric',
275 'month' => 'long',
276 'year' => 'numeric',
277 'calendar' => 'persian',
278 ],
279 'pattern' => '{day} {month} {year}'
280 ]
281 ],
282 [
283 'xmj xmF xmY',
284 [
285 'options' => [
286 'numberingSystem' => 'latn',
287 'day' => 'numeric',
288 'month' => 'long',
289 'year' => 'numeric',
290 'calendar' => 'islamic',
291 ],
292 'pattern' => '{day} {month} {year}'
293 ]
294 ],
295 [
296 'xjj xjF xjY l',
297 [
298 'options' => [
299 'numberingSystem' => 'latn',
300 'day' => 'numeric',
301 'month' => 'long',
302 'year' => 'numeric',
303 'weekday' => 'long',
304 'calendar' => 'hebrew',
305 ],
306 'pattern' => '{day} {month} {year} {weekday}'
307 ]
308 ],
309 [
310 'xtY',
311 [
312 'options' => [
313 'numberingSystem' => 'latn',
314 'calendar' => 'japanese',
315 'year' => 'numeric',
316 'era' => 'short',
317 ],
318 'pattern' => '{era}{year}',
319 ]
320 ],
321 [
322 'H:i:s',
323 [
324 'options' => [
325 'numberingSystem' => 'latn',
326 'hour' => '2-digit',
327 'minute' => '2-digit',
328 'second' => '2-digit',
329 'hour12' => false,
330 ],
331 'pattern' => '{hour}:{minute}:{second}'
332 ]
333 ],
334 [
335 'h:i a',
336 [
337 'options' => [
338 'numberingSystem' => 'latn',
339 'hour' => '2-digit',
340 'minute' => '2-digit',
341 'hour12' => true,
342 ],
343 'pattern' => '{hour}:{minute} {dayPeriod}'
344 ]
345 ],
346 [
347 'g P',
348 [
349 'options' => [
350 'numberingSystem' => 'latn',
351 'hour' => 'numeric',
352 'hour12' => true,
353 'timeZoneName' => 'longOffset'
354 ],
355 'pattern' => '{hour} {timeZoneName}'
356 ]
357 ],
358 ];
359 }
360 }