924a1a5cd2d27514a9042e0150d6134766eecd74
[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 = SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD;
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 SCHEMA_COMPAT contants 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 // FIXME: fails under sqlite
54 $this->markTestSkippedIfDbType( 'sqlite' );
55
56 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
57 $this->overrideMwServices();
58
59 if ( isset( $params['ucuserids'] ) ) {
60 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
61 }
62 if ( isset( $params['ucuser'] ) ) {
63 $params['ucuser'] = implode( '|', $params['ucuser'] );
64 }
65
66 $sort = 'rsort';
67 if ( $reverse ) {
68 $params['ucdir'] = 'newer';
69 $sort = 'sort';
70 }
71
72 $params += [
73 'action' => 'query',
74 'list' => 'usercontribs',
75 'ucprop' => 'ids',
76 ];
77
78 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
79 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
80 $this->assertArrayHasKey( 'query', $apiResult[0] );
81 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
82
83 $count = 0;
84 $ids = [];
85 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
86 $count++;
87 $ids[$page['user']][] = $page['revid'];
88 }
89 $this->assertSame( $revs, $count, 'Expected number of revisions' );
90 foreach ( $ids as $user => $revids ) {
91 $sorted = $revids;
92 call_user_func_array( $sort, [ &$sorted ] );
93 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
94 }
95
96 for ( $limit = 1; $limit < $revs; $limit++ ) {
97 $continue = [];
98 $count = 0;
99 $batchedIds = [];
100 while ( $continue !== null ) {
101 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
102 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
103 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
104 "Batching with limit $limit" );
105 $continue = $apiResult[0]['continue'] ?? null;
106 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
107 $count++;
108 $batchedIds[$page['user']][] = $page['revid'];
109 }
110 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
111 }
112 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
113 }
114 }
115
116 public static function provideSorting() {
117 $users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
118 $users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
119 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
120
121 foreach (
122 [
123 'old' => SCHEMA_COMPAT_OLD,
124 'read old' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD,
125 'read new' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW,
126 'new' => SCHEMA_COMPAT_NEW,
127 ] as $stageName => $stage
128 ) {
129 foreach ( [ false, true ] as $reverse ) {
130 $name = $stageName . ( $reverse ? ', reverse' : '' );
131 yield "Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
132 yield "Named users including a no-edit user, $name" => [
133 $stage, [ 'ucuser' => $users2 ], $reverse, 6
134 ];
135 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
136 yield "All users, $name" => [
137 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
138 ];
139 yield "User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
140 yield "Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
141 yield "IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
142 }
143 }
144 }
145
146 /**
147 * @dataProvider provideInterwikiUser
148 * @param int $stage SCHEMA_COMPAT constants for $wgActorTableSchemaMigrationStage
149 */
150 public function testInterwikiUser( $stage ) {
151 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
152 $this->overrideMwServices();
153
154 $params = [
155 'action' => 'query',
156 'list' => 'usercontribs',
157 'ucuser' => 'IW>' . __CLASS__,
158 'ucprop' => 'ids',
159 'uclimit' => 'max',
160 ];
161
162 $apiResult = $this->doApiRequest( $params );
163 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
164 $this->assertArrayHasKey( 'query', $apiResult[0] );
165 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
166
167 $count = 0;
168 $ids = [];
169 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
170 $count++;
171 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
172 $ids[] = $page['revid'];
173 }
174 $this->assertSame( 3, $count, 'Expected number of revisions' );
175 $sorted = $ids;
176 rsort( $sorted );
177 $this->assertSame( $sorted, $ids, "IDs are sorted" );
178 }
179
180 public static function provideInterwikiUser() {
181 return [
182 'old' => [ SCHEMA_COMPAT_OLD ],
183 'read old' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD ],
184 'read new' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW ],
185 'new' => [ SCHEMA_COMPAT_NEW ],
186 ];
187 }
188
189 }