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'] = $this->getMutableTestUser();
25 self
::$users['ApiQueryWatchlistIntegrationTestUser2'] = $this->getMutableTestUser();
26 $this->doLogin( 'ApiQueryWatchlistIntegrationTestUser' );
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 $page = WikiPage
::factory( $title );
94 $status = $page->doEditContent(
95 ContentHandler
::makeContent( $content, $title ),
101 /** @var Revision $rev */
102 $rev = $status->value
['revision'];
103 $rc = $rev->getRecentChange();
104 $rc->doMarkPatrolled( $patrollingUser, false, [] );
107 private function deletePage( LinkTarget
$target, $reason ) {
108 $title = Title
::newFromLinkTarget( $target );
109 $page = WikiPage
::factory( $title );
110 $page->doDeleteArticleReal( $reason );
114 * Performs a batch of page edits as a specified user
116 * @param array $editData associative array, keys:
117 * - target => LinkTarget page to edit
118 * - content => string new content
119 * - summary => string edit summary
120 * - minorEdit => bool mark as minor edit if true (defaults to false)
121 * - botEdit => bool mark as bot edit if true (defaults to false)
123 private function doPageEdits( User
$user, array $editData ) {
124 foreach ( $editData as $singleEditData ) {
125 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
126 $this->doMinorPageEdit(
128 $singleEditData['target'],
129 $singleEditData['content'],
130 $singleEditData['summary']
134 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
135 $this->doBotPageEdit(
137 $singleEditData['target'],
138 $singleEditData['content'],
139 $singleEditData['summary']
145 $singleEditData['target'],
146 $singleEditData['content'],
147 $singleEditData['summary']
152 private function getWatchedItemStore() {
153 return MediaWikiServices
::getInstance()->getWatchedItemStore();
158 * @param LinkTarget[] $targets
160 private function watchPages( User
$user, array $targets ) {
161 $store = $this->getWatchedItemStore();
162 $store->addWatchBatchForUser( $user, $targets );
165 private function doListWatchlistRequest( array $params = [], $user = null ) {
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' ],
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,
634 $this->getItemsFromApiResponse( $result )
638 private function createPageAndDeleteIt( LinkTarget
$target ) {
640 $this->getLoggedInTestUser(),
643 'Create the page that will be deleted'
645 $this->deletePage( $target, 'Important Reason' );
648 public function testLoginfoPropParameter() {
649 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
650 $this->createPageAndDeleteIt( $target );
652 $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
654 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
656 $this->assertArraySubsetsEqual(
657 $this->getItemsFromApiResponse( $result ),
661 'logtype' => 'delete',
662 'logaction' => 'delete',
666 [ 'type', 'logtype', 'logaction', 'logparams' ],
671 public function testEmptyPropParameter() {
672 $user = $this->getLoggedInTestUser();
673 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
680 $this->watchPages( $user, [ $target ] );
682 $result = $this->doListWatchlistRequest( [ 'wlprop' => '', ] );
690 $this->getItemsFromApiResponse( $result )
694 public function testNamespaceParam() {
695 $user = $this->getLoggedInTestUser();
696 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
697 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
702 'target' => $subjectTarget,
703 'content' => 'Some Content',
704 'summary' => 'Create the page',
707 'target' => $talkTarget,
708 'content' => 'Some Content',
709 'summary' => 'Create the talk page',
713 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
715 $result = $this->doListWatchlistRequest( [ 'wlnamespace' => '0', ] );
717 $this->assertArraySubsetsEqual(
718 $this->getItemsFromApiResponse( $result ),
722 'title' => $this->getPrefixedText( $subjectTarget ),
729 public function testUserParam() {
730 $user = $this->getLoggedInTestUser();
731 $otherUser = $this->getNonLoggedInTestUser();
732 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
733 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
743 'What is this page about?',
744 'Create the talk page'
746 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
748 $result = $this->doListWatchlistRequest( [
749 'wlprop' => 'user|title',
750 'wluser' => $otherUser->getName(),
757 'ns' => $talkTarget->getNamespace(),
758 'title' => $this->getPrefixedText( $talkTarget ),
759 'user' => $otherUser->getName(),
762 $this->getItemsFromApiResponse( $result )
766 public function testExcludeUserParam() {
767 $user = $this->getLoggedInTestUser();
768 $otherUser = $this->getNonLoggedInTestUser();
769 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
770 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
780 'What is this page about?',
781 'Create the talk page'
783 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
785 $result = $this->doListWatchlistRequest( [
786 'wlprop' => 'user|title',
787 'wlexcludeuser' => $otherUser->getName(),
794 'ns' => $subjectTarget->getNamespace(),
795 'title' => $this->getPrefixedText( $subjectTarget ),
796 'user' => $user->getName(),
799 $this->getItemsFromApiResponse( $result )
803 public function testShowMinorParams() {
804 $user = $this->getLoggedInTestUser();
805 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
811 'content' => 'Some Content',
812 'summary' => 'Create the page',
816 'content' => 'Slightly Better Content',
817 'summary' => 'Change content',
822 $this->watchPages( $user, [ $target ] );
824 $resultMinor = $this->doListWatchlistRequest( [
825 'wlshow' => WatchedItemQueryService
::FILTER_MINOR
,
828 $resultNotMinor = $this->doListWatchlistRequest( [
829 'wlshow' => WatchedItemQueryService
::FILTER_NOT_MINOR
, 'wlprop' => 'flags'
832 $this->assertArraySubsetsEqual(
833 $this->getItemsFromApiResponse( $resultMinor ),
839 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotMinor ) );
842 public function testShowBotParams() {
843 $user = $this->getLoggedInTestUser();
844 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
845 $this->doBotPageEdit(
851 $this->watchPages( $user, [ $target ] );
853 $resultBot = $this->doListWatchlistRequest( [
854 'wlshow' => WatchedItemQueryService
::FILTER_BOT
856 $resultNotBot = $this->doListWatchlistRequest( [
857 'wlshow' => WatchedItemQueryService
::FILTER_NOT_BOT
860 $this->assertArraySubsetsEqual(
861 $this->getItemsFromApiResponse( $resultBot ),
867 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotBot ) );
870 public function testShowAnonParams() {
871 $user = $this->getLoggedInTestUser();
872 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
873 $this->doAnonPageEdit(
878 $this->watchPages( $user, [ $target ] );
880 $resultAnon = $this->doListWatchlistRequest( [
882 'wlshow' => WatchedItemQueryService
::FILTER_ANON
884 $resultNotAnon = $this->doListWatchlistRequest( [
886 'wlshow' => WatchedItemQueryService
::FILTER_NOT_ANON
889 $this->assertArraySubsetsEqual(
890 $this->getItemsFromApiResponse( $resultAnon ),
896 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotAnon ) );
899 public function testShowUnreadParams() {
900 $user = $this->getLoggedInTestUser();
901 $otherUser = $this->getNonLoggedInTestUser();
902 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
903 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
914 'Create the talk page'
916 $store = $this->getWatchedItemStore();
917 $store->addWatchBatchForUser( $user, [ $subjectTarget, $talkTarget ] );
918 $store->updateNotificationTimestamp(
924 $resultUnread = $this->doListWatchlistRequest( [
925 'wlprop' => 'notificationtimestamp|title',
926 'wlshow' => WatchedItemQueryService
::FILTER_UNREAD
928 $resultNotUnread = $this->doListWatchlistRequest( [
929 'wlprop' => 'notificationtimestamp|title',
930 'wlshow' => WatchedItemQueryService
::FILTER_NOT_UNREAD
937 'notificationtimestamp' => '2015-12-12T01:01:01Z',
938 'ns' => $talkTarget->getNamespace(),
939 'title' => $this->getPrefixedText( $talkTarget )
942 $this->getItemsFromApiResponse( $resultUnread )
948 'notificationtimestamp' => '',
949 'ns' => $subjectTarget->getNamespace(),
950 'title' => $this->getPrefixedText( $subjectTarget )
953 $this->getItemsFromApiResponse( $resultNotUnread )
957 public function testShowPatrolledParams() {
958 $user = static::getTestSysop()->getUser();
959 $this->setupPatrolledSpecificFixtures( $user );
961 $resultPatrolled = $this->doListWatchlistRequest( [
962 'wlprop' => 'patrol',
963 'wlshow' => WatchedItemQueryService
::FILTER_PATROLLED
965 $resultNotPatrolled = $this->doListWatchlistRequest( [
966 'wlprop' => 'patrol',
967 'wlshow' => WatchedItemQueryService
::FILTER_NOT_PATROLLED
975 'unpatrolled' => false,
978 $this->getItemsFromApiResponse( $resultPatrolled )
980 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotPatrolled ) );
983 public function testNewAndEditTypeParameters() {
984 $user = $this->getLoggedInTestUser();
985 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
986 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
991 'target' => $subjectTarget,
992 'content' => 'Some Content',
993 'summary' => 'Create the page',
996 'target' => $subjectTarget,
997 'content' => 'Some Other Content',
998 'summary' => 'Change the content',
1001 'target' => $talkTarget,
1002 'content' => 'Some Talk Page Content',
1003 'summary' => 'Create Talk page',
1007 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1009 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1010 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1012 $this->assertEquals(
1016 'ns' => $talkTarget->getNamespace(),
1017 'title' => $this->getPrefixedText( $talkTarget ),
1020 $this->getItemsFromApiResponse( $resultNew )
1022 $this->assertEquals(
1026 'ns' => $subjectTarget->getNamespace(),
1027 'title' => $this->getPrefixedText( $subjectTarget ),
1030 $this->getItemsFromApiResponse( $resultEdit )
1034 public function testLogTypeParameters() {
1035 $user = $this->getLoggedInTestUser();
1036 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1037 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1038 $this->createPageAndDeleteIt( $subjectTarget );
1042 'Some Talk Page Content',
1045 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1047 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1049 $this->assertEquals(
1053 'ns' => $subjectTarget->getNamespace(),
1054 'title' => $this->getPrefixedText( $subjectTarget ),
1057 $this->getItemsFromApiResponse( $result )
1061 private function getExternalRC( LinkTarget
$target ) {
1062 $title = Title
::newFromLinkTarget( $target );
1064 $rc = new RecentChange
;
1065 $rc->mTitle
= $title;
1067 'rc_timestamp' => wfTimestamp( TS_MW
),
1068 'rc_namespace' => $title->getNamespace(),
1069 'rc_title' => $title->getDBkey(),
1070 'rc_type' => RC_EXTERNAL
,
1071 'rc_source' => 'foo',
1073 'rc_cur_id' => $title->getArticleID(),
1075 'rc_user_text' => 'External User',
1077 'rc_this_oldid' => $title->getLatestRevID(),
1078 'rc_last_oldid' => $title->getLatestRevID(),
1081 'rc_patrolled' => 0,
1083 'rc_old_len' => $title->getLength(),
1084 'rc_new_len' => $title->getLength(),
1087 'rc_log_type' => null,
1088 'rc_log_action' => '',
1092 'prefixedDBkey' => $title->getPrefixedDBkey(),
1093 'lastTimestamp' => 0,
1094 'oldSize' => $title->getLength(),
1095 'newSize' => $title->getLength(),
1096 'pageStatus' => 'changed'
1102 public function testExternalTypeParameters() {
1103 $user = $this->getLoggedInTestUser();
1104 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1105 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1115 'Some Talk Page Content',
1119 $rc = $this->getExternalRC( $subjectTarget );
1122 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1124 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1126 $this->assertEquals(
1129 'type' => 'external',
1130 'ns' => $subjectTarget->getNamespace(),
1131 'title' => $this->getPrefixedText( $subjectTarget ),
1134 $this->getItemsFromApiResponse( $result )
1138 public function testCategorizeTypeParameter() {
1139 $user = $this->getLoggedInTestUser();
1140 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1141 $categoryTarget = new TitleValue( NS_CATEGORY
, 'ApiQueryWatchlistIntegrationTestCategory' );
1146 'target' => $categoryTarget,
1147 'content' => 'Some Content',
1148 'summary' => 'Create the category',
1151 'target' => $subjectTarget,
1152 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1153 'summary' => 'Create the page and add it to the category',
1157 $title = Title
::newFromLinkTarget( $subjectTarget );
1158 $revision = Revision
::newFromTitle( $title );
1160 $rc = RecentChange
::newForCategorization(
1161 $revision->getTimestamp(),
1162 Title
::newFromLinkTarget( $categoryTarget ),
1164 $revision->getComment(),
1173 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1175 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1177 $this->assertEquals(
1180 'type' => 'categorize',
1181 'ns' => $categoryTarget->getNamespace(),
1182 'title' => $this->getPrefixedText( $categoryTarget ),
1185 $this->getItemsFromApiResponse( $result )
1189 public function testLimitParam() {
1190 $user = $this->getLoggedInTestUser();
1191 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1192 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1193 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1198 'target' => $target1,
1199 'content' => 'Some Content',
1200 'summary' => 'Create the page',
1203 'target' => $target2,
1204 'content' => 'Some Talk Page Content',
1205 'summary' => 'Create Talk page',
1208 'target' => $target3,
1209 'content' => 'Some Other Content',
1210 'summary' => 'Create the page',
1214 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1216 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1217 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1219 $this->assertEquals(
1223 'ns' => $target3->getNamespace(),
1224 'title' => $this->getPrefixedText( $target3 )
1228 'ns' => $target2->getNamespace(),
1229 'title' => $this->getPrefixedText( $target2 )
1233 'ns' => $target1->getNamespace(),
1234 'title' => $this->getPrefixedText( $target1 )
1237 $this->getItemsFromApiResponse( $resultWithoutLimit )
1239 $this->assertEquals(
1243 'ns' => $target3->getNamespace(),
1244 'title' => $this->getPrefixedText( $target3 )
1248 'ns' => $target2->getNamespace(),
1249 'title' => $this->getPrefixedText( $target2 )
1252 $this->getItemsFromApiResponse( $resultWithLimit )
1254 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1255 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1258 public function testAllRevParam() {
1259 $user = $this->getLoggedInTestUser();
1260 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1265 'target' => $target,
1266 'content' => 'Some Content',
1267 'summary' => 'Create the page',
1270 'target' => $target,
1271 'content' => 'Some Other Content',
1272 'summary' => 'Change the content',
1276 $this->watchPages( $user, [ $target ] );
1278 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1279 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1281 $this->assertEquals(
1285 'ns' => $target->getNamespace(),
1286 'title' => $this->getPrefixedText( $target ),
1289 $this->getItemsFromApiResponse( $resultNoAllRev )
1291 $this->assertEquals(
1295 'ns' => $target->getNamespace(),
1296 'title' => $this->getPrefixedText( $target ),
1300 'ns' => $target->getNamespace(),
1301 'title' => $this->getPrefixedText( $target ),
1304 $this->getItemsFromApiResponse( $resultAllRev )
1308 public function testDirParams() {
1309 $user = $this->getLoggedInTestUser();
1310 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1311 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1316 'target' => $subjectTarget,
1317 'content' => 'Some Content',
1318 'summary' => 'Create the page',
1321 'target' => $talkTarget,
1322 'content' => 'Some Talk Page Content',
1323 'summary' => 'Create Talk page',
1327 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1329 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1330 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1332 $this->assertEquals(
1336 'ns' => $talkTarget->getNamespace(),
1337 'title' => $this->getPrefixedText( $talkTarget )
1341 'ns' => $subjectTarget->getNamespace(),
1342 'title' => $this->getPrefixedText( $subjectTarget )
1345 $this->getItemsFromApiResponse( $resultDirOlder )
1347 $this->assertEquals(
1351 'ns' => $subjectTarget->getNamespace(),
1352 'title' => $this->getPrefixedText( $subjectTarget )
1356 'ns' => $talkTarget->getNamespace(),
1357 'title' => $this->getPrefixedText( $talkTarget )
1360 $this->getItemsFromApiResponse( $resultDirNewer )
1364 public function testStartEndParams() {
1365 $user = $this->getLoggedInTestUser();
1366 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1373 $this->watchPages( $user, [ $target ] );
1375 $resultStart = $this->doListWatchlistRequest( [
1376 'wlstart' => '20010115000000',
1378 'wlprop' => 'title',
1380 $resultEnd = $this->doListWatchlistRequest( [
1381 'wlend' => '20010115000000',
1383 'wlprop' => 'title',
1386 $this->assertEquals(
1390 'ns' => $target->getNamespace(),
1391 'title' => $this->getPrefixedText( $target ),
1394 $this->getItemsFromApiResponse( $resultStart )
1396 $this->assertEmpty( $this->getItemsFromApiResponse( $resultEnd ) );
1399 public function testContinueParam() {
1400 $user = $this->getLoggedInTestUser();
1401 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1402 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1403 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1408 'target' => $target1,
1409 'content' => 'Some Content',
1410 'summary' => 'Create the page',
1413 'target' => $target2,
1414 'content' => 'Some Talk Page Content',
1415 'summary' => 'Create Talk page',
1418 'target' => $target3,
1419 'content' => 'Some Other Content',
1420 'summary' => 'Create the page',
1424 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1426 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1427 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1428 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1430 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1432 $continuedResult = $this->doListWatchlistRequest(
1433 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1436 $this->assertEquals(
1440 'ns' => $target3->getNamespace(),
1441 'title' => $this->getPrefixedText( $target3 ),
1445 'ns' => $target2->getNamespace(),
1446 'title' => $this->getPrefixedText( $target2 ),
1449 $this->getItemsFromApiResponse( $firstResult )
1451 $this->assertEquals(
1455 'ns' => $target1->getNamespace(),
1456 'title' => $this->getPrefixedText( $target1 )
1459 $this->getItemsFromApiResponse( $continuedResult )
1463 public function testOwnerAndTokenParams() {
1464 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1466 $this->getLoggedInTestUser(),
1472 $otherUser = $this->getNonLoggedInTestUser();
1473 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1474 $otherUser->saveSettings();
1476 $this->watchPages( $otherUser, [ $target ] );
1478 $result = $this->doListWatchlistRequest( [
1479 'wlowner' => $otherUser->getName(),
1480 'wltoken' => '1234567890',
1481 'wlprop' => 'title',
1484 $this->assertEquals(
1488 'ns' => $target->getNamespace(),
1489 'title' => $this->getPrefixedText( $target )
1492 $this->getItemsFromApiResponse( $result )
1496 public function testOwnerAndTokenParams_wrongToken() {
1497 $otherUser = $this->getNonLoggedInTestUser();
1498 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1499 $otherUser->saveSettings();
1501 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1503 $this->doListWatchlistRequest( [
1504 'wlowner' => $otherUser->getName(),
1505 'wltoken' => 'wrong-token',
1509 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1510 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1512 $this->doListWatchlistRequest( [
1513 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1514 'wltoken' => 'some-token',
1518 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1519 $user = $this->getLoggedInTestUser();
1520 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1527 $this->watchPages( $user, [ $target ] );
1529 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1531 $this->assertArrayHasKey( 'query', $result[0] );
1532 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1534 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1535 $pages = array_values( $result[0]['query']['pages'] );
1537 $this->assertArraySubsetsEqual(
1541 'ns' => $target->getNamespace(),
1542 'title' => $this->getPrefixedText( $target ),
1546 [ 'ns', 'title', 'new' ]
1550 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1551 $user = $this->getLoggedInTestUser();
1552 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1557 'target' => $target,
1558 'content' => 'Some Content',
1559 'summary' => 'Create the page',
1562 'target' => $target,
1563 'content' => 'Some Other Content',
1564 'summary' => 'Change the content',
1568 $this->watchPages( $user, [ $target ] );
1570 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1572 $this->assertArrayHasKey( 'query', $result[0] );
1573 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1575 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1576 $pages = array_values( $result[0]['query']['pages'] );
1578 $this->assertCount( 1, $pages );
1579 $this->assertEquals( 0, $pages[0]['ns'] );
1580 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1581 $this->assertArraySubsetsEqual(
1582 $pages[0]['revisions'],
1585 'comment' => 'Create the page',
1586 'user' => $user->getName(),
1590 'comment' => 'Change the content',
1591 'user' => $user->getName(),
1595 [ 'comment', 'user', 'minor' ]