3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Storage\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 getTitleFormatter() {
232 return new MediaWikiTitleCodec(
233 Language
::factory( 'en' ),
234 MediaWikiServices
::getInstance()->getGenderCache()
238 private function getPrefixedText( LinkTarget
$target ) {
239 $formatter = $this->getTitleFormatter();
240 return $formatter->getPrefixedText( $target );
243 private function cleanTestUsersWatchlist() {
244 $user = $this->getLoggedInTestUser();
245 $store = $this->getWatchedItemStore();
246 $items = $store->getWatchedItemsForUser( $user );
247 foreach ( $items as $item ) {
248 $store->removeWatch( $user, $item->getLinkTarget() );
252 public function testListWatchlist_returnsWatchedItemsWithRCInfo() {
253 // Clean up after previous tests that might have added something to the watchlist of
254 // the user with the same user ID as user used here as the test user
255 $this->cleanTestUsersWatchlist();
257 $user = $this->getLoggedInTestUser();
258 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
265 $this->watchPages( $user, [ $target ] );
267 $result = $this->doListWatchlistRequest();
269 $this->assertArrayHasKey( 'query', $result[0] );
270 $this->assertArrayHasKey( 'watchlist', $result[0]['query'] );
272 $this->assertArraySubsetsEqual(
273 $this->getItemsFromApiResponse( $result ),
277 'ns' => $target->getNamespace(),
278 'title' => $this->getPrefixedText( $target ),
284 [ 'type', 'ns', 'title', 'bot', 'new', 'minor' ],
285 [ 'pageid', 'revid', 'old_revid' ]
289 public function testIdsPropParameter() {
290 $user = $this->getLoggedInTestUser();
291 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
298 $this->watchPages( $user, [ $target ] );
300 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'ids', ] );
301 $items = $this->getItemsFromApiResponse( $result );
303 $this->assertCount( 1, $items );
304 $this->assertArrayHasKey( 'pageid', $items[0] );
305 $this->assertArrayHasKey( 'revid', $items[0] );
306 $this->assertArrayHasKey( 'old_revid', $items[0] );
307 $this->assertEquals( 'new', $items[0]['type'] );
310 public function testTitlePropParameter() {
311 $user = $this->getLoggedInTestUser();
312 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
313 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
318 'target' => $subjectTarget,
319 'content' => 'Some Content',
320 'summary' => 'Create the page',
323 'target' => $talkTarget,
324 'content' => 'Some Talk Page Content',
325 'summary' => 'Create Talk page',
329 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
331 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', ] );
337 'ns' => $talkTarget->getNamespace(),
338 'title' => $this->getPrefixedText( $talkTarget ),
342 'ns' => $subjectTarget->getNamespace(),
343 'title' => $this->getPrefixedText( $subjectTarget ),
346 $this->getItemsFromApiResponse( $result )
350 public function testFlagsPropParameter() {
351 $user = $this->getLoggedInTestUser();
352 $normalEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
353 $minorEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageM' );
354 $botEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageB' );
359 'target' => $normalEditTarget,
360 'content' => 'Some Content',
361 'summary' => 'Create the page',
364 'target' => $minorEditTarget,
365 'content' => 'Some Content',
366 'summary' => 'Create the page',
369 'target' => $minorEditTarget,
370 'content' => 'Slightly Better Content',
371 'summary' => 'Change content',
375 'target' => $botEditTarget,
376 'content' => 'Some Content',
377 'summary' => 'Create the page with a bot',
382 $this->watchPages( $user, [ $normalEditTarget, $minorEditTarget, $botEditTarget ] );
384 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'flags', ] );
407 $this->getItemsFromApiResponse( $result )
411 public function testUserPropParameter() {
412 $user = $this->getLoggedInTestUser();
413 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
414 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
421 $this->doAnonPageEdit(
426 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
428 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'user', ] );
435 'user' => User
::newFromId( 0 )->getName(),
439 'user' => $user->getName(),
442 $this->getItemsFromApiResponse( $result )
446 public function testUserIdPropParameter() {
447 $user = $this->getLoggedInTestUser();
448 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
449 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
456 $this->doAnonPageEdit(
461 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
463 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'userid', ] );
475 'user' => $user->getId(),
476 'userid' => $user->getId(),
479 $this->getItemsFromApiResponse( $result )
483 public function testCommentPropParameter() {
484 $user = $this->getLoggedInTestUser();
485 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
490 'Create the <b>page</b>'
492 $this->watchPages( $user, [ $target ] );
494 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'comment', ] );
500 'comment' => 'Create the <b>page</b>',
503 $this->getItemsFromApiResponse( $result )
507 public function testParsedCommentPropParameter() {
508 $user = $this->getLoggedInTestUser();
509 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
514 'Create the <b>page</b>'
516 $this->watchPages( $user, [ $target ] );
518 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'parsedcomment', ] );
524 'parsedcomment' => 'Create the <b>page</b>',
527 $this->getItemsFromApiResponse( $result )
531 public function testTimestampPropParameter() {
532 $user = $this->getLoggedInTestUser();
533 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
540 $this->watchPages( $user, [ $target ] );
542 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'timestamp', ] );
543 $items = $this->getItemsFromApiResponse( $result );
545 $this->assertCount( 1, $items );
546 $this->assertArrayHasKey( 'timestamp', $items[0] );
547 $this->assertInternalType( 'string', $items[0]['timestamp'] );
550 public function testSizesPropParameter() {
551 $user = $this->getLoggedInTestUser();
552 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
559 $this->watchPages( $user, [ $target ] );
561 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'sizes', ] );
571 $this->getItemsFromApiResponse( $result )
575 public function testNotificationTimestampPropParameter() {
576 $otherUser = $this->getNonLoggedInTestUser();
577 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
584 $store = $this->getWatchedItemStore();
585 $store->addWatch( $this->getLoggedInTestUser(), $target );
586 $store->updateNotificationTimestamp(
592 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'notificationtimestamp', ] );
598 'notificationtimestamp' => '2015-12-12T01:01:01Z',
601 $this->getItemsFromApiResponse( $result )
605 private function setupPatrolledSpecificFixtures( User
$user ) {
606 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
608 $this->doPatrolledPageEdit(
612 'Create the page (this gets patrolled)',
616 $this->watchPages( $user, [ $target ] );
619 public function testPatrolPropParameter() {
620 $testUser = static::getTestSysop();
621 $user = $testUser->getUser();
622 $this->setupPatrolledSpecificFixtures( $user );
624 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
631 'unpatrolled' => false,
632 'autopatrolled' => false,
635 $this->getItemsFromApiResponse( $result )
639 private function createPageAndDeleteIt( LinkTarget
$target ) {
641 $this->getLoggedInTestUser(),
644 'Create the page that will be deleted'
646 $this->deletePage( $target, 'Important Reason' );
649 public function testLoginfoPropParameter() {
650 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
651 $this->createPageAndDeleteIt( $target );
653 $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
655 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
657 $this->assertArraySubsetsEqual(
658 $this->getItemsFromApiResponse( $result ),
662 'logtype' => 'delete',
663 'logaction' => 'delete',
667 [ 'type', 'logtype', 'logaction', 'logparams' ],
672 public function testEmptyPropParameter() {
673 $user = $this->getLoggedInTestUser();
674 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
681 $this->watchPages( $user, [ $target ] );
683 $result = $this->doListWatchlistRequest( [ 'wlprop' => '', ] );
691 $this->getItemsFromApiResponse( $result )
695 public function testNamespaceParam() {
696 $user = $this->getLoggedInTestUser();
697 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
698 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
703 'target' => $subjectTarget,
704 'content' => 'Some Content',
705 'summary' => 'Create the page',
708 'target' => $talkTarget,
709 'content' => 'Some Content',
710 'summary' => 'Create the talk page',
714 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
716 $result = $this->doListWatchlistRequest( [ 'wlnamespace' => '0', ] );
718 $this->assertArraySubsetsEqual(
719 $this->getItemsFromApiResponse( $result ),
723 'title' => $this->getPrefixedText( $subjectTarget ),
730 public function testUserParam() {
731 $user = $this->getLoggedInTestUser();
732 $otherUser = $this->getNonLoggedInTestUser();
733 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
734 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
744 'What is this page about?',
745 'Create the talk page'
747 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
749 $result = $this->doListWatchlistRequest( [
750 'wlprop' => 'user|title',
751 'wluser' => $otherUser->getName(),
758 'ns' => $talkTarget->getNamespace(),
759 'title' => $this->getPrefixedText( $talkTarget ),
760 'user' => $otherUser->getName(),
763 $this->getItemsFromApiResponse( $result )
767 public function testExcludeUserParam() {
768 $user = $this->getLoggedInTestUser();
769 $otherUser = $this->getNonLoggedInTestUser();
770 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
771 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
781 'What is this page about?',
782 'Create the talk page'
784 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
786 $result = $this->doListWatchlistRequest( [
787 'wlprop' => 'user|title',
788 'wlexcludeuser' => $otherUser->getName(),
795 'ns' => $subjectTarget->getNamespace(),
796 'title' => $this->getPrefixedText( $subjectTarget ),
797 'user' => $user->getName(),
800 $this->getItemsFromApiResponse( $result )
804 public function testShowMinorParams() {
805 $user = $this->getLoggedInTestUser();
806 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
812 'content' => 'Some Content',
813 'summary' => 'Create the page',
817 'content' => 'Slightly Better Content',
818 'summary' => 'Change content',
823 $this->watchPages( $user, [ $target ] );
825 $resultMinor = $this->doListWatchlistRequest( [
826 'wlshow' => WatchedItemQueryService
::FILTER_MINOR
,
829 $resultNotMinor = $this->doListWatchlistRequest( [
830 'wlshow' => WatchedItemQueryService
::FILTER_NOT_MINOR
, 'wlprop' => 'flags'
833 $this->assertArraySubsetsEqual(
834 $this->getItemsFromApiResponse( $resultMinor ),
840 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotMinor ) );
843 public function testShowBotParams() {
844 $user = $this->getLoggedInTestUser();
845 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
846 $this->doBotPageEdit(
852 $this->watchPages( $user, [ $target ] );
854 $resultBot = $this->doListWatchlistRequest( [
855 'wlshow' => WatchedItemQueryService
::FILTER_BOT
857 $resultNotBot = $this->doListWatchlistRequest( [
858 'wlshow' => WatchedItemQueryService
::FILTER_NOT_BOT
861 $this->assertArraySubsetsEqual(
862 $this->getItemsFromApiResponse( $resultBot ),
868 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotBot ) );
871 public function testShowAnonParams() {
872 $user = $this->getLoggedInTestUser();
873 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
874 $this->doAnonPageEdit(
879 $this->watchPages( $user, [ $target ] );
881 $resultAnon = $this->doListWatchlistRequest( [
883 'wlshow' => WatchedItemQueryService
::FILTER_ANON
885 $resultNotAnon = $this->doListWatchlistRequest( [
887 'wlshow' => WatchedItemQueryService
::FILTER_NOT_ANON
890 $this->assertArraySubsetsEqual(
891 $this->getItemsFromApiResponse( $resultAnon ),
897 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotAnon ) );
900 public function testShowUnreadParams() {
901 $user = $this->getLoggedInTestUser();
902 $otherUser = $this->getNonLoggedInTestUser();
903 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
904 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
915 'Create the talk page'
917 $store = $this->getWatchedItemStore();
918 $store->addWatchBatchForUser( $user, [ $subjectTarget, $talkTarget ] );
919 $store->updateNotificationTimestamp(
925 $resultUnread = $this->doListWatchlistRequest( [
926 'wlprop' => 'notificationtimestamp|title',
927 'wlshow' => WatchedItemQueryService
::FILTER_UNREAD
929 $resultNotUnread = $this->doListWatchlistRequest( [
930 'wlprop' => 'notificationtimestamp|title',
931 'wlshow' => WatchedItemQueryService
::FILTER_NOT_UNREAD
938 'notificationtimestamp' => '2015-12-12T01:01:01Z',
939 'ns' => $talkTarget->getNamespace(),
940 'title' => $this->getPrefixedText( $talkTarget )
943 $this->getItemsFromApiResponse( $resultUnread )
949 'notificationtimestamp' => '',
950 'ns' => $subjectTarget->getNamespace(),
951 'title' => $this->getPrefixedText( $subjectTarget )
954 $this->getItemsFromApiResponse( $resultNotUnread )
958 public function testShowPatrolledParams() {
959 $user = static::getTestSysop()->getUser();
960 $this->setupPatrolledSpecificFixtures( $user );
962 $resultPatrolled = $this->doListWatchlistRequest( [
963 'wlprop' => 'patrol',
964 'wlshow' => WatchedItemQueryService
::FILTER_PATROLLED
966 $resultNotPatrolled = $this->doListWatchlistRequest( [
967 'wlprop' => 'patrol',
968 'wlshow' => WatchedItemQueryService
::FILTER_NOT_PATROLLED
976 'unpatrolled' => false,
977 'autopatrolled' => false,
980 $this->getItemsFromApiResponse( $resultPatrolled )
982 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotPatrolled ) );
985 public function testNewAndEditTypeParameters() {
986 $user = $this->getLoggedInTestUser();
987 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
988 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
993 'target' => $subjectTarget,
994 'content' => 'Some Content',
995 'summary' => 'Create the page',
998 'target' => $subjectTarget,
999 'content' => 'Some Other Content',
1000 'summary' => 'Change the content',
1003 'target' => $talkTarget,
1004 'content' => 'Some Talk Page Content',
1005 'summary' => 'Create Talk page',
1009 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1011 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1012 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1014 $this->assertEquals(
1018 'ns' => $talkTarget->getNamespace(),
1019 'title' => $this->getPrefixedText( $talkTarget ),
1022 $this->getItemsFromApiResponse( $resultNew )
1024 $this->assertEquals(
1028 'ns' => $subjectTarget->getNamespace(),
1029 'title' => $this->getPrefixedText( $subjectTarget ),
1032 $this->getItemsFromApiResponse( $resultEdit )
1036 public function testLogTypeParameters() {
1037 $user = $this->getLoggedInTestUser();
1038 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1039 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1040 $this->createPageAndDeleteIt( $subjectTarget );
1044 'Some Talk Page Content',
1047 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1049 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1051 $this->assertEquals(
1055 'ns' => $subjectTarget->getNamespace(),
1056 'title' => $this->getPrefixedText( $subjectTarget ),
1059 $this->getItemsFromApiResponse( $result )
1063 private function getExternalRC( LinkTarget
$target ) {
1064 $title = Title
::newFromLinkTarget( $target );
1066 $rc = new RecentChange
;
1067 $rc->mTitle
= $title;
1069 'rc_timestamp' => wfTimestamp( TS_MW
),
1070 'rc_namespace' => $title->getNamespace(),
1071 'rc_title' => $title->getDBkey(),
1072 'rc_type' => RC_EXTERNAL
,
1073 'rc_source' => 'foo',
1075 'rc_cur_id' => $title->getArticleID(),
1077 'rc_user_text' => 'ext>External User',
1079 'rc_comment_text' => '',
1080 'rc_comment_data' => null,
1081 'rc_this_oldid' => $title->getLatestRevID(),
1082 'rc_last_oldid' => $title->getLatestRevID(),
1085 'rc_patrolled' => 0,
1087 'rc_old_len' => $title->getLength(),
1088 'rc_new_len' => $title->getLength(),
1091 'rc_log_type' => null,
1092 'rc_log_action' => '',
1096 'prefixedDBkey' => $title->getPrefixedDBkey(),
1097 'lastTimestamp' => 0,
1098 'oldSize' => $title->getLength(),
1099 'newSize' => $title->getLength(),
1100 'pageStatus' => 'changed'
1106 public function testExternalTypeParameters() {
1107 $user = $this->getLoggedInTestUser();
1108 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1109 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1119 'Some Talk Page Content',
1123 $rc = $this->getExternalRC( $subjectTarget );
1126 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1128 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1130 $this->assertEquals(
1133 'type' => 'external',
1134 'ns' => $subjectTarget->getNamespace(),
1135 'title' => $this->getPrefixedText( $subjectTarget ),
1138 $this->getItemsFromApiResponse( $result )
1142 public function testCategorizeTypeParameter() {
1143 $user = $this->getLoggedInTestUser();
1144 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1145 $categoryTarget = new TitleValue( NS_CATEGORY
, 'ApiQueryWatchlistIntegrationTestCategory' );
1150 'target' => $categoryTarget,
1151 'content' => 'Some Content',
1152 'summary' => 'Create the category',
1155 'target' => $subjectTarget,
1156 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1157 'summary' => 'Create the page and add it to the category',
1161 $title = Title
::newFromLinkTarget( $subjectTarget );
1162 $revision = Revision
::newFromTitle( $title );
1164 $rc = RecentChange
::newForCategorization(
1165 $revision->getTimestamp(),
1166 Title
::newFromLinkTarget( $categoryTarget ),
1168 $revision->getComment(),
1177 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1179 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1181 $this->assertEquals(
1184 'type' => 'categorize',
1185 'ns' => $categoryTarget->getNamespace(),
1186 'title' => $this->getPrefixedText( $categoryTarget ),
1189 $this->getItemsFromApiResponse( $result )
1193 public function testLimitParam() {
1194 $user = $this->getLoggedInTestUser();
1195 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1196 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1197 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1202 'target' => $target1,
1203 'content' => 'Some Content',
1204 'summary' => 'Create the page',
1207 'target' => $target2,
1208 'content' => 'Some Talk Page Content',
1209 'summary' => 'Create Talk page',
1212 'target' => $target3,
1213 'content' => 'Some Other Content',
1214 'summary' => 'Create the page',
1218 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1220 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1221 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1223 $this->assertEquals(
1227 'ns' => $target3->getNamespace(),
1228 'title' => $this->getPrefixedText( $target3 )
1232 'ns' => $target2->getNamespace(),
1233 'title' => $this->getPrefixedText( $target2 )
1237 'ns' => $target1->getNamespace(),
1238 'title' => $this->getPrefixedText( $target1 )
1241 $this->getItemsFromApiResponse( $resultWithoutLimit )
1243 $this->assertEquals(
1247 'ns' => $target3->getNamespace(),
1248 'title' => $this->getPrefixedText( $target3 )
1252 'ns' => $target2->getNamespace(),
1253 'title' => $this->getPrefixedText( $target2 )
1256 $this->getItemsFromApiResponse( $resultWithLimit )
1258 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1259 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1262 public function testAllRevParam() {
1263 $user = $this->getLoggedInTestUser();
1264 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1269 'target' => $target,
1270 'content' => 'Some Content',
1271 'summary' => 'Create the page',
1274 'target' => $target,
1275 'content' => 'Some Other Content',
1276 'summary' => 'Change the content',
1280 $this->watchPages( $user, [ $target ] );
1282 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1283 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1285 $this->assertEquals(
1289 'ns' => $target->getNamespace(),
1290 'title' => $this->getPrefixedText( $target ),
1293 $this->getItemsFromApiResponse( $resultNoAllRev )
1295 $this->assertEquals(
1299 'ns' => $target->getNamespace(),
1300 'title' => $this->getPrefixedText( $target ),
1304 'ns' => $target->getNamespace(),
1305 'title' => $this->getPrefixedText( $target ),
1308 $this->getItemsFromApiResponse( $resultAllRev )
1312 public function testDirParams() {
1313 $user = $this->getLoggedInTestUser();
1314 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1315 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1320 'target' => $subjectTarget,
1321 'content' => 'Some Content',
1322 'summary' => 'Create the page',
1325 'target' => $talkTarget,
1326 'content' => 'Some Talk Page Content',
1327 'summary' => 'Create Talk page',
1331 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1333 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1334 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1336 $this->assertEquals(
1340 'ns' => $talkTarget->getNamespace(),
1341 'title' => $this->getPrefixedText( $talkTarget )
1345 'ns' => $subjectTarget->getNamespace(),
1346 'title' => $this->getPrefixedText( $subjectTarget )
1349 $this->getItemsFromApiResponse( $resultDirOlder )
1351 $this->assertEquals(
1355 'ns' => $subjectTarget->getNamespace(),
1356 'title' => $this->getPrefixedText( $subjectTarget )
1360 'ns' => $talkTarget->getNamespace(),
1361 'title' => $this->getPrefixedText( $talkTarget )
1364 $this->getItemsFromApiResponse( $resultDirNewer )
1368 public function testStartEndParams() {
1369 $user = $this->getLoggedInTestUser();
1370 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1377 $this->watchPages( $user, [ $target ] );
1379 $resultStart = $this->doListWatchlistRequest( [
1380 'wlstart' => '20010115000000',
1382 'wlprop' => 'title',
1384 $resultEnd = $this->doListWatchlistRequest( [
1385 'wlend' => '20010115000000',
1387 'wlprop' => 'title',
1390 $this->assertEquals(
1394 'ns' => $target->getNamespace(),
1395 'title' => $this->getPrefixedText( $target ),
1398 $this->getItemsFromApiResponse( $resultStart )
1400 $this->assertEmpty( $this->getItemsFromApiResponse( $resultEnd ) );
1403 public function testContinueParam() {
1404 $user = $this->getLoggedInTestUser();
1405 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1406 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1407 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1412 'target' => $target1,
1413 'content' => 'Some Content',
1414 'summary' => 'Create the page',
1417 'target' => $target2,
1418 'content' => 'Some Talk Page Content',
1419 'summary' => 'Create Talk page',
1422 'target' => $target3,
1423 'content' => 'Some Other Content',
1424 'summary' => 'Create the page',
1428 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1430 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1431 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1432 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1434 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1436 $continuedResult = $this->doListWatchlistRequest(
1437 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1440 $this->assertEquals(
1444 'ns' => $target3->getNamespace(),
1445 'title' => $this->getPrefixedText( $target3 ),
1449 'ns' => $target2->getNamespace(),
1450 'title' => $this->getPrefixedText( $target2 ),
1453 $this->getItemsFromApiResponse( $firstResult )
1455 $this->assertEquals(
1459 'ns' => $target1->getNamespace(),
1460 'title' => $this->getPrefixedText( $target1 )
1463 $this->getItemsFromApiResponse( $continuedResult )
1467 public function testOwnerAndTokenParams() {
1468 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1470 $this->getLoggedInTestUser(),
1476 $otherUser = $this->getNonLoggedInTestUser();
1477 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1478 $otherUser->saveSettings();
1480 $this->watchPages( $otherUser, [ $target ] );
1482 $reloadedUser = User
::newFromName( $otherUser->getName() );
1483 $this->assertEquals( '1234567890', $reloadedUser->getOption( 'watchlisttoken' ) );
1485 $result = $this->doListWatchlistRequest( [
1486 'wlowner' => $otherUser->getName(),
1487 'wltoken' => '1234567890',
1488 'wlprop' => 'title',
1491 $this->assertEquals(
1495 'ns' => $target->getNamespace(),
1496 'title' => $this->getPrefixedText( $target )
1499 $this->getItemsFromApiResponse( $result )
1503 public function testOwnerAndTokenParams_wrongToken() {
1504 $otherUser = $this->getNonLoggedInTestUser();
1505 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1506 $otherUser->saveSettings();
1508 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1510 $this->doListWatchlistRequest( [
1511 'wlowner' => $otherUser->getName(),
1512 'wltoken' => 'wrong-token',
1516 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1517 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1519 $this->doListWatchlistRequest( [
1520 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1521 'wltoken' => 'some-token',
1525 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1526 $user = $this->getLoggedInTestUser();
1527 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1534 $this->watchPages( $user, [ $target ] );
1536 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1538 $this->assertArrayHasKey( 'query', $result[0] );
1539 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1541 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1542 $pages = array_values( $result[0]['query']['pages'] );
1544 $this->assertArraySubsetsEqual(
1548 'ns' => $target->getNamespace(),
1549 'title' => $this->getPrefixedText( $target ),
1553 [ 'ns', 'title', 'new' ]
1557 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1558 $user = $this->getLoggedInTestUser();
1559 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1564 'target' => $target,
1565 'content' => 'Some Content',
1566 'summary' => 'Create the page',
1569 'target' => $target,
1570 'content' => 'Some Other Content',
1571 'summary' => 'Change the content',
1575 $this->watchPages( $user, [ $target ] );
1577 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1579 $this->assertArrayHasKey( 'query', $result[0] );
1580 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1582 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1583 $pages = array_values( $result[0]['query']['pages'] );
1585 $this->assertCount( 1, $pages );
1586 $this->assertEquals( 0, $pages[0]['ns'] );
1587 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1588 $this->assertArraySubsetsEqual(
1589 $pages[0]['revisions'],
1592 'comment' => 'Create the page',
1593 'user' => $user->getName(),
1597 'comment' => 'Change the content',
1598 'user' => $user->getName(),
1602 [ 'comment', 'user', 'minor' ]