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