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';
28 Language
::clearCaches();
31 private function doQuery( array $params, $microtimeFunction = null ): array {
34 'meta' => 'languageinfo',
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(
48 'class' => ApiQueryLanguageinfo
::class,
49 'factory' => function ( $parent, $name ) use ( $microtimeFunction ) {
50 return new ApiQueryLanguageinfo(
62 $res = $this->doApiRequest( $params );
64 $this->assertArrayNotHasKey( 'warnings', $res[0] );
66 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ??
null ];
69 public function testAllPropsForSingleLanguage() {
70 list( $response, $continue ) = $this->doQuery( [
71 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
75 $this->assertArrayEquals( [
79 'autonym' => 'srpskohrvatski / српскохрватски',
80 'name' => 'Serbo-Croatian',
81 'fallbacks' => [ 'bs', 'sr-el', 'hr' ],
83 'variants' => [ 'sh' ],
88 public function testAllPropsForSingleCustomLanguage() {
89 list( $response, $continue ) = $this->doQuery( [
90 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
91 'licode' => 'qtp', // reserved for local use by ISO 639; registered in setUp()
94 $this->assertArrayEquals( [
99 'name' => 'a custom language code MediaWiki knows nothing about',
102 'variants' => [ 'qtp' ],
107 public function testNameInOtherLanguageForSingleLanguage() {
108 list( $response, $continue ) = $this->doQuery( [
114 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
117 public function testContinuationNecessary() {
119 $microtimeFunction = function () use ( &$time ) {
120 return $time +
= 0.75;
123 list( $response, $continue ) = $this->doQuery( [], $microtimeFunction );
125 $this->assertCount( 2, $response );
126 $this->assertArrayHasKey( 'licontinue', $continue );
129 public function testContinuationNotNecessary() {
131 $microtimeFunction = function () use ( &$time ) {
135 list( $response, $continue ) = $this->doQuery( [
137 ], $microtimeFunction );
139 $this->assertNull( $continue );
142 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
144 $microtimeFunction = function () use ( &$time ) {
145 return $time +
= 0.75;
147 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
149 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
151 $this->assertCount( 2, $response );
152 $this->assertArrayHasKey( 'licontinue', $continue );
153 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
156 $params = $continue +
$params;
157 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
159 $this->assertCount( 2, $response );
160 $this->assertArrayHasKey( 'licontinue', $continue );
161 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
164 $params = $continue +
$params;
165 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
167 $this->assertCount( 1, $response );
168 $this->assertNull( $continue );
169 $this->assertSame( [ 'zh' ], array_keys( $response ) );
172 public function testResponseHasModulePathEvenIfEmpty() {
173 list( $response, $continue ) = $this->doQuery( [ 'licode' => '' ] );
174 $this->assertEmpty( $response );
175 // the real test is that $res[0]['query']['languageinfo'] in doQuery() didn’t fail