3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\Block\DatabaseBlock
;
6 use MediaWiki\Context\RequestContext
;
7 use MediaWiki\MainConfigNames
;
8 use MediaWiki\Tests\Api\ApiTestCase
;
9 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
10 use MediaWiki\Title\Title
;
11 use MediaWiki\User\User
;
12 use Wikimedia\Timestamp\ConvertibleTimestamp
;
19 * @covers \MediaWiki\Api\ApiQueryInfo
21 class ApiQueryInfoTest
extends ApiTestCase
{
22 use TempUserTestTrait
;
24 protected function setUp(): void
{
27 $this->overrideConfigValues( [
28 MainConfigNames
::WatchlistExpiry
=> true,
29 MainConfigNames
::WatchlistExpiryMaxDuration
=> '6 months',
33 public function testExecute() {
34 // Mock time for a deterministic result, without cut off from dynamic "max duration"
35 ConvertibleTimestamp
::setFakeTime( '2011-01-01T00:00:00Z' );
37 $page = $this->getExistingTestPage( 'Pluto' );
38 $title = $page->getTitle();
39 $user = $this->getTestUser()->getUser();
40 RequestContext
::getMain()->setUser( $user );
41 $this->getServiceContainer()->getWatchlistManager()->addWatch(
45 '2011-04-01T00:00:00Z'
48 [ $data ] = $this->doApiRequest( [
51 'inprop' => 'watched|notificationtimestamp',
52 'titles' => $title->getText() . '|' . 'NonExistingPage_lkasdoiewlmasdoiwem7483',
53 ], null, false, $user );
55 $this->assertArrayHasKey( 'query', $data );
56 $this->assertArrayHasKey( 'pages', $data['query'] );
57 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
59 $info = $data['query']['pages'][$page->getId()];
60 $this->assertSame( $page->getId(), $info['pageid'] );
61 $this->assertSame( NS_MAIN
, $info['ns'] );
62 $this->assertSame( 'Pluto', $info['title'] );
63 $this->assertSame( 'wikitext', $info['contentmodel'] );
64 $this->assertSame( 'en', $info['pagelanguage'] );
65 $this->assertSame( 'en', $info['pagelanguagehtmlcode'] );
66 $this->assertSame( 'ltr', $info['pagelanguagedir'] );
67 $this->assertSame( '2011-01-01T00:00:00Z', $info['touched'] );
68 $this->assertSame( $title->getLatestRevID(), $info['lastrevid'] );
69 $this->assertSame( $title->getLength(), $info['length'] );
70 $this->assertSame( true, $info['new'] );
71 $this->assertSame( true, $info['watched'] );
72 $this->assertSame( '2011-04-01T00:00:00Z', $info['watchlistexpiry'] );
73 $this->assertArrayNotHasKey( 'actions', $info );
74 $this->assertArrayNotHasKey( 'linkclasses', $info );
77 public function testExecuteLinkClasses() {
78 $page = $this->getExistingTestPage( 'Pluto' );
79 $title = $page->getTitle();
81 [ $data ] = $this->doApiRequest( [
84 'titles' => $title->getText(),
85 'inprop' => 'linkclasses',
86 'inlinkcontext' => $title->getText(),
89 $this->assertArrayHasKey( 'query', $data );
90 $this->assertArrayHasKey( 'pages', $data['query'] );
91 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
93 $info = $data['query']['pages'][$page->getId()];
94 $this->assertArrayHasKey( 'linkclasses', $info );
95 $this->assertEquals( [], $info['linkclasses'] );
98 public function testExecuteEditActions() {
99 $page = $this->getExistingTestPage( 'Pluto' );
100 $title = $page->getTitle();
102 [ $data ] = $this->doApiRequest( [
105 'titles' => $title->getText(),
106 'intestactions' => 'edit'
109 $this->assertArrayHasKey( 'query', $data );
110 $this->assertArrayHasKey( 'pages', $data['query'] );
111 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
113 $info = $data['query']['pages'][$page->getId()];
114 $this->assertArrayHasKey( 'actions', $info );
115 $this->assertArrayHasKey( 'edit', $info['actions'] );
116 $this->assertTrue( $info['actions']['edit'] );
119 public function testExecuteEditActionsAutoCreate() {
120 $page = $this->getExistingTestPage( 'Pluto' );
121 $title = $page->getTitle();
124 $this->disableAutoCreateTempUser();
125 [ $data ] = $this->doApiRequest( [
128 'titles' => $title->getText(),
129 'intestactions' => 'edit',
130 'intestactionsautocreate' => true,
131 ], null, false, new User() );
132 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit'];
133 $this->assertFalse( $result );
136 $this->setGroupPermissions( '*', 'createaccount', true );
137 $this->enableAutoCreateTempUser();
138 [ $data ] = $this->doApiRequest( [
141 'titles' => $title->getText(),
142 'intestactions' => 'edit',
143 'intestactionsautocreate' => true,
144 ], null, false, new User() );
145 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit'];
146 $this->assertTrue( $result );
148 [ $data ] = $this->doApiRequest( [
151 'titles' => $title->getText(),
152 'intestactions' => 'create',
153 'intestactionsautocreate' => true,
154 ], null, false, new User() );
155 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['create'];
156 $this->assertTrue( $result );
158 // Enabled - 'read' is not an autocreate action
159 [ $data ] = $this->doApiRequest( [
162 'titles' => $title->getText(),
163 'intestactions' => 'read',
164 'intestactionsautocreate' => true,
165 ], null, false, new User() );
166 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['read'];
167 $this->assertFalse( $result );
169 // Enabled - but the user is logged in
170 [ $data ] = $this->doApiRequest( [
173 'titles' => $title->getText(),
174 'intestactions' => 'edit',
175 'intestactionsautocreate' => true,
176 ], null, false, static::getTestSysop()->getAuthority() );
177 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit'];
178 $this->assertFalse( $result );
180 // Enabled - but the user isn't allowed to create accounts
181 $this->setGroupPermissions( '*', 'createaccount', false );
182 [ $data ] = $this->doApiRequest( [
185 'titles' => $title->getText(),
186 'intestactions' => 'edit',
187 'intestactionsautocreate' => true,
188 ], null, false, new User() );
189 $result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit'];
190 $this->assertFalse( $result );
193 public function testExecuteEditActionsFull() {
194 $page = $this->getExistingTestPage( 'Pluto' );
195 $title = $page->getTitle();
197 [ $data ] = $this->doApiRequest( [
200 'titles' => $title->getText(),
201 'intestactions' => 'edit',
202 'intestactionsdetail' => 'full',
205 $this->assertArrayHasKey( 'query', $data );
206 $this->assertArrayHasKey( 'pages', $data['query'] );
207 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
209 $info = $data['query']['pages'][$page->getId()];
210 $this->assertArrayHasKey( 'actions', $info );
211 $this->assertArrayHasKey( 'edit', $info['actions'] );
212 $this->assertIsArray( $info['actions']['edit'] );
213 $this->assertSame( [], $info['actions']['edit'] );
216 public function testExecuteEditActionsFullBlock() {
217 $badActor = $this->getTestUser()->getUser();
218 $sysop = $this->getTestSysop()->getUser();
220 $block = new DatabaseBlock( [
221 'address' => $badActor,
223 'expiry' => 'infinity',
225 'enableAutoblock' => true,
228 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
229 $blockStore->insertBlock( $block );
231 $page = $this->getExistingTestPage( 'Pluto' );
232 $title = $page->getTitle();
234 [ $data ] = $this->doApiRequest( [
237 'titles' => $title->getText(),
238 'intestactions' => 'edit',
239 'intestactionsdetail' => 'full',
240 ], null, false, $badActor );
242 $this->assertArrayHasKey( 'query', $data );
243 $this->assertArrayHasKey( 'pages', $data['query'] );
244 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
246 $info = $data['query']['pages'][$page->getId()];
247 $this->assertArrayHasKey( 'actions', $info );
248 $this->assertArrayHasKey( 'edit', $info['actions'] );
249 $this->assertIsArray( $info['actions']['edit'] );
250 $this->assertArrayHasKey( 0, $info['actions']['edit'] );
251 $this->assertArrayHasKey( 'code', $info['actions']['edit'][0] );
252 $this->assertSame( 'blocked', $info['actions']['edit'][0]['code'] );
253 $this->assertArrayHasKey( 'data', $info['actions']['edit'][0] );
254 $this->assertArrayHasKey( 'blockinfo', $info['actions']['edit'][0]['data'] );
255 $this->assertArrayHasKey( 'blockid', $info['actions']['edit'][0]['data']['blockinfo'] );
256 $this->assertSame( $block->getId(), $info['actions']['edit'][0]['data']['blockinfo']['blockid'] );
259 public function testAssociatedPage() {
260 $page = $this->getExistingTestPage( 'Demo' );
261 $title = $page->getTitle();
263 $title2 = Title
::makeTitle( NS_TALK
, 'Page does not exist' );
264 // Make sure it doesn't exist
265 $this->getNonexistingTestPage( $title2 );
267 [ $data ] = $this->doApiRequest( [
270 'titles' => $title->getPrefixedText() . '|' . $title2->getPrefixedText(),
271 'inprop' => 'associatedpage',
274 $this->assertArrayHasKey( 'query', $data );
275 $this->assertArrayHasKey( 'pages', $data['query'] );
276 $this->assertArrayHasKey( $page->getId(), $data['query']['pages'] );
278 $info = $data['query']['pages'][$page->getId()];
279 $this->assertArrayHasKey( 'associatedpage', $info );
282 $info['associatedpage']
285 // For the non-existing page
286 $this->assertArrayHasKey( -1, $data['query']['pages'] );
288 $info = $data['query']['pages'][ -1 ];
289 $this->assertArrayHasKey( 'associatedpage', $info );
291 'Page does not exist',
292 $info['associatedpage']
296 public function testDisplayTitle() {
297 [ $data ] = $this->doApiRequest( [
300 'inprop' => 'displaytitle',
301 'titles' => 'Art©',
304 $this->assertArrayHasKey( 'query', $data );
305 $this->assertArrayHasKey( 'pages', $data['query'] );
307 // For the non-existing page
308 $this->assertArrayHasKey( -1, $data['query']['pages'] );
310 $info = $data['query']['pages'][ -1 ];
311 $this->assertArrayHasKey( 'displaytitle', $info );
314 $info['displaytitle']