3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Revision\SlotRecord
;
12 * @covers ApiQueryWatchlist
14 class ApiQueryWatchlistIntegrationTest
extends ApiTestCase
{
16 public function __construct( $name = null, array $data = [], $dataName = '' ) {
17 parent
::__construct( $name, $data, $dataName );
18 $this->tablesUsed
= array_unique(
19 array_merge( $this->tablesUsed
, [ 'watchlist', 'recentchanges', 'page' ] )
23 protected function setUp() {
25 self
::$users['ApiQueryWatchlistIntegrationTestUser'] = $this->getMutableTestUser();
26 self
::$users['ApiQueryWatchlistIntegrationTestUser2'] = $this->getMutableTestUser();
29 private function getLoggedInTestUser() {
30 return self
::$users['ApiQueryWatchlistIntegrationTestUser']->getUser();
33 private function getNonLoggedInTestUser() {
34 return self
::$users['ApiQueryWatchlistIntegrationTestUser2']->getUser();
37 private function doPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
38 $title = Title
::newFromLinkTarget( $target );
39 $page = WikiPage
::factory( $title );
41 ContentHandler
::makeContent( $content, $title ),
49 private function doMinorPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
50 $title = Title
::newFromLinkTarget( $target );
51 $page = WikiPage
::factory( $title );
53 ContentHandler
::makeContent( $content, $title ),
61 private function doBotPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
62 $title = Title
::newFromLinkTarget( $target );
63 $page = WikiPage
::factory( $title );
65 ContentHandler
::makeContent( $content, $title ),
73 private function doAnonPageEdit( LinkTarget
$target, $content, $summary ) {
74 $title = Title
::newFromLinkTarget( $target );
75 $page = WikiPage
::factory( $title );
77 ContentHandler
::makeContent( $content, $title ),
85 private function doPatrolledPageEdit(
92 $title = Title
::newFromLinkTarget( $target );
93 $summary = CommentStoreComment
::newUnsavedComment( trim( $summary ) );
94 $page = WikiPage
::factory( $title );
96 $updater = $page->newPageUpdater( $user );
97 $updater->setContent( SlotRecord
::MAIN
, ContentHandler
::makeContent( $content, $title ) );
98 $rev = $updater->saveRevision( $summary );
100 $rc = MediaWikiServices
::getInstance()->getRevisionStore()->getRecentChange( $rev );
101 $rc->doMarkPatrolled( $patrollingUser, false, [] );
104 private function deletePage( LinkTarget
$target, $reason ) {
105 $title = Title
::newFromLinkTarget( $target );
106 $page = WikiPage
::factory( $title );
107 $page->doDeleteArticleReal( $reason );
111 * Performs a batch of page edits as a specified user
113 * @param array $editData associative array, keys:
114 * - target => LinkTarget page to edit
115 * - content => string new content
116 * - summary => string edit summary
117 * - minorEdit => bool mark as minor edit if true (defaults to false)
118 * - botEdit => bool mark as bot edit if true (defaults to false)
120 private function doPageEdits( User
$user, array $editData ) {
121 foreach ( $editData as $singleEditData ) {
122 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
123 $this->doMinorPageEdit(
125 $singleEditData['target'],
126 $singleEditData['content'],
127 $singleEditData['summary']
131 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
132 $this->doBotPageEdit(
134 $singleEditData['target'],
135 $singleEditData['content'],
136 $singleEditData['summary']
142 $singleEditData['target'],
143 $singleEditData['content'],
144 $singleEditData['summary']
149 private function getWatchedItemStore() {
150 return MediaWikiServices
::getInstance()->getWatchedItemStore();
155 * @param LinkTarget[] $targets
157 private function watchPages( User
$user, array $targets ) {
158 $store = $this->getWatchedItemStore();
159 $store->addWatchBatchForUser( $user, $targets );
162 private function doListWatchlistRequest( array $params = [], $user = null ) {
163 if ( $user === null ) {
164 $user = $this->getLoggedInTestUser();
166 return $this->doApiRequest(
168 [ 'action' => 'query', 'list' => 'watchlist' ],
170 ), null, false, $user
174 private function doGeneratorWatchlistRequest( array $params = [] ) {
175 return $this->doApiRequest(
177 [ 'action' => 'query', 'generator' => 'watchlist' ],
179 ), null, false, $this->getLoggedInTestUser()
183 private function getItemsFromApiResponse( array $response ) {
184 return $response[0]['query']['watchlist'];
188 * Convenience method to assert that actual items array fetched from API is equal to the expected
189 * array, Unlike assertEquals this only checks if values of specified keys are equal in both
190 * arrays. This could be used e.g. not to compare IDs that could change between test run
191 * but only stable keys.
192 * Optionally this also checks that specified keys are present in the actual item without
193 * performing any checks on the related values.
195 * @param array $actualItems array of actual items (associative arrays)
196 * @param array $expectedItems array of expected items (associative arrays),
197 * those items have less keys than actual items
198 * @param array $keysUsedInValueComparison list of keys of the actual item that will be used
199 * in the comparison of values
200 * @param array $requiredKeys optional, list of keys that must be present in the
201 * actual items. Values of those keys are not checked.
203 private function assertArraySubsetsEqual(
205 array $expectedItems,
206 array $keysUsedInValueComparison,
207 array $requiredKeys = []
209 $this->assertCount( count( $expectedItems ), $actualItems );
211 // not checking values of all keys of the actual item, so removing unwanted keys from comparison
212 $actualItemsOnlyComparedValues = array_map(
213 function ( array $item ) use ( $keysUsedInValueComparison ) {
214 return array_intersect_key( $item, array_flip( $keysUsedInValueComparison ) );
221 $actualItemsOnlyComparedValues
224 // Check that each item in $actualItems contains all of keys specified in $requiredKeys
225 $actualItemsKeysOnly = array_map( 'array_keys', $actualItems );
226 foreach ( $actualItemsKeysOnly as $keysOfTheItem ) {
227 $this->assertEmpty( array_diff( $requiredKeys, $keysOfTheItem ) );
231 private function getPrefixedText( LinkTarget
$target ) {
232 return MediaWikiServices
::getInstance()->getTitleFormatter()->getPrefixedText( $target );
235 private function cleanTestUsersWatchlist() {
236 $user = $this->getLoggedInTestUser();
237 $store = $this->getWatchedItemStore();
238 $items = $store->getWatchedItemsForUser( $user );
239 foreach ( $items as $item ) {
240 $store->removeWatch( $user, $item->getLinkTarget() );
244 public function testListWatchlist_returnsWatchedItemsWithRCInfo() {
245 // Clean up after previous tests that might have added something to the watchlist of
246 // the user with the same user ID as user used here as the test user
247 $this->cleanTestUsersWatchlist();
249 $user = $this->getLoggedInTestUser();
250 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
257 $this->watchPages( $user, [ $target ] );
259 $result = $this->doListWatchlistRequest();
261 $this->assertArrayHasKey( 'query', $result[0] );
262 $this->assertArrayHasKey( 'watchlist', $result[0]['query'] );
264 $this->assertArraySubsetsEqual(
265 $this->getItemsFromApiResponse( $result ),
269 'ns' => $target->getNamespace(),
270 'title' => $this->getPrefixedText( $target ),
276 [ 'type', 'ns', 'title', 'bot', 'new', 'minor' ],
277 [ 'pageid', 'revid', 'old_revid' ]
281 public function testIdsPropParameter() {
282 $user = $this->getLoggedInTestUser();
283 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
290 $this->watchPages( $user, [ $target ] );
292 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'ids', ] );
293 $items = $this->getItemsFromApiResponse( $result );
295 $this->assertCount( 1, $items );
296 $this->assertArrayHasKey( 'pageid', $items[0] );
297 $this->assertArrayHasKey( 'revid', $items[0] );
298 $this->assertArrayHasKey( 'old_revid', $items[0] );
299 $this->assertEquals( 'new', $items[0]['type'] );
302 public function testTitlePropParameter() {
303 $user = $this->getLoggedInTestUser();
304 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
305 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
310 'target' => $subjectTarget,
311 'content' => 'Some Content',
312 'summary' => 'Create the page',
315 'target' => $talkTarget,
316 'content' => 'Some Talk Page Content',
317 'summary' => 'Create Talk page',
321 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
323 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', ] );
329 'ns' => $talkTarget->getNamespace(),
330 'title' => $this->getPrefixedText( $talkTarget ),
334 'ns' => $subjectTarget->getNamespace(),
335 'title' => $this->getPrefixedText( $subjectTarget ),
338 $this->getItemsFromApiResponse( $result )
342 public function testFlagsPropParameter() {
343 $user = $this->getLoggedInTestUser();
344 $normalEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
345 $minorEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageM' );
346 $botEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageB' );
351 'target' => $normalEditTarget,
352 'content' => 'Some Content',
353 'summary' => 'Create the page',
356 'target' => $minorEditTarget,
357 'content' => 'Some Content',
358 'summary' => 'Create the page',
361 'target' => $minorEditTarget,
362 'content' => 'Slightly Better Content',
363 'summary' => 'Change content',
367 'target' => $botEditTarget,
368 'content' => 'Some Content',
369 'summary' => 'Create the page with a bot',
374 $this->watchPages( $user, [ $normalEditTarget, $minorEditTarget, $botEditTarget ] );
376 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'flags', ] );
399 $this->getItemsFromApiResponse( $result )
403 public function testUserPropParameter() {
404 $user = $this->getLoggedInTestUser();
405 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
406 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
413 $this->doAnonPageEdit(
418 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
420 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'user', ] );
427 'user' => User
::newFromId( 0 )->getName(),
431 'user' => $user->getName(),
434 $this->getItemsFromApiResponse( $result )
438 public function testUserIdPropParameter() {
439 $user = $this->getLoggedInTestUser();
440 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
441 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
448 $this->doAnonPageEdit(
453 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
455 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'userid', ] );
467 'user' => $user->getId(),
468 'userid' => $user->getId(),
471 $this->getItemsFromApiResponse( $result )
475 public function testCommentPropParameter() {
476 $user = $this->getLoggedInTestUser();
477 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
482 'Create the <b>page</b>'
484 $this->watchPages( $user, [ $target ] );
486 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'comment', ] );
492 'comment' => 'Create the <b>page</b>',
495 $this->getItemsFromApiResponse( $result )
499 public function testParsedCommentPropParameter() {
500 $user = $this->getLoggedInTestUser();
501 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
506 'Create the <b>page</b>'
508 $this->watchPages( $user, [ $target ] );
510 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'parsedcomment', ] );
516 'parsedcomment' => 'Create the <b>page</b>',
519 $this->getItemsFromApiResponse( $result )
523 public function testTimestampPropParameter() {
524 $user = $this->getLoggedInTestUser();
525 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
532 $this->watchPages( $user, [ $target ] );
534 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'timestamp', ] );
535 $items = $this->getItemsFromApiResponse( $result );
537 $this->assertCount( 1, $items );
538 $this->assertArrayHasKey( 'timestamp', $items[0] );
539 $this->assertInternalType( 'string', $items[0]['timestamp'] );
542 public function testSizesPropParameter() {
543 $user = $this->getLoggedInTestUser();
544 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
551 $this->watchPages( $user, [ $target ] );
553 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'sizes', ] );
563 $this->getItemsFromApiResponse( $result )
567 public function testNotificationTimestampPropParameter() {
568 $otherUser = $this->getNonLoggedInTestUser();
569 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
576 $store = $this->getWatchedItemStore();
577 $store->addWatch( $this->getLoggedInTestUser(), $target );
578 $store->updateNotificationTimestamp(
584 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'notificationtimestamp', ] );
590 'notificationtimestamp' => '2015-12-12T01:01:01Z',
593 $this->getItemsFromApiResponse( $result )
597 private function setupPatrolledSpecificFixtures( User
$user ) {
598 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
600 $this->doPatrolledPageEdit(
604 'Create the page (this gets patrolled)',
608 $this->watchPages( $user, [ $target ] );
611 public function testPatrolPropParameter() {
612 $testUser = static::getTestSysop();
613 $user = $testUser->getUser();
614 $this->setupPatrolledSpecificFixtures( $user );
616 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
623 'unpatrolled' => false,
624 'autopatrolled' => false,
627 $this->getItemsFromApiResponse( $result )
631 private function createPageAndDeleteIt( LinkTarget
$target ) {
633 $this->getLoggedInTestUser(),
636 'Create the page that will be deleted'
638 $this->deletePage( $target, 'Important Reason' );
641 public function testLoginfoPropParameter() {
642 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
643 $this->createPageAndDeleteIt( $target );
645 $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
647 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
649 $this->assertArraySubsetsEqual(
650 $this->getItemsFromApiResponse( $result ),
654 'logtype' => 'delete',
655 'logaction' => 'delete',
659 [ 'type', 'logtype', 'logaction', 'logparams' ],
664 public function testEmptyPropParameter() {
665 $user = $this->getLoggedInTestUser();
666 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
673 $this->watchPages( $user, [ $target ] );
675 $result = $this->doListWatchlistRequest( [ 'wlprop' => '', ] );
683 $this->getItemsFromApiResponse( $result )
687 public function testNamespaceParam() {
688 $user = $this->getLoggedInTestUser();
689 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
690 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
695 'target' => $subjectTarget,
696 'content' => 'Some Content',
697 'summary' => 'Create the page',
700 'target' => $talkTarget,
701 'content' => 'Some Content',
702 'summary' => 'Create the talk page',
706 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
708 $result = $this->doListWatchlistRequest( [ 'wlnamespace' => '0', ] );
710 $this->assertArraySubsetsEqual(
711 $this->getItemsFromApiResponse( $result ),
715 'title' => $this->getPrefixedText( $subjectTarget ),
722 public function testUserParam() {
723 $user = $this->getLoggedInTestUser();
724 $otherUser = $this->getNonLoggedInTestUser();
725 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
726 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
736 'What is this page about?',
737 'Create the talk page'
739 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
741 $result = $this->doListWatchlistRequest( [
742 'wlprop' => 'user|title',
743 'wluser' => $otherUser->getName(),
750 'ns' => $talkTarget->getNamespace(),
751 'title' => $this->getPrefixedText( $talkTarget ),
752 'user' => $otherUser->getName(),
755 $this->getItemsFromApiResponse( $result )
759 public function testExcludeUserParam() {
760 $user = $this->getLoggedInTestUser();
761 $otherUser = $this->getNonLoggedInTestUser();
762 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
763 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
773 'What is this page about?',
774 'Create the talk page'
776 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
778 $result = $this->doListWatchlistRequest( [
779 'wlprop' => 'user|title',
780 'wlexcludeuser' => $otherUser->getName(),
787 'ns' => $subjectTarget->getNamespace(),
788 'title' => $this->getPrefixedText( $subjectTarget ),
789 'user' => $user->getName(),
792 $this->getItemsFromApiResponse( $result )
796 public function testShowMinorParams() {
797 $user = $this->getLoggedInTestUser();
798 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
804 'content' => 'Some Content',
805 'summary' => 'Create the page',
809 'content' => 'Slightly Better Content',
810 'summary' => 'Change content',
815 $this->watchPages( $user, [ $target ] );
817 $resultMinor = $this->doListWatchlistRequest( [
818 'wlshow' => WatchedItemQueryService
::FILTER_MINOR
,
821 $resultNotMinor = $this->doListWatchlistRequest( [
822 'wlshow' => WatchedItemQueryService
::FILTER_NOT_MINOR
, 'wlprop' => 'flags'
825 $this->assertArraySubsetsEqual(
826 $this->getItemsFromApiResponse( $resultMinor ),
832 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotMinor ) );
835 public function testShowBotParams() {
836 $user = $this->getLoggedInTestUser();
837 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
838 $this->doBotPageEdit(
844 $this->watchPages( $user, [ $target ] );
846 $resultBot = $this->doListWatchlistRequest( [
847 'wlshow' => WatchedItemQueryService
::FILTER_BOT
849 $resultNotBot = $this->doListWatchlistRequest( [
850 'wlshow' => WatchedItemQueryService
::FILTER_NOT_BOT
853 $this->assertArraySubsetsEqual(
854 $this->getItemsFromApiResponse( $resultBot ),
860 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotBot ) );
863 public function testShowAnonParams() {
864 $user = $this->getLoggedInTestUser();
865 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
866 $this->doAnonPageEdit(
871 $this->watchPages( $user, [ $target ] );
873 $resultAnon = $this->doListWatchlistRequest( [
875 'wlshow' => WatchedItemQueryService
::FILTER_ANON
877 $resultNotAnon = $this->doListWatchlistRequest( [
879 'wlshow' => WatchedItemQueryService
::FILTER_NOT_ANON
882 $this->assertArraySubsetsEqual(
883 $this->getItemsFromApiResponse( $resultAnon ),
889 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotAnon ) );
892 public function testShowUnreadParams() {
893 $user = $this->getLoggedInTestUser();
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',
919 'wlshow' => WatchedItemQueryService
::FILTER_UNREAD
921 $resultNotUnread = $this->doListWatchlistRequest( [
922 'wlprop' => 'notificationtimestamp|title',
923 'wlshow' => WatchedItemQueryService
::FILTER_NOT_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 = static::getTestSysop()->getUser();
952 $this->setupPatrolledSpecificFixtures( $user );
954 $resultPatrolled = $this->doListWatchlistRequest( [
955 'wlprop' => 'patrol',
956 'wlshow' => WatchedItemQueryService
::FILTER_PATROLLED
958 $resultNotPatrolled = $this->doListWatchlistRequest( [
959 'wlprop' => 'patrol',
960 'wlshow' => WatchedItemQueryService
::FILTER_NOT_PATROLLED
968 'unpatrolled' => false,
969 'autopatrolled' => false,
972 $this->getItemsFromApiResponse( $resultPatrolled )
974 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotPatrolled ) );
977 public function testNewAndEditTypeParameters() {
978 $user = $this->getLoggedInTestUser();
979 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
980 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
985 'target' => $subjectTarget,
986 'content' => 'Some Content',
987 'summary' => 'Create the page',
990 'target' => $subjectTarget,
991 'content' => 'Some Other Content',
992 'summary' => 'Change the content',
995 'target' => $talkTarget,
996 'content' => 'Some Talk Page Content',
997 'summary' => 'Create Talk page',
1001 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1003 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1004 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1006 $this->assertEquals(
1010 'ns' => $talkTarget->getNamespace(),
1011 'title' => $this->getPrefixedText( $talkTarget ),
1014 $this->getItemsFromApiResponse( $resultNew )
1016 $this->assertEquals(
1020 'ns' => $subjectTarget->getNamespace(),
1021 'title' => $this->getPrefixedText( $subjectTarget ),
1024 $this->getItemsFromApiResponse( $resultEdit )
1028 public function testLogTypeParameters() {
1029 $user = $this->getLoggedInTestUser();
1030 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1031 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1032 $this->createPageAndDeleteIt( $subjectTarget );
1036 'Some Talk Page Content',
1039 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1041 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1043 $this->assertEquals(
1047 'ns' => $subjectTarget->getNamespace(),
1048 'title' => $this->getPrefixedText( $subjectTarget ),
1051 $this->getItemsFromApiResponse( $result )
1055 private function getExternalRC( LinkTarget
$target ) {
1056 $title = Title
::newFromLinkTarget( $target );
1058 $rc = new RecentChange
;
1059 $rc->mTitle
= $title;
1061 'rc_timestamp' => wfTimestamp( TS_MW
),
1062 'rc_namespace' => $title->getNamespace(),
1063 'rc_title' => $title->getDBkey(),
1064 'rc_type' => RC_EXTERNAL
,
1065 'rc_source' => 'foo',
1067 'rc_cur_id' => $title->getArticleID(),
1069 'rc_user_text' => 'ext>External User',
1071 'rc_comment_text' => '',
1072 'rc_comment_data' => null,
1073 'rc_this_oldid' => $title->getLatestRevID(),
1074 'rc_last_oldid' => $title->getLatestRevID(),
1077 'rc_patrolled' => 0,
1079 'rc_old_len' => $title->getLength(),
1080 'rc_new_len' => $title->getLength(),
1083 'rc_log_type' => null,
1084 'rc_log_action' => '',
1088 'prefixedDBkey' => $title->getPrefixedDBkey(),
1089 'lastTimestamp' => 0,
1090 'oldSize' => $title->getLength(),
1091 'newSize' => $title->getLength(),
1092 'pageStatus' => 'changed'
1098 public function testExternalTypeParameters() {
1099 $user = $this->getLoggedInTestUser();
1100 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1101 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1111 'Some Talk Page Content',
1115 $rc = $this->getExternalRC( $subjectTarget );
1118 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1120 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1122 $this->assertEquals(
1125 'type' => 'external',
1126 'ns' => $subjectTarget->getNamespace(),
1127 'title' => $this->getPrefixedText( $subjectTarget ),
1130 $this->getItemsFromApiResponse( $result )
1134 public function testCategorizeTypeParameter() {
1135 $user = $this->getLoggedInTestUser();
1136 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1137 $categoryTarget = new TitleValue( NS_CATEGORY
, 'ApiQueryWatchlistIntegrationTestCategory' );
1142 'target' => $categoryTarget,
1143 'content' => 'Some Content',
1144 'summary' => 'Create the category',
1147 'target' => $subjectTarget,
1148 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1149 'summary' => 'Create the page and add it to the category',
1153 $title = Title
::newFromLinkTarget( $subjectTarget );
1154 $revision = Revision
::newFromTitle( $title );
1156 $rc = RecentChange
::newForCategorization(
1157 $revision->getTimestamp(),
1158 Title
::newFromLinkTarget( $categoryTarget ),
1160 $revision->getComment(),
1169 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1171 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1173 $this->assertEquals(
1176 'type' => 'categorize',
1177 'ns' => $categoryTarget->getNamespace(),
1178 'title' => $this->getPrefixedText( $categoryTarget ),
1181 $this->getItemsFromApiResponse( $result )
1185 public function testLimitParam() {
1186 $user = $this->getLoggedInTestUser();
1187 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1188 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1189 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1194 'target' => $target1,
1195 'content' => 'Some Content',
1196 'summary' => 'Create the page',
1199 'target' => $target2,
1200 'content' => 'Some Talk Page Content',
1201 'summary' => 'Create Talk page',
1204 'target' => $target3,
1205 'content' => 'Some Other Content',
1206 'summary' => 'Create the page',
1210 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1212 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1213 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1215 $this->assertEquals(
1219 'ns' => $target3->getNamespace(),
1220 'title' => $this->getPrefixedText( $target3 )
1224 'ns' => $target2->getNamespace(),
1225 'title' => $this->getPrefixedText( $target2 )
1229 'ns' => $target1->getNamespace(),
1230 'title' => $this->getPrefixedText( $target1 )
1233 $this->getItemsFromApiResponse( $resultWithoutLimit )
1235 $this->assertEquals(
1239 'ns' => $target3->getNamespace(),
1240 'title' => $this->getPrefixedText( $target3 )
1244 'ns' => $target2->getNamespace(),
1245 'title' => $this->getPrefixedText( $target2 )
1248 $this->getItemsFromApiResponse( $resultWithLimit )
1250 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1251 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1254 public function testAllRevParam() {
1255 $user = $this->getLoggedInTestUser();
1256 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1261 'target' => $target,
1262 'content' => 'Some Content',
1263 'summary' => 'Create the page',
1266 'target' => $target,
1267 'content' => 'Some Other Content',
1268 'summary' => 'Change the content',
1272 $this->watchPages( $user, [ $target ] );
1274 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1275 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1277 $this->assertEquals(
1281 'ns' => $target->getNamespace(),
1282 'title' => $this->getPrefixedText( $target ),
1285 $this->getItemsFromApiResponse( $resultNoAllRev )
1287 $this->assertEquals(
1291 'ns' => $target->getNamespace(),
1292 'title' => $this->getPrefixedText( $target ),
1296 'ns' => $target->getNamespace(),
1297 'title' => $this->getPrefixedText( $target ),
1300 $this->getItemsFromApiResponse( $resultAllRev )
1304 public function testDirParams() {
1305 $user = $this->getLoggedInTestUser();
1306 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1307 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1312 'target' => $subjectTarget,
1313 'content' => 'Some Content',
1314 'summary' => 'Create the page',
1317 'target' => $talkTarget,
1318 'content' => 'Some Talk Page Content',
1319 'summary' => 'Create Talk page',
1323 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1325 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1326 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1328 $this->assertEquals(
1332 'ns' => $talkTarget->getNamespace(),
1333 'title' => $this->getPrefixedText( $talkTarget )
1337 'ns' => $subjectTarget->getNamespace(),
1338 'title' => $this->getPrefixedText( $subjectTarget )
1341 $this->getItemsFromApiResponse( $resultDirOlder )
1343 $this->assertEquals(
1347 'ns' => $subjectTarget->getNamespace(),
1348 'title' => $this->getPrefixedText( $subjectTarget )
1352 'ns' => $talkTarget->getNamespace(),
1353 'title' => $this->getPrefixedText( $talkTarget )
1356 $this->getItemsFromApiResponse( $resultDirNewer )
1360 public function testStartEndParams() {
1361 $user = $this->getLoggedInTestUser();
1362 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1369 $this->watchPages( $user, [ $target ] );
1371 $resultStart = $this->doListWatchlistRequest( [
1372 'wlstart' => '20010115000000',
1374 'wlprop' => 'title',
1376 $resultEnd = $this->doListWatchlistRequest( [
1377 'wlend' => '20010115000000',
1379 'wlprop' => 'title',
1382 $this->assertEquals(
1386 'ns' => $target->getNamespace(),
1387 'title' => $this->getPrefixedText( $target ),
1390 $this->getItemsFromApiResponse( $resultStart )
1392 $this->assertEmpty( $this->getItemsFromApiResponse( $resultEnd ) );
1395 public function testContinueParam() {
1396 $user = $this->getLoggedInTestUser();
1397 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1398 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1399 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1404 'target' => $target1,
1405 'content' => 'Some Content',
1406 'summary' => 'Create the page',
1409 'target' => $target2,
1410 'content' => 'Some Talk Page Content',
1411 'summary' => 'Create Talk page',
1414 'target' => $target3,
1415 'content' => 'Some Other Content',
1416 'summary' => 'Create the page',
1420 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1422 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1423 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1424 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1426 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1428 $continuedResult = $this->doListWatchlistRequest(
1429 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1432 $this->assertEquals(
1436 'ns' => $target3->getNamespace(),
1437 'title' => $this->getPrefixedText( $target3 ),
1441 'ns' => $target2->getNamespace(),
1442 'title' => $this->getPrefixedText( $target2 ),
1445 $this->getItemsFromApiResponse( $firstResult )
1447 $this->assertEquals(
1451 'ns' => $target1->getNamespace(),
1452 'title' => $this->getPrefixedText( $target1 )
1455 $this->getItemsFromApiResponse( $continuedResult )
1459 public function testOwnerAndTokenParams() {
1460 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1462 $this->getLoggedInTestUser(),
1468 $otherUser = $this->getNonLoggedInTestUser();
1469 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1470 $otherUser->saveSettings();
1472 $this->watchPages( $otherUser, [ $target ] );
1474 $reloadedUser = User
::newFromName( $otherUser->getName() );
1475 $this->assertEquals( '1234567890', $reloadedUser->getOption( 'watchlisttoken' ) );
1477 $result = $this->doListWatchlistRequest( [
1478 'wlowner' => $otherUser->getName(),
1479 'wltoken' => '1234567890',
1480 'wlprop' => 'title',
1483 $this->assertEquals(
1487 'ns' => $target->getNamespace(),
1488 'title' => $this->getPrefixedText( $target )
1491 $this->getItemsFromApiResponse( $result )
1495 public function testOwnerAndTokenParams_wrongToken() {
1496 $otherUser = $this->getNonLoggedInTestUser();
1497 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1498 $otherUser->saveSettings();
1500 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1502 $this->doListWatchlistRequest( [
1503 'wlowner' => $otherUser->getName(),
1504 'wltoken' => 'wrong-token',
1508 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1509 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1511 $this->doListWatchlistRequest( [
1512 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1513 'wltoken' => 'some-token',
1517 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1518 $user = $this->getLoggedInTestUser();
1519 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1526 $this->watchPages( $user, [ $target ] );
1528 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1530 $this->assertArrayHasKey( 'query', $result[0] );
1531 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1533 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1534 $pages = array_values( $result[0]['query']['pages'] );
1536 $this->assertArraySubsetsEqual(
1540 'ns' => $target->getNamespace(),
1541 'title' => $this->getPrefixedText( $target ),
1545 [ 'ns', 'title', 'new' ]
1549 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1550 $user = $this->getLoggedInTestUser();
1551 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1556 'target' => $target,
1557 'content' => 'Some Content',
1558 'summary' => 'Create the page',
1561 'target' => $target,
1562 'content' => 'Some Other Content',
1563 'summary' => 'Change the content',
1567 $this->watchPages( $user, [ $target ] );
1569 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1571 $this->assertArrayHasKey( 'query', $result[0] );
1572 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1574 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1575 $pages = array_values( $result[0]['query']['pages'] );
1577 $this->assertCount( 1, $pages );
1578 $this->assertEquals( 0, $pages[0]['ns'] );
1579 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1580 $this->assertArraySubsetsEqual(
1581 $pages[0]['revisions'],
1584 'comment' => 'Create the page',
1585 'user' => $user->getName(),
1589 'comment' => 'Change the content',
1590 'user' => $user->getName(),
1594 [ 'comment', 'user', 'minor' ]