7 * @covers ApiQueryContributions
9 class ApiQueryContributionsTest
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' ),
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 One of the MIGRATION_* constants 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 if ( isset( $params['ucuserprefix'] ) &&
53 ( $stage === MIGRATION_WRITE_BOTH ||
$stage === MIGRATION_WRITE_NEW
) &&
54 $this->db
->getType() === 'mysql' && $this->usesTemporaryTables()
56 // https://bugs.mysql.com/bug.php?id=10327
57 $this->markTestSkipped( 'MySQL bug 10327 - can\'t reopen temporary tables' );
60 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
61 $this->overrideMwServices();
63 if ( isset( $params['ucuserids'] ) ) {
64 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
66 if ( isset( $params['ucuser'] ) ) {
67 $params['ucuser'] = implode( '|', $params['ucuser'] );
72 $params['ucdir'] = 'newer';
78 'list' => 'usercontribs',
82 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => 500 ] );
83 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
84 $this->assertArrayHasKey( 'query', $apiResult[0] );
85 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
89 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
91 $ids[$page['user']][] = $page['revid'];
93 $this->assertSame( $revs, $count, 'Expected number of revisions' );
94 foreach ( $ids as $user => $revids ) {
96 call_user_func_array( $sort, [ &$sorted ] );
97 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
100 for ( $limit = 1; $limit < $revs; $limit++
) {
104 while ( $continue !== null ) {
105 $apiResult = $this->doApiRequest( $params +
[ 'uclimit' => $limit ] +
$continue );
106 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
107 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
108 "Batching with limit $limit" );
109 $continue = isset( $apiResult[0]['continue'] ) ?
$apiResult[0]['continue'] : null;
110 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
112 $batchedIds[$page['user']][] = $page['revid'];
114 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
116 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
120 public static function provideSorting() {
121 $users = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' C' ];
122 $users2 = [ __CLASS__
. ' A', __CLASS__
. ' B', __CLASS__
. ' D' ];
123 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
127 'old' => MIGRATION_OLD
,
128 'write both' => MIGRATION_WRITE_BOTH
,
129 'write new' => MIGRATION_WRITE_NEW
,
130 'new' => MIGRATION_NEW
,
131 ] as $stageName => $stage
133 foreach ( [ false, true ] as $reverse ) {
134 $name = $stageName . ( $reverse ?
', reverse' : '' );
135 yield
"Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
136 yield
"Named users including a no-edit user, $name" => [
137 $stage, [ 'ucuser' => $users2 ], $reverse, 6
139 yield
"IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
140 yield
"All users, $name" => [
141 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
143 yield
"User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
144 yield
"Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__
], $reverse, 9 ];
145 yield
"IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];