ca6a929ac8e8b02a45371feb6a21fac65e5cb5b6
[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 User::newFromName( 'IW>' . __CLASS__, false ),
29 ];
30
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
37 );
38 if ( !$status->isOK() ) {
39 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
40 }
41 }
42 }
43 }
44
45 /**
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
51 */
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()
56 ) {
57 // https://bugs.mysql.com/bug.php?id=10327
58 $this->markTestSkipped( 'MySQL bug 10327 - can\'t reopen temporary tables' );
59 }
60
61 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
62 $this->overrideMwServices();
63
64 if ( isset( $params['ucuserids'] ) ) {
65 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
66 }
67 if ( isset( $params['ucuser'] ) ) {
68 $params['ucuser'] = implode( '|', $params['ucuser'] );
69 }
70
71 $sort = 'rsort';
72 if ( $reverse ) {
73 $params['ucdir'] = 'newer';
74 $sort = 'sort';
75 }
76
77 $params += [
78 'action' => 'query',
79 'list' => 'usercontribs',
80 'ucprop' => 'ids',
81 ];
82
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'] );
87
88 $count = 0;
89 $ids = [];
90 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
91 $count++;
92 $ids[$page['user']][] = $page['revid'];
93 }
94 $this->assertSame( $revs, $count, 'Expected number of revisions' );
95 foreach ( $ids as $user => $revids ) {
96 $sorted = $revids;
97 call_user_func_array( $sort, [ &$sorted ] );
98 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
99 }
100
101 for ( $limit = 1; $limit < $revs; $limit++ ) {
102 $continue = [];
103 $count = 0;
104 $batchedIds = [];
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 ) {
112 $count++;
113 $batchedIds[$page['user']][] = $page['revid'];
114 }
115 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
116 }
117 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
118 }
119 }
120
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' ];
125
126 foreach (
127 [
128 'old' => MIGRATION_OLD,
129 'write both' => MIGRATION_WRITE_BOTH,
130 'write new' => MIGRATION_WRITE_NEW,
131 'new' => MIGRATION_NEW,
132 ] as $stageName => $stage
133 ) {
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
139 ];
140 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
141 yield "All users, $name" => [
142 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
143 ];
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 ];
147 }
148 }
149 }
150
151 /**
152 * @dataProvider provideInterwikiUser
153 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
154 */
155 public function testInterwikiUser( $stage ) {
156 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
157 $this->overrideMwServices();
158
159 $params = [
160 'action' => 'query',
161 'list' => 'usercontribs',
162 'ucuser' => 'IW>' . __CLASS__,
163 'ucprop' => 'ids',
164 'uclimit' => 'max',
165 ];
166
167 $apiResult = $this->doApiRequest( $params );
168 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
169 $this->assertArrayHasKey( 'query', $apiResult[0] );
170 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
171
172 $count = 0;
173 $ids = [];
174 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
175 $count++;
176 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
177 $ids[] = $page['revid'];
178 }
179 $this->assertSame( 3, $count, 'Expected number of revisions' );
180 $sorted = $ids;
181 rsort( $sorted );
182 $this->assertSame( $sorted, $ids, "IDs are sorted" );
183 }
184
185 public static function provideInterwikiUser() {
186 return [
187 'old' => [ MIGRATION_OLD ],
188 'write both' => [ MIGRATION_WRITE_BOTH ],
189 'write new' => [ MIGRATION_WRITE_NEW ],
190 'new' => [ MIGRATION_NEW ],
191 ];
192 }
193
194 }