7 * @covers ApiQueryUserContribs
9 class ApiQueryUserContribsTest
extends ApiTestCase
{
10 public function addDBDataOnce() {
11 global $wgActorTableSchemaMigrationStage;
13 $reset = new \Wikimedia\
ScopedCallback( function ( $v ) {
14 global $wgActorTableSchemaMigrationStage;
15 $wgActorTableSchemaMigrationStage = $v;
16 $this->overrideMwServices();
17 }, [ $wgActorTableSchemaMigrationStage ] );
18 $wgActorTableSchemaMigrationStage = SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
;
19 $this->overrideMwServices();
22 User
::newFromName( '192.168.2.2', false ),
23 User
::newFromName( '192.168.2.1', false ),
24 User
::newFromName( '192.168.2.3', false ),
25 User
::createNew( __CLASS__
. ' B' ),
26 User
::createNew( __CLASS__
. ' A' ),
27 User
::createNew( __CLASS__
. ' C' ),
28 User
::newFromName( 'IW>' . __CLASS__
, false ),
31 $title = Title
::newFromText( __CLASS__
);
32 $page = WikiPage
::factory( $title );
33 for ( $i = 0; $i < 3; $i++
) {
34 foreach ( array_reverse( $users ) as $user ) {
35 $status = $page->doEditContent(
36 ContentHandler
::makeContent( "Test revision $user #$i", $title ), 'Test edit', 0, false, $user
38 if ( !$status->isOK() ) {
39 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
46 * @dataProvider provideSorting
47 * @param int $stage SCHEMA_COMPAT contants for $wgActorTableSchemaMigrationStage
48 * @param array $params Extra parameters for the query
49 * @param bool $reverse Reverse order?
50 * @param int $revs Number of revisions to expect
52 public function testSorting( $stage, $params, $reverse, $revs ) {
53 // FIXME: fails under sqlite
54 $this->markTestSkippedIfDbType( 'sqlite' );
56 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
57 $this->overrideMwServices();
59 if ( isset( $params['ucuserids'] ) ) {
60 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
62 if ( isset( $params['ucuser'] ) ) {
63 $params['ucuser'] = implode( '|', $params['ucuser'] );
68 $params['ucdir'] = 'newer';
74 'list' => 'usercontribs',
78 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => 500 ] );
79 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
80 $this->assertArrayHasKey( 'query', $apiResult[0] );
81 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
85 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
87 $ids[$page['user']][] = $page['revid'];
89 $this->assertSame( $revs, $count, 'Expected number of revisions' );
90 foreach ( $ids as $user => $revids ) {
92 call_user_func_array( $sort, [ &$sorted ] );
93 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
96 for ( $limit = 1; $limit < $revs; $limit++
) {
100 while ( $continue !== null ) {
101 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => $limit ] +
$continue );
102 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
103 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
104 "Batching with limit $limit" );
105 $continue = $apiResult[0]['continue'] ??
null;
106 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
108 $batchedIds[$page['user']][] = $page['revid'];
110 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
112 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
116 public static function provideSorting() {
117 $users = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' C' ];
118 $users2 = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' D' ];
119 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
123 'old' => SCHEMA_COMPAT_OLD
,
124 'read old' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
125 'read new' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
126 'new' => SCHEMA_COMPAT_NEW
,
127 ] as $stageName => $stage
129 foreach ( [ false, true ] as $reverse ) {
130 $name = $stageName . ( $reverse ?
', reverse' : '' );
131 yield
"Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
132 yield
"Named users including a no-edit user, $name" => [
133 $stage, [ 'ucuser' => $users2 ], $reverse, 6
135 yield
"IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
136 yield
"All users, $name" => [
137 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
139 yield
"User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
140 yield
"Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__
], $reverse, 9 ];
141 yield
"IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
147 * @dataProvider provideInterwikiUser
148 * @param int $stage SCHEMA_COMPAT constants for $wgActorTableSchemaMigrationStage
150 public function testInterwikiUser( $stage ) {
151 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
152 $this->overrideMwServices();
156 'list' => 'usercontribs',
157 'ucuser' => 'IW>' . __CLASS__
,
162 $apiResult = $this->doApiRequest( $params );
163 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
164 $this->assertArrayHasKey( 'query', $apiResult[0] );
165 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
169 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
171 $this->assertSame( 'IW>' . __CLASS__
, $page['user'], 'Correct user returned' );
172 $ids[] = $page['revid'];
174 $this->assertSame( 3, $count, 'Expected number of revisions' );
177 $this->assertSame( $sorted, $ids, "IDs are sorted" );
180 public static function provideInterwikiUser() {
182 'old' => [ SCHEMA_COMPAT_OLD
],
183 'read old' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
],
184 'read new' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
],
185 'new' => [ SCHEMA_COMPAT_NEW
],