Debian Codegraph @

tests/phpunit/includes/api/query/ApiQueryWatchlistRawIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api\Query;
4
5 use MediaWiki\Tests\Api\ApiTestCase;
6 use MediaWiki\Title\TitleValue;
7 use MediaWiki\User\User;
8 use MediaWiki\Watchlist\WatchedItemQueryService;
9
10 /**
11 * @group API
12 * @group Database
13 * @group medium
14 *
15 * @covers MediaWiki\Api\ApiQueryWatchlistRaw
16 */
17 class ApiQueryWatchlistRawIntegrationTest extends ApiTestCase {
18 // TODO: This test should use Authority, but can't due to User::saveSettings
19 /** @var User */
20 private $loggedInUser;
21 /** @var User */
22 private $notLoggedInUser;
23
24 protected function setUp(): void {
25 parent::setUp();
26
27 $this->loggedInUser = $this->getMutableTestUser()->getUser();
28 $this->notLoggedInUser = $this->getMutableTestUser()->getUser();
29 }
30
31 private function getLoggedInTestUser(): User {
32 return $this->loggedInUser;
33 }
34
35 private function getNotLoggedInTestUser(): User {
36 return $this->notLoggedInUser;
37 }
38
39 private function getWatchedItemStore() {
40 return $this->getServiceContainer()->getWatchedItemStore();
41 }
42
43 private function doListWatchlistRawRequest( array $params = [] ) {
44 return $this->doApiRequest( array_merge(
45 [ 'action' => 'query', 'list' => 'watchlistraw' ],
46 $params
47 ), null, false, $this->getLoggedInTestUser() );
48 }
49
50 private function doGeneratorWatchlistRawRequest( array $params = [] ) {
51 return $this->doApiRequest( array_merge(
52 [ 'action' => 'query', 'generator' => 'watchlistraw' ],
53 $params
54 ), null, false, $this->getLoggedInTestUser() );
55 }
56
57 private function getItemsFromApiResponse( array $response ) {
58 return $response[0]['watchlistraw'];
59 }
60
61 public function testListWatchlistRaw_returnsWatchedItems() {
62 $store = $this->getWatchedItemStore();
63 $store->addWatch(
64 $this->getLoggedInTestUser(),
65 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage' )
66 );
67
68 $result = $this->doListWatchlistRawRequest();
69
70 $this->assertArrayHasKey( 'watchlistraw', $result[0] );
71
72 $this->assertEquals(
73 [
74 [
75 'ns' => NS_MAIN,
76 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
77 ],
78 ],
79 $this->getItemsFromApiResponse( $result )
80 );
81 }
82
83 public function testPropChanged_addsNotificationTimestamp() {
84 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage' );
85 $otherUser = $this->getNotLoggedInTestUser();
86
87 $store = $this->getWatchedItemStore();
88
89 $store->addWatch( $this->getLoggedInTestUser(), $target );
90 $store->updateNotificationTimestamp(
91 $otherUser,
92 $target,
93 '20151212010101'
94 );
95
96 $result = $this->doListWatchlistRawRequest( [ 'wrprop' => 'changed' ] );
97
98 $this->assertEquals(
99 [
100 [
101 'ns' => NS_MAIN,
102 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
103 'changed' => '2015-12-12T01:01:01Z',
104 ],
105 ],
106 $this->getItemsFromApiResponse( $result )
107 );
108 }
109
110 public function testNamespaceParam() {
111 $store = $this->getWatchedItemStore();
112
113 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
114 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage' ),
115 new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage' ),
116 ] );
117
118 $result = $this->doListWatchlistRawRequest( [ 'wrnamespace' => '0' ] );
119
120 $this->assertEquals(
121 [
122 [
123 'ns' => NS_MAIN,
124 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
125 ],
126 ],
127 $this->getItemsFromApiResponse( $result )
128 );
129 }
130
131 public function testShowChangedParams() {
132 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage' );
133 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage' );
134 $otherUser = $this->getNotLoggedInTestUser();
135
136 $store = $this->getWatchedItemStore();
137
138 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
139 $subjectTarget,
140 $talkTarget,
141 ] );
142 $store->updateNotificationTimestamp(
143 $otherUser,
144 $subjectTarget,
145 '20151212010101'
146 );
147
148 $resultChanged = $this->doListWatchlistRawRequest(
149 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService::FILTER_CHANGED ]
150 );
151 $resultNotChanged = $this->doListWatchlistRawRequest(
152 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService::FILTER_NOT_CHANGED ]
153 );
154
155 $this->assertEquals(
156 [
157 [
158 'ns' => NS_MAIN,
159 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
160 'changed' => '2015-12-12T01:01:01Z',
161 ],
162 ],
163 $this->getItemsFromApiResponse( $resultChanged )
164 );
165
166 $this->assertEquals(
167 [
168 [
169 'ns' => NS_TALK,
170 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage',
171 ],
172 ],
173 $this->getItemsFromApiResponse( $resultNotChanged )
174 );
175 }
176
177 public function testLimitParam() {
178 $store = $this->getWatchedItemStore();
179
180 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
181 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
182 new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
183 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
184 ] );
185
186 $resultWithoutLimit = $this->doListWatchlistRawRequest();
187 $resultWithLimit = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
188
189 $this->assertEquals(
190 [
191 [
192 'ns' => NS_MAIN,
193 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
194 ],
195 [
196 'ns' => NS_MAIN,
197 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
198 ],
199 [
200 'ns' => NS_TALK,
201 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
202 ],
203 ],
204 $this->getItemsFromApiResponse( $resultWithoutLimit )
205 );
206 $this->assertEquals(
207 [
208 [
209 'ns' => NS_MAIN,
210 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
211 ],
212 [
213 'ns' => NS_MAIN,
214 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
215 ],
216 ],
217 $this->getItemsFromApiResponse( $resultWithLimit )
218 );
219
220 $this->assertArrayNotHasKey( 'continue', $resultWithoutLimit[0] );
221 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
222 $this->assertArrayHasKey( 'wrcontinue', $resultWithLimit[0]['continue'] );
223 }
224
225 public function testDirParams() {
226 $store = $this->getWatchedItemStore();
227
228 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
229 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
230 new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
231 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
232 ] );
233
234 $resultDirAsc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
235 $resultDirDesc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'descending' ] );
236
237 $this->assertEquals(
238 [
239 [
240 'ns' => NS_MAIN,
241 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
242 ],
243 [
244 'ns' => NS_MAIN,
245 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
246 ],
247 [
248 'ns' => NS_TALK,
249 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
250 ],
251 ],
252 $this->getItemsFromApiResponse( $resultDirAsc )
253 );
254
255 $this->assertEquals(
256 [
257 [
258 'ns' => NS_TALK,
259 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
260 ],
261 [
262 'ns' => NS_MAIN,
263 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
264 ],
265 [
266 'ns' => NS_MAIN,
267 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
268 ],
269 ],
270 $this->getItemsFromApiResponse( $resultDirDesc )
271 );
272 }
273
274 public function testAscendingIsDefaultOrder() {
275 $store = $this->getWatchedItemStore();
276
277 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
278 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
279 new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
280 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
281 ] );
282
283 $resultNoDir = $this->doListWatchlistRawRequest();
284 $resultAscDir = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
285
286 $this->assertEquals(
287 $this->getItemsFromApiResponse( $resultNoDir ),
288 $this->getItemsFromApiResponse( $resultAscDir )
289 );
290 }
291
292 public function testFromTitleParam() {
293 $store = $this->getWatchedItemStore();
294
295 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
296 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
297 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
298 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
299 ] );
300
301 $result = $this->doListWatchlistRawRequest( [
302 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
303 ] );
304
305 $this->assertEquals(
306 [
307 [
308 'ns' => NS_MAIN,
309 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
310 ],
311 [
312 'ns' => NS_MAIN,
313 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
314 ],
315 ],
316 $this->getItemsFromApiResponse( $result )
317 );
318 }
319
320 public function testToTitleParam() {
321 $store = $this->getWatchedItemStore();
322
323 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
324 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
325 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
326 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
327 ] );
328
329 $result = $this->doListWatchlistRawRequest( [
330 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
331 ] );
332
333 $this->assertEquals(
334 [
335 [
336 'ns' => NS_MAIN,
337 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
338 ],
339 [
340 'ns' => NS_MAIN,
341 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
342 ],
343 ],
344 $this->getItemsFromApiResponse( $result )
345 );
346 }
347
348 public function testContinueParam() {
349 $store = $this->getWatchedItemStore();
350
351 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
352 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
353 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
354 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
355 ] );
356
357 $firstResult = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
358 $continuationParam = $firstResult[0]['continue']['wrcontinue'];
359
360 $this->assertSame( '0|ApiQueryWatchlistRawIntegrationTestPage3', $continuationParam );
361
362 $continuedResult = $this->doListWatchlistRawRequest( [ 'wrcontinue' => $continuationParam ] );
363
364 $this->assertEquals(
365 [
366 [
367 'ns' => NS_MAIN,
368 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
369 ]
370 ],
371 $this->getItemsFromApiResponse( $continuedResult )
372 );
373 }
374
375 public static function fromTitleToTitleContinueComboProvider() {
376 return [
377 [
378 [
379 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
380 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
381 ],
382 [
383 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1' ],
384 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
385 ],
386 ],
387 [
388 [
389 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
390 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
391 ],
392 [
393 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
394 ],
395 ],
396 [
397 [
398 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
399 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
400 ],
401 [
402 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
403 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
404 ],
405 ],
406 [
407 [
408 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
409 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
410 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
411 ],
412 [
413 [ 'ns' => NS_MAIN, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
414 ],
415 ],
416 ];
417 }
418
419 /**
420 * @dataProvider fromTitleToTitleContinueComboProvider
421 */
422 public function testFromTitleToTitleContinueCombo( array $params, array $expectedItems ) {
423 $store = $this->getWatchedItemStore();
424
425 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
426 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
427 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
428 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
429 ] );
430
431 $result = $this->doListWatchlistRawRequest( $params );
432
433 $this->assertEquals( $expectedItems, $this->getItemsFromApiResponse( $result ) );
434 }
435
436 public static function fromTitleToTitleContinueSelfContradictoryComboProvider() {
437 return [
438 [
439 [
440 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
441 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
442 ]
443 ],
444 [
445 [
446 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
447 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
448 'wrdir' => 'descending',
449 ]
450 ],
451 [
452 [
453 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
454 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
455 ]
456 ],
457 ];
458 }
459
460 /**
461 * @dataProvider fromTitleToTitleContinueSelfContradictoryComboProvider
462 */
463 public function testFromTitleToTitleContinueSelfContradictoryCombo( array $params ) {
464 $store = $this->getWatchedItemStore();
465
466 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
467 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
468 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
469 ] );
470
471 $result = $this->doListWatchlistRawRequest( $params );
472
473 $this->assertSame( [], $this->getItemsFromApiResponse( $result ) );
474 $this->assertArrayNotHasKey( 'continue', $result[0] );
475 }
476
477 public function testOwnerAndTokenParams() {
478 $services = $this->getServiceContainer();
479 $userOptionsManager = $services->getUserOptionsManager();
480
481 $otherUser = $this->getNotLoggedInTestUser();
482 $userOptionsManager->setOption( $otherUser, 'watchlisttoken', '1234567890' );
483 $otherUser->saveSettings();
484
485 $store = $this->getWatchedItemStore();
486 $store->addWatchBatchForUser( $otherUser, [
487 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
488 new TitleValue( NS_TALK, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
489 ] );
490
491 $services->getMainWANObjectCache()->clearProcessCache();
492 $result = $this->doListWatchlistRawRequest( [
493 'wrowner' => $otherUser->getName(),
494 'wrtoken' => '1234567890',
495 ] );
496
497 $this->assertEquals(
498 [
499 [
500 'ns' => NS_MAIN,
501 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
502 ],
503 [
504 'ns' => NS_TALK,
505 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
506 ],
507 ],
508 $this->getItemsFromApiResponse( $result )
509 );
510 }
511
512 public function testOwnerAndTokenParams_wrongToken() {
513 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
514
515 $otherUser = $this->getNotLoggedInTestUser();
516 $userOptionsManager->setOption( $otherUser, 'watchlisttoken', '1234567890' );
517 $otherUser->saveSettings();
518
519 $this->expectApiErrorCode( 'bad_wltoken' );
520
521 $this->doListWatchlistRawRequest( [
522 'wrowner' => $otherUser->getName(),
523 'wrtoken' => 'wrong-token',
524 ] );
525 }
526
527 public function testOwnerAndTokenParams_userHasNoWatchlistToken() {
528 $this->expectApiErrorCode( 'bad_wltoken' );
529
530 $this->doListWatchlistRawRequest( [
531 'wrowner' => $this->getNotLoggedInTestUser()->getName(),
532 'wrtoken' => 'some-watchlist-token',
533 ] );
534 }
535
536 public function testGeneratorWatchlistRawPropInfo_returnsWatchedItems() {
537 $store = $this->getWatchedItemStore();
538 $store->addWatch(
539 $this->getLoggedInTestUser(),
540 new TitleValue( NS_MAIN, 'ApiQueryWatchlistRawIntegrationTestPage' )
541 );
542
543 $result = $this->doGeneratorWatchlistRawRequest( [ 'prop' => 'info' ] );
544
545 $this->assertArrayHasKey( 'query', $result[0] );
546 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
547 $this->assertCount( 1, $result[0]['query']['pages'] );
548
549 // $result[0]['query']['pages'] uses page ids as keys
550 $item = array_values( $result[0]['query']['pages'] )[0];
551
552 $this->assertSame( NS_MAIN, $item['ns'] );
553 $this->assertEquals( 'ApiQueryWatchlistRawIntegrationTestPage', $item['title'] );
554 }
555
556 }