Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiQueryLanguageinfoTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group medium
6 *
7 * @covers ApiQueryLanguageinfo
8 */
9 class ApiQueryLanguageinfoTest extends ApiTestCase {
10
11 protected function setUp() {
12 parent::setUp();
13 // register custom language names so this test is independent of CLDR
14 $this->setTemporaryHook(
15 'LanguageGetTranslatedLanguageNames',
16 function ( array &$names, $code ) {
17 switch ( $code ) {
18 case 'en':
19 $names['sh'] = 'Serbo-Croatian';
20 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
21 break;
22 case 'pt':
23 $names['de'] = 'alemão';
24 break;
25 }
26 }
27 );
28 Language::clearCaches();
29 }
30
31 private function doQuery( array $params, $microtimeFunction = null ): array {
32 $params += [
33 'action' => 'query',
34 'meta' => 'languageinfo',
35 'uselang' => 'en',
36 ];
37
38 if ( $microtimeFunction !== null ) {
39 // hook into the module manager to override the factory function
40 // so we can call the constructor with the custom $microtimeFunction
41 $this->setTemporaryHook(
42 'ApiQuery::moduleManager',
43 function ( ApiModuleManager $moduleManager ) use ( $microtimeFunction ) {
44 $moduleManager->addModule(
45 'languageinfo',
46 'meta',
47 ApiQueryLanguageinfo::class,
48 function ( $parent, $name ) use ( $microtimeFunction ) {
49 return new ApiQueryLanguageinfo(
50 $parent,
51 $name,
52 $microtimeFunction
53 );
54 }
55 );
56 }
57 );
58 }
59
60 $res = $this->doApiRequest( $params );
61
62 $this->assertArrayNotHasKey( 'warnings', $res[0] );
63
64 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ?? null ];
65 }
66
67 public function testAllPropsForSingleLanguage() {
68 list( $response, $continue ) = $this->doQuery( [
69 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
70 'licode' => 'sh',
71 ] );
72
73 $this->assertArrayEquals( [
74 'sh' => [
75 'code' => 'sh',
76 'bcp47' => 'sh',
77 'autonym' => 'srpskohrvatski / српскохрватски',
78 'name' => 'Serbo-Croatian',
79 'fallbacks' => [ 'bs', 'sr-el', 'hr' ],
80 'dir' => 'ltr',
81 'variants' => [ 'sh' ],
82 ],
83 ], $response );
84 }
85
86 public function testAllPropsForSingleCustomLanguage() {
87 list( $response, $continue ) = $this->doQuery( [
88 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
89 'licode' => 'qtp', // reserved for local use by ISO 639; registered in setUp()
90 ] );
91
92 $this->assertArrayEquals( [
93 'qtp' => [
94 'code' => 'qtp',
95 'bcp47' => 'qtp',
96 'autonym' => '',
97 'name' => 'a custom language code MediaWiki knows nothing about',
98 'fallbacks' => [],
99 'dir' => 'ltr',
100 'variants' => [ 'qtp' ],
101 ],
102 ], $response );
103 }
104
105 public function testNameInOtherLanguageForSingleLanguage() {
106 list( $response, $continue ) = $this->doQuery( [
107 'liprop' => 'name',
108 'licode' => 'de',
109 'uselang' => 'pt',
110 ] );
111
112 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
113 }
114
115 public function testContinuationNecessary() {
116 $time = 0;
117 $microtimeFunction = function () use ( &$time ) {
118 return $time += 0.75;
119 };
120
121 list( $response, $continue ) = $this->doQuery( [], $microtimeFunction );
122
123 $this->assertCount( 2, $response );
124 $this->assertArrayHasKey( 'licontinue', $continue );
125 }
126
127 public function testContinuationNotNecessary() {
128 $time = 0;
129 $microtimeFunction = function () use ( &$time ) {
130 return $time += 1.5;
131 };
132
133 list( $response, $continue ) = $this->doQuery( [
134 'licode' => 'de',
135 ], $microtimeFunction );
136
137 $this->assertNull( $continue );
138 }
139
140 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
141 $time = 0;
142 $microtimeFunction = function () use ( &$time ) {
143 return $time += 0.75;
144 };
145 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
146
147 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
148
149 $this->assertCount( 2, $response );
150 $this->assertArrayHasKey( 'licontinue', $continue );
151 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
152
153 $time = 0;
154 $params = $continue + $params;
155 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
156
157 $this->assertCount( 2, $response );
158 $this->assertArrayHasKey( 'licontinue', $continue );
159 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
160
161 $time = 0;
162 $params = $continue + $params;
163 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
164
165 $this->assertCount( 1, $response );
166 $this->assertNull( $continue );
167 $this->assertSame( [ 'zh' ], array_keys( $response ) );
168 }
169
170 public function testResponseHasModulePathEvenIfEmpty() {
171 list( $response, $continue ) = $this->doQuery( [ 'licode' => '' ] );
172 $this->assertEmpty( $response );
173 // the real test is that $res[0]['query']['languageinfo'] in doQuery() didn’t fail
174 }
175
176 }