Add `actor` table and code to start using it
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryUserContributionsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @covers ApiQueryContributions
8 */
9 class ApiQueryContributionsTest extends ApiTestCase {
10 public function addDBDataOnce() {
11 global $wgActorTableSchemaMigrationStage;
12
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();
20
21 $users = [
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 ];
29
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
36 );
37 if ( !$status->isOK() ) {
38 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
39 }
40 }
41 }
42 }
43
44 /**
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
50 */
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()
55 ) {
56 // https://bugs.mysql.com/bug.php?id=10327
57 $this->markTestSkipped( 'MySQL bug 10327 - can\'t reopen temporary tables' );
58 }
59
60 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
61 $this->overrideMwServices();
62
63 if ( isset( $params['ucuserids'] ) ) {
64 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
65 }
66 if ( isset( $params['ucuser'] ) ) {
67 $params['ucuser'] = implode( '|', $params['ucuser'] );
68 }
69
70 $sort = 'rsort';
71 if ( $reverse ) {
72 $params['ucdir'] = 'newer';
73 $sort = 'sort';
74 }
75
76 $params += [
77 'action' => 'query',
78 'list' => 'usercontribs',
79 'ucprop' => 'ids',
80 ];
81
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'] );
86
87 $count = 0;
88 $ids = [];
89 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
90 $count++;
91 $ids[$page['user']][] = $page['revid'];
92 }
93 $this->assertSame( $revs, $count, 'Expected number of revisions' );
94 foreach ( $ids as $user => $revids ) {
95 $sorted = $revids;
96 call_user_func_array( $sort, [ &$sorted ] );
97 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
98 }
99
100 for ( $limit = 1; $limit < $revs; $limit++ ) {
101 $continue = [];
102 $count = 0;
103 $batchedIds = [];
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 ) {
111 $count++;
112 $batchedIds[$page['user']][] = $page['revid'];
113 }
114 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
115 }
116 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
117 }
118 }
119
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' ];
124
125 foreach (
126 [
127 'old' => MIGRATION_OLD,
128 'write both' => MIGRATION_WRITE_BOTH,
129 'write new' => MIGRATION_WRITE_NEW,
130 'new' => MIGRATION_NEW,
131 ] as $stageName => $stage
132 ) {
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
138 ];
139 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
140 yield "All users, $name" => [
141 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
142 ];
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 ];
146 }
147 }
148 }
149 }