Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryUserContribsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 * @covers ApiQueryUserContribs
8 */
9 class ApiQueryUserContribsTest extends ApiTestCase {
10 public function addDBDataOnce() {
11 global $wgActorTableSchemaMigrationStage;
12
13 $reset = new \Wikimedia\ScopedCallback( function ( $v ) {
14 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $v );
15 }, [ $wgActorTableSchemaMigrationStage ] );
16 // Needs to WRITE_BOTH so READ_OLD tests below work. READ mode here doesn't really matter.
17 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage',
18 SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW );
19
20 $users = [
21 User::newFromName( '192.168.2.2', false ),
22 User::newFromName( '192.168.2.1', false ),
23 User::newFromName( '192.168.2.3', false ),
24 User::createNew( __CLASS__ . ' B' ),
25 User::createNew( __CLASS__ . ' A' ),
26 User::createNew( __CLASS__ . ' C' ),
27 User::newFromName( 'IW>' . __CLASS__, false ),
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 SCHEMA_COMPAT contants 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 // FIXME: fails under sqlite
53 $this->markTestSkippedIfDbType( 'sqlite' );
54
55 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
56
57 if ( isset( $params['ucuserids'] ) ) {
58 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
59 }
60 if ( isset( $params['ucuser'] ) ) {
61 $params['ucuser'] = implode( '|', $params['ucuser'] );
62 }
63
64 $sort = 'rsort';
65 if ( $reverse ) {
66 $params['ucdir'] = 'newer';
67 $sort = 'sort';
68 }
69
70 $params += [
71 'action' => 'query',
72 'list' => 'usercontribs',
73 'ucprop' => 'ids',
74 ];
75
76 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
77 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
78 $this->assertArrayHasKey( 'query', $apiResult[0] );
79 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
80
81 $count = 0;
82 $ids = [];
83 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
84 $count++;
85 $ids[$page['user']][] = $page['revid'];
86 }
87 $this->assertSame( $revs, $count, 'Expected number of revisions' );
88 foreach ( $ids as $user => $revids ) {
89 $sorted = $revids;
90 call_user_func_array( $sort, [ &$sorted ] );
91 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
92 }
93
94 for ( $limit = 1; $limit < $revs; $limit++ ) {
95 $continue = [];
96 $count = 0;
97 $batchedIds = [];
98 while ( $continue !== null ) {
99 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
100 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
101 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
102 "Batching with limit $limit" );
103 $continue = $apiResult[0]['continue'] ?? null;
104 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
105 $count++;
106 $batchedIds[$page['user']][] = $page['revid'];
107 }
108 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
109 }
110 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
111 }
112 }
113
114 public static function provideSorting() {
115 $users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
116 $users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
117 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
118
119 foreach (
120 [
121 'old' => SCHEMA_COMPAT_OLD,
122 'read old' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD,
123 'read new' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW,
124 'new' => SCHEMA_COMPAT_NEW,
125 ] as $stageName => $stage
126 ) {
127 foreach ( [ false, true ] as $reverse ) {
128 $name = $stageName . ( $reverse ? ', reverse' : '' );
129 yield "Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
130 yield "Named users including a no-edit user, $name" => [
131 $stage, [ 'ucuser' => $users2 ], $reverse, 6
132 ];
133 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
134 yield "All users, $name" => [
135 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
136 ];
137 yield "User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
138 yield "Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
139 yield "IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
140 }
141 }
142 }
143
144 /**
145 * @dataProvider provideInterwikiUser
146 * @param int $stage SCHEMA_COMPAT constants for $wgActorTableSchemaMigrationStage
147 */
148 public function testInterwikiUser( $stage ) {
149 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
150
151 $params = [
152 'action' => 'query',
153 'list' => 'usercontribs',
154 'ucuser' => 'IW>' . __CLASS__,
155 'ucprop' => 'ids',
156 'uclimit' => 'max',
157 ];
158
159 $apiResult = $this->doApiRequest( $params );
160 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
161 $this->assertArrayHasKey( 'query', $apiResult[0] );
162 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
163
164 $count = 0;
165 $ids = [];
166 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
167 $count++;
168 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
169 $ids[] = $page['revid'];
170 }
171 $this->assertSame( 3, $count, 'Expected number of revisions' );
172 $sorted = $ids;
173 rsort( $sorted );
174 $this->assertSame( $sorted, $ids, "IDs are sorted" );
175 }
176
177 public static function provideInterwikiUser() {
178 return [
179 'old' => [ SCHEMA_COMPAT_OLD ],
180 'read old' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD ],
181 'read new' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW ],
182 'new' => [ SCHEMA_COMPAT_NEW ],
183 ];
184 }
185
186 }