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(
47 ApiQueryLanguageinfo
::class,
48 function ( $parent, $name ) use ( $microtimeFunction ) {
49 return new ApiQueryLanguageinfo(
60 $res = $this->doApiRequest( $params );
62 $this->assertArrayNotHasKey( 'warnings', $res[0] );
64 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ??
null ];
67 public function testAllPropsForSingleLanguage() {
68 list( $response, $continue ) = $this->doQuery( [
69 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
73 $this->assertArrayEquals( [
77 'autonym' => 'srpskohrvatski / српскохрватски',
78 'name' => 'Serbo-Croatian',
79 'fallbacks' => [ 'bs', 'sr-el', 'hr' ],
81 'variants' => [ 'sh' ],
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()
92 $this->assertArrayEquals( [
97 'name' => 'a custom language code MediaWiki knows nothing about',
100 'variants' => [ 'qtp' ],
105 public function testNameInOtherLanguageForSingleLanguage() {
106 list( $response, $continue ) = $this->doQuery( [
112 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
115 public function testContinuationNecessary() {
117 $microtimeFunction = function () use ( &$time ) {
118 return $time +
= 0.75;
121 list( $response, $continue ) = $this->doQuery( [], $microtimeFunction );
123 $this->assertCount( 2, $response );
124 $this->assertArrayHasKey( 'licontinue', $continue );
127 public function testContinuationNotNecessary() {
129 $microtimeFunction = function () use ( &$time ) {
133 list( $response, $continue ) = $this->doQuery( [
135 ], $microtimeFunction );
137 $this->assertNull( $continue );
140 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
142 $microtimeFunction = function () use ( &$time ) {
143 return $time +
= 0.75;
145 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
147 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
149 $this->assertCount( 2, $response );
150 $this->assertArrayHasKey( 'licontinue', $continue );
151 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
154 $params = $continue +
$params;
155 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
157 $this->assertCount( 2, $response );
158 $this->assertArrayHasKey( 'licontinue', $continue );
159 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
162 $params = $continue +
$params;
163 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
165 $this->assertCount( 1, $response );
166 $this->assertNull( $continue );
167 $this->assertSame( [ 'zh' ], array_keys( $response ) );
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