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();
28 private function getLoggedInTestUser() {
29 return self
::$users['ApiQueryWatchlistIntegrationTestUser']->getUser();
32 private function getNonLoggedInTestUser() {
33 return self
::$users['ApiQueryWatchlistIntegrationTestUser2']->getUser();
36 private function doPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
37 $title = Title
::newFromLinkTarget( $target );
38 $page = WikiPage
::factory( $title );
40 ContentHandler
::makeContent( $content, $title ),
48 private function doMinorPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
49 $title = Title
::newFromLinkTarget( $target );
50 $page = WikiPage
::factory( $title );
52 ContentHandler
::makeContent( $content, $title ),
60 private function doBotPageEdit( User
$user, LinkTarget
$target, $content, $summary ) {
61 $title = Title
::newFromLinkTarget( $target );
62 $page = WikiPage
::factory( $title );
64 ContentHandler
::makeContent( $content, $title ),
72 private function doAnonPageEdit( LinkTarget
$target, $content, $summary ) {
73 $title = Title
::newFromLinkTarget( $target );
74 $page = WikiPage
::factory( $title );
76 ContentHandler
::makeContent( $content, $title ),
84 private function doPatrolledPageEdit(
91 $title = Title
::newFromLinkTarget( $target );
92 $summary = CommentStoreComment
::newUnsavedComment( trim( $summary ) );
93 $page = WikiPage
::factory( $title );
95 $updater = $page->newPageUpdater( $user );
96 $updater->setContent( 'main', ContentHandler
::makeContent( $content, $title ) );
97 $rev = $updater->saveRevision( $summary );
99 $rc = MediaWikiServices
::getInstance()->getRevisionStore()->getRecentChange( $rev );
100 $rc->doMarkPatrolled( $patrollingUser, false, [] );
103 private function deletePage( LinkTarget
$target, $reason ) {
104 $title = Title
::newFromLinkTarget( $target );
105 $page = WikiPage
::factory( $title );
106 $page->doDeleteArticleReal( $reason );
110 * Performs a batch of page edits as a specified user
112 * @param array $editData associative array, keys:
113 * - target => LinkTarget page to edit
114 * - content => string new content
115 * - summary => string edit summary
116 * - minorEdit => bool mark as minor edit if true (defaults to false)
117 * - botEdit => bool mark as bot edit if true (defaults to false)
119 private function doPageEdits( User
$user, array $editData ) {
120 foreach ( $editData as $singleEditData ) {
121 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
122 $this->doMinorPageEdit(
124 $singleEditData['target'],
125 $singleEditData['content'],
126 $singleEditData['summary']
130 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
131 $this->doBotPageEdit(
133 $singleEditData['target'],
134 $singleEditData['content'],
135 $singleEditData['summary']
141 $singleEditData['target'],
142 $singleEditData['content'],
143 $singleEditData['summary']
148 private function getWatchedItemStore() {
149 return MediaWikiServices
::getInstance()->getWatchedItemStore();
154 * @param LinkTarget[] $targets
156 private function watchPages( User
$user, array $targets ) {
157 $store = $this->getWatchedItemStore();
158 $store->addWatchBatchForUser( $user, $targets );
161 private function doListWatchlistRequest( array $params = [], $user = null ) {
162 if ( $user === null ) {
163 $user = $this->getLoggedInTestUser();
165 return $this->doApiRequest(
167 [ 'action' => 'query', 'list' => 'watchlist' ],
169 ), null, false, $user
173 private function doGeneratorWatchlistRequest( array $params = [] ) {
174 return $this->doApiRequest(
176 [ 'action' => 'query', 'generator' => 'watchlist' ],
178 ), null, false, $this->getLoggedInTestUser()
182 private function getItemsFromApiResponse( array $response ) {
183 return $response[0]['query']['watchlist'];
187 * Convenience method to assert that actual items array fetched from API is equal to the expected
188 * array, Unlike assertEquals this only checks if values of specified keys are equal in both
189 * arrays. This could be used e.g. not to compare IDs that could change between test run
190 * but only stable keys.
191 * Optionally this also checks that specified keys are present in the actual item without
192 * performing any checks on the related values.
194 * @param array $actualItems array of actual items (associative arrays)
195 * @param array $expectedItems array of expected items (associative arrays),
196 * those items have less keys than actual items
197 * @param array $keysUsedInValueComparison list of keys of the actual item that will be used
198 * in the comparison of values
199 * @param array $requiredKeys optional, list of keys that must be present in the
200 * actual items. Values of those keys are not checked.
202 private function assertArraySubsetsEqual(
204 array $expectedItems,
205 array $keysUsedInValueComparison,
206 array $requiredKeys = []
208 $this->assertCount( count( $expectedItems ), $actualItems );
210 // not checking values of all keys of the actual item, so removing unwanted keys from comparison
211 $actualItemsOnlyComparedValues = array_map(
212 function ( array $item ) use ( $keysUsedInValueComparison ) {
213 return array_intersect_key( $item, array_flip( $keysUsedInValueComparison ) );
220 $actualItemsOnlyComparedValues
223 // Check that each item in $actualItems contains all of keys specified in $requiredKeys
224 $actualItemsKeysOnly = array_map( 'array_keys', $actualItems );
225 foreach ( $actualItemsKeysOnly as $keysOfTheItem ) {
226 $this->assertEmpty( array_diff( $requiredKeys, $keysOfTheItem ) );
230 private function getTitleFormatter() {
231 return new MediaWikiTitleCodec(
232 Language
::factory( 'en' ),
233 MediaWikiServices
::getInstance()->getGenderCache()
237 private function getPrefixedText( LinkTarget
$target ) {
238 $formatter = $this->getTitleFormatter();
239 return $formatter->getPrefixedText( $target );
242 private function cleanTestUsersWatchlist() {
243 $user = $this->getLoggedInTestUser();
244 $store = $this->getWatchedItemStore();
245 $items = $store->getWatchedItemsForUser( $user );
246 foreach ( $items as $item ) {
247 $store->removeWatch( $user, $item->getLinkTarget() );
251 public function testListWatchlist_returnsWatchedItemsWithRCInfo() {
252 // Clean up after previous tests that might have added something to the watchlist of
253 // the user with the same user ID as user used here as the test user
254 $this->cleanTestUsersWatchlist();
256 $user = $this->getLoggedInTestUser();
257 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
264 $this->watchPages( $user, [ $target ] );
266 $result = $this->doListWatchlistRequest();
268 $this->assertArrayHasKey( 'query', $result[0] );
269 $this->assertArrayHasKey( 'watchlist', $result[0]['query'] );
271 $this->assertArraySubsetsEqual(
272 $this->getItemsFromApiResponse( $result ),
276 'ns' => $target->getNamespace(),
277 'title' => $this->getPrefixedText( $target ),
283 [ 'type', 'ns', 'title', 'bot', 'new', 'minor' ],
284 [ 'pageid', 'revid', 'old_revid' ]
288 public function testIdsPropParameter() {
289 $user = $this->getLoggedInTestUser();
290 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
297 $this->watchPages( $user, [ $target ] );
299 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'ids', ] );
300 $items = $this->getItemsFromApiResponse( $result );
302 $this->assertCount( 1, $items );
303 $this->assertArrayHasKey( 'pageid', $items[0] );
304 $this->assertArrayHasKey( 'revid', $items[0] );
305 $this->assertArrayHasKey( 'old_revid', $items[0] );
306 $this->assertEquals( 'new', $items[0]['type'] );
309 public function testTitlePropParameter() {
310 $user = $this->getLoggedInTestUser();
311 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
312 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
317 'target' => $subjectTarget,
318 'content' => 'Some Content',
319 'summary' => 'Create the page',
322 'target' => $talkTarget,
323 'content' => 'Some Talk Page Content',
324 'summary' => 'Create Talk page',
328 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
330 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', ] );
336 'ns' => $talkTarget->getNamespace(),
337 'title' => $this->getPrefixedText( $talkTarget ),
341 'ns' => $subjectTarget->getNamespace(),
342 'title' => $this->getPrefixedText( $subjectTarget ),
345 $this->getItemsFromApiResponse( $result )
349 public function testFlagsPropParameter() {
350 $user = $this->getLoggedInTestUser();
351 $normalEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
352 $minorEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageM' );
353 $botEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageB' );
358 'target' => $normalEditTarget,
359 'content' => 'Some Content',
360 'summary' => 'Create the page',
363 'target' => $minorEditTarget,
364 'content' => 'Some Content',
365 'summary' => 'Create the page',
368 'target' => $minorEditTarget,
369 'content' => 'Slightly Better Content',
370 'summary' => 'Change content',
374 'target' => $botEditTarget,
375 'content' => 'Some Content',
376 'summary' => 'Create the page with a bot',
381 $this->watchPages( $user, [ $normalEditTarget, $minorEditTarget, $botEditTarget ] );
383 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'flags', ] );
406 $this->getItemsFromApiResponse( $result )
410 public function testUserPropParameter() {
411 $user = $this->getLoggedInTestUser();
412 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
413 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
420 $this->doAnonPageEdit(
425 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
427 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'user', ] );
434 'user' => User
::newFromId( 0 )->getName(),
438 'user' => $user->getName(),
441 $this->getItemsFromApiResponse( $result )
445 public function testUserIdPropParameter() {
446 $user = $this->getLoggedInTestUser();
447 $userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
448 $anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
455 $this->doAnonPageEdit(
460 $this->watchPages( $user, [ $userEditTarget, $anonEditTarget ] );
462 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'userid', ] );
474 'user' => $user->getId(),
475 'userid' => $user->getId(),
478 $this->getItemsFromApiResponse( $result )
482 public function testCommentPropParameter() {
483 $user = $this->getLoggedInTestUser();
484 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
489 'Create the <b>page</b>'
491 $this->watchPages( $user, [ $target ] );
493 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'comment', ] );
499 'comment' => 'Create the <b>page</b>',
502 $this->getItemsFromApiResponse( $result )
506 public function testParsedCommentPropParameter() {
507 $user = $this->getLoggedInTestUser();
508 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
513 'Create the <b>page</b>'
515 $this->watchPages( $user, [ $target ] );
517 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'parsedcomment', ] );
523 'parsedcomment' => 'Create the <b>page</b>',
526 $this->getItemsFromApiResponse( $result )
530 public function testTimestampPropParameter() {
531 $user = $this->getLoggedInTestUser();
532 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
539 $this->watchPages( $user, [ $target ] );
541 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'timestamp', ] );
542 $items = $this->getItemsFromApiResponse( $result );
544 $this->assertCount( 1, $items );
545 $this->assertArrayHasKey( 'timestamp', $items[0] );
546 $this->assertInternalType( 'string', $items[0]['timestamp'] );
549 public function testSizesPropParameter() {
550 $user = $this->getLoggedInTestUser();
551 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
558 $this->watchPages( $user, [ $target ] );
560 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'sizes', ] );
570 $this->getItemsFromApiResponse( $result )
574 public function testNotificationTimestampPropParameter() {
575 $otherUser = $this->getNonLoggedInTestUser();
576 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
583 $store = $this->getWatchedItemStore();
584 $store->addWatch( $this->getLoggedInTestUser(), $target );
585 $store->updateNotificationTimestamp(
591 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'notificationtimestamp', ] );
597 'notificationtimestamp' => '2015-12-12T01:01:01Z',
600 $this->getItemsFromApiResponse( $result )
604 private function setupPatrolledSpecificFixtures( User
$user ) {
605 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
607 $this->doPatrolledPageEdit(
611 'Create the page (this gets patrolled)',
615 $this->watchPages( $user, [ $target ] );
618 public function testPatrolPropParameter() {
619 $testUser = static::getTestSysop();
620 $user = $testUser->getUser();
621 $this->setupPatrolledSpecificFixtures( $user );
623 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
630 'unpatrolled' => false,
631 'autopatrolled' => 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,
976 'autopatrolled' => false,
979 $this->getItemsFromApiResponse( $resultPatrolled )
981 $this->assertEmpty( $this->getItemsFromApiResponse( $resultNotPatrolled ) );
984 public function testNewAndEditTypeParameters() {
985 $user = $this->getLoggedInTestUser();
986 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
987 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
992 'target' => $subjectTarget,
993 'content' => 'Some Content',
994 'summary' => 'Create the page',
997 'target' => $subjectTarget,
998 'content' => 'Some Other Content',
999 'summary' => 'Change the content',
1002 'target' => $talkTarget,
1003 'content' => 'Some Talk Page Content',
1004 'summary' => 'Create Talk page',
1008 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1010 $resultNew = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'new' ] );
1011 $resultEdit = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'edit' ] );
1013 $this->assertEquals(
1017 'ns' => $talkTarget->getNamespace(),
1018 'title' => $this->getPrefixedText( $talkTarget ),
1021 $this->getItemsFromApiResponse( $resultNew )
1023 $this->assertEquals(
1027 'ns' => $subjectTarget->getNamespace(),
1028 'title' => $this->getPrefixedText( $subjectTarget ),
1031 $this->getItemsFromApiResponse( $resultEdit )
1035 public function testLogTypeParameters() {
1036 $user = $this->getLoggedInTestUser();
1037 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1038 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1039 $this->createPageAndDeleteIt( $subjectTarget );
1043 'Some Talk Page Content',
1046 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1048 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'log' ] );
1050 $this->assertEquals(
1054 'ns' => $subjectTarget->getNamespace(),
1055 'title' => $this->getPrefixedText( $subjectTarget ),
1058 $this->getItemsFromApiResponse( $result )
1062 private function getExternalRC( LinkTarget
$target ) {
1063 $title = Title
::newFromLinkTarget( $target );
1065 $rc = new RecentChange
;
1066 $rc->mTitle
= $title;
1068 'rc_timestamp' => wfTimestamp( TS_MW
),
1069 'rc_namespace' => $title->getNamespace(),
1070 'rc_title' => $title->getDBkey(),
1071 'rc_type' => RC_EXTERNAL
,
1072 'rc_source' => 'foo',
1074 'rc_cur_id' => $title->getArticleID(),
1076 'rc_user_text' => 'ext>External User',
1078 'rc_comment_text' => '',
1079 'rc_comment_data' => null,
1080 'rc_this_oldid' => $title->getLatestRevID(),
1081 'rc_last_oldid' => $title->getLatestRevID(),
1084 'rc_patrolled' => 0,
1086 'rc_old_len' => $title->getLength(),
1087 'rc_new_len' => $title->getLength(),
1090 'rc_log_type' => null,
1091 'rc_log_action' => '',
1095 'prefixedDBkey' => $title->getPrefixedDBkey(),
1096 'lastTimestamp' => 0,
1097 'oldSize' => $title->getLength(),
1098 'newSize' => $title->getLength(),
1099 'pageStatus' => 'changed'
1105 public function testExternalTypeParameters() {
1106 $user = $this->getLoggedInTestUser();
1107 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1108 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1118 'Some Talk Page Content',
1122 $rc = $this->getExternalRC( $subjectTarget );
1125 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1127 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'external' ] );
1129 $this->assertEquals(
1132 'type' => 'external',
1133 'ns' => $subjectTarget->getNamespace(),
1134 'title' => $this->getPrefixedText( $subjectTarget ),
1137 $this->getItemsFromApiResponse( $result )
1141 public function testCategorizeTypeParameter() {
1142 $user = $this->getLoggedInTestUser();
1143 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1144 $categoryTarget = new TitleValue( NS_CATEGORY
, 'ApiQueryWatchlistIntegrationTestCategory' );
1149 'target' => $categoryTarget,
1150 'content' => 'Some Content',
1151 'summary' => 'Create the category',
1154 'target' => $subjectTarget,
1155 'content' => 'Some Content [[Category:ApiQueryWatchlistIntegrationTestCategory]]t',
1156 'summary' => 'Create the page and add it to the category',
1160 $title = Title
::newFromLinkTarget( $subjectTarget );
1161 $revision = Revision
::newFromTitle( $title );
1163 $rc = RecentChange
::newForCategorization(
1164 $revision->getTimestamp(),
1165 Title
::newFromLinkTarget( $categoryTarget ),
1167 $revision->getComment(),
1176 $this->watchPages( $user, [ $subjectTarget, $categoryTarget ] );
1178 $result = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wltype' => 'categorize' ] );
1180 $this->assertEquals(
1183 'type' => 'categorize',
1184 'ns' => $categoryTarget->getNamespace(),
1185 'title' => $this->getPrefixedText( $categoryTarget ),
1188 $this->getItemsFromApiResponse( $result )
1192 public function testLimitParam() {
1193 $user = $this->getLoggedInTestUser();
1194 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1195 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1196 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1201 'target' => $target1,
1202 'content' => 'Some Content',
1203 'summary' => 'Create the page',
1206 'target' => $target2,
1207 'content' => 'Some Talk Page Content',
1208 'summary' => 'Create Talk page',
1211 'target' => $target3,
1212 'content' => 'Some Other Content',
1213 'summary' => 'Create the page',
1217 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1219 $resultWithoutLimit = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1220 $resultWithLimit = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1222 $this->assertEquals(
1226 'ns' => $target3->getNamespace(),
1227 'title' => $this->getPrefixedText( $target3 )
1231 'ns' => $target2->getNamespace(),
1232 'title' => $this->getPrefixedText( $target2 )
1236 'ns' => $target1->getNamespace(),
1237 'title' => $this->getPrefixedText( $target1 )
1240 $this->getItemsFromApiResponse( $resultWithoutLimit )
1242 $this->assertEquals(
1246 'ns' => $target3->getNamespace(),
1247 'title' => $this->getPrefixedText( $target3 )
1251 'ns' => $target2->getNamespace(),
1252 'title' => $this->getPrefixedText( $target2 )
1255 $this->getItemsFromApiResponse( $resultWithLimit )
1257 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
1258 $this->assertArrayHasKey( 'wlcontinue', $resultWithLimit[0]['continue'] );
1261 public function testAllRevParam() {
1262 $user = $this->getLoggedInTestUser();
1263 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1268 'target' => $target,
1269 'content' => 'Some Content',
1270 'summary' => 'Create the page',
1273 'target' => $target,
1274 'content' => 'Some Other Content',
1275 'summary' => 'Change the content',
1279 $this->watchPages( $user, [ $target ] );
1281 $resultAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title', 'wlallrev' => '', ] );
1282 $resultNoAllRev = $this->doListWatchlistRequest( [ 'wlprop' => 'title' ] );
1284 $this->assertEquals(
1288 'ns' => $target->getNamespace(),
1289 'title' => $this->getPrefixedText( $target ),
1292 $this->getItemsFromApiResponse( $resultNoAllRev )
1294 $this->assertEquals(
1298 'ns' => $target->getNamespace(),
1299 'title' => $this->getPrefixedText( $target ),
1303 'ns' => $target->getNamespace(),
1304 'title' => $this->getPrefixedText( $target ),
1307 $this->getItemsFromApiResponse( $resultAllRev )
1311 public function testDirParams() {
1312 $user = $this->getLoggedInTestUser();
1313 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1314 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1319 'target' => $subjectTarget,
1320 'content' => 'Some Content',
1321 'summary' => 'Create the page',
1324 'target' => $talkTarget,
1325 'content' => 'Some Talk Page Content',
1326 'summary' => 'Create Talk page',
1330 $this->watchPages( $user, [ $subjectTarget, $talkTarget ] );
1332 $resultDirOlder = $this->doListWatchlistRequest( [ 'wldir' => 'older', 'wlprop' => 'title' ] );
1333 $resultDirNewer = $this->doListWatchlistRequest( [ 'wldir' => 'newer', 'wlprop' => 'title' ] );
1335 $this->assertEquals(
1339 'ns' => $talkTarget->getNamespace(),
1340 'title' => $this->getPrefixedText( $talkTarget )
1344 'ns' => $subjectTarget->getNamespace(),
1345 'title' => $this->getPrefixedText( $subjectTarget )
1348 $this->getItemsFromApiResponse( $resultDirOlder )
1350 $this->assertEquals(
1354 'ns' => $subjectTarget->getNamespace(),
1355 'title' => $this->getPrefixedText( $subjectTarget )
1359 'ns' => $talkTarget->getNamespace(),
1360 'title' => $this->getPrefixedText( $talkTarget )
1363 $this->getItemsFromApiResponse( $resultDirNewer )
1367 public function testStartEndParams() {
1368 $user = $this->getLoggedInTestUser();
1369 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1376 $this->watchPages( $user, [ $target ] );
1378 $resultStart = $this->doListWatchlistRequest( [
1379 'wlstart' => '20010115000000',
1381 'wlprop' => 'title',
1383 $resultEnd = $this->doListWatchlistRequest( [
1384 'wlend' => '20010115000000',
1386 'wlprop' => 'title',
1389 $this->assertEquals(
1393 'ns' => $target->getNamespace(),
1394 'title' => $this->getPrefixedText( $target ),
1397 $this->getItemsFromApiResponse( $resultStart )
1399 $this->assertEmpty( $this->getItemsFromApiResponse( $resultEnd ) );
1402 public function testContinueParam() {
1403 $user = $this->getLoggedInTestUser();
1404 $target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1405 $target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
1406 $target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
1411 'target' => $target1,
1412 'content' => 'Some Content',
1413 'summary' => 'Create the page',
1416 'target' => $target2,
1417 'content' => 'Some Talk Page Content',
1418 'summary' => 'Create Talk page',
1421 'target' => $target3,
1422 'content' => 'Some Other Content',
1423 'summary' => 'Create the page',
1427 $this->watchPages( $user, [ $target1, $target2, $target3 ] );
1429 $firstResult = $this->doListWatchlistRequest( [ 'wllimit' => 2, 'wlprop' => 'title' ] );
1430 $this->assertArrayHasKey( 'continue', $firstResult[0] );
1431 $this->assertArrayHasKey( 'wlcontinue', $firstResult[0]['continue'] );
1433 $continuationParam = $firstResult[0]['continue']['wlcontinue'];
1435 $continuedResult = $this->doListWatchlistRequest(
1436 [ 'wlcontinue' => $continuationParam, 'wlprop' => 'title' ]
1439 $this->assertEquals(
1443 'ns' => $target3->getNamespace(),
1444 'title' => $this->getPrefixedText( $target3 ),
1448 'ns' => $target2->getNamespace(),
1449 'title' => $this->getPrefixedText( $target2 ),
1452 $this->getItemsFromApiResponse( $firstResult )
1454 $this->assertEquals(
1458 'ns' => $target1->getNamespace(),
1459 'title' => $this->getPrefixedText( $target1 )
1462 $this->getItemsFromApiResponse( $continuedResult )
1466 public function testOwnerAndTokenParams() {
1467 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1469 $this->getLoggedInTestUser(),
1475 $otherUser = $this->getNonLoggedInTestUser();
1476 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1477 $otherUser->saveSettings();
1479 $this->watchPages( $otherUser, [ $target ] );
1481 $reloadedUser = User
::newFromName( $otherUser->getName() );
1482 $this->assertEquals( '1234567890', $reloadedUser->getOption( 'watchlisttoken' ) );
1484 $result = $this->doListWatchlistRequest( [
1485 'wlowner' => $otherUser->getName(),
1486 'wltoken' => '1234567890',
1487 'wlprop' => 'title',
1490 $this->assertEquals(
1494 'ns' => $target->getNamespace(),
1495 'title' => $this->getPrefixedText( $target )
1498 $this->getItemsFromApiResponse( $result )
1502 public function testOwnerAndTokenParams_wrongToken() {
1503 $otherUser = $this->getNonLoggedInTestUser();
1504 $otherUser->setOption( 'watchlisttoken', '1234567890' );
1505 $otherUser->saveSettings();
1507 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1509 $this->doListWatchlistRequest( [
1510 'wlowner' => $otherUser->getName(),
1511 'wltoken' => 'wrong-token',
1515 public function testOwnerAndTokenParams_noWatchlistTokenSet() {
1516 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
1518 $this->doListWatchlistRequest( [
1519 'wlowner' => $this->getNonLoggedInTestUser()->getName(),
1520 'wltoken' => 'some-token',
1524 public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
1525 $user = $this->getLoggedInTestUser();
1526 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1533 $this->watchPages( $user, [ $target ] );
1535 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'info' ] );
1537 $this->assertArrayHasKey( 'query', $result[0] );
1538 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1540 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1541 $pages = array_values( $result[0]['query']['pages'] );
1543 $this->assertArraySubsetsEqual(
1547 'ns' => $target->getNamespace(),
1548 'title' => $this->getPrefixedText( $target ),
1552 [ 'ns', 'title', 'new' ]
1556 public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
1557 $user = $this->getLoggedInTestUser();
1558 $target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
1563 'target' => $target,
1564 'content' => 'Some Content',
1565 'summary' => 'Create the page',
1568 'target' => $target,
1569 'content' => 'Some Other Content',
1570 'summary' => 'Change the content',
1574 $this->watchPages( $user, [ $target ] );
1576 $result = $this->doGeneratorWatchlistRequest( [ 'prop' => 'revisions', 'gwlallrev' => '' ] );
1578 $this->assertArrayHasKey( 'query', $result[0] );
1579 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
1581 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1582 $pages = array_values( $result[0]['query']['pages'] );
1584 $this->assertCount( 1, $pages );
1585 $this->assertEquals( 0, $pages[0]['ns'] );
1586 $this->assertEquals( $this->getPrefixedText( $target ), $pages[0]['title'] );
1587 $this->assertArraySubsetsEqual(
1588 $pages[0]['revisions'],
1591 'comment' => 'Create the page',
1592 'user' => $user->getName(),
1596 'comment' => 'Change the content',
1597 'user' => $user->getName(),
1601 [ 'comment', 'user', 'minor' ]