Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryInfoTest.php
blob7c6520e5ca4da1bd1df9d751d33877dfc3c15b4e
1 <?php
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;
14 /**
15 * @group API
16 * @group medium
17 * @group Database
19 * @covers \MediaWiki\Api\ApiQueryInfo
21 class ApiQueryInfoTest extends ApiTestCase {
22 use TempUserTestTrait;
24 protected function setUp(): void {
25 parent::setUp();
27 $this->overrideConfigValues( [
28 MainConfigNames::WatchlistExpiry => true,
29 MainConfigNames::WatchlistExpiryMaxDuration => '6 months',
30 ] );
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(
42 $user,
43 $title,
44 // 3 months later
45 '2011-04-01T00:00:00Z'
48 [ $data ] = $this->doApiRequest( [
49 'action' => 'query',
50 'prop' => 'info',
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( [
82 'action' => 'query',
83 'prop' => 'info',
84 'titles' => $title->getText(),
85 'inprop' => 'linkclasses',
86 'inlinkcontext' => $title->getText(),
87 ] );
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( [
103 'action' => 'query',
104 'prop' => 'info',
105 'titles' => $title->getText(),
106 'intestactions' => 'edit'
107 ] );
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();
123 // Disabled
124 $this->disableAutoCreateTempUser();
125 [ $data ] = $this->doApiRequest( [
126 'action' => 'query',
127 'prop' => 'info',
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 );
135 // Enabled
136 $this->setGroupPermissions( '*', 'createaccount', true );
137 $this->enableAutoCreateTempUser();
138 [ $data ] = $this->doApiRequest( [
139 'action' => 'query',
140 'prop' => 'info',
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( [
149 'action' => 'query',
150 'prop' => 'info',
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( [
160 'action' => 'query',
161 'prop' => 'info',
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( [
171 'action' => 'query',
172 'prop' => 'info',
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( [
183 'action' => 'query',
184 'prop' => 'info',
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( [
198 'action' => 'query',
199 'prop' => 'info',
200 'titles' => $title->getText(),
201 'intestactions' => 'edit',
202 'intestactionsdetail' => 'full',
203 ] );
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,
222 'by' => $sysop,
223 'expiry' => 'infinity',
224 'sitewide' => 1,
225 'enableAutoblock' => true,
226 ] );
228 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
229 $blockStore->insertBlock( $block );
231 $page = $this->getExistingTestPage( 'Pluto' );
232 $title = $page->getTitle();
234 [ $data ] = $this->doApiRequest( [
235 'action' => 'query',
236 'prop' => 'info',
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( [
268 'action' => 'query',
269 'prop' => 'info',
270 'titles' => $title->getPrefixedText() . '|' . $title2->getPrefixedText(),
271 'inprop' => 'associatedpage',
272 ] );
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 );
280 $this->assertEquals(
281 'Talk:Demo',
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 );
290 $this->assertEquals(
291 'Page does not exist',
292 $info['associatedpage']
296 public function testDisplayTitle() {
297 [ $data ] = $this->doApiRequest( [
298 'action' => 'query',
299 'prop' => 'info',
300 'inprop' => 'displaytitle',
301 'titles' => 'Art&copy',
302 ] );
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 );
312 $this->assertEquals(
313 'Art&amp;copy',
314 $info['displaytitle']