Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryLanguageinfoTest.php
bloba7543b67b8a2ce3d7ef1a1e3283156a46104d419
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use MediaWiki\Tests\Api\ApiTestCase;
6 use Wikimedia\Timestamp\ConvertibleTimestamp;
8 /**
9 * @group API
10 * @group medium
12 * @covers \MediaWiki\Api\ApiQueryLanguageinfo
14 class ApiQueryLanguageinfoTest extends ApiTestCase {
16 protected function setUp(): void {
17 parent::setUp();
18 // register custom language names so this test is independent of CLDR
19 $this->setTemporaryHook(
20 'LanguageGetTranslatedLanguageNames',
21 static function ( array &$names, $code ) {
22 switch ( $code ) {
23 case 'en':
24 $names['sh'] = 'Serbo-Croatian';
25 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
26 break;
27 case 'pt':
28 $names['de'] = 'alemão';
29 break;
35 private function doQuery( array $params ): array {
36 $params += [
37 'action' => 'query',
38 'meta' => 'languageinfo',
39 'uselang' => 'en',
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() {
50 yield [
51 'sr',
53 'code' => 'sr',
54 'bcp47' => 'sr',
55 'autonym' => 'српски / srpski',
56 'name' => 'српски / srpski',
57 'fallbacks' => [ 'sr-ec', 'sr-cyrl', 'sr-el', 'sr-latn' ],
58 'dir' => 'ltr',
59 'variants' => [ 'sr', 'sr-ec', 'sr-el' ],
60 'variantnames' => [
61 'sr' => 'Ћир./lat.',
62 'sr-ec' => 'Ћирилица',
63 'sr-el' => 'Latinica',
68 yield [
69 'qtp', // reserved for local use by ISO 639; registered in setUp()
71 'code' => 'qtp',
72 'bcp47' => 'qtp',
73 'autonym' => '',
74 'name' => 'a custom language code MediaWiki knows nothing about',
75 'fallbacks' => [],
76 'dir' => 'ltr',
77 'variants' => [ 'qtp' ],
78 'variantnames' => [ 'qtp' => 'qtp' ],
83 /**
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,
90 ] );
92 $this->assertArrayEquals( [ $langCode => $expected ], $response );
95 public function testNameInOtherLanguageForSingleLanguage() {
96 [ $response, $continue ] = $this->doQuery( [
97 'liprop' => 'name',
98 'licode' => 'de',
99 'uselang' => 'pt',
100 ] );
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() {
114 $time = 0;
115 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
116 $time++;
117 return $time;
118 } );
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() {
135 $time = 0;
136 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
137 $time += 2;
138 return $time;
139 } );
141 [ $response, $continue ] = $this->doQuery( [
142 'licode' => 'de',
143 ] );
145 $this->assertNull( $continue );
148 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
149 $time = 0;
150 ConvertibleTimestamp::setFakeTime( static function () use ( &$time ) {
151 $time++;
152 return $time;
153 } );
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 ) );
162 $time = 0;
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 ) );
170 $time = 0;
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