Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api\Query;
4
5 use MediaWiki\CommentStore\CommentStoreComment;
6 use MediaWiki\Linker\LinkTarget;
7 use MediaWiki\Permissions\Authority;
8 use MediaWiki\Request\FauxRequest;
9 use MediaWiki\Revision\SlotRecord;
10 use MediaWiki\Tests\Api\ApiTestCase;
11 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
12 use MediaWiki\Title\TitleValue;
13 use MediaWiki\User\User;
14 use MediaWiki\Watchlist\WatchedItemQueryService;
15 use RecentChange;
16
17 /**
18 * @group medium
19 * @group API
20 * @group Database
21 *
22 * @covers MediaWiki\Api\ApiQueryWatchlist
23 * @covers \MediaWiki\Watchlist\WatchedItemQueryService
24 */
25 class ApiQueryWatchlistIntegrationTest extends ApiTestCase {
26 use TempUserTestTrait;
27
28 // TODO: This test should use Authority, but can't due to User::saveSettings
29
30 /** @var User */
31 private $loggedInUser;
32 /** @var User */
33 private $notLoggedInUser;
34
35 protected function setUp(): void {
36 parent::setUp();
37
38 $this->loggedInUser = $this->getMutableTestUser()->getUser();
39 $this->notLoggedInUser = $this->getMutableTestUser()->getUser();
40 }
41
42 private function getLoggedInTestUser(): User {
43 return $this->loggedInUser;
44 }
45
46 private function getNonLoggedInTestUser(): User {
47 return $this->notLoggedInUser;
48 }
49
50 private function doPageEdit( Authority $performer, $target, $content, $summary ) {
51 $this->editPage(
52 $target,
53 $content,
54 $summary,
55 NS_MAIN,
56 $performer
57 );
58 }
59
60 private function doMinorPageEdit( User $user, LinkTarget $target, $content, $summary ) {
61 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
62 $page->doUserEditContent(
63 $page->getContentHandler()->unserializeContent( $content ),
64 $user,
65 $summary,
66 EDIT_MINOR
67 );
68 }
69
70 private function doBotPageEdit( User $user, LinkTarget $target, $content, $summary ) {
71 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
72 $page->doUserEditContent(
73 $page->getContentHandler()->unserializeContent( $content ),
74 $user,
75 $summary,
76 EDIT_FORCE_BOT
77 );
78 }
79
80 private function doAnonPageEdit( LinkTarget $target, $content, $summary ) {
81 $this->disableAutoCreateTempUser();
82 $this->editPage(
83 $target,
84 $content,
85 $summary,
86 NS_MAIN,
87 $this->getServiceContainer()->getUserFactory()->newAnonymous()
88 );
89 }
90
91 private function doTempPageEdit( LinkTarget $target, $content, $summary ) {
92 $this->enableAutoCreateTempUser();
93 $this->editPage(
94 $target,
95 $content,
96 $summary,
97 NS_MAIN,
98 $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser()
99 );
100 }
101
102 private function doPatrolledPageEdit(
103 User $user,
104 LinkTarget $target,
105 $content,
106 $summary,
107 User $patrollingUser
108 ) {
109 $summary = CommentStoreComment::newUnsavedComment( trim( $summary ) );
110 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
111
112 $updater = $page->newPageUpdater( $user );
113 $updater->setContent( SlotRecord::MAIN, $page->getContentHandler()->unserializeContent( $content ) );
114 $rev = $updater->saveRevision( $summary );
115
116 $rc = $this->getServiceContainer()->getRevisionStore()->getRecentChange( $rev );
117 $rc->markPatrolled( $patrollingUser, [] );
118 }
119
120 /**
121 * Performs a batch of page edits as a specified user
122 * @param User $user
123 * @param array $editData associative array, keys:
124 * - target => LinkTarget page to edit
125 * - content => string new content
126 * - summary => string edit summary
127 * - minorEdit => bool mark as minor edit if true (defaults to false)
128 * - botEdit => bool mark as bot edit if true (defaults to false)
129 */
130 private function doPageEdits( User $user, array $editData ) {
131 foreach ( $editData as $singleEditData ) {
132 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
133 $this->doMinorPageEdit(
134 $user,
135 $singleEditData['target'],
136 $singleEditData['content'],
137 $singleEditData['summary']
138 );
139 continue;
140 }
141 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
142 $this->doBotPageEdit(
143 $user,
144 $singleEditData['target'],
145 $singleEditData['content'],
146 $singleEditData['summary']
147 );
148 continue;
149 }
150 $this->doPageEdit(
151 $user,
152 $singleEditData['target'],
153 $singleEditData['content'],
154 $singleEditData['summary']
155 );
156 }
157 }
158
159 private function getWatchedItemStore() {
160 return $this->getServiceContainer()->getWatchedItemStore();
161 }
162
163 /**
164 * @param User $user
165 * @param LinkTarget[] $targets
166 */
167 private function watchPages( User $user, array $targets ) {
168 $store = $this->getWatchedItemStore();
169 $store->addWatchBatchForUser( $user, $targets );
170 }
171
172 private function doListWatchlistRequest( array $params = [], $user = null ) {
173 $user ??= $this->getLoggedInTestUser();
174 return $this->doApiRequest(
175 array_merge(
176 [ 'action' => 'query', 'list' => 'watchlist' ],
177 $params
178 ), null, false, $user
179 );
180 }
181
182 private function doGeneratorWatchlistRequest( array $params = [] ) {
183 return $this->doApiRequest(
184 array_merge(
185 [ 'action' => 'query', 'generator' => 'watchlist' ],
186 $params
187 ), null, false, $this->getLoggedInTestUser()
188 );
189 }
190
191 private function getItemsFromApiResponse( array $response ) {
192 return $response[0]['query']['watchlist'];
193 }
194
195 /**
196 * Convenience method to assert that actual items array fetched from API is equal to the expected
197 * array, Unlike assertEquals this only checks if values of specified keys are equal in both
198 * arrays. This could be used e.g. not to compare IDs that could change between test run
199 * but only stable keys.
200 * Optionally this also checks that specified keys are present in the actual item without
201 * performing any checks on the related values.
202 *
203 * @param array $actualItems array of actual items (associative arrays)
204 * @param array $expectedItems array of expected items (associative arrays),
205 * those items have less keys than actual items
206 * @param array $keysUsedInValueComparison list of keys of the actual item that will be used
207 * in the comparison of values
208 * @param array $requiredKeys optional, list of keys that must be present in the
209 * actual items. Values of those keys are not checked.
210 */
211 private function assertArraySubsetsEqual(
212 array $actualItems,
213 array $expectedItems,
214 array $keysUsedInValueComparison,
215 array $requiredKeys = []
216 ) {
217 $this->assertSameSize( $expectedItems, $actualItems );
218
219 // not checking values of all keys of the actual item, so removing unwanted keys from comparison
220 $actualItemsOnlyComparedValues = array_map(
221 static function ( array $item ) use ( $keysUsedInValueComparison ) {
222 return array_intersect_key( $item,
223 array_fill_keys( $keysUsedInValueComparison, true ) );
224 },
225 $actualItems
226 );
227
228 $this->assertEquals(
229 $expectedItems,
230 $actualItemsOnlyComparedValues
231 );
232
233 // Check that each item in $actualItems contains all of keys specified in $requiredKeys
234 $actualItemsKeysOnly = array_map( 'array_keys', $actualItems );
235 foreach ( $actualItemsKeysOnly as $keysOfTheItem ) {
236 $this->assertSame( [], array_diff( $requiredKeys, $keysOfTheItem ) );
237 }
238 }
239
240 private function getPrefixedText( LinkTarget $target ) {
241 return $this->getServiceContainer()->getTitleFormatter()->getPrefixedText( $target );
242 }
243
244 private function cleanTestUsersWatchlist() {
245 $user = $this->getLoggedInTestUser();
246 $store = $this->getWatchedItemStore();
247 $items = $store->getWatchedItemsForUser( $user );
248 foreach ( $items as $item ) {
249 $store->removeWatch( $user, $item->getTarget() );
250 }
251 }
252
253 public function testListWatchlist_returnsWatchedItemsWithRCInfo() {
254 // Clean up after previous tests that might have added something to the watchlist of
255 // the user with the same user ID as user used here as the test user
256 $this->cleanTestUsersWatchlist();
257
258 $user = $this->getLoggedInTestUser();
259 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
260 $this->doPageEdit(
261 $user,
262 $target,
263 'Some Content',
264 'Create the page'
265 );
266 $this->watchPages( $user, [ $target ] );
267
268 $result = $this->doListWatchlistRequest();
269
270 $this->assertArrayHasKey( 'query', $result[0] );
271 $this->assertArrayHasKey( 'watchlist', $result[0]['query'] );
272
273 $this->assertArraySubsetsEqual(
274 $this->getItemsFromApiResponse( $result ),
275 [
276 [
277 'type' => 'new',
278 'ns' => $target->getNamespace(),
279 'title' => $this->getPrefixedText( $target ),
280 'bot' => false,
281 'new' => true,
282 'minor' => false,
283 ]
284 ],
285 [ 'type', 'ns', 'title', 'bot', 'new', 'minor' ],
286 [ 'pageid', 'revid', 'old_revid' ]
287 );
288 }
289
290 public function testIdsPropParameter() {
291 $user = $this->getLoggedInTestUser();
292 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
293 $this->doPageEdit(
294 $user,
295 $target,
296 'Some Content',
297 'Create the page'
298 );
299 $this->watchPages( $user, [ $target ] );
300
301 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'ids', ] );
302 $items = $this->getItemsFromApiResponse( $result );
303
304 $this->assertCount( 1, $items );
305 $this->assertArrayHasKey( 'pageid', $items[0] );
306 $this->assertArrayHasKey( 'revid', $items[0] );
307 $this->assertArrayHasKey( 'old_revid', $items[0] );
308 $this->assertEquals( 'new', $items[0]['type'] );
309 }
310
311 public function testTitlePropParameter() {
312 $user = $this->getLoggedInTestUser();
313 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
314 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
315 $this->doPageEdits(
316 $user,
317 [
318 [
319 'target' => $subjectTarget,
320 'content' => 'Some Content',
321 'summary' => 'Create the page',
322 ],
323 [
324 'target' => $talkTarget,
325 'content' => 'Some Talk Page Content',
326 'summary' => 'Create Talk page',
327 ],
328 ]
329 );
330 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
331
332 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', ] );
333
334 $this->assertEquals(
335 [
336 [
337 'type' => 'new',
338 'ns' => $talkTarget->getNamespace(),
339 'title' => $this->getPrefixedText( $talkTarget ),
340 ],
341 [
342 'type' => 'new',
343 'ns' => $subjectTarget->getNamespace(),
344 'title' => $this->getPrefixedText( $subjectTarget ),
345 ],
346 ],
347 $this->getItemsFromApiResponse( $result )
348 );
349 }
350
351 public function testFlagsPropParameter() {
352 $user = $this->getLoggedInTestUser();
353 $normalEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
354 $minorEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPageM' );
355 $botEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPageB' );
356 $this->doPageEdits(
357 $user,
358 [
359 [
360 'target' => $normalEditTarget,
361 'content' => 'Some Content',
362 'summary' => 'Create the page',
363 ],
364 [
365 'target' => $minorEditTarget,
366 'content' => 'Some Content',
367 'summary' => 'Create the page',
368 ],
369 [
370 'target' => $minorEditTarget,
371 'content' => 'Slightly Better Content',
372 'summary' => 'Change content',
373 'minorEdit' => true,
374 ],
375 [
376 'target' => $botEditTarget,
377 'content' => 'Some Content',
378 'summary' => 'Create the page with a bot',
379 'botEdit' => true,
380 ],
381 ]
382 );
383 $this->watchPages( $user, [ $normalEditTarget, $minorEditTarget, $botEditTarget ] );
384
385 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'flags', ] );
386
387 $this->assertEquals(
388 [
389 [
390 'type' => 'new',
391 'new' => true,
392 'minor' => false,
393 'bot' => true,
394 ],
395 [
396 'type' => 'edit',
397 'new' => false,
398 'minor' => true,
399 'bot' => false,
400 ],
401 [
402 'type' => 'new',
403 'new' => true,
404 'minor' => false,
405 'bot' => false,
406 ],
407 ],
408 $this->getItemsFromApiResponse( $result )
409 );
410 }
411
412 public function testUserPropParameter() {
413 $user = $this->getLoggedInTestUser();
414 $userEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
415 $anonEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPageA' );
416 $this->doPageEdit(
417 $user,
418 $userEditTarget,
419 'Some Content',
420 'Create the page'
421 );
422 $this->doAnonPageEdit(
423 $anonEditTarget,
424 'Some Content',
425 'Create the page'
426 );
427 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
428
429 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'user', ] );
430
431 $this->assertEquals(
432 [
433 [
434 'type' => 'new',
435 'anon' => true,
436 'temp' => false,
437 'user' => $this->getServiceContainer()->getUserFactory()->newAnonymous()->getName(),
438 ],
439 [
440 'type' => 'new',
441 'anon' => false,
442 'temp' => false,
443 'user' => $user->getName(),
444 ],
445 ],
446 $this->getItemsFromApiResponse( $result )
447 );
448 }
449
450 public function testUserIdPropParameter() {
451 $user = $this->getLoggedInTestUser();
452 $userEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
453 $anonEditTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPageA' );
454 $this->doPageEdit(
455 $user,
456 $userEditTarget,
457 'Some Content',
458 'Create the page'
459 );
460 $this->doAnonPageEdit(
461 $anonEditTarget,
462 'Some Content',
463 'Create the page'
464 );
465 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
466
467 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'userid', ] );
468
469 $this->assertEquals(
470 [
471 [
472 'type' => 'new',
473 'anon' => true,
474 'user' => 0,
475 'userid' => 0,
476 ],
477 [
478 'type' => 'new',
479 'anon' => false,
480 'user' => $user->getId(),
481 'userid' => $user->getId(),
482 ],
483 ],
484 $this->getItemsFromApiResponse( $result )
485 );
486 }
487
488 public function testCommentPropParameter() {
489 $user = $this->getLoggedInTestUser();
490 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
491 $this->doPageEdit(
492 $user,
493 $target,
494 'Some Content',
495 'Create the <b>page</b>'
496 );
497 $this->watchPages( $user, [ $target ] );
498
499 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'comment', ] );
500
501 $this->assertEquals(
502 [
503 [
504 'type' => 'new',
505 'comment' => 'Create the <b>page</b>',
506 ],
507 ],
508 $this->getItemsFromApiResponse( $result )
509 );
510 }
511
512 public function testParsedCommentPropParameter() {
513 $user = $this->getLoggedInTestUser();
514 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
515 $this->doPageEdit(
516 $user,
517 $target,
518 'Some Content',
519 'Create the <b>page</b>'
520 );
521 $this->watchPages( $user, [ $target ] );
522
523 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'parsedcomment', ] );
524
525 $this->assertEquals(
526 [
527 [
528 'type' => 'new',
529 'parsedcomment' => 'Create the &lt;b&gt;page&lt;/b&gt;',
530 ],
531 ],
532 $this->getItemsFromApiResponse( $result )
533 );
534 }
535
536 public function testTimestampPropParameter() {
537 $user = $this->getLoggedInTestUser();
538 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
539 $this->doPageEdit(
540 $user,
541 $target,
542 'Some Content',
543 'Create the page'
544 );
545 $this->watchPages( $user, [ $target ] );
546
547 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'timestamp', ] );
548 $items = $this->getItemsFromApiResponse( $result );
549
550 $this->assertCount( 1, $items );
551 $this->assertArrayHasKey( 'timestamp', $items[0] );
552 $this->assertIsString( $items[0]['timestamp'] );
553 }
554
555 public function testSizesPropParameter() {
556 $user = $this->getLoggedInTestUser();
557 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
558 $this->doPageEdit(
559 $user,
560 $target,
561 'Some Content',
562 'Create the page'
563 );
564 $this->watchPages( $user, [ $target ] );
565
566 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'sizes', ] );
567
568 $this->assertEquals(
569 [
570 [
571 'type' => 'new',
572 'oldlen' => 0,
573 'newlen' => 12,
574 ],
575 ],
576 $this->getItemsFromApiResponse( $result )
577 );
578 }
579
580 public function testNotificationTimestampPropParameter() {
581 $otherUser = $this->getNonLoggedInTestUser();
582 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
583 $this->doPageEdit(
584 $otherUser,
585 $target,
586 'Some Content',
587 'Create the page'
588 );
589 $store = $this->getWatchedItemStore();
590 $store->addWatch( $this->getLoggedInTestUser(), $target );
591 $store->updateNotificationTimestamp(
592 $otherUser,
593 $target,
594 '20151212010101'
595 );
596
597 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'notificationtimestamp', ] );
598
599 $this->assertEquals(
600 [
601 [
602 'type' => 'new',
603 'notificationtimestamp' => '2015-12-12T01:01:01Z',
604 ],
605 ],
606 $this->getItemsFromApiResponse( $result )
607 );
608 }
609
610 private function setupPatrolledSpecificFixtures( User $user ) {
611 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
612
613 $this->doPatrolledPageEdit(
614 $user,
615 $target,
616 'Some Content',
617 'Create the page (this gets patrolled)',
618 $user
619 );
620
621 $this->watchPages( $user, [ $target ] );
622 }
623
624 public function testPatrolPropParameter() {
625 $testUser = static::getTestSysop();
626 $user = $testUser->getUser();
627 $this->setupPatrolledSpecificFixtures( $user );
628
629 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
630
631 $this->assertEquals(
632 [
633 [
634 'type' => 'new',
635 'patrolled' => true,
636 'unpatrolled' => false,
637 'autopatrolled' => false,
638 ]
639 ],
640 $this->getItemsFromApiResponse( $result )
641 );
642 }
643
644 private function createPageAndDeleteIt( LinkTarget $target ) {
645 $wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
646 $this->doPageEdit(
647 $this->getLoggedInTestUser(),
648 $wikiPage,
649 'Some Content',
650 'Create the page that will be deleted'
651 );
652 $this->deletePage( $wikiPage, 'Important Reason' );
653 }
654
655 public function testLoginfoPropParameter() {
656 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
657 $this->createPageAndDeleteIt( $target );
658
659 $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
660
661 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
662
663 $this->assertArraySubsetsEqual(
664 $this->getItemsFromApiResponse( $result ),
665 [
666 [
667 'type' => 'log',
668 'logtype' => 'delete',
669 'logaction' => 'delete',
670 'logparams' => [],
671 ],
672 ],
673 [ 'type', 'logtype', 'logaction', 'logparams' ],
674 [ 'logid' ]
675 );
676 }
677
678 public function testEmptyPropParameter() {
679 $user = $this->getLoggedInTestUser();
680 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
681 $this->doPageEdit(
682 $user,
683 $target,
684 'Some Content',
685 'Create the page'
686 );
687 $this->watchPages( $user, [ $target ] );
688
689 $result = $this->doListWatchlistRequest( [ 'wlprop' => '', ] );
690
691 $this->assertEquals(
692 [
693 [
694 'type' => 'new',
695 ]
696 ],
697 $this->getItemsFromApiResponse( $result )
698 );
699 }
700
701 public function testNamespaceParam() {
702 $user = $this->getLoggedInTestUser();
703 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
704 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
705 $this->doPageEdits(
706 $user,
707 [
708 [
709 'target' => $subjectTarget,
710 'content' => 'Some Content',
711 'summary' => 'Create the page',
712 ],
713 [
714 'target' => $talkTarget,
715 'content' => 'Some Content',
716 'summary' => 'Create the talk page',
717 ],
718 ]
719 );
720 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
721
722 $result = $this->doListWatchlistRequest( [ 'wlnamespace' => NS_MAIN, ] );
723
724 $this->assertArraySubsetsEqual(
725 $this->getItemsFromApiResponse( $result ),
726 [
727 [
728 'ns' => $subjectTarget->getNamespace(),
729 'title' => $this->getPrefixedText( $subjectTarget ),
730 ],
731 ],
732 [ 'ns', 'title' ]
733 );
734 }
735
736 public function testUserParam() {
737 $user = $this->getLoggedInTestUser();
738 $otherUser = $this->getNonLoggedInTestUser();
739 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
740 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
741 $this->doPageEdit(
742 $user,
743 $subjectTarget,
744 'Some Content',
745 'Create the page'
746 );
747 $this->doPageEdit(
748 $otherUser,
749 $talkTarget,
750 'What is this page about?',
751 'Create the talk page'
752 );
753 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
754
755 $result = $this->doListWatchlistRequest( [
756 'wlprop' => 'user|title',
757 'wluser' => $otherUser->getName(),
758 ] );
759
760 $this->assertEquals(
761 [
762 [
763 'type' => 'new',
764 'ns' => $talkTarget->getNamespace(),
765 'title' => $this->getPrefixedText( $talkTarget ),
766 'user' => $otherUser->getName(),
767 'anon' => false,
768 'temp' => false,
769 ],
770 ],
771 $this->getItemsFromApiResponse( $result )
772 );
773 }
774
775 public function testExcludeUserParam() {
776 $user = $this->getLoggedInTestUser();
777 $otherUser = $this->getNonLoggedInTestUser();
778 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
779 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
780 $this->doPageEdit(
781 $user,
782 $subjectTarget,
783 'Some Content',
784 'Create the page'
785 );
786 $this->doPageEdit(
787 $otherUser,
788 $talkTarget,
789 'What is this page about?',
790 'Create the talk page'
791 );
792 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
793
794 $result = $this->doListWatchlistRequest( [
795 'wlprop' => 'user|title',
796 'wlexcludeuser' => $otherUser->getName(),
797 ] );
798
799 $this->assertEquals(
800 [
801 [
802 'type' => 'new',
803 'ns' => $subjectTarget->getNamespace(),
804 'title' => $this->getPrefixedText( $subjectTarget ),
805 'user' => $user->getName(),
806 'anon' => false,
807 'temp' => false,
808 ]
809 ],
810 $this->getItemsFromApiResponse( $result )
811 );
812 }
813
814 public function testShowMinorParams() {
815 $user = $this->getLoggedInTestUser();
816 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
817 $this->doPageEdits(
818 $user,
819 [
820 [
821 'target' => $target,
822 'content' => 'Some Content',
823 'summary' => 'Create the page',
824 ],
825 [
826 'target' => $target,
827 'content' => 'Slightly Better Content',
828 'summary' => 'Change content',
829 'minorEdit' => true,
830 ],
831 ]
832 );
833 $this->watchPages( $user, [ $target ] );
834
835 $resultMinor = $this->doListWatchlistRequest( [
836 'wlshow' => WatchedItemQueryService::FILTER_MINOR,
837 'wlprop' => 'flags'
838 ] );
839 $resultNotMinor = $this->doListWatchlistRequest( [
840 'wlshow' => WatchedItemQueryService::FILTER_NOT_MINOR, 'wlprop' => 'flags'
841 ] );
842
843 $this->assertArraySubsetsEqual(
844 $this->getItemsFromApiResponse( $resultMinor ),
845 [
846 [ 'minor' => true, ]
847 ],
848 [ 'minor' ]
849 );
850 $this->assertSame( [], $this->getItemsFromApiResponse( $resultNotMinor ) );
851 }
852
853 public function testShowBotParams() {
854 $user = $this->getLoggedInTestUser();
855 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
856 $this->doBotPageEdit(
857 $user,
858 $target,
859 'Some Content',
860 'Create the page'
861 );
862 $this->watchPages( $user, [ $target ] );
863
864 $resultBot = $this->doListWatchlistRequest( [
865 'wlshow' => WatchedItemQueryService::FILTER_BOT
866 ] );
867 $resultNotBot = $this->doListWatchlistRequest( [
868 'wlshow' => WatchedItemQueryService::FILTER_NOT_BOT
869 ] );
870
871 $this->assertArraySubsetsEqual(
872 $this->getItemsFromApiResponse( $resultBot ),
873 [
874 [ 'bot' => true ],
875 ],
876 [ 'bot' ]
877 );
878 $this->assertSame( [], $this->getItemsFromApiResponse( $resultNotBot ) );
879 }
880
881 public function testShowAnonParams() {
882 $user = $this->getLoggedInTestUser();
883 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
884 $this->doAnonPageEdit(
885 $target,
886 'Some Content',
887 'Create the page'
888 );
889 $this->watchPages( $user, [ $target ] );
890
891 $resultAnon = $this->doListWatchlistRequest( [
892 'wlprop' => 'user',
893 'wlshow' => WatchedItemQueryService::FILTER_ANON
894 ] );
895 $resultNotAnon = $this->doListWatchlistRequest( [
896 'wlprop' => 'user',
897 'wlshow' => WatchedItemQueryService::FILTER_NOT_ANON
898 ] );
899
900 $this->assertArraySubsetsEqual(
901 $this->getItemsFromApiResponse( $resultAnon ),
902 [
903 [
904 'anon' => true,
905 'temp' => false,
906 ],
907 ],
908 [ 'anon', 'temp' ]
909 );
910 $this->assertSame( [], $this->getItemsFromApiResponse( $resultNotAnon ) );
911 }
912
913 public function testShowAnonParamsTemp() {
914 $user = $this->getLoggedInTestUser();
915 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
916 $this->doTempPageEdit(
917 $target,
918 'Some more content',
919 'Add more content'
920 );
921 $this->watchPages( $user, [ $target ] );
922
923 $resultAnon = $this->doListWatchlistRequest( [
924 'wlprop' => 'user',
925 'wlshow' => WatchedItemQueryService::FILTER_ANON
926 ] );
927 $resultNotAnon = $this->doListWatchlistRequest( [
928 'wlprop' => 'user',
929 'wlshow' => WatchedItemQueryService::FILTER_NOT_ANON
930 ] );
931
932 $this->assertArraySubsetsEqual(
933 $this->getItemsFromApiResponse( $resultAnon ),
934 [
935 [
936 'anon' => false,
937 'temp' => true
938 ],
939 ],
940 [ 'anon', 'temp' ]
941 );
942 $this->assertSame( [], $this->getItemsFromApiResponse( $resultNotAnon ) );
943 }
944
945 public function testShowUnreadParams() {
946 $user = $this->getLoggedInTestUser();
947 $otherUser = $this->getNonLoggedInTestUser();
948 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
949 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
950 $this->doPageEdit(
951 $user,
952 $subjectTarget,
953 'Some Content',
954 'Create the page'
955 );
956 $this->doPageEdit(
957 $otherUser,
958 $talkTarget,
959 'Some Content',
960 'Create the talk page'
961 );
962 $store = $this->getWatchedItemStore();
963 $store->addWatchBatchForUser( $user, [ $subjectTarget, $talkTarget ] );
964 $store->updateNotificationTimestamp(
965 $otherUser,
966 $talkTarget,
967 '20151212010101'
968 );
969
970 $resultUnread = $this->doListWatchlistRequest( [
971 'wlprop' => 'notificationtimestamp|title',
972 'wlshow' => WatchedItemQueryService::FILTER_UNREAD
973 ] );
974 $resultNotUnread = $this->doListWatchlistRequest( [
975 'wlprop' => 'notificationtimestamp|title',
976 'wlshow' => WatchedItemQueryService::FILTER_NOT_UNREAD
977 ] );
978
979 $this->assertEquals(
980 [
981 [
982 'type' => 'new',
983 'notificationtimestamp' => '2015-12-12T01:01:01Z',
984 'ns' => $talkTarget->getNamespace(),
985 'title' => $this->getPrefixedText( $talkTarget )
986 ]
987 ],
988 $this->getItemsFromApiResponse( $resultUnread )
989 );
990 $this->assertEquals(
991 [
992 [
993 'type' => 'new',
994 'notificationtimestamp' => '',
995 'ns' => $subjectTarget->getNamespace(),
996 'title' => $this->getPrefixedText( $subjectTarget )
997 ]
998 ],
999 $this->getItemsFromApiResponse( $resultNotUnread )
1000 );
1001 }
1002
1003 public function testShowPatrolledParams() {
1004 $user = static::getTestSysop()->getUser();
1005 $this->setupPatrolledSpecificFixtures( $user );
1006
1007 $resultPatrolled = $this->doListWatchlistRequest( [
1008 'wlprop' => 'patrol',
1009 'wlshow' => WatchedItemQueryService::FILTER_PATROLLED
1010 ], $user );
1011 $resultNotPatrolled = $this->doListWatchlistRequest( [
1012 'wlprop' => 'patrol',
1013 'wlshow' => WatchedItemQueryService::FILTER_NOT_PATROLLED
1014 ], $user );
1015
1016 $this->assertEquals(
1017 [
1018 [
1019 'type' => 'new',
1020 'patrolled' => true,
1021 'unpatrolled' => false,
1022 'autopatrolled' => false,
1023 ]
1024 ],
1025 $this->getItemsFromApiResponse( $resultPatrolled )
1026 );
1027 $this->assertSame( [], $this->getItemsFromApiResponse( $resultNotPatrolled ) );
1028 }
1029
1030 public function testNewAndEditTypeParameters() {
1031 $user = $this->getLoggedInTestUser();
1032 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1033 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1034 $this->doPageEdits(
1035 $user,
1036 [
1037 [
1038 'target' => $subjectTarget,
1039 'content' => 'Some Content',
1040 'summary' => 'Create the page',
1041 ],
1042 [
1043 'target' => $subjectTarget,
1044 'content' => 'Some Other Content',
1045 'summary' => 'Change the content',
1046 ],
1047 [
1048 'target' => $talkTarget,
1049 'content' => 'Some Talk Page Content',
1050 'summary' => 'Create Talk page',
1051 ],
1052 ]
1053 );
1054 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1055
1056 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1057 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1058
1059 $this->assertEquals(
1060 [
1061 [
1062 'type' => 'new',
1063 'ns' => $talkTarget->getNamespace(),
1064 'title' => $this->getPrefixedText( $talkTarget ),
1065 ],
1066 ],
1067 $this->getItemsFromApiResponse( $resultNew )
1068 );
1069 $this->assertEquals(
1070 [
1071 [
1072 'type' => 'edit',
1073 'ns' => $subjectTarget->getNamespace(),
1074 'title' => $this->getPrefixedText( $subjectTarget ),
1075 ],
1076 ],
1077 $this->getItemsFromApiResponse( $resultEdit )
1078 );
1079 }
1080
1081 public function testLogTypeParameters() {
1082 $user = $this->getLoggedInTestUser();
1083 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1084 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1085 $this->createPageAndDeleteIt( $subjectTarget );
1086 $this->doPageEdit(
1087 $user,
1088 $talkTarget,
1089 'Some Talk Page Content',
1090 'Create Talk page'
1091 );
1092 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1093
1094 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1095
1096 $this->assertEquals(
1097 [
1098 [
1099 'type' => 'log',
1100 'ns' => $subjectTarget->getNamespace(),
1101 'title' => $this->getPrefixedText( $subjectTarget ),
1102 ],
1103 ],
1104 $this->getItemsFromApiResponse( $result )
1105 );
1106 }
1107
1108 private function getExternalRC( LinkTarget $target ) {
1109 $title = $this->getServiceContainer()->getTitleFactory()->newFromLinkTarget( $target );
1110
1111 $rc = new RecentChange;
1112 $rc->mAttribs = [
1113 'rc_timestamp' => wfTimestamp( TS_MW ),
1114 'rc_namespace' => $title->getNamespace(),
1115 'rc_title' => $title->getDBkey(),
1116 'rc_type' => RC_EXTERNAL,
1117 'rc_source' => 'foo',
1118 'rc_minor' => 0,
1119 'rc_cur_id' => $title->getArticleID(),
1120 'rc_user' => 0,
1121 'rc_user_text' => 'ext>External User',
1122 'rc_comment' => '',
1123 'rc_comment_text' => '',
1124 'rc_comment_data' => null,
1125 'rc_this_oldid' => $title->getLatestRevID(),
1126 'rc_last_oldid' => $title->getLatestRevID(),
1127 'rc_bot' => 0,
1128 'rc_ip' => '',
1129 'rc_patrolled' => 0,
1130 'rc_new' => 0,
1131 'rc_old_len' => $title->getLength(),
1132 'rc_new_len' => $title->getLength(),
1133 'rc_deleted' => 0,
1134 'rc_logid' => 0,
1135 'rc_log_type' => null,
1136 'rc_log_action' => '',
1137 'rc_params' => '',
1138 ];
1139 $rc->mExtra = [
1140 'prefixedDBkey' => $title->getPrefixedDBkey(),
1141 'lastTimestamp' => 0,
1142 'oldSize' => $title->getLength(),
1143 'newSize' => $title->getLength(),
1144 'pageStatus' => 'changed'
1145 ];
1146
1147 return $rc;
1148 }
1149
1150 public function testExternalTypeParameters() {
1151 $user = $this->getLoggedInTestUser();
1152 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1153 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1154 $this->doPageEdit(
1155 $user,
1156 $subjectTarget,
1157 'Some Content',
1158 'Create the page'
1159 );
1160 $this->doPageEdit(
1161 $user,
1162 $talkTarget,
1163 'Some Talk Page Content',
1164 'Create Talk page'
1165 );
1166
1167 $rc = $this->getExternalRC( $subjectTarget );
1168 $rc->save();
1169
1170 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1171
1172 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1173
1174 $this->assertEquals(
1175 [
1176 [
1177 'type' => 'external',
1178 'ns' => $subjectTarget->getNamespace(),
1179 'title' => $this->getPrefixedText( $subjectTarget ),
1180 ],
1181 ],
1182 $this->getItemsFromApiResponse( $result )
1183 );
1184 }
1185
1186 public function testCategorizeTypeParameter() {
1187 $user = $this->getLoggedInTestUser();
1188 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1189 $categoryTarget = new TitleValue( NS_CATEGORY, 'ApiQueryWatchlistIntegrationTestCategory' );
1190 $this->doPageEdits(
1191 $user,
1192 [
1193 [
1194 'target' => $categoryTarget,
1195 'content' => 'Some Content',
1196 'summary' => 'Create the category',
1197 ],
1198 [
1199 'target' => $subjectTarget,
1200 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1201 'summary' => 'Create the page and add it to the category',
1202 ],
1203 ]
1204 );
1205 $title = $this->getServiceContainer()->getTitleFactory()->newFromLinkTarget( $subjectTarget );
1206 $revision = $this->getServiceContainer()
1207 ->getRevisionLookup()
1208 ->getRevisionByTitle( $title );
1209
1210 $comment = $revision->getComment();
1211 $rc = RecentChange::newForCategorization(
1212 $revision->getTimestamp(),
1213 $this->getServiceContainer()->getTitleFactory()->newFromLinkTarget( $categoryTarget ),
1214 $user,
1215 $comment ? $comment->text : '',
1216 $title,
1217 0,
1218 $revision->getId(),
1219 null,
1220 false
1221 );
1222 $rc->save();
1223
1224 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1225
1226 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1227
1228 $this->assertEquals(
1229 [
1230 [
1231 'type' => 'categorize',
1232 'ns' => $categoryTarget->getNamespace(),
1233 'title' => $this->getPrefixedText( $categoryTarget ),
1234 ],
1235 ],
1236 $this->getItemsFromApiResponse( $result )
1237 );
1238 }
1239
1240 public function testLimitParam() {
1241 $user = $this->getLoggedInTestUser();
1242 $target1 = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1243 $target2 = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1244 $target3 = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage2' );
1245 $this->doPageEdits(
1246 $user,
1247 [
1248 [
1249 'target' => $target1,
1250 'content' => 'Some Content',
1251 'summary' => 'Create the page',
1252 ],
1253 [
1254 'target' => $target2,
1255 'content' => 'Some Talk Page Content',
1256 'summary' => 'Create Talk page',
1257 ],
1258 [
1259 'target' => $target3,
1260 'content' => 'Some Other Content',
1261 'summary' => 'Create the page',
1262 ],
1263 ]
1264 );
1265 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1266
1267 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1268 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1269
1270 $this->assertEquals(
1271 [
1272 [
1273 'type' => 'new',
1274 'ns' => $target3->getNamespace(),
1275 'title' => $this->getPrefixedText( $target3 )
1276 ],
1277 [
1278 'type' => 'new',
1279 'ns' => $target2->getNamespace(),
1280 'title' => $this->getPrefixedText( $target2 )
1281 ],
1282 [
1283 'type' => 'new',
1284 'ns' => $target1->getNamespace(),
1285 'title' => $this->getPrefixedText( $target1 )
1286 ],
1287 ],
1288 $this->getItemsFromApiResponse( $resultWithoutLimit )
1289 );
1290 $this->assertEquals(
1291 [
1292 [
1293 'type' => 'new',
1294 'ns' => $target3->getNamespace(),
1295 'title' => $this->getPrefixedText( $target3 )
1296 ],
1297 [
1298 'type' => 'new',
1299 'ns' => $target2->getNamespace(),
1300 'title' => $this->getPrefixedText( $target2 )
1301 ],
1302 ],
1303 $this->getItemsFromApiResponse( $resultWithLimit )
1304 );
1305 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1306 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1307 }
1308
1309 public function testAllRevParam() {
1310 $user = $this->getLoggedInTestUser();
1311 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1312 $this->doPageEdits(
1313 $user,
1314 [
1315 [
1316 'target' => $target,
1317 'content' => 'Some Content',
1318 'summary' => 'Create the page',
1319 ],
1320 [
1321 'target' => $target,
1322 'content' => 'Some Other Content',
1323 'summary' => 'Change the content',
1324 ],
1325 ]
1326 );
1327 $this->watchPages( $user, [ $target ] );
1328
1329 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1330 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1331
1332 $this->assertEquals(
1333 [
1334 [
1335 'type' => 'edit',
1336 'ns' => $target->getNamespace(),
1337 'title' => $this->getPrefixedText( $target ),
1338 ],
1339 ],
1340 $this->getItemsFromApiResponse( $resultNoAllRev )
1341 );
1342 $this->assertEquals(
1343 [
1344 [
1345 'type' => 'edit',
1346 'ns' => $target->getNamespace(),
1347 'title' => $this->getPrefixedText( $target ),
1348 ],
1349 [
1350 'type' => 'new',
1351 'ns' => $target->getNamespace(),
1352 'title' => $this->getPrefixedText( $target ),
1353 ],
1354 ],
1355 $this->getItemsFromApiResponse( $resultAllRev )
1356 );
1357 }
1358
1359 public function testDirParams() {
1360 $user = $this->getLoggedInTestUser();
1361 $subjectTarget = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1362 $talkTarget = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1363 $this->doPageEdits(
1364 $user,
1365 [
1366 [
1367 'target' => $subjectTarget,
1368 'content' => 'Some Content',
1369 'summary' => 'Create the page',
1370 ],
1371 [
1372 'target' => $talkTarget,
1373 'content' => 'Some Talk Page Content',
1374 'summary' => 'Create Talk page',
1375 ],
1376 ]
1377 );
1378 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1379
1380 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1381 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1382
1383 $this->assertEquals(
1384 [
1385 [
1386 'type' => 'new',
1387 'ns' => $talkTarget->getNamespace(),
1388 'title' => $this->getPrefixedText( $talkTarget )
1389 ],
1390 [
1391 'type' => 'new',
1392 'ns' => $subjectTarget->getNamespace(),
1393 'title' => $this->getPrefixedText( $subjectTarget )
1394 ],
1395 ],
1396 $this->getItemsFromApiResponse( $resultDirOlder )
1397 );
1398 $this->assertEquals(
1399 [
1400 [
1401 'type' => 'new',
1402 'ns' => $subjectTarget->getNamespace(),
1403 'title' => $this->getPrefixedText( $subjectTarget )
1404 ],
1405 [
1406 'type' => 'new',
1407 'ns' => $talkTarget->getNamespace(),
1408 'title' => $this->getPrefixedText( $talkTarget )
1409 ],
1410 ],
1411 $this->getItemsFromApiResponse( $resultDirNewer )
1412 );
1413 }
1414
1415 public function testStartEndParams() {
1416 $user = $this->getLoggedInTestUser();
1417 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1418 $this->doPageEdit(
1419 $user,
1420 $target,
1421 'Some Content',
1422 'Create the page'
1423 );
1424 $this->watchPages( $user, [ $target ] );
1425
1426 $resultStart = $this->doListWatchlistRequest( [
1427 'wlstart' => '20010115000000',
1428 'wldir' => 'newer',
1429 'wlprop' => 'title',
1430 ] );
1431 $resultEnd = $this->doListWatchlistRequest( [
1432 'wlend' => '20010115000000',
1433 'wldir' => 'newer',
1434 'wlprop' => 'title',
1435 ] );
1436
1437 $this->assertEquals(
1438 [
1439 [
1440 'type' => 'new',
1441 'ns' => $target->getNamespace(),
1442 'title' => $this->getPrefixedText( $target ),
1443 ]
1444 ],
1445 $this->getItemsFromApiResponse( $resultStart )
1446 );
1447 $this->assertSame( [], $this->getItemsFromApiResponse( $resultEnd ) );
1448 }
1449
1450 public function testContinueParam() {
1451 $user = $this->getLoggedInTestUser();
1452 $target1 = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1453 $target2 = new TitleValue( NS_TALK, 'ApiQueryWatchlistIntegrationTestPage' );
1454 $target3 = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage2' );
1455 $this->doPageEdits(
1456 $user,
1457 [
1458 [
1459 'target' => $target1,
1460 'content' => 'Some Content',
1461 'summary' => 'Create the page',
1462 ],
1463 [
1464 'target' => $target2,
1465 'content' => 'Some Talk Page Content',
1466 'summary' => 'Create Talk page',
1467 ],
1468 [
1469 'target' => $target3,
1470 'content' => 'Some Other Content',
1471 'summary' => 'Create the page',
1472 ],
1473 ]
1474 );
1475 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1476
1477 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1478 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1479 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1480
1481 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1482
1483 $continuedResult = $this->doListWatchlistRequest(
1484 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1485 );
1486
1487 $this->assertEquals(
1488 [
1489 [
1490 'type' => 'new',
1491 'ns' => $target3->getNamespace(),
1492 'title' => $this->getPrefixedText( $target3 ),
1493 ],
1494 [
1495 'type' => 'new',
1496 'ns' => $target2->getNamespace(),
1497 'title' => $this->getPrefixedText( $target2 ),
1498 ],
1499 ],
1500 $this->getItemsFromApiResponse( $firstResult )
1501 );
1502 $this->assertEquals(
1503 [
1504 [
1505 'type' => 'new',
1506 'ns' => $target1->getNamespace(),
1507 'title' => $this->getPrefixedText( $target1 )
1508 ]
1509 ],
1510 $this->getItemsFromApiResponse( $continuedResult )
1511 );
1512 }
1513
1514 public function testOwnerAndTokenParams() {
1515 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1516 $this->doPageEdit(
1517 $this->getLoggedInTestUser(),
1518 $target,
1519 'Some Content',
1520 'Create the page'
1521 );
1522
1523 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
1524
1525 $otherUser = $this->getNonLoggedInTestUser();
1526 $userOptionsManager->setOption( $otherUser, 'watchlisttoken', '1234567890' );
1527 $otherUser->saveSettings();
1528
1529 $this->watchPages( $otherUser, [ $target ] );
1530
1531 $reloadedUser = $this->getServiceContainer()->getUserFactory()->newFromName( $otherUser->getName() );
1532 $option = $userOptionsManager->getOption( $reloadedUser, 'watchlisttoken' );
1533 $this->assertSame( '1234567890', $option );
1534
1535 $result = $this->doListWatchlistRequest( [
1536 'wlowner' => $otherUser->getName(),
1537 'wltoken' => '1234567890',
1538 'wlprop' => 'title',
1539 ] );
1540
1541 $this->assertEquals(
1542 [
1543 [
1544 'type' => 'new',
1545 'ns' => $target->getNamespace(),
1546 'title' => $this->getPrefixedText( $target )
1547 ]
1548 ],
1549 $this->getItemsFromApiResponse( $result )
1550 );
1551 }
1552
1553 public function testOwnerAndTokenParams_wrongToken() {
1554 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
1555
1556 $otherUser = $this->getNonLoggedInTestUser();
1557 $userOptionsManager->setOption( $otherUser, 'watchlisttoken', '1234567890' );
1558 $otherUser->saveSettings();
1559
1560 $this->expectApiErrorCode( 'bad_wltoken' );
1561
1562 $this->doListWatchlistRequest( [
1563 'wlowner' => $otherUser->getName(),
1564 'wltoken' => 'wrong-token',
1565 ] );
1566 }
1567
1568 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1569 $this->expectApiErrorCode( 'bad_wltoken' );
1570
1571 $this->doListWatchlistRequest( [
1572 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1573 'wltoken' => 'some-token',
1574 ] );
1575 }
1576
1577 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1578 $user = $this->getLoggedInTestUser();
1579 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1580 $this->doPageEdit(
1581 $user,
1582 $target,
1583 'Some Content',
1584 'Create the page'
1585 );
1586 $this->watchPages( $user, [ $target ] );
1587
1588 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1589
1590 $this->assertArrayHasKey( 'query', $result[0] );
1591 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1592
1593 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1594 $pages = array_values( $result[0]['query']['pages'] );
1595
1596 $this->assertArraySubsetsEqual(
1597 $pages,
1598 [
1599 [
1600 'ns' => $target->getNamespace(),
1601 'title' => $this->getPrefixedText( $target ),
1602 'new' => true,
1603 ]
1604 ],
1605 [ 'ns', 'title', 'new' ]
1606 );
1607 }
1608
1609 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1610 $user = $this->getLoggedInTestUser();
1611 $target = new TitleValue( NS_MAIN, 'ApiQueryWatchlistIntegrationTestPage' );
1612 $this->doPageEdits(
1613 $user,
1614 [
1615 [
1616 'target' => $target,
1617 'content' => 'Some Content',
1618 'summary' => 'Create the page',
1619 ],
1620 [
1621 'target' => $target,
1622 'content' => 'Some Other Content',
1623 'summary' => 'Change the content',
1624 ],
1625 ]
1626 );
1627 $this->watchPages( $user, [ $target ] );
1628
1629 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1630
1631 $this->assertArrayHasKey( 'query', $result[0] );
1632 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1633
1634 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1635 $pages = array_values( $result[0]['query']['pages'] );
1636
1637 $this->assertCount( 1, $pages );
1638 $this->assertSame( $target->getNamespace(), $pages[0]['ns'] );
1639 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1640 $this->assertArraySubsetsEqual(
1641 $pages[0]['revisions'],
1642 [
1643 [
1644 'comment' => 'Create the page',
1645 'user' => $user->getName(),
1646 'minor' => false,
1647 ],
1648 [
1649 'comment' => 'Change the content',
1650 'user' => $user->getName(),
1651 'minor' => false,
1652 ],
1653 ],
1654 [ 'comment', 'user', 'minor' ]
1655 );
1656 }
1657
1658 }