Add ext-dom to composer.json
[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 // Needs to WRITE_BOTH so READ_OLD tests below work. READ mode here doesn't really matter.
19 $wgActorTableSchemaMigrationStage = SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW;
20 $this->overrideMwServices();
21
22 $users = [
23 User::newFromName( '192.168.2.2', false ),
24 User::newFromName( '192.168.2.1', false ),
25 User::newFromName( '192.168.2.3', false ),
26 User::createNew( __CLASS__ . ' B' ),
27 User::createNew( __CLASS__ . ' A' ),
28 User::createNew( __CLASS__ . ' C' ),
29 User::newFromName( 'IW>' . __CLASS__, false ),
30 ];
31
32 $title = Title::newFromText( __CLASS__ );
33 $page = WikiPage::factory( $title );
34 for ( $i = 0; $i < 3; $i++ ) {
35 foreach ( array_reverse( $users ) as $user ) {
36 $status = $page->doEditContent(
37 ContentHandler::makeContent( "Test revision $user #$i", $title ), 'Test edit', 0, false, $user
38 );
39 if ( !$status->isOK() ) {
40 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
41 }
42 }
43 }
44 }
45
46 /**
47 * @dataProvider provideSorting
48 * @param int $stage SCHEMA_COMPAT contants for $wgActorTableSchemaMigrationStage
49 * @param array $params Extra parameters for the query
50 * @param bool $reverse Reverse order?
51 * @param int $revs Number of revisions to expect
52 */
53 public function testSorting( $stage, $params, $reverse, $revs ) {
54 // FIXME: fails under sqlite
55 $this->markTestSkippedIfDbType( 'sqlite' );
56
57 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
58 $this->overrideMwServices();
59
60 if ( isset( $params['ucuserids'] ) ) {
61 $params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
62 }
63 if ( isset( $params['ucuser'] ) ) {
64 $params['ucuser'] = implode( '|', $params['ucuser'] );
65 }
66
67 $sort = 'rsort';
68 if ( $reverse ) {
69 $params['ucdir'] = 'newer';
70 $sort = 'sort';
71 }
72
73 $params += [
74 'action' => 'query',
75 'list' => 'usercontribs',
76 'ucprop' => 'ids',
77 ];
78
79 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
80 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
81 $this->assertArrayHasKey( 'query', $apiResult[0] );
82 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
83
84 $count = 0;
85 $ids = [];
86 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
87 $count++;
88 $ids[$page['user']][] = $page['revid'];
89 }
90 $this->assertSame( $revs, $count, 'Expected number of revisions' );
91 foreach ( $ids as $user => $revids ) {
92 $sorted = $revids;
93 call_user_func_array( $sort, [ &$sorted ] );
94 $this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
95 }
96
97 for ( $limit = 1; $limit < $revs; $limit++ ) {
98 $continue = [];
99 $count = 0;
100 $batchedIds = [];
101 while ( $continue !== null ) {
102 $apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
103 $this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
104 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
105 "Batching with limit $limit" );
106 $continue = $apiResult[0]['continue'] ?? null;
107 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
108 $count++;
109 $batchedIds[$page['user']][] = $page['revid'];
110 }
111 $this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
112 }
113 $this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
114 }
115 }
116
117 public static function provideSorting() {
118 $users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
119 $users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
120 $ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
121
122 foreach (
123 [
124 'old' => SCHEMA_COMPAT_OLD,
125 'read old' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD,
126 'read new' => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW,
127 'new' => SCHEMA_COMPAT_NEW,
128 ] as $stageName => $stage
129 ) {
130 foreach ( [ false, true ] as $reverse ) {
131 $name = $stageName . ( $reverse ? ', reverse' : '' );
132 yield "Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
133 yield "Named users including a no-edit user, $name" => [
134 $stage, [ 'ucuser' => $users2 ], $reverse, 6
135 ];
136 yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
137 yield "All users, $name" => [
138 $stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
139 ];
140 yield "User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
141 yield "Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
142 yield "IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
143 }
144 }
145 }
146
147 /**
148 * @dataProvider provideInterwikiUser
149 * @param int $stage SCHEMA_COMPAT constants for $wgActorTableSchemaMigrationStage
150 */
151 public function testInterwikiUser( $stage ) {
152 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
153 $this->overrideMwServices();
154
155 $params = [
156 'action' => 'query',
157 'list' => 'usercontribs',
158 'ucuser' => 'IW>' . __CLASS__,
159 'ucprop' => 'ids',
160 'uclimit' => 'max',
161 ];
162
163 $apiResult = $this->doApiRequest( $params );
164 $this->assertArrayNotHasKey( 'continue', $apiResult[0] );
165 $this->assertArrayHasKey( 'query', $apiResult[0] );
166 $this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
167
168 $count = 0;
169 $ids = [];
170 foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
171 $count++;
172 $this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
173 $ids[] = $page['revid'];
174 }
175 $this->assertSame( 3, $count, 'Expected number of revisions' );
176 $sorted = $ids;
177 rsort( $sorted );
178 $this->assertSame( $sorted, $ids, "IDs are sorted" );
179 }
180
181 public static function provideInterwikiUser() {
182 return [
183 'old' => [ SCHEMA_COMPAT_OLD ],
184 'read old' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD ],
185 'read new' => [ SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW ],
186 'new' => [ SCHEMA_COMPAT_NEW ],
187 ];
188 }
189
190 }