7 * @covers ApiQueryLanguageinfo
9 class ApiQueryLanguageinfoTest
extends ApiTestCase
{
11 protected function setUp() {
13 // register custom language names so this test is independent of CLDR
14 $this->setTemporaryHook(
15 'LanguageGetTranslatedLanguageNames',
16 function ( array &$names, $code ) {
19 $names['sh'] = 'Serbo-Croatian';
20 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
23 $names['de'] = 'alemão';
30 private function doQuery( array $params, $microtimeFunction = null ): array {
33 'meta' => 'languageinfo',
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(
46 ApiQueryLanguageinfo
::class,
47 function ( $parent, $name ) use ( $microtimeFunction ) {
48 return new ApiQueryLanguageinfo(
59 $res = $this->doApiRequest( $params );
61 $this->assertArrayNotHasKey( 'warnings', $res[0] );
63 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ??
null ];
66 public function testAllPropsForSingleLanguage() {
67 list( $response, $continue ) = $this->doQuery( [
68 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
72 $this->assertArrayEquals( [
76 'autonym' => 'srpskohrvatski / српскохрватски',
77 'name' => 'Serbo-Croatian',
78 'fallbacks' => [ 'bs', 'sr-el', 'hr' ],
80 'variants' => [ 'sh' ],
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()
91 $this->assertArrayEquals( [
96 'name' => 'a custom language code MediaWiki knows nothing about',
99 'variants' => [ 'qtp' ],
104 public function testNameInOtherLanguageForSingleLanguage() {
105 list( $response, $continue ) = $this->doQuery( [
111 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
114 public function testContinuationNecessary() {
116 $microtimeFunction = function () use ( &$time ) {
117 return $time +
= 0.75;
120 list( $response, $continue ) = $this->doQuery( [], $microtimeFunction );
122 $this->assertCount( 2, $response );
123 $this->assertArrayHasKey( 'licontinue', $continue );
126 public function testContinuationNotNecessary() {
128 $microtimeFunction = function () use ( &$time ) {
132 list( $response, $continue ) = $this->doQuery( [
134 ], $microtimeFunction );
136 $this->assertNull( $continue );
139 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
141 $microtimeFunction = function () use ( &$time ) {
142 return $time +
= 0.75;
144 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
146 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
148 $this->assertCount( 2, $response );
149 $this->assertArrayHasKey( 'licontinue', $continue );
150 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
153 $params = $continue +
$params;
154 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
156 $this->assertCount( 2, $response );
157 $this->assertArrayHasKey( 'licontinue', $continue );
158 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
161 $params = $continue +
$params;
162 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
164 $this->assertCount( 1, $response );
165 $this->assertNull( $continue );
166 $this->assertSame( [ 'zh' ], array_keys( $response ) );
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