ac8637781a3c2ff30d9739cd1f881e49ef883f8c
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' );
60 // FIXME: fails under sqlite
61 $this->markTestSkippedIfDbType( 'sqlite' );
63 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
64 $this->overrideMwServices();
66 if ( isset( $params['ucuserids'] ) ) {
67 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
69 if ( isset( $params['ucuser'] ) ) {
70 $params['ucuser'] = implode( '|', $params['ucuser'] );
75 $params['ucdir'] = 'newer';
81 'list' => 'usercontribs',
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'] );
92 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
94 $ids[$page['user']][] = $page['revid'];
96 $this->assertSame( $revs, $count, 'Expected number of revisions' );
97 foreach ( $ids as $user => $revids ) {
99 call_user_func_array( $sort, [ &$sorted ] );
100 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
103 for ( $limit = 1; $limit < $revs; $limit++
) {
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 ) {
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' ];
130 'old' => MIGRATION_OLD
,
131 'write both' => MIGRATION_WRITE_BOTH
,
132 'write new' => MIGRATION_WRITE_NEW
,
133 'new' => MIGRATION_NEW
,
134 ] as $stageName => $stage
136 foreach ( [ false, true ] as $reverse ) {
137 $name = $stageName . ( $reverse ?
', reverse' : '' );
138 yield
"Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
139 yield
"Named users including a no-edit user, $name" => [
140 $stage, [ 'ucuser' => $users2 ], $reverse, 6
142 yield
"IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
143 yield
"All users, $name" => [
144 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
146 yield
"User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
147 yield
"Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__
], $reverse, 9 ];
148 yield
"IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
154 * @dataProvider provideInterwikiUser
155 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
157 public function testInterwikiUser( $stage ) {
158 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
159 $this->overrideMwServices();
163 'list' => 'usercontribs',
164 'ucuser' => 'IW>' . __CLASS__
,
169 $apiResult = $this->doApiRequest( $params );
170 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
171 $this->assertArrayHasKey( 'query', $apiResult[0] );
172 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
176 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
178 $this->assertSame( 'IW>' . __CLASS__
, $page['user'], 'Correct user returned' );
179 $ids[] = $page['revid'];
181 $this->assertSame( 3, $count, 'Expected number of revisions' );
184 $this->assertSame( $sorted, $ids, "IDs are sorted" );
187 public static function provideInterwikiUser() {
189 'old' => [ MIGRATION_OLD
],
190 'write both' => [ MIGRATION_WRITE_BOTH
],
191 'write new' => [ MIGRATION_WRITE_NEW
],
192 'new' => [ MIGRATION_NEW
],