MediaWiki REL1_32
ApiPageSetTest.php
Go to the documentation of this file.
1<?php
2
10 public static function provideRedirectMergePolicy() {
11 return [
12 'By default nothing is merged' => [
13 null,
14 []
15 ],
16
17 'A simple merge policy adds the redirect data in' => [
18 function ( $current, $new ) {
19 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
20 $current['index'] = $new['index'];
21 }
22 return $current;
23 },
24 [ 'index' => 1 ],
25 ],
26 ];
27 }
28
32 public function testRedirectMergePolicyWithArrayResult( $mergePolicy, $expect ) {
33 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
34 $pageSet->setRedirectMergePolicy( $mergePolicy );
35 $result = [
36 $target->getArticleID() => []
37 ];
38 $pageSet->populateGeneratorData( $result );
39 $this->assertEquals( $expect, $result[$target->getArticleID()] );
40 }
41
45 public function testRedirectMergePolicyWithApiResult( $mergePolicy, $expect ) {
46 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
47 $pageSet->setRedirectMergePolicy( $mergePolicy );
48 $result = new ApiResult( false );
49 $result->addValue( null, 'pages', [
50 $target->getArticleID() => []
51 ] );
52 $pageSet->populateGeneratorData( $result, [ 'pages' ] );
53 $this->assertEquals(
54 $expect,
55 $result->getResultData( [ 'pages', $target->getArticleID() ] )
56 );
57 }
58
59 protected function createPageSetWithRedirect() {
60 $target = Title::makeTitle( NS_MAIN, 'UTRedirectTarget' );
61 $sourceA = Title::makeTitle( NS_MAIN, 'UTRedirectSourceA' );
62 $sourceB = Title::makeTitle( NS_MAIN, 'UTRedirectSourceB' );
63 self::editPage( 'UTRedirectTarget', 'api page set test' );
64 self::editPage( 'UTRedirectSourceA', '#REDIRECT [[UTRedirectTarget]]' );
65 self::editPage( 'UTRedirectSourceB', '#REDIRECT [[UTRedirectTarget]]' );
66
67 $request = new FauxRequest( [ 'redirects' => 1 ] );
69 $context->setRequest( $request );
70 $main = new ApiMain( $context );
71 $pageSet = new ApiPageSet( $main );
72
73 $pageSet->setGeneratorData( $sourceA, [ 'index' => 1 ] );
74 $pageSet->setGeneratorData( $sourceB, [ 'index' => 3 ] );
75 $pageSet->populateFromTitles( [ $sourceA, $sourceB ] );
76
77 return [ $target, $pageSet ];
78 }
79
80 public function testHandleNormalization() {
82 $context->setRequest( new FauxRequest( [ 'titles' => "a|B|a\xcc\x8a" ] ) );
83 $main = new ApiMain( $context );
84 $pageSet = new ApiPageSet( $main );
85 $pageSet->execute();
86
87 $this->assertSame(
88 [ 0 => [ 'A' => -1, 'B' => -2, 'Å' => -3 ] ],
89 $pageSet->getAllTitlesByNamespace()
90 );
91 $this->assertSame(
92 [
93 [ 'fromencoded' => true, 'from' => 'a%CC%8A', 'to' => 'å' ],
94 [ 'fromencoded' => false, 'from' => 'a', 'to' => 'A' ],
95 [ 'fromencoded' => false, 'from' => 'å', 'to' => 'Å' ],
96 ],
97 $pageSet->getNormalizedTitlesAsResult()
98 );
99 }
100
101 public function testSpecialRedirects() {
102 $id1 = self::editPage( 'UTApiPageSet', 'UTApiPageSet in the default language' )
103 ->value['revision']->getTitle()->getArticleID();
104 $id2 = self::editPage( 'UTApiPageSet/de', 'UTApiPageSet in German' )
105 ->value['revision']->getTitle()->getArticleID();
106
107 $user = $this->getTestUser()->getUser();
108 $userName = $user->getName();
109 $userDbkey = str_replace( ' ', '_', $userName );
110 $request = new FauxRequest( [
111 'titles' => implode( '|', [
112 'Special:MyContributions',
113 'Special:MyPage',
114 'Special:MyTalk/subpage',
115 'Special:MyLanguage/UTApiPageSet',
116 ] ),
117 ] );
118 $context = new RequestContext();
119 $context->setRequest( $request );
120 $context->setUser( $user );
121
122 $main = new ApiMain( $context );
123 $pageSet = new ApiPageSet( $main );
124 $pageSet->execute();
125
126 $this->assertEquals( [
127 ], $pageSet->getRedirectTitlesAsResult() );
128 $this->assertEquals( [
129 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
130 [ 'ns' => -1, 'title' => 'Special:MyPage', 'special' => true ],
131 [ 'ns' => -1, 'title' => 'Special:MyTalk/subpage', 'special' => true ],
132 [ 'ns' => -1, 'title' => 'Special:MyLanguage/UTApiPageSet', 'special' => true ],
133 ], $pageSet->getInvalidTitlesAndRevisions() );
134 $this->assertEquals( [
135 ], $pageSet->getAllTitlesByNamespace() );
136
137 $request->setVal( 'redirects', 1 );
138 $main = new ApiMain( $context );
139 $pageSet = new ApiPageSet( $main );
140 $pageSet->execute();
141
142 $this->assertEquals( [
143 [ 'from' => 'Special:MyPage', 'to' => "User:$userName" ],
144 [ 'from' => 'Special:MyTalk/subpage', 'to' => "User talk:$userName/subpage" ],
145 [ 'from' => 'Special:MyLanguage/UTApiPageSet', 'to' => 'UTApiPageSet' ],
146 ], $pageSet->getRedirectTitlesAsResult() );
147 $this->assertEquals( [
148 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
149 [ 'ns' => 2, 'title' => "User:$userName", 'missing' => true ],
150 [ 'ns' => 3, 'title' => "User talk:$userName/subpage", 'missing' => true ],
151 ], $pageSet->getInvalidTitlesAndRevisions() );
152 $this->assertEquals( [
153 0 => [ 'UTApiPageSet' => $id1 ],
154 2 => [ $userDbkey => -2 ],
155 3 => [ "$userDbkey/subpage" => -3 ],
156 ], $pageSet->getAllTitlesByNamespace() );
157
158 $context->setLanguage( 'de' );
159 $main = new ApiMain( $context );
160 $pageSet = new ApiPageSet( $main );
161 $pageSet->execute();
162
163 $this->assertEquals( [
164 [ 'from' => 'Special:MyPage', 'to' => "User:$userName" ],
165 [ 'from' => 'Special:MyTalk/subpage', 'to' => "User talk:$userName/subpage" ],
166 [ 'from' => 'Special:MyLanguage/UTApiPageSet', 'to' => 'UTApiPageSet/de' ],
167 ], $pageSet->getRedirectTitlesAsResult() );
168 $this->assertEquals( [
169 [ 'ns' => -1, 'title' => 'Special:MyContributions', 'special' => true ],
170 [ 'ns' => 2, 'title' => "User:$userName", 'missing' => true ],
171 [ 'ns' => 3, 'title' => "User talk:$userName/subpage", 'missing' => true ],
172 ], $pageSet->getInvalidTitlesAndRevisions() );
173 $this->assertEquals( [
174 0 => [ 'UTApiPageSet/de' => $id2 ],
175 2 => [ $userDbkey => -2 ],
176 3 => [ "$userDbkey/subpage" => -3 ],
177 ], $pageSet->getAllTitlesByNamespace() );
178 }
179}
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:41
API medium Database ApiPageSet.
static provideRedirectMergePolicy()
testRedirectMergePolicyWithApiResult( $mergePolicy, $expect)
provideRedirectMergePolicy
testRedirectMergePolicyWithArrayResult( $mergePolicy, $expect)
provideRedirectMergePolicy
This class contains a list of pages that the client has requested.
This class represents the result of the API operations.
Definition ApiResult.php:35
WebRequest clone which takes values from a provided array.
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Group all the pieces relevant to the context of a request into one instance.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2880
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2885
const NS_MAIN
Definition Defines.php:64