7 * @covers ApiQueryUserContribs
9 class ApiQueryUserContribsTest
extends ApiTestCase
{
10 public function addDBDataOnce() {
11 global $wgActorTableSchemaMigrationStage;
13 $reset = new \Wikimedia\
ScopedCallback( function ( $v ) {
14 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $v );
15 }, [ $wgActorTableSchemaMigrationStage ] );
16 // Needs to WRITE_BOTH so READ_OLD tests below work. READ mode here doesn't really matter.
17 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage',
18 SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
);
21 User
::newFromName( '192.168.2.2', false ),
22 User
::newFromName( '192.168.2.1', false ),
23 User
::newFromName( '192.168.2.3', false ),
24 User
::createNew( __CLASS__
. ' B' ),
25 User
::createNew( __CLASS__
. ' A' ),
26 User
::createNew( __CLASS__
. ' C' ),
27 User
::newFromName( 'IW>' . __CLASS__
, false ),
30 $title = Title
::newFromText( __CLASS__
);
31 $page = WikiPage
::factory( $title );
32 for ( $i = 0; $i < 3; $i++
) {
33 foreach ( array_reverse( $users ) as $user ) {
34 $status = $page->doEditContent(
35 ContentHandler
::makeContent( "Test revision $user #$i", $title ), 'Test edit', 0, false, $user
37 if ( !$status->isOK() ) {
38 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
45 * @dataProvider provideSorting
46 * @param int $stage SCHEMA_COMPAT contants for $wgActorTableSchemaMigrationStage
47 * @param array $params Extra parameters for the query
48 * @param bool $reverse Reverse order?
49 * @param int $revs Number of revisions to expect
51 public function testSorting( $stage, $params, $reverse, $revs ) {
52 // FIXME: fails under sqlite
53 $this->markTestSkippedIfDbType( 'sqlite' );
55 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
57 if ( isset( $params['ucuserids'] ) ) {
58 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
60 if ( isset( $params['ucuser'] ) ) {
61 $params['ucuser'] = implode( '|', $params['ucuser'] );
66 $params['ucdir'] = 'newer';
72 'list' => 'usercontribs',
76 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => 500 ] );
77 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
78 $this->assertArrayHasKey( 'query', $apiResult[0] );
79 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
83 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
85 $ids[$page['user']][] = $page['revid'];
87 $this->assertSame( $revs, $count, 'Expected number of revisions' );
88 foreach ( $ids as $user => $revids ) {
90 call_user_func_array( $sort, [ &$sorted ] );
91 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
94 for ( $limit = 1; $limit < $revs; $limit++
) {
98 while ( $continue !== null ) {
99 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => $limit ] +
$continue );
100 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
101 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
102 "Batching with limit $limit" );
103 $continue = $apiResult[0]['continue'] ??
null;
104 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
106 $batchedIds[$page['user']][] = $page['revid'];
108 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
110 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
114 public static function provideSorting() {
115 $users = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' C' ];
116 $users2 = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' D' ];
117 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
121 'old' => SCHEMA_COMPAT_OLD
,
122 'read old' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
123 'read new' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
124 'new' => SCHEMA_COMPAT_NEW
,
125 ] as $stageName => $stage
127 foreach ( [ false, true ] as $reverse ) {
128 $name = $stageName . ( $reverse ?
', reverse' : '' );
129 yield
"Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
130 yield
"Named users including a no-edit user, $name" => [
131 $stage, [ 'ucuser' => $users2 ], $reverse, 6
133 yield
"IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
134 yield
"All users, $name" => [
135 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
137 yield
"User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
138 yield
"Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__
], $reverse, 9 ];
139 yield
"IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
145 * @dataProvider provideInterwikiUser
146 * @param int $stage SCHEMA_COMPAT constants for $wgActorTableSchemaMigrationStage
148 public function testInterwikiUser( $stage ) {
149 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
153 'list' => 'usercontribs',
154 'ucuser' => 'IW>' . __CLASS__
,
159 $apiResult = $this->doApiRequest( $params );
160 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
161 $this->assertArrayHasKey( 'query', $apiResult[0] );
162 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
166 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
168 $this->assertSame( 'IW>' . __CLASS__
, $page['user'], 'Correct user returned' );
169 $ids[] = $page['revid'];
171 $this->assertSame( 3, $count, 'Expected number of revisions' );
174 $this->assertSame( $sorted, $ids, "IDs are sorted" );
177 public static function provideInterwikiUser() {
179 'old' => [ SCHEMA_COMPAT_OLD
],
180 'read old' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
],
181 'read new' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
],
182 'new' => [ SCHEMA_COMPAT_NEW
],