ac8637781a3c2ff30d9739cd1f881e49ef883f8c
[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 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 // FIXME: fails under sqlite
61 $this->markTestSkippedIfDbType( 'sqlite' );
62
63 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
64 $this->overrideMwServices();
65
66 if ( isset( $params['ucuserids'] ) ) {
67 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
68 }
69 if ( isset( $params['ucuser'] ) ) {
70 $params['ucuser'] = implode( '|', $params['ucuser'] );
71 }
72
73 $sort = 'rsort';
74 if ( $reverse ) {
75 $params['ucdir'] = 'newer';
76 $sort = 'sort';
77 }
78
79 $params += [
80 'action' => 'query',
81 'list' => 'usercontribs',
82 'ucprop' => 'ids',
83 ];
84
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'] );
89
90 $count = 0;
91 $ids = [];
92 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
93 $count++;
94 $ids[$page['user']][] = $page['revid'];
95 }
96 $this->assertSame( $revs, $count, 'Expected number of revisions' );
97 foreach ( $ids as $user => $revids ) {
98 $sorted = $revids;
99 call_user_func_array( $sort, [ &$sorted ] );
100 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
101 }
102
103 for ( $limit = 1; $limit < $revs; $limit++ ) {
104 $continue = [];
105 $count = 0;
106 $batchedIds = [];
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 ) {
114 $count++;
115 $batchedIds[$page['user']][] = $page['revid'];
116 }
117 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
118 }
119 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
120 }
121 }
122
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' ];
127
128 foreach (
129 [
130 'old' => MIGRATION_OLD,
131 'write both' => MIGRATION_WRITE_BOTH,
132 'write new' => MIGRATION_WRITE_NEW,
133 'new' => MIGRATION_NEW,
134 ] as $stageName => $stage
135 ) {
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
141 ];
142 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
143 yield "All users, $name" => [
144 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
145 ];
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 ];
149 }
150 }
151 }
152
153 /**
154 * @dataProvider provideInterwikiUser
155 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
156 */
157 public function testInterwikiUser( $stage ) {
158 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
159 $this->overrideMwServices();
160
161 $params = [
162 'action' => 'query',
163 'list' => 'usercontribs',
164 'ucuser' => 'IW>' . __CLASS__,
165 'ucprop' => 'ids',
166 'uclimit' => 'max',
167 ];
168
169 $apiResult = $this->doApiRequest( $params );
170 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
171 $this->assertArrayHasKey( 'query', $apiResult[0] );
172 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
173
174 $count = 0;
175 $ids = [];
176 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
177 $count++;
178 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
179 $ids[] = $page['revid'];
180 }
181 $this->assertSame( 3, $count, 'Expected number of revisions' );
182 $sorted = $ids;
183 rsort( $sorted );
184 $this->assertSame( $sorted, $ids, "IDs are sorted" );
185 }
186
187 public static function provideInterwikiUser() {
188 return [
189 'old' => [ MIGRATION_OLD ],
190 'write both' => [ MIGRATION_WRITE_BOTH ],
191 'write new' => [ MIGRATION_WRITE_NEW ],
192 'new' => [ MIGRATION_NEW ],
193 ];
194 }
195
196 }