3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\Tests\Api\ApiTestCase
;
6 use Wikimedia\Timestamp\ConvertibleTimestamp
;
12 * @covers \MediaWiki\Api\ApiQueryLanguageinfo
14 class ApiQueryLanguageinfoTest
extends ApiTestCase
{
16 protected function setUp(): void
{
18 // register custom language names so this test is independent of CLDR
19 $this->setTemporaryHook(
20 'LanguageGetTranslatedLanguageNames',
21 static function ( array &$names, $code ) {
24 $names['sh'] = 'Serbo-Croatian';
25 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
28 $names['de'] = 'alemão';
35 private function doQuery( array $params ): array {
38 'meta' => 'languageinfo',
42 $res = $this->doApiRequest( $params );
44 $this->assertArrayNotHasKey( 'warnings', $res[0] );
46 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ??
null ];
49 public static function provideTestAllPropsForSingleLanguage() {
55 'autonym' => 'српски / srpski',
56 'name' => 'српски / srpski',
57 'fallbacks' => [ 'sr-ec', 'sr-cyrl', 'sr-el', 'sr-latn' ],
59 'variants' => [ 'sr', 'sr-ec', 'sr-el' ],
62 'sr-ec' => 'Ћирилица',
63 'sr-el' => 'Latinica',
69 'qtp', // reserved for local use by ISO 639; registered in setUp()
74 'name' => 'a custom language code MediaWiki knows nothing about',
77 'variants' => [ 'qtp' ],
78 'variantnames' => [ 'qtp' => 'qtp' ],
84 * @dataProvider provideTestAllPropsForSingleLanguage
86 public function testAllPropsForSingleLanguage( string $langCode, array $expected ) {
87 [ $response, $continue ] = $this->doQuery( [
88 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants|variantnames',
89 'licode' => $langCode,
92 $this->assertArrayEquals( [ $langCode => $expected ], $response );
95 public function testNameInOtherLanguageForSingleLanguage() {
96 [ $response, $continue ] = $this->doQuery( [
102 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
106 * Test ensures continuation is applied if the test runs for longer than allowed
108 * ApiQueryLanguageinfo::MAX_EXECUTE_SECONDS controls the speed the API has to have before
109 * applying continuation.
111 * @see T329609#8613954
113 public function testContinuationNecessary() {
115 ConvertibleTimestamp
::setFakeTime( static function () use ( &$time ) {
120 [ $response, $continue ] = $this->doQuery( [] );
122 $this->assertCount( 2, $response );
123 $this->assertArrayHasKey( 'licontinue', $continue );
127 * Test ensures continuation is applied if the test runs for longer than allowed
129 * ApiQueryLanguageinfo::MAX_EXECUTE_SECONDS controls the speed the API has to have before
130 * applying continuation.
132 * @see T329609#8613954
134 public function testContinuationNotNecessary() {
136 ConvertibleTimestamp
::setFakeTime( static function () use ( &$time ) {
141 [ $response, $continue ] = $this->doQuery( [
145 $this->assertNull( $continue );
148 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
150 ConvertibleTimestamp
::setFakeTime( static function () use ( &$time ) {
154 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
156 [ $response, $continue ] = $this->doQuery( $params );
158 $this->assertCount( 2, $response );
159 $this->assertArrayHasKey( 'licontinue', $continue );
160 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
163 $params = $continue +
$params;
164 [ $response, $continue ] = $this->doQuery( $params );
166 $this->assertCount( 2, $response );
167 $this->assertArrayHasKey( 'licontinue', $continue );
168 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
171 $params = $continue +
$params;
172 [ $response, $continue ] = $this->doQuery( $params );
174 $this->assertCount( 1, $response );
175 $this->assertNull( $continue );
176 $this->assertSame( [ 'zh' ], array_keys( $response ) );
179 public function testResponseHasModulePathEvenIfEmpty() {
180 [ $response, $continue ] = $this->doQuery( [ 'licode' => '' ] );
181 $this->assertSame( [], $response );
182 // the real test is that $res[0]['query']['languageinfo'] in doQuery() didn’t fail