3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\Linker\LinkTarget
;
6 use MediaWiki\Permissions\Authority
;
7 use MediaWiki\Tests\Api\ApiTestCase
;
8 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
9 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
10 use MediaWiki\Title\TitleValue
;
11 use MediaWiki\User\User
;
12 use MediaWiki\Watchlist\WatchedItemQueryService
;
20 * @covers \MediaWiki\Api\ApiQueryRecentChanges
22 class ApiQueryRecentChangesIntegrationTest
extends ApiTestCase
{
23 use MockAuthorityTrait
;
24 use TempUserTestTrait
;
26 private function getLoggedInTestUser() {
27 return $this->getTestUser()->getUser();
30 private function doPageEdit( Authority
$performer, $target, $summary ) {
42 private function doMinorPageEdit( User
$user, LinkTarget
$target, $summary ) {
43 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
44 $page->doUserEditContent(
45 $page->getContentHandler()->unserializeContent( __CLASS__
),
52 private function doBotPageEdit( User
$user, LinkTarget
$target, $summary ) {
53 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
54 $page->doUserEditContent(
55 $page->getContentHandler()->unserializeContent( __CLASS__
),
62 private function doAnonPageEdit( LinkTarget
$target, $summary ) {
63 $this->disableAutoCreateTempUser();
64 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
65 $page->doUserEditContent(
66 $page->getContentHandler()->unserializeContent( __CLASS__
),
67 $this->getServiceContainer()->getUserFactory()->newAnonymous(),
72 private function doTempPageEdit( LinkTarget
$target, $summary ) {
73 // Set up temp user config
74 $this->enableAutoCreateTempUser();
75 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
76 $page->doUserEditContent(
77 $page->getContentHandler()->unserializeContent( __CLASS__
),
78 $this->mockTempUltimateAuthority(),
84 * Performs a batch of page edits as a specified user
86 * @param array $editData associative array, keys:
87 * - target => LinkTarget page to edit
88 * - summary => string edit summary
89 * - minorEdit => bool mark as minor edit if true (defaults to false)
90 * - botEdit => bool mark as bot edit if true (defaults to false)
92 private function doPageEdits( User
$user, array $editData ) {
93 foreach ( $editData as $singleEditData ) {
94 if ( array_key_exists( 'minorEdit', $singleEditData ) && $singleEditData['minorEdit'] ) {
95 $this->doMinorPageEdit(
97 $singleEditData['target'],
98 $singleEditData['summary']
102 if ( array_key_exists( 'botEdit', $singleEditData ) && $singleEditData['botEdit'] ) {
103 $this->doBotPageEdit(
105 $singleEditData['target'],
106 $singleEditData['summary']
112 $singleEditData['target'],
113 $singleEditData['summary']
118 private function doListRecentChangesRequest( array $params = [] ) {
119 return $this->doApiRequest(
121 [ 'action' => 'query', 'list' => 'recentchanges' ],
126 $this->getLoggedInTestUser()
130 private function doGeneratorRecentChangesRequest( array $params = [] ) {
131 return $this->doApiRequest(
133 [ 'action' => 'query', 'generator' => 'recentchanges' ],
138 $this->getLoggedInTestUser()
142 private function getItemsFromRecentChangesResult( array $result ) {
143 return $result[0]['query']['recentchanges'];
146 public function testListRecentChanges_returnsRCInfo() {
147 $target = new TitleValue( NS_MAIN
, 'ApiQueryRecentChangesIntegrationTestPage' );
148 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
150 $result = $this->doListRecentChangesRequest();
151 $items = $this->getItemsFromRecentChangesResult( $result );
153 // Default contains at least props for 'title', 'timestamp', and 'ids'.
154 $this->assertCount( 1, $items );
163 // Assert key but ignore value
164 $this->assertArrayHasKey( $key, $item );
165 unset( $item[ $key ] );
168 // The rest must equal exactly, with no additional keys (e.g. 'minor' or 'bot').
173 'title' => 'ApiQueryRecentChangesIntegrationTestPage',
179 public function testIdsPropParameter() {
180 $target = new TitleValue( NS_MAIN
, 'ApiQueryRecentChangesIntegrationTestPage' );
181 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
182 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'ids' ] );
183 $items = $this->getItemsFromRecentChangesResult( $result );
185 $this->assertCount( 1, $items );
193 // Assert key but ignore value
194 $this->assertArrayHasKey( $key, $item );
195 unset( $item[ $key ] );
206 public function testTitlePropParameter() {
208 $this->getLoggedInTestUser(),
211 'target' => new TitleValue( NS_MAIN
, 'Thing' ),
212 'summary' => 'Create the page',
215 'target' => new TitleValue( NS_TALK
, 'Thing' ),
216 'summary' => 'Create Talk page',
221 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'title' ] );
228 'title' => 'Talk:Thing',
236 $this->getItemsFromRecentChangesResult( $result )
240 public function testFlagsPropParameter() {
242 $this->getLoggedInTestUser(),
245 'summary' => 'Create the page',
246 'target' => new TitleValue( NS_MAIN
, 'Regularpage' ),
249 'summary' => 'Create the page for minor change',
250 'target' => new TitleValue( NS_MAIN
, 'Minorpage' ),
253 'summary' => 'Make minor content',
254 'target' => new TitleValue( NS_MAIN
, 'Minorpage' ),
258 'summary' => 'Create the page as a bot',
259 'target' => new TitleValue( NS_MAIN
, 'Botpage' ),
265 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'flags' ] );
294 $this->getItemsFromRecentChangesResult( $result )
298 public function testUserPropParameter() {
299 $userEditTarget = new TitleValue( NS_MAIN
, 'Foo' );
300 $anonEditTarget = new TitleValue( NS_MAIN
, 'Bar' );
301 $tempEditTarget = new TitleValue( NS_MAIN
, 'Baz' );
302 $this->doPageEdit( $this->getLoggedInTestUser(), $userEditTarget, 'Create the page' );
303 $this->doAnonPageEdit( $anonEditTarget, 'Create the page' );
305 // Test that querying for anonymous edits works even if temporary accounts are disabled
306 $this->disableAutoCreateTempUser();
307 $result = $this->doListRecentChangesRequest( [
309 'rcshow' => WatchedItemQueryService
::FILTER_NOT_ANON
,
315 'user' => $this->getLoggedInTestUser()->getName(),
318 $this->getItemsFromRecentChangesResult( $result )
321 // Test that temporary accounts are treated as anonymous
322 $this->doTempPageEdit( $tempEditTarget, 'Create the page' );
323 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'user' ] );
334 'user' => '127.0.0.1',
338 'user' => $this->getLoggedInTestUser()->getName(),
341 $this->getItemsFromRecentChangesResult( $result )
345 public function testUserIdPropParameter() {
346 $user = $this->getLoggedInTestUser();
347 $userEditTarget = new TitleValue( NS_MAIN
, 'Foo' );
348 $anonEditTarget = new TitleValue( NS_MAIN
, 'Bar' );
349 $this->doPageEdit( $user, $userEditTarget, 'Create the page' );
350 $this->doAnonPageEdit( $anonEditTarget, 'Create the page' );
352 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'userid' ] );
363 'userid' => $user->getId(),
366 $this->getItemsFromRecentChangesResult( $result )
370 public function testCommentPropParameter() {
371 $target = new TitleValue( NS_MAIN
, 'Thing' );
372 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the <b>page</b>' );
374 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'comment' ] );
380 'comment' => 'Create the <b>page</b>',
383 $this->getItemsFromRecentChangesResult( $result )
387 public function testParsedCommentPropParameter() {
388 $target = new TitleValue( NS_MAIN
, 'Thing' );
389 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the <b>page</b>' );
391 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'parsedcomment' ] );
397 'parsedcomment' => 'Create the <b>page</b>',
400 $this->getItemsFromRecentChangesResult( $result )
404 public function testTimestampPropParameter() {
405 $target = new TitleValue( NS_MAIN
, 'Thing' );
406 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
408 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'timestamp' ] );
409 $items = $this->getItemsFromRecentChangesResult( $result );
411 $this->assertCount( 1, $items );
412 $this->assertIsString( $items[0]['timestamp'] );
415 public function testSizesPropParameter() {
416 $target = new TitleValue( NS_MAIN
, 'Thing' );
417 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
419 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'sizes' ] );
426 // strlen( __CLASS__ ) - 2 = 64
430 $this->getItemsFromRecentChangesResult( $result )
434 private function createPageAndDeleteIt( LinkTarget
$target ) {
435 $wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
436 $this->doPageEdit( $this->getLoggedInTestUser(),
438 'Create the page that will be deleted'
440 $this->deletePage( $wikiPage, 'Important Reason' );
443 public function testLoginfoPropParameter() {
444 $target = new TitleValue( NS_MAIN
, 'Thing' );
445 $this->createPageAndDeleteIt( $target );
447 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'loginfo' ] );
448 $items = $this->getItemsFromRecentChangesResult( $result );
450 $this->assertCount( 1, $items );
454 // Assert key but ignore value
455 $this->assertArrayHasKey( $key, $items[0] );
456 unset( $items[0][ $key ] );
461 'logtype' => 'delete',
462 'logaction' => 'delete',
469 public function testEmptyPropParameter() {
470 $user = $this->getLoggedInTestUser();
471 $target = new TitleValue( NS_MAIN
, 'Thing' );
472 $this->doPageEdit( $user, $target, 'Create the page' );
474 $result = $this->doListRecentChangesRequest( [ 'rcprop' => '' ] );
482 $this->getItemsFromRecentChangesResult( $result )
486 public function testNamespaceParam() {
487 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
488 $talkTarget = new TitleValue( NS_TALK
, 'Foo' );
490 $this->getLoggedInTestUser(),
493 'target' => $subjectTarget,
494 'summary' => 'Create the page',
497 'target' => $talkTarget,
498 'summary' => 'Create the talk page',
503 $result = $this->doListRecentChangesRequest( [ 'rcnamespace' => '0', 'rcprop' => 'title' ] );
504 $items = $this->getItemsFromRecentChangesResult( $result );
506 $this->assertCount( 1, $items );
517 public function testShowAnonParams() {
518 $target = new TitleValue( NS_MAIN
, 'Thing' );
519 $this->doAnonPageEdit( $target, 'Create the page' );
521 $tempEditTarget = new TitleValue( NS_MAIN
, 'Baz' );
522 $this->doTempPageEdit( $tempEditTarget, 'Create the page' );
524 $resultAnon = $this->doListRecentChangesRequest( [
526 'rcshow' => WatchedItemQueryService
::FILTER_ANON
528 $resultNotAnon = $this->doListRecentChangesRequest( [
530 'rcshow' => WatchedItemQueryService
::FILTER_NOT_ANON
533 $items = $this->getItemsFromRecentChangesResult( $resultAnon );
534 $this->assertCount( 2, $items );
545 'user' => '127.0.0.1',
550 $this->assertSame( [], $this->getItemsFromRecentChangesResult( $resultNotAnon ) );
553 public function testNewAndEditTypeParameters() {
554 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
555 $talkTarget = new TitleValue( NS_TALK
, 'Foo' );
557 $this->getLoggedInTestUser(),
560 'target' => $subjectTarget,
561 'summary' => 'Create the page',
564 'target' => $subjectTarget,
565 'summary' => 'Change the content',
568 'target' => $talkTarget,
569 'summary' => 'Create Talk page',
574 $resultNew = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rctype' => 'new' ] );
575 $resultEdit = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rctype' => 'edit' ] );
582 'title' => 'Talk:Foo',
590 $this->getItemsFromRecentChangesResult( $resultNew )
600 $this->getItemsFromRecentChangesResult( $resultEdit )
604 public function testLogTypeParameters() {
605 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
606 $talkTarget = new TitleValue( NS_TALK
, 'Foo' );
607 $this->createPageAndDeleteIt( $subjectTarget );
608 $this->doPageEdit( $this->getLoggedInTestUser(), $talkTarget, 'Create Talk page' );
610 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rctype' => 'log' ] );
620 $this->getItemsFromRecentChangesResult( $result )
624 private function getExternalRC( LinkTarget
$target ) {
625 $title = $this->getServiceContainer()->getTitleFactory()->newFromLinkTarget( $target );
627 $rc = new RecentChange
;
629 'rc_timestamp' => wfTimestamp( TS_MW
),
630 'rc_namespace' => $title->getNamespace(),
631 'rc_title' => $title->getDBkey(),
632 'rc_type' => RC_EXTERNAL
,
633 'rc_source' => 'foo',
635 'rc_cur_id' => $title->getArticleID(),
637 'rc_user_text' => 'm>External User',
639 'rc_comment_text' => '',
640 'rc_comment_data' => null,
641 'rc_this_oldid' => $title->getLatestRevID(),
642 'rc_last_oldid' => $title->getLatestRevID(),
647 'rc_old_len' => $title->getLength(),
648 'rc_new_len' => $title->getLength(),
651 'rc_log_type' => null,
652 'rc_log_action' => '',
656 'prefixedDBkey' => $title->getPrefixedDBkey(),
657 'lastTimestamp' => 0,
658 'oldSize' => $title->getLength(),
659 'newSize' => $title->getLength(),
660 'pageStatus' => 'changed'
666 public function testExternalTypeParameters() {
667 $user = $this->getLoggedInTestUser();
668 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
669 $talkTarget = new TitleValue( NS_TALK
, 'Foo' );
670 $this->doPageEdit( $user, $subjectTarget, 'Create the page' );
671 $this->doPageEdit( $user, $talkTarget, 'Create Talk page' );
672 $rc = $this->getExternalRC( $subjectTarget );
675 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rctype' => 'external' ] );
680 'type' => 'external',
685 $this->getItemsFromRecentChangesResult( $result )
689 public function testCategorizeTypeParameter() {
690 $user = $this->getLoggedInTestUser();
691 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
692 $categoryTarget = new TitleValue( NS_CATEGORY
, 'Bar' );
697 'target' => $categoryTarget,
698 'summary' => 'Create the category',
701 'target' => $subjectTarget,
702 'summary' => 'Create the page and add it to the category',
706 $titleFactory = $this->getServiceContainer()->getTitleFactory();
707 $title = $titleFactory->newFromLinkTarget( $subjectTarget );
708 $revision = $this->getServiceContainer()
709 ->getRevisionLookup()
710 ->getRevisionByTitle( $title );
712 $comment = $revision->getComment();
713 $rc = RecentChange
::newForCategorization(
714 $revision->getTimestamp(),
715 $titleFactory->newFromLinkTarget( $categoryTarget ),
717 $comment ?
$comment->text
: '',
726 $result = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rctype' => 'categorize' ] );
731 'type' => 'categorize',
733 'title' => 'Category:Bar',
736 $this->getItemsFromRecentChangesResult( $result )
740 public function testLimitParam() {
742 $this->getLoggedInTestUser(),
745 'target' => new TitleValue( NS_MAIN
, 'Foo' ),
746 'summary' => 'Create the page',
749 'target' => new TitleValue( NS_TALK
, 'Foo' ),
750 'summary' => 'Create Talk page',
753 'target' => new TitleValue( NS_MAIN
, 'Bar' ),
754 'summary' => 'Create another page',
759 $resultWithoutLimit = $this->doListRecentChangesRequest( [ 'rcprop' => 'title' ] );
760 $resultWithLimit = $this->doListRecentChangesRequest( [ 'rclimit' => 2, 'rcprop' => 'title' ] );
772 'title' => 'Talk:Foo'
780 $this->getItemsFromRecentChangesResult( $resultWithoutLimit )
792 'title' => 'Talk:Foo'
795 $this->getItemsFromRecentChangesResult( $resultWithLimit )
797 $this->assertArrayHasKey( 'rccontinue', $resultWithLimit[0]['continue'] );
800 public function testAllRevParam() {
801 $target = new TitleValue( NS_MAIN
, 'Thing' );
803 $this->getLoggedInTestUser(),
807 'summary' => 'Create the page',
811 'summary' => 'Change the content',
816 $resultAllRev = $this->doListRecentChangesRequest( [ 'rcprop' => 'title', 'rcallrev' => '' ] );
817 $resultNoAllRev = $this->doListRecentChangesRequest( [ 'rcprop' => 'title' ] );
832 $this->getItemsFromRecentChangesResult( $resultNoAllRev )
847 $this->getItemsFromRecentChangesResult( $resultAllRev )
851 public function testDirParams() {
852 $subjectTarget = new TitleValue( NS_MAIN
, 'Foo' );
853 $talkTarget = new TitleValue( NS_TALK
, 'Foo' );
855 $this->getLoggedInTestUser(),
858 'target' => $subjectTarget,
859 'summary' => 'Create the page',
862 'target' => $talkTarget,
863 'summary' => 'Create Talk page',
868 $resultDirOlder = $this->doListRecentChangesRequest(
869 [ 'rcdir' => 'older', 'rcprop' => 'title' ]
871 $resultDirNewer = $this->doListRecentChangesRequest(
872 [ 'rcdir' => 'newer', 'rcprop' => 'title' ]
880 'title' => 'Talk:Foo'
888 $this->getItemsFromRecentChangesResult( $resultDirOlder )
900 'title' => 'Talk:Foo'
903 $this->getItemsFromRecentChangesResult( $resultDirNewer )
907 public function testTitleParams() {
909 $this->getLoggedInTestUser(),
912 'target' => new TitleValue( NS_MAIN
, 'Foo' ),
913 'summary' => 'Create the page',
916 'target' => new TitleValue( NS_TALK
, 'Bar' ),
917 'summary' => 'Create the page',
920 'target' => new TitleValue( NS_MAIN
, 'Quux' ),
921 'summary' => 'Create the page',
926 $result1 = $this->doListRecentChangesRequest(
932 $result2 = $this->doListRecentChangesRequest(
934 'rctitle' => 'Talk:Bar',
947 $this->getItemsFromRecentChangesResult( $result1 )
954 'title' => 'Talk:Bar'
957 $this->getItemsFromRecentChangesResult( $result2 )
961 public function testStartEndParams() {
962 $target = new TitleValue( NS_MAIN
, 'Thing' );
963 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
965 $resultStart = $this->doListRecentChangesRequest( [
966 'rcstart' => '20010115000000',
970 $resultEnd = $this->doListRecentChangesRequest( [
971 'rcend' => '20010115000000',
984 $this->getItemsFromRecentChangesResult( $resultStart )
986 $this->assertSame( [], $this->getItemsFromRecentChangesResult( $resultEnd ) );
989 public function testContinueParam() {
991 $this->getLoggedInTestUser(),
994 'target' => new TitleValue( NS_MAIN
, 'Foo' ),
995 'summary' => 'Create the page',
998 'target' => new TitleValue( NS_TALK
, 'Foo' ),
999 'summary' => 'Create Talk page',
1002 'target' => new TitleValue( NS_MAIN
, 'Bar' ),
1003 'summary' => 'Create the page',
1008 $firstResult = $this->doListRecentChangesRequest( [ 'rclimit' => 2, 'rcprop' => 'title' ] );
1010 $continuationParam = $firstResult[0]['continue']['rccontinue'];
1012 $continuedResult = $this->doListRecentChangesRequest(
1013 [ 'rccontinue' => $continuationParam, 'rcprop' => 'title' ]
1016 $this->assertEquals(
1026 'title' => 'Talk:Foo',
1029 $this->getItemsFromRecentChangesResult( $firstResult )
1031 $this->assertEquals(
1039 $this->getItemsFromRecentChangesResult( $continuedResult )
1043 public function testGeneratorRecentChangesPropInfo_returnsRCPages() {
1044 $target = new TitleValue( NS_MAIN
, 'Thing' );
1045 $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' );
1047 $result = $this->doGeneratorRecentChangesRequest( [ 'prop' => 'info' ] );
1049 // $result[0]['query']['pages'] uses page ids as keys. Page ids don't matter here, so drop them
1050 $pages = array_values( $result[0]['query']['pages'] );
1051 $this->assertCount( 1, $pages );
1060 // Assert key but ignore value
1061 $this->assertArrayHasKey( $key, $page );
1062 unset( $page[ $key ] );
1065 $this->assertEquals(
1070 'contentmodel' => 'wikitext',
1071 'pagelanguage' => 'en',
1072 'pagelanguagehtmlcode' => 'en',
1073 'pagelanguagedir' => 'ltr',