3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\MediaWikiServices
;
11 * @covers ApiQueryWatchlist
13 class ApiQueryWatchlistIntegrationTest
extends ApiTestCase
{
15 public function __construct( $name = null, array $data = [], $dataName = '' ) {
16 parent
::__construct( $name, $data, $dataName );
17 $this->tablesUsed
= array_unique(
18 array_merge( $this->tablesUsed
, [ 'watchlist', 'recentchanges', 'page' ] )
22 protected function setUp() {
24 self
::$users['ApiQueryWatchlistIntegrationTestUser']
25 = new TestUser( 'ApiQueryWatchlistIntegrationTestUser' );
26 self
::$users['ApiQueryWatchlistIntegrationTestUser2']
27 = new TestUser( 'ApiQueryWatchlistIntegrationTestUser2' );
28 $this->doLogin( 'ApiQueryWatchlistIntegrationTestUser' );
31 private function getTestUser() {
32 return self
::$users['ApiQueryWatchlistIntegrationTestUser']->getUser();
35 private function getNonLoggedInTestUser() {
36 return self
::$users['ApiQueryWatchlistIntegrationTestUser2']->getUser();
39 private function getSysopTestUser() {
40 return self
::$users['sysop']->getUser();
43 private function doPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
44 $title = Title
::newFromLinkTarget( $target );
45 $page = WikiPage
::factory( $title );
47 ContentHandler
::makeContent( $content, $title ),
55 private function doMinorPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
56 $title = Title
::newFromLinkTarget( $target );
57 $page = WikiPage
::factory( $title );
59 ContentHandler
::makeContent( $content, $title ),
67 private function doBotPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
68 $title = Title
::newFromLinkTarget( $target );
69 $page = WikiPage
::factory( $title );
71 ContentHandler
::makeContent( $content, $title ),
79 private function doAnonPageEdit( LinkTarget
$target, $content, $summary ) {
80 $title = Title
::newFromLinkTarget( $target );
81 $page = WikiPage
::factory( $title );
83 ContentHandler
::makeContent( $content, $title ),
91 private function doPatrolledPageEdit(
98 $title = Title
::newFromLinkTarget( $target );
99 $page = WikiPage
::factory( $title );
100 $status = $page->doEditContent(
101 ContentHandler
::makeContent( $content, $title ),
107 /** @var Revision $rev */
108 $rev = $status->value
['revision'];
109 $rc = $rev->getRecentChange();
110 $rc->doMarkPatrolled( $patrollingUser, false, [] );
113 private function deletePage( LinkTarget
$target, $reason ) {
114 $title = Title
::newFromLinkTarget( $target );
115 $page = WikiPage
::factory( $title );
116 $page->doDeleteArticleReal( $reason );
120 * Performs a batch of page edits as a specified user
122 * @param array $editData associative array, keys:
123 * - target => LinkTarget page to edit
124 * - content => string new content
125 * - summary => string edit summary
126 * - minorEdit => bool mark as minor edit if true (defaults to false)
127 * - botEdit => bool mark as bot edit if true (defaults to false)
129 private function doPageEdits( User
$user, array $editData ) {
130 foreach ( $editData as $singleEditData ) {
131 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
132 $this->doMinorPageEdit(
134 $singleEditData['target'],
135 $singleEditData['content'],
136 $singleEditData['summary']
140 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
141 $this->doBotPageEdit(
143 $singleEditData['target'],
144 $singleEditData['content'],
145 $singleEditData['summary']
151 $singleEditData['target'],
152 $singleEditData['content'],
153 $singleEditData['summary']
158 private function getWatchedItemStore() {
159 return MediaWikiServices
::getInstance()->getWatchedItemStore();
164 * @param LinkTarget[] $targets
166 private function watchPages( User
$user, array $targets ) {
167 $store = $this->getWatchedItemStore();
168 $store->addWatchBatchForUser( $user, $targets );
171 private function doListWatchlistRequest( array $params = [], $user = null ) {
172 return $this->doApiRequest(
174 [ 'action' => 'query', 'list' => 'watchlist' ],
176 ), null, false, $user
180 private function doGeneratorWatchlistRequest( array $params = [] ) {
181 return $this->doApiRequest(
183 [ 'action' => 'query', 'generator' => 'watchlist' ],
189 private function getItemsFromApiResponse( array $response ) {
190 return $response[0]['query']['watchlist'];
194 * Convenience method to assert that actual items array fetched from API is equal to the expected
195 * array, Unlike assertEquals this only checks if values of specified keys are equal in both
196 * arrays. This could be used e.g. not to compare IDs that could change between test run
197 * but only stable keys.
198 * Optionally this also checks that specified keys are present in the actual item without
199 * performing any checks on the related values.
201 * @param array $actualItems array of actual items (associative arrays)
202 * @param array $expectedItems array of expected items (associative arrays),
203 * those items have less keys than actual items
204 * @param array $keysUsedInValueComparison list of keys of the actual item that will be used
205 * in the comparison of values
206 * @param array $requiredKeys optional, list of keys that must be present in the
207 * actual items. Values of those keys are not checked.
209 private function assertArraySubsetsEqual(
211 array $expectedItems,
212 array $keysUsedInValueComparison,
213 array $requiredKeys = []
215 $this->assertCount( count( $expectedItems ), $actualItems );
217 // not checking values of all keys of the actual item, so removing unwanted keys from comparison
218 $actualItemsOnlyComparedValues = array_map(
219 function( array $item ) use ( $keysUsedInValueComparison ) {
220 return array_intersect_key( $item, array_flip( $keysUsedInValueComparison ) );
227 $actualItemsOnlyComparedValues
230 // Check that each item in $actualItems contains all of keys specified in $requiredKeys
231 $actualItemsKeysOnly = array_map( 'array_keys', $actualItems );
232 foreach ( $actualItemsKeysOnly as $keysOfTheItem ) {
233 $this->assertEmpty( array_diff( $requiredKeys, $keysOfTheItem ) );
237 private function getTitleFormatter() {
238 return new MediaWikiTitleCodec( Language
::factory( 'en' ), GenderCache
::singleton() );
241 private function getPrefixedText( LinkTarget
$target ) {
242 $formatter = $this->getTitleFormatter();
243 return $formatter->getPrefixedText( $target );
246 private function cleanTestUsersWatchlist() {
247 $user = $this->getTestUser();
248 $store = $this->getWatchedItemStore();
249 $items = $store->getWatchedItemsForUser( $user );
250 foreach ( $items as $item ) {
251 $store->removeWatch( $user, $item->getLinkTarget() );
255 public function testListWatchlist_returnsWatchedItemsWithRCInfo() {
256 // Clean up after previous tests that might have added something to the watchlist of
257 // the user with the same user ID as user used here as the test user
258 $this->cleanTestUsersWatchlist();
260 $user = $this->getTestUser();
261 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
268 $this->watchPages( $user, [ $target ] );
270 $result = $this->doListWatchlistRequest();
272 $this->assertArrayHasKey( 'query', $result[0] );
273 $this->assertArrayHasKey( 'watchlist', $result[0]['query'] );
275 $this->assertArraySubsetsEqual(
276 $this->getItemsFromApiResponse( $result ),
280 'ns' => $target->getNamespace(),
281 'title' => $this->getPrefixedText( $target ),
287 [ 'type', 'ns', 'title', 'bot', 'new', 'minor' ],
288 [ 'pageid', 'revid', 'old_revid' ]
292 public function testIdsPropParameter() {
293 $user = $this->getTestUser();
294 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
301 $this->watchPages( $user, [ $target ] );
303 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'ids', ] );
304 $items = $this->getItemsFromApiResponse( $result );
306 $this->assertCount( 1, $items );
307 $this->assertArrayHasKey( 'pageid', $items[0] );
308 $this->assertArrayHasKey( 'revid', $items[0] );
309 $this->assertArrayHasKey( 'old_revid', $items[0] );
310 $this->assertEquals( 'new', $items[0]['type'] );
313 public function testTitlePropParameter() {
314 $user = $this->getTestUser();
315 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
316 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
321 'target' => $subjectTarget,
322 'content' => 'Some Content',
323 'summary' => 'Create the page',
326 'target' => $talkTarget,
327 'content' => 'Some Talk Page Content',
328 'summary' => 'Create Talk page',
332 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
334 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', ] );
340 'ns' => $talkTarget->getNamespace(),
341 'title' => $this->getPrefixedText( $talkTarget ),
345 'ns' => $subjectTarget->getNamespace(),
346 'title' => $this->getPrefixedText( $subjectTarget ),
349 $this->getItemsFromApiResponse( $result )
353 public function testFlagsPropParameter() {
354 $user = $this->getTestUser();
355 $normalEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
356 $minorEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageM' );
357 $botEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageB' );
362 'target' => $normalEditTarget,
363 'content' => 'Some Content',
364 'summary' => 'Create the page',
367 'target' => $minorEditTarget,
368 'content' => 'Some Content',
369 'summary' => 'Create the page',
372 'target' => $minorEditTarget,
373 'content' => 'Slightly Better Content',
374 'summary' => 'Change content',
378 'target' => $botEditTarget,
379 'content' => 'Some Content',
380 'summary' => 'Create the page with a bot',
385 $this->watchPages( $user, [ $normalEditTarget, $minorEditTarget, $botEditTarget ] );
387 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'flags', ] );
410 $this->getItemsFromApiResponse( $result )
414 public function testUserPropParameter() {
415 $user = $this->getTestUser();
416 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
417 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
424 $this->doAnonPageEdit(
429 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
431 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'user', ] );
438 'user' => User
::newFromId( 0 )->getName(),
442 'user' => $user->getName(),
445 $this->getItemsFromApiResponse( $result )
449 public function testUserIdPropParameter() {
450 $user = $this->getTestUser();
451 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
452 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
459 $this->doAnonPageEdit(
464 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
466 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'userid', ] );
478 'user' => $user->getId(),
479 'userid' => $user->getId(),
482 $this->getItemsFromApiResponse( $result )
486 public function testCommentPropParameter() {
487 $user = $this->getTestUser();
488 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
493 'Create the <b>page</b>'
495 $this->watchPages( $user, [ $target ] );
497 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'comment', ] );
503 'comment' => 'Create the <b>page</b>',
506 $this->getItemsFromApiResponse( $result )
510 public function testParsedCommentPropParameter() {
511 $user = $this->getTestUser();
512 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
517 'Create the <b>page</b>'
519 $this->watchPages( $user, [ $target ] );
521 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'parsedcomment', ] );
527 'parsedcomment' => 'Create the <b>page</b>',
530 $this->getItemsFromApiResponse( $result )
534 public function testTimestampPropParameter() {
535 $user = $this->getTestUser();
536 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
543 $this->watchPages( $user, [ $target ] );
545 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'timestamp', ] );
546 $items = $this->getItemsFromApiResponse( $result );
548 $this->assertCount( 1, $items );
549 $this->assertArrayHasKey( 'timestamp', $items[0] );
550 $this->assertInternalType( 'string', $items[0]['timestamp'] );
553 public function testSizesPropParameter() {
554 $user = $this->getTestUser();
555 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
562 $this->watchPages( $user, [ $target ] );
564 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'sizes', ] );
574 $this->getItemsFromApiResponse( $result )
578 public function testNotificationTimestampPropParameter() {
579 $otherUser = $this->getNonLoggedInTestUser();
580 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
587 $store = $this->getWatchedItemStore();
588 $store->addWatch( $this->getTestUser(), $target );
589 $store->updateNotificationTimestamp(
595 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'notificationtimestamp', ] );
601 'notificationtimestamp' => '2015-12-12T01:01:01Z',
604 $this->getItemsFromApiResponse( $result )
608 private function setupPatrolledSpecificFixtures( User
$user ) {
609 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
611 $this->doPatrolledPageEdit(
615 'Create the page (this gets patrolled)',
619 $this->watchPages( $user, [ $target ] );
622 public function testPatrolPropParameter() {
623 $user = $this->getSysopTestUser();
624 $this->setupPatrolledSpecificFixtures( $user );
626 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
633 'unpatrolled' => false,
636 $this->getItemsFromApiResponse( $result )
640 private function createPageAndDeleteIt( LinkTarget
$target ) {
642 $this->getTestUser(),
645 'Create the page that will be deleted'
647 $this->deletePage( $target, 'Important Reason' );
650 public function testLoginfoPropParameter() {
651 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
652 $this->createPageAndDeleteIt( $target );
654 $this->watchPages( $this->getTestUser(), [ $target ] );
656 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
658 $this->assertArraySubsetsEqual(
659 $this->getItemsFromApiResponse( $result ),
663 'logtype' => 'delete',
664 'logaction' => 'delete',
668 [ 'type', 'logtype', 'logaction', 'logparams' ],
673 public function testEmptyPropParameter() {
674 $user = $this->getTestUser();
675 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
682 $this->watchPages( $user, [ $target ] );
684 $result = $this->doListWatchlistRequest( [ 'wlprop' => '', ] );
692 $this->getItemsFromApiResponse( $result )
696 public function testNamespaceParam() {
697 $user = $this->getTestUser();
698 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
699 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
704 'target' => $subjectTarget,
705 'content' => 'Some Content',
706 'summary' => 'Create the page',
709 'target' => $talkTarget,
710 'content' => 'Some Content',
711 'summary' => 'Create the talk page',
715 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
717 $result = $this->doListWatchlistRequest( [ 'wlnamespace' => '0', ] );
719 $this->assertArraySubsetsEqual(
720 $this->getItemsFromApiResponse( $result ),
724 'title' => $this->getPrefixedText( $subjectTarget ),
731 public function testUserParam() {
732 $user = $this->getTestUser();
733 $otherUser = $this->getNonLoggedInTestUser();
734 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
735 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
745 'What is this page about?',
746 'Create the talk page'
748 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
750 $result = $this->doListWatchlistRequest( [
751 'wlprop' => 'user|title',
752 'wluser' => $otherUser->getName(),
759 'ns' => $talkTarget->getNamespace(),
760 'title' => $this->getPrefixedText( $talkTarget ),
761 'user' => $otherUser->getName(),
764 $this->getItemsFromApiResponse( $result )
768 public function testExcludeUserParam() {
769 $user = $this->getTestUser();
770 $otherUser = $this->getNonLoggedInTestUser();
771 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
772 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
782 'What is this page about?',
783 'Create the talk page'
785 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
787 $result = $this->doListWatchlistRequest( [
788 'wlprop' => 'user|title',
789 'wlexcludeuser' => $otherUser->getName(),
796 'ns' => $subjectTarget->getNamespace(),
797 'title' => $this->getPrefixedText( $subjectTarget ),
798 'user' => $user->getName(),
801 $this->getItemsFromApiResponse( $result )
805 public function testShowMinorParams() {
806 $user = $this->getTestUser();
807 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
813 'content' => 'Some Content',
814 'summary' => 'Create the page',
818 'content' => 'Slightly Better Content',
819 'summary' => 'Change content',
824 $this->watchPages( $user, [ $target ] );
826 $resultMinor = $this->doListWatchlistRequest( [ 'wlshow' => 'minor', 'wlprop' => 'flags' ] );
827 $resultNotMinor = $this->doListWatchlistRequest( [ 'wlshow' => '!minor', 'wlprop' => 'flags' ] );
829 $this->assertArraySubsetsEqual(
830 $this->getItemsFromApiResponse( $resultMinor ),
836 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotMinor ) );
839 public function testShowBotParams() {
840 $user = $this->getTestUser();
841 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
842 $this->doBotPageEdit(
848 $this->watchPages( $user, [ $target ] );
850 $resultBot = $this->doListWatchlistRequest( [ 'wlshow' => 'bot' ] );
851 $resultNotBot = $this->doListWatchlistRequest( [ 'wlshow' => '!bot' ] );
853 $this->assertArraySubsetsEqual(
854 $this->getItemsFromApiResponse( $resultBot ),
860 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotBot ) );
863 public function testShowAnonParams() {
864 $user = $this->getTestUser();
865 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
866 $this->doAnonPageEdit(
871 $this->watchPages( $user, [ $target ] );
873 $resultAnon = $this->doListWatchlistRequest( [
877 $resultNotAnon = $this->doListWatchlistRequest( [
882 $this->assertArraySubsetsEqual(
883 $this->getItemsFromApiResponse( $resultAnon ),
889 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotAnon ) );
892 public function testShowUnreadParams() {
893 $user = $this->getTestUser();
894 $otherUser = $this->getNonLoggedInTestUser();
895 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
896 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
907 'Create the talk page'
909 $store = $this->getWatchedItemStore();
910 $store->addWatchBatchForUser( $user, [ $subjectTarget, $talkTarget ] );
911 $store->updateNotificationTimestamp(
917 $resultUnread = $this->doListWatchlistRequest( [
918 'wlprop' => 'notificationtimestamp|title',
921 $resultNotUnread = $this->doListWatchlistRequest( [
922 'wlprop' => 'notificationtimestamp|title',
923 'wlshow' => '!unread'
930 'notificationtimestamp' => '2015-12-12T01:01:01Z',
931 'ns' => $talkTarget->getNamespace(),
932 'title' => $this->getPrefixedText( $talkTarget )
935 $this->getItemsFromApiResponse( $resultUnread )
941 'notificationtimestamp' => '',
942 'ns' => $subjectTarget->getNamespace(),
943 'title' => $this->getPrefixedText( $subjectTarget )
946 $this->getItemsFromApiResponse( $resultNotUnread )
950 public function testShowPatrolledParams() {
951 $user = $this->getSysopTestUser();
952 $this->setupPatrolledSpecificFixtures( $user );
954 $resultPatrolled = $this->doListWatchlistRequest( [
955 'wlprop' => 'patrol',
956 'wlshow' => 'patrolled'
958 $resultNotPatrolled = $this->doListWatchlistRequest( [
959 'wlprop' => 'patrol',
960 'wlshow' => '!patrolled'
968 'unpatrolled' => false,
971 $this->getItemsFromApiResponse( $resultPatrolled )
973 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotPatrolled ) );
976 public function testNewAndEditTypeParameters() {
977 $user = $this->getTestUser();
978 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
979 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
984 'target' => $subjectTarget,
985 'content' => 'Some Content',
986 'summary' => 'Create the page',
989 'target' => $subjectTarget,
990 'content' => 'Some Other Content',
991 'summary' => 'Change the content',
994 'target' => $talkTarget,
995 'content' => 'Some Talk Page Content',
996 'summary' => 'Create Talk page',
1000 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1002 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1003 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1005 $this->assertEquals(
1009 'ns' => $talkTarget->getNamespace(),
1010 'title' => $this->getPrefixedText( $talkTarget ),
1013 $this->getItemsFromApiResponse( $resultNew )
1015 $this->assertEquals(
1019 'ns' => $subjectTarget->getNamespace(),
1020 'title' => $this->getPrefixedText( $subjectTarget ),
1023 $this->getItemsFromApiResponse( $resultEdit )
1027 public function testLogTypeParameters() {
1028 $user = $this->getTestUser();
1029 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1030 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1031 $this->createPageAndDeleteIt( $subjectTarget );
1035 'Some Talk Page Content',
1038 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1040 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1042 $this->assertEquals(
1046 'ns' => $subjectTarget->getNamespace(),
1047 'title' => $this->getPrefixedText( $subjectTarget ),
1050 $this->getItemsFromApiResponse( $result )
1054 private function getExternalRC( LinkTarget
$target ) {
1055 $title = Title
::newFromLinkTarget( $target );
1057 $rc = new RecentChange
;
1058 $rc->mTitle
= $title;
1060 'rc_timestamp' => wfTimestamp( TS_MW
),
1061 'rc_namespace' => $title->getNamespace(),
1062 'rc_title' => $title->getDBkey(),
1063 'rc_type' => RC_EXTERNAL
,
1064 'rc_source' => 'foo',
1066 'rc_cur_id' => $title->getArticleID(),
1068 'rc_user_text' => 'External User',
1070 'rc_this_oldid' => $title->getLatestRevID(),
1071 'rc_last_oldid' => $title->getLatestRevID(),
1074 'rc_patrolled' => 0,
1076 'rc_old_len' => $title->getLength(),
1077 'rc_new_len' => $title->getLength(),
1080 'rc_log_type' => null,
1081 'rc_log_action' => '',
1085 'prefixedDBkey' => $title->getPrefixedDBkey(),
1086 'lastTimestamp' => 0,
1087 'oldSize' => $title->getLength(),
1088 'newSize' => $title->getLength(),
1089 'pageStatus' => 'changed'
1095 public function testExternalTypeParameters() {
1096 $user = $this->getTestUser();
1097 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1098 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1108 'Some Talk Page Content',
1112 $rc = $this->getExternalRC( $subjectTarget );
1115 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1117 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1119 $this->assertEquals(
1122 'type' => 'external',
1123 'ns' => $subjectTarget->getNamespace(),
1124 'title' => $this->getPrefixedText( $subjectTarget ),
1127 $this->getItemsFromApiResponse( $result )
1131 public function testCategorizeTypeParameter() {
1132 $user = $this->getTestUser();
1133 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1134 $categoryTarget = new TitleValue( NS_CATEGORY
, 'ApiQueryWatchlistIntegrationTestCategory' );
1139 'target' => $categoryTarget,
1140 'content' => 'Some Content',
1141 'summary' => 'Create the category',
1144 'target' => $subjectTarget,
1145 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1146 'summary' => 'Create the page and add it to the category',
1150 $title = Title
::newFromLinkTarget( $subjectTarget );
1151 $revision = Revision
::newFromTitle( $title );
1153 $rc = RecentChange
::newForCategorization(
1154 $revision->getTimestamp(),
1155 Title
::newFromLinkTarget( $categoryTarget ),
1157 $revision->getComment(),
1166 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1168 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1170 $this->assertEquals(
1173 'type' => 'categorize',
1174 'ns' => $categoryTarget->getNamespace(),
1175 'title' => $this->getPrefixedText( $categoryTarget ),
1178 $this->getItemsFromApiResponse( $result )
1182 public function testLimitParam() {
1183 $user = $this->getTestUser();
1184 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1185 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1186 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1191 'target' => $target1,
1192 'content' => 'Some Content',
1193 'summary' => 'Create the page',
1196 'target' => $target2,
1197 'content' => 'Some Talk Page Content',
1198 'summary' => 'Create Talk page',
1201 'target' => $target3,
1202 'content' => 'Some Other Content',
1203 'summary' => 'Create the page',
1207 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1209 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1210 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1212 $this->assertEquals(
1216 'ns' => $target3->getNamespace(),
1217 'title' => $this->getPrefixedText( $target3 )
1221 'ns' => $target2->getNamespace(),
1222 'title' => $this->getPrefixedText( $target2 )
1226 'ns' => $target1->getNamespace(),
1227 'title' => $this->getPrefixedText( $target1 )
1230 $this->getItemsFromApiResponse( $resultWithoutLimit )
1232 $this->assertEquals(
1236 'ns' => $target3->getNamespace(),
1237 'title' => $this->getPrefixedText( $target3 )
1241 'ns' => $target2->getNamespace(),
1242 'title' => $this->getPrefixedText( $target2 )
1245 $this->getItemsFromApiResponse( $resultWithLimit )
1247 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1248 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1251 public function testAllRevParam() {
1252 $user = $this->getTestUser();
1253 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1258 'target' => $target,
1259 'content' => 'Some Content',
1260 'summary' => 'Create the page',
1263 'target' => $target,
1264 'content' => 'Some Other Content',
1265 'summary' => 'Change the content',
1269 $this->watchPages( $user, [ $target ] );
1271 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1272 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1274 $this->assertEquals(
1278 'ns' => $target->getNamespace(),
1279 'title' => $this->getPrefixedText( $target ),
1282 $this->getItemsFromApiResponse( $resultNoAllRev )
1284 $this->assertEquals(
1288 'ns' => $target->getNamespace(),
1289 'title' => $this->getPrefixedText( $target ),
1293 'ns' => $target->getNamespace(),
1294 'title' => $this->getPrefixedText( $target ),
1297 $this->getItemsFromApiResponse( $resultAllRev )
1301 public function testDirParams() {
1302 $user = $this->getTestUser();
1303 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1304 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1309 'target' => $subjectTarget,
1310 'content' => 'Some Content',
1311 'summary' => 'Create the page',
1314 'target' => $talkTarget,
1315 'content' => 'Some Talk Page Content',
1316 'summary' => 'Create Talk page',
1320 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1322 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1323 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1325 $this->assertEquals(
1329 'ns' => $talkTarget->getNamespace(),
1330 'title' => $this->getPrefixedText( $talkTarget )
1334 'ns' => $subjectTarget->getNamespace(),
1335 'title' => $this->getPrefixedText( $subjectTarget )
1338 $this->getItemsFromApiResponse( $resultDirOlder )
1340 $this->assertEquals(
1344 'ns' => $subjectTarget->getNamespace(),
1345 'title' => $this->getPrefixedText( $subjectTarget )
1349 'ns' => $talkTarget->getNamespace(),
1350 'title' => $this->getPrefixedText( $talkTarget )
1353 $this->getItemsFromApiResponse( $resultDirNewer )
1357 public function testStartEndParams() {
1358 $user = $this->getTestUser();
1359 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1366 $this->watchPages( $user, [ $target ] );
1368 $resultStart = $this->doListWatchlistRequest( [
1369 'wlstart' => '20010115000000',
1371 'wlprop' => 'title',
1373 $resultEnd = $this->doListWatchlistRequest( [
1374 'wlend' => '20010115000000',
1376 'wlprop' => 'title',
1379 $this->assertEquals(
1383 'ns' => $target->getNamespace(),
1384 'title' => $this->getPrefixedText( $target ),
1387 $this->getItemsFromApiResponse( $resultStart )
1389 $this->assertEmpty( $this->getItemsFromApiResponse( $resultEnd ) );
1392 public function testContinueParam() {
1393 $user = $this->getTestUser();
1394 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1395 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1396 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1401 'target' => $target1,
1402 'content' => 'Some Content',
1403 'summary' => 'Create the page',
1406 'target' => $target2,
1407 'content' => 'Some Talk Page Content',
1408 'summary' => 'Create Talk page',
1411 'target' => $target3,
1412 'content' => 'Some Other Content',
1413 'summary' => 'Create the page',
1417 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1419 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1420 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1421 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1423 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1425 $continuedResult = $this->doListWatchlistRequest(
1426 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1429 $this->assertEquals(
1433 'ns' => $target3->getNamespace(),
1434 'title' => $this->getPrefixedText( $target3 ),
1438 'ns' => $target2->getNamespace(),
1439 'title' => $this->getPrefixedText( $target2 ),
1442 $this->getItemsFromApiResponse( $firstResult )
1444 $this->assertEquals(
1448 'ns' => $target1->getNamespace(),
1449 'title' => $this->getPrefixedText( $target1 )
1452 $this->getItemsFromApiResponse( $continuedResult )
1456 public function testOwnerAndTokenParams() {
1457 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1459 $this->getTestUser(),
1465 $otherUser = $this->getNonLoggedInTestUser();
1466 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1467 $otherUser->saveSettings();
1469 $this->watchPages( $otherUser, [ $target ] );
1471 $result = $this->doListWatchlistRequest( [
1472 'wlowner' => $otherUser->getName(),
1473 'wltoken' => '1234567890',
1474 'wlprop' => 'title',
1477 $this->assertEquals(
1481 'ns' => $target->getNamespace(),
1482 'title' => $this->getPrefixedText( $target )
1485 $this->getItemsFromApiResponse( $result )
1489 public function testOwnerAndTokenParams_wrongToken() {
1490 $otherUser = $this->getNonLoggedInTestUser();
1491 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1492 $otherUser->saveSettings();
1494 $this->setExpectedException( UsageException
::class, 'Incorrect watchlist token provided' );
1496 $this->doListWatchlistRequest( [
1497 'wlowner' => $otherUser->getName(),
1498 'wltoken' => 'wrong-token',
1502 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1503 $this->setExpectedException( UsageException
::class, 'Incorrect watchlist token provided' );
1505 $this->doListWatchlistRequest( [
1506 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1507 'wltoken' => 'some-token',
1511 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1512 $user = $this->getTestUser();
1513 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1520 $this->watchPages( $user, [ $target ] );
1522 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1524 $this->assertArrayHasKey( 'query', $result[0] );
1525 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1527 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1528 $pages = array_values( $result[0]['query']['pages'] );
1530 $this->assertArraySubsetsEqual(
1534 'ns' => $target->getNamespace(),
1535 'title' => $this->getPrefixedText( $target ),
1539 [ 'ns', 'title', 'new' ]
1543 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1544 $user = $this->getTestUser();
1545 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1550 'target' => $target,
1551 'content' => 'Some Content',
1552 'summary' => 'Create the page',
1555 'target' => $target,
1556 'content' => 'Some Other Content',
1557 'summary' => 'Change the content',
1561 $this->watchPages( $user, [ $target ] );
1563 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1565 $this->assertArrayHasKey( 'query', $result[0] );
1566 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1568 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1569 $pages = array_values( $result[0]['query']['pages'] );
1571 $this->assertCount( 1, $pages );
1572 $this->assertEquals( 0, $pages[0]['ns'] );
1573 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1574 $this->assertArraySubsetsEqual(
1575 $pages[0]['revisions'],
1578 'comment' => 'Create the page',
1579 'user' => $user->getName(),
1583 'comment' => 'Change the content',
1584 'user' => $user->getName(),
1588 [ 'comment', 'user', 'minor' ]