Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiQueryLanguageinfoTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group medium
6 *
7 * @covers ApiQueryLanguageinfo
8 */
9 class ApiQueryLanguageinfoTest extends ApiTestCase {
10
11 protected function setUp() {
12 parent::setUp();
13 // register custom language names so this test is independent of CLDR
14 $this->setTemporaryHook(
15 'LanguageGetTranslatedLanguageNames',
16 function ( array &$names, $code ) {
17 switch ( $code ) {
18 case 'en':
19 $names['sh'] = 'Serbo-Croatian';
20 $names['qtp'] = 'a custom language code MediaWiki knows nothing about';
21 break;
22 case 'pt':
23 $names['de'] = 'alemão';
24 break;
25 }
26 }
27 );
28 Language::clearCaches();
29 }
30
31 private function doQuery( array $params, $microtimeFunction = null ): array {
32 $params += [
33 'action' => 'query',
34 'meta' => 'languageinfo',
35 'uselang' => 'en',
36 ];
37
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(
45 'languageinfo',
46 'meta',
47 [
48 'class' => ApiQueryLanguageinfo::class,
49 'factory' => function ( $parent, $name ) use ( $microtimeFunction ) {
50 return new ApiQueryLanguageinfo(
51 $parent,
52 $name,
53 $microtimeFunction
54 );
55 }
56 ]
57 );
58 }
59 );
60 }
61
62 $res = $this->doApiRequest( $params );
63
64 $this->assertArrayNotHasKey( 'warnings', $res[0] );
65
66 return [ $res[0]['query']['languageinfo'], $res[0]['continue'] ?? null ];
67 }
68
69 public function testAllPropsForSingleLanguage() {
70 list( $response, $continue ) = $this->doQuery( [
71 'liprop' => 'code|bcp47|dir|autonym|name|fallbacks|variants',
72 'licode' => 'sh',
73 ] );
74
75 $this->assertArrayEquals( [
76 'sh' => [
77 'code' => 'sh',
78 'bcp47' => 'sh',
79 'autonym' => 'srpskohrvatski / српскохрватски',
80 'name' => 'Serbo-Croatian',
81 'fallbacks' => [ 'bs', 'sr-el', 'hr' ],
82 'dir' => 'ltr',
83 'variants' => [ 'sh' ],
84 ],
85 ], $response );
86 }
87
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()
92 ] );
93
94 $this->assertArrayEquals( [
95 'qtp' => [
96 'code' => 'qtp',
97 'bcp47' => 'qtp',
98 'autonym' => '',
99 'name' => 'a custom language code MediaWiki knows nothing about',
100 'fallbacks' => [],
101 'dir' => 'ltr',
102 'variants' => [ 'qtp' ],
103 ],
104 ], $response );
105 }
106
107 public function testNameInOtherLanguageForSingleLanguage() {
108 list( $response, $continue ) = $this->doQuery( [
109 'liprop' => 'name',
110 'licode' => 'de',
111 'uselang' => 'pt',
112 ] );
113
114 $this->assertArrayEquals( [ 'de' => [ 'name' => 'alemão' ] ], $response );
115 }
116
117 public function testContinuationNecessary() {
118 $time = 0;
119 $microtimeFunction = function () use ( &$time ) {
120 return $time += 0.75;
121 };
122
123 list( $response, $continue ) = $this->doQuery( [], $microtimeFunction );
124
125 $this->assertCount( 2, $response );
126 $this->assertArrayHasKey( 'licontinue', $continue );
127 }
128
129 public function testContinuationNotNecessary() {
130 $time = 0;
131 $microtimeFunction = function () use ( &$time ) {
132 return $time += 1.5;
133 };
134
135 list( $response, $continue ) = $this->doQuery( [
136 'licode' => 'de',
137 ], $microtimeFunction );
138
139 $this->assertNull( $continue );
140 }
141
142 public function testContinuationInAlphabeticalOrderNotParameterOrder() {
143 $time = 0;
144 $microtimeFunction = function () use ( &$time ) {
145 return $time += 0.75;
146 };
147 $params = [ 'licode' => 'en|ru|zh|de|yue' ];
148
149 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
150
151 $this->assertCount( 2, $response );
152 $this->assertArrayHasKey( 'licontinue', $continue );
153 $this->assertSame( [ 'de', 'en' ], array_keys( $response ) );
154
155 $time = 0;
156 $params = $continue + $params;
157 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
158
159 $this->assertCount( 2, $response );
160 $this->assertArrayHasKey( 'licontinue', $continue );
161 $this->assertSame( [ 'ru', 'yue' ], array_keys( $response ) );
162
163 $time = 0;
164 $params = $continue + $params;
165 list( $response, $continue ) = $this->doQuery( $params, $microtimeFunction );
166
167 $this->assertCount( 1, $response );
168 $this->assertNull( $continue );
169 $this->assertSame( [ 'zh' ], array_keys( $response ) );
170 }
171
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
176 }
177
178 }