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