Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryUserContribsTest.php
blob679472dda0e77cc01ed1527b7a7eee81060353da
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use MediaWiki\Content\WikitextContent;
6 use MediaWiki\Tests\Api\ApiTestCase;
7 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
8 use MediaWiki\Title\TitleValue;
9 use MediaWiki\User\User;
10 use MediaWiki\User\UserRigorOptions;
12 /**
13 * @group API
14 * @group Database
15 * @group medium
16 * @covers \MediaWiki\Api\ApiQueryUserContribs
18 class ApiQueryUserContribsTest extends ApiTestCase {
20 use TempUserTestTrait;
22 public function addDBDataOnce() {
23 $this->disableAutoCreateTempUser();
24 $userFactory = $this->getServiceContainer()->getUserFactory();
25 $users = [
26 $userFactory->newFromName( '192.168.2.2', UserRigorOptions::RIGOR_NONE ),
27 $userFactory->newFromName( '192.168.2.1', UserRigorOptions::RIGOR_NONE ),
28 $userFactory->newFromName( '192.168.2.3', UserRigorOptions::RIGOR_NONE ),
29 User::createNew( __CLASS__ . ' B' ),
30 User::createNew( __CLASS__ . ' A' ),
31 User::createNew( __CLASS__ . ' C' ),
32 $userFactory->newFromName( 'IW>' . __CLASS__, UserRigorOptions::RIGOR_NONE ),
35 $page = $this->getServiceContainer()->getWikiPageFactory()
36 ->newFromLinkTarget( new TitleValue( NS_MAIN, 'ApiQueryUserContribsTest' ) );
37 for ( $i = 0; $i < 3; $i++ ) {
38 foreach ( array_reverse( $users ) as $user ) {
39 $status = $this->editPage(
40 $page,
41 new WikitextContent( "Test revision $user #$i" ),
42 'Test edit',
43 NS_MAIN,
44 $user
46 if ( !$status->isOK() ) {
47 $this->fail( 'Failed to edit: ' . $status->getWikiText( false, false, 'en' ) );
53 /**
54 * @dataProvider provideSorting
55 * @param array $params Extra parameters for the query
56 * @param bool $reverse Reverse order?
57 * @param int $revs Number of revisions to expect
59 public function testSorting( $params, $reverse, $revs ) {
60 if ( isset( $params['ucuserids'] ) ) {
61 $userIdentities = $this->getServiceContainer()->getUserIdentityLookup()
62 ->newSelectQueryBuilder()
63 ->whereUserNames( $params['ucuserids'] )
64 ->fetchUserIdentities();
65 $userIds = [];
66 foreach ( $userIdentities as $userIdentity ) {
67 $userIds[] = $userIdentity->getId();
69 $params['ucuserids'] = implode( '|', $userIds );
71 if ( isset( $params['ucuser'] ) ) {
72 $params['ucuser'] = implode( '|', $params['ucuser'] );
75 if ( $reverse ) {
76 $params['ucdir'] = 'newer';
79 $params += [
80 'action' => 'query',
81 'list' => 'usercontribs',
82 'ucprop' => 'ids',
85 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
86 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
87 $this->assertArrayHasKey( 'query', $apiResult[0] );
88 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
90 $count = 0;
91 $ids = [];
92 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
93 $count++;
94 $ids[$page['user']][] = $page['revid'];
96 $this->assertSame( $revs, $count, 'Expected number of revisions' );
97 foreach ( $ids as $user => $revids ) {
98 $sorted = $revids;
99 $reverse ? sort( $sorted ) : rsort( $sorted );
100 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
103 for ( $limit = 1; $limit < $revs; $limit++ ) {
104 $continue = [];
105 $count = 0;
106 $batchedIds = [];
107 while ( $continue !== null ) {
108 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
109 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
110 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
111 "Batching with limit $limit" );
112 $continue = $apiResult[0]['continue'] ?? null;
113 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
114 $count++;
115 $batchedIds[$page['user']][] = $page['revid'];
117 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
119 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
123 public static function provideSorting() {
124 $users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
125 $users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
126 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
128 foreach ( [ false, true ] as $reverse ) {
129 $name = ( $reverse ? ', reverse' : '' );
130 yield "Named users, $name" => [ [ 'ucuser' => $users ], $reverse, 9 ];
131 yield "Named users including a no-edit user, $name" => [
132 [ 'ucuser' => $users2 ], $reverse, 6
134 yield "IP users, $name" => [ [ 'ucuser' => $ips ], $reverse, 9 ];
135 yield "All users, $name" => [
136 [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
138 yield "User IDs, $name" => [ [ 'ucuserids' => $users ], $reverse, 9 ];
139 yield "Users by prefix, $name" => [ [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
140 yield "IPs by prefix, $name" => [ [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
141 yield "IPs by range, $name" => [ [ 'uciprange' => '192.168.2.0/24' ], $reverse, 9 ];
145 public function testInterwikiUser() {
146 $params = [
147 'action' => 'query',
148 'list' => 'usercontribs',
149 'ucuser' => 'IW>' . __CLASS__,
150 'ucprop' => 'ids',
151 'uclimit' => 'max',
154 $apiResult = $this->doApiRequest( $params );
155 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
156 $this->assertArrayHasKey( 'query', $apiResult[0] );
157 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
159 $count = 0;
160 $ids = [];
161 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
162 $count++;
163 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
164 $ids[] = $page['revid'];
166 $this->assertSame( 3, $count, 'Expected number of revisions' );
167 $sorted = $ids;
168 rsort( $sorted );
169 $this->assertSame( $sorted, $ids, "IDs are sorted" );