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 = MIGRATION_WRITE_BOTH
;
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 One of the MIGRATION_* constants 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 if ( isset( $params['ucuserprefix'] ) &&
54 ( $stage === MIGRATION_WRITE_BOTH ||
$stage === MIGRATION_WRITE_NEW
) &&
55 $this->db
->getType() === 'mysql' && $this->usesTemporaryTables()
57 // https://bugs.mysql.com/bug.php?id=10327
58 $this->markTestSkipped( 'MySQL bug 10327 - can\'t reopen temporary tables' );
61 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
62 $this->overrideMwServices();
64 if ( isset( $params['ucuserids'] ) ) {
65 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
67 if ( isset( $params['ucuser'] ) ) {
68 $params['ucuser'] = implode( '|', $params['ucuser'] );
73 $params['ucdir'] = 'newer';
79 'list' => 'usercontribs',
83 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => 500 ] );
84 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
85 $this->assertArrayHasKey( 'query', $apiResult[0] );
86 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
90 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
92 $ids[$page['user']][] = $page['revid'];
94 $this->assertSame( $revs, $count, 'Expected number of revisions' );
95 foreach ( $ids as $user => $revids ) {
97 call_user_func_array( $sort, [ &$sorted ] );
98 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
101 for ( $limit = 1; $limit < $revs; $limit++
) {
105 while ( $continue !== null ) {
106 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => $limit ] +
$continue );
107 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
108 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
109 "Batching with limit $limit" );
110 $continue = isset( $apiResult[0]['continue'] ) ?
$apiResult[0]['continue'] : null;
111 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
113 $batchedIds[$page['user']][] = $page['revid'];
115 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
117 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
121 public static function provideSorting() {
122 $users = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' C' ];
123 $users2 = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' D' ];
124 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
128 'old' => MIGRATION_OLD
,
129 'write both' => MIGRATION_WRITE_BOTH
,
130 'write new' => MIGRATION_WRITE_NEW
,
131 'new' => MIGRATION_NEW
,
132 ] as $stageName => $stage
134 foreach ( [ false, true ] as $reverse ) {
135 $name = $stageName . ( $reverse ?
', reverse' : '' );
136 yield
"Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
137 yield
"Named users including a no-edit user, $name" => [
138 $stage, [ 'ucuser' => $users2 ], $reverse, 6
140 yield
"IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
141 yield
"All users, $name" => [
142 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
144 yield
"User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
145 yield
"Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__
], $reverse, 9 ];
146 yield
"IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
152 * @dataProvider provideInterwikiUser
153 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
155 public function testInterwikiUser( $stage ) {
156 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
157 $this->overrideMwServices();
161 'list' => 'usercontribs',
162 'ucuser' => 'IW>' . __CLASS__
,
167 $apiResult = $this->doApiRequest( $params );
168 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
169 $this->assertArrayHasKey( 'query', $apiResult[0] );
170 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
174 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
176 $this->assertSame( 'IW>' . __CLASS__
, $page['user'], 'Correct user returned' );
177 $ids[] = $page['revid'];
179 $this->assertSame( 3, $count, 'Expected number of revisions' );
182 $this->assertSame( $sorted, $ids, "IDs are sorted" );
185 public static function provideInterwikiUser() {
187 'old' => [ MIGRATION_OLD
],
188 'write both' => [ MIGRATION_WRITE_BOTH
],
189 'write new' => [ MIGRATION_WRITE_NEW
],
190 'new' => [ MIGRATION_NEW
],