3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\TestingAccessWrapper
;
12 class ApiPageSetTest
extends ApiTestCase
{
13 public static function provideRedirectMergePolicy() {
15 'By default nothing is merged' => [
20 'A simple merge policy adds the redirect data in' => [
21 function ( $current, $new ) {
22 if ( !isset( $current['index'] ) ||
$new['index'] < $current['index'] ) {
23 $current['index'] = $new['index'];
33 * @dataProvider provideRedirectMergePolicy
35 public function testRedirectMergePolicyWithArrayResult( $mergePolicy, $expect ) {
36 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
37 $pageSet->setRedirectMergePolicy( $mergePolicy );
39 $target->getArticleID() => []
41 $pageSet->populateGeneratorData( $result );
42 $this->assertEquals( $expect, $result[$target->getArticleID()] );
46 * @dataProvider provideRedirectMergePolicy
48 public function testRedirectMergePolicyWithApiResult( $mergePolicy, $expect ) {
49 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
50 $pageSet->setRedirectMergePolicy( $mergePolicy );
51 $result = new ApiResult( false );
52 $result->addValue( null, 'pages', [
53 $target->getArticleID() => []
55 $pageSet->populateGeneratorData( $result, [ 'pages' ] );
58 $result->getResultData( [ 'pages', $target->getArticleID() ] )
62 protected function createPageSetWithRedirect() {
63 $target = Title
::makeTitle( NS_MAIN
, 'UTRedirectTarget' );
64 $sourceA = Title
::makeTitle( NS_MAIN
, 'UTRedirectSourceA' );
65 $sourceB = Title
::makeTitle( NS_MAIN
, 'UTRedirectSourceB' );
66 self
::editPage( 'UTRedirectTarget', 'api page set test' );
67 self
::editPage( 'UTRedirectSourceA', '#REDIRECT [[UTRedirectTarget]]' );
68 self
::editPage( 'UTRedirectSourceB', '#REDIRECT [[UTRedirectTarget]]' );
70 $request = new FauxRequest( [ 'redirects' => 1 ] );
71 $context = new RequestContext();
72 $context->setRequest( $request );
73 $main = new ApiMain( $context );
74 $pageSet = new ApiPageSet( $main );
76 $pageSet->setGeneratorData( $sourceA, [ 'index' => 1 ] );
77 $pageSet->setGeneratorData( $sourceB, [ 'index' => 3 ] );
78 $pageSet->populateFromTitles( [ $sourceA, $sourceB ] );
80 return [ $target, $pageSet ];
83 public function testHandleNormalization() {
84 $context = new RequestContext();
85 $context->setRequest( new FauxRequest( [ 'titles' => "a|B|a\xcc\x8a" ] ) );
86 $main = new ApiMain( $context );
87 $pageSet = new ApiPageSet( $main );
91 [ 0 => [ 'A' => -1, 'B' => -2, 'Å' => -3 ] ],
92 $pageSet->getAllTitlesByNamespace()
96 [ 'fromencoded' => true, 'from' => 'a%CC%8A', 'to' => 'å' ],
97 [ 'fromencoded' => false, 'from' => 'a', 'to' => 'A' ],
98 [ 'fromencoded' => false, 'from' => 'å', 'to' => 'Å' ],
100 $pageSet->getNormalizedTitlesAsResult()
104 public function testSpecialRedirects() {
105 $id1 = self
::editPage( 'UTApiPageSet', 'UTApiPageSet in the default language' )
106 ->value
['revision']->getTitle()->getArticleID();
107 $id2 = self
::editPage( 'UTApiPageSet/de', 'UTApiPageSet in German' )
108 ->value
['revision']->getTitle()->getArticleID();
110 $user = $this->getTestUser()->getUser();
111 $userName = $user->getName();
112 $userDbkey = str_replace( ' ', '_', $userName );
113 $request = new FauxRequest( [
114 'titles' => implode( '|', [
115 'Special:MyContributions',
117 'Special:MyTalk/subpage',
118 'Special:MyLanguage/UTApiPageSet',
121 $context = new RequestContext();
122 $context->setRequest( $request );
123 $context->setUser( $user );
125 $main = new ApiMain( $context );
126 $pageSet = new ApiPageSet( $main );
129 $this->assertEquals( [
130 ], $pageSet->getRedirectTitlesAsResult() );
131 $this->assertEquals( [
132 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
133 [ 'ns' => -1, 'title' => 'Special:MyPage', 'special' => true ],
134 [ 'ns' => -1, 'title' => 'Special:MyTalk/subpage', 'special' => true ],
135 [ 'ns' => -1, 'title' => 'Special:MyLanguage/UTApiPageSet', 'special' => true ],
136 ], $pageSet->getInvalidTitlesAndRevisions() );
137 $this->assertEquals( [
138 ], $pageSet->getAllTitlesByNamespace() );
140 $request->setVal( 'redirects', 1 );
141 $main = new ApiMain( $context );
142 $pageSet = new ApiPageSet( $main );
145 $this->assertEquals( [
146 [ 'from' => 'Special:MyPage', 'to' => "User:$userName" ],
147 [ 'from' => 'Special:MyTalk/subpage', 'to' => "User talk:$userName/subpage" ],
148 [ 'from' => 'Special:MyLanguage/UTApiPageSet', 'to' => 'UTApiPageSet' ],
149 ], $pageSet->getRedirectTitlesAsResult() );
150 $this->assertEquals( [
151 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
152 [ 'ns' => 2, 'title' => "User:$userName", 'missing' => true ],
153 [ 'ns' => 3, 'title' => "User talk:$userName/subpage", 'missing' => true ],
154 ], $pageSet->getInvalidTitlesAndRevisions() );
155 $this->assertEquals( [
156 0 => [ 'UTApiPageSet' => $id1 ],
157 2 => [ $userDbkey => -2 ],
158 3 => [ "$userDbkey/subpage" => -3 ],
159 ], $pageSet->getAllTitlesByNamespace() );
161 $context->setLanguage( 'de' );
162 $main = new ApiMain( $context );
163 $pageSet = new ApiPageSet( $main );
166 $this->assertEquals( [
167 [ 'from' => 'Special:MyPage', 'to' => "User:$userName" ],
168 [ 'from' => 'Special:MyTalk/subpage', 'to' => "User talk:$userName/subpage" ],
169 [ 'from' => 'Special:MyLanguage/UTApiPageSet', 'to' => 'UTApiPageSet/de' ],
170 ], $pageSet->getRedirectTitlesAsResult() );
171 $this->assertEquals( [
172 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
173 [ 'ns' => 2, 'title' => "User:$userName", 'missing' => true ],
174 [ 'ns' => 3, 'title' => "User talk:$userName/subpage", 'missing' => true ],
175 ], $pageSet->getInvalidTitlesAndRevisions() );
176 $this->assertEquals( [
177 0 => [ 'UTApiPageSet/de' => $id2 ],
178 2 => [ $userDbkey => -2 ],
179 3 => [ "$userDbkey/subpage" => -3 ],
180 ], $pageSet->getAllTitlesByNamespace() );
184 * Test that ApiPageSet is calling GenderCache for provided user names to prefill the
185 * GenderCache and avoid a performance issue when loading each users' gender on it's own.
186 * The test is setting the "missLimit" to 0 on the GenderCache to trigger misses logic.
187 * When the "misses" property is no longer 0 at the end of the test,
188 * something was requested which is not part of the cache. Than the test is failing.
190 public function testGenderCaching() {
191 // Set up the user namespace to have gender aliases to trigger the gender cache
192 $this->setMwGlobals( [
193 'wgExtraGenderNamespaces' => [ NS_USER
=> [ 'male' => 'Male', 'female' => 'Female' ] ]
195 $this->overrideMwServices();
197 // User names to test with - it is not needed that the user exists in the database
198 // to trigger gender cache
205 // Prepare the gender cache for testing - this is a fresh instance due to service override
206 $genderCache = TestingAccessWrapper
::newFromObject(
207 MediaWikiServices
::getInstance()->getGenderCache()
209 $genderCache->missLimit
= 0;
211 // Do an api request to trigger ApiPageSet code
212 $this->doApiRequest( [
214 'titles' => 'User:' . implode( '|User:', $userNames ),
217 $this->assertEquals( 0, $genderCache->misses
,
218 'ApiPageSet does not prefill the gender cache correctly' );
219 $this->assertEquals( $userNames, array_keys( $genderCache->cache
),
220 'ApiPageSet does not prefill all users into the gender cache' );