Debian Codegraph @

tests/phpunit/includes/api/query/ApiQueryLanguageinfoTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api\Query;
4
5 use MediaWiki\Tests\Api\ApiTestCase;
6 use Wikimedia\Timestamp\ConvertibleTimestamp;
7
8 /**
9 * @group API
10 * @group medium
11 *
12 * @covers MediaWiki\Api\ApiQueryLanguageinfo
13 */
14 class ApiQueryLanguageinfoTest extends ApiTestCase {
15
16 protected function setUp(): void {
17 parent::setUp();
18 // register custom language names so this test is independent of CLDR
19 $this->setTemporaryHook(
20 'LanguageGetTranslatedLanguageNames',
21 static function ( array &$names, $code ) {
22 switch ( $code ) {
23 case 'en':
24 $names['sh'] = 'Serbo-Croatian';
25 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
26 break;
27 case 'pt':
28 $names['de'] = 'alemão';
29 break;
30 }
31 }
32 );
33 }
34
35 private function doQuery( array $params ): array {
36 $params += [
37 'action' => 'query',
38 'meta' => 'languageinfo',
39 'uselang' => 'en',
40 ];
41
42 $res = $this->doApiRequest( $params );
43
44 $this->assertArrayNotHasKey( 'warnings', $res[0] );
45
46 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ?? null ];
47 }
48
49 public static function provideTestAllPropsForSingleLanguage() {
50 yield [
51 'sr',
52 [
53 'code' => 'sr',
54 'bcp47' => 'sr',
55 'autonym' => 'српски / srpski',
56 'name' => 'српски / srpski',
57 'fallbacks' => [ 'sr-ec', 'sr-cyrl', 'sr-el', 'sr-latn' ],
58 'dir' => 'ltr',
59 'variants' => [ 'sr', 'sr-ec', 'sr-el' ],
60 'variantnames' => [
61 'sr' => 'Ћир./lat.',
62 'sr-ec' => 'Ћирилица',
63 'sr-el' => 'Latinica',
64 ],
65 ]
66 ];
67
68 yield [
69 'qtp', // reserved for local use by ISO 639; registered in setUp()
70 [
71 'code' => 'qtp',
72 'bcp47' => 'qtp',
73 'autonym' => '',
74 'name' => 'a custom language code MediaWiki knows nothing about',
75 'fallbacks' => [],
76 'dir' => 'ltr',
77 'variants' => [ 'qtp' ],
78 'variantnames' => [ 'qtp' => 'qtp' ],
79 ]
80 ];
81 }
82
83 /**
84 * @dataProvider provideTestAllPropsForSingleLanguage
85 */
86 public function testAllPropsForSingleLanguage( string $langCode, array $expected ) {
87 [ $response, $continue ] = $this->doQuery( [
88 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants|variantnames',
89 'licode' => $langCode,
90 ] );
91
92 $this->assertArrayEquals( [ $langCode => $expected ], $response );
93 }
94
95 public function testNameInOtherLanguageForSingleLanguage() {
96 [ $response, $continue ] = $this->doQuery( [
97 'liprop' => 'name',
98 'licode' => 'de',
99 'uselang' => 'pt',
100 ] );
101
102 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
103 }
104
105 /**
106 * Test ensures continuation is applied if the test runs for longer than allowed
107 *
108 * ApiQueryLanguageinfo::MAX_EXECUTE_SECONDS controls the speed the API has to have before
109 * applying continuation.
110 *
111 * @see T329609#8613954
112 */
113 public function testContinuationNecessary() {
114 $time = 0;
115 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
116 return $time += 1;
117 } );
118
119 [ $response, $continue ] = $this->doQuery( [] );
120
121 $this->assertCount( 2, $response );
122 $this->assertArrayHasKey( 'licontinue', $continue );
123 }
124
125 /**
126 * Test ensures continuation is applied if the test runs for longer than allowed
127 *
128 * ApiQueryLanguageinfo::MAX_EXECUTE_SECONDS controls the speed the API has to have before
129 * applying continuation.
130 *
131 * @see T329609#8613954
132 */
133 public function testContinuationNotNecessary() {
134 $time = 0;
135 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
136 return $time += 2;
137 } );
138
139 [ $response, $continue ] = $this->doQuery( [
140 'licode' => 'de',
141 ] );
142
143 $this->assertNull( $continue );
144 }
145
146 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
147 $time = 0;
148 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
149 return $time += 1;
150 } );
151 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
152
153 [ $response, $continue ] = $this->doQuery( $params );
154
155 $this->assertCount( 2, $response );
156 $this->assertArrayHasKey( 'licontinue', $continue );
157 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
158
159 $time = 0;
160 $params = $continue + $params;
161 [ $response, $continue ] = $this->doQuery( $params );
162
163 $this->assertCount( 2, $response );
164 $this->assertArrayHasKey( 'licontinue', $continue );
165 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
166
167 $time = 0;
168 $params = $continue + $params;
169 [ $response, $continue ] = $this->doQuery( $params );
170
171 $this->assertCount( 1, $response );
172 $this->assertNull( $continue );
173 $this->assertSame( [ 'zh' ], array_keys( $response ) );
174 }
175
176 public function testResponseHasModulePathEvenIfEmpty() {
177 [ $response, $continue ] = $this->doQuery( [ 'licode' => '' ] );
178 $this->assertSame( [], $response );
179 // the real test is that $res[0]['query']['languageinfo'] in doQuery() didn’t fail
180 }
181
182 }