summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/api/query/ApiQueryUserContributionsTest.php
blob: ca6a929ac8e8b02a45371feb6a21fac65e5cb5b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

/**
 * @group API
 * @group Database
 * @group medium
 * @covers ApiQueryContributions
 */
class ApiQueryContributionsTest extends ApiTestCase {
	public function addDBDataOnce() {
		global $wgActorTableSchemaMigrationStage;

		$reset = new \Wikimedia\ScopedCallback( function ( $v ) {
			global $wgActorTableSchemaMigrationStage;
			$wgActorTableSchemaMigrationStage = $v;
			$this->overrideMwServices();
		}, [ $wgActorTableSchemaMigrationStage ] );
		$wgActorTableSchemaMigrationStage = MIGRATION_WRITE_BOTH;
		$this->overrideMwServices();

		$users = [
			User::newFromName( '192.168.2.2', false ),
			User::newFromName( '192.168.2.1', false ),
			User::newFromName( '192.168.2.3', false ),
			User::createNew( __CLASS__ . ' B' ),
			User::createNew( __CLASS__ . ' A' ),
			User::createNew( __CLASS__ . ' C' ),
			User::newFromName( 'IW>' . __CLASS__, false ),
		];

		$title = Title::newFromText( __CLASS__ );
		$page = WikiPage::factory( $title );
		for ( $i = 0; $i < 3; $i++ ) {
			foreach ( array_reverse( $users ) as $user ) {
				$status = $page->doEditContent(
					ContentHandler::makeContent( "Test revision $user #$i", $title ), 'Test edit', 0, false, $user
				);
				if ( !$status->isOK() ) {
					$this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
				}
			}
		}
	}

	/**
	 * @dataProvider provideSorting
	 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
	 * @param array $params Extra parameters for the query
	 * @param bool $reverse Reverse order?
	 * @param int $revs Number of revisions to expect
	 */
	public function testSorting( $stage, $params, $reverse, $revs ) {
		if ( isset( $params['ucuserprefix'] ) &&
			( $stage === MIGRATION_WRITE_BOTH || $stage === MIGRATION_WRITE_NEW ) &&
			$this->db->getType() === 'mysql' && $this->usesTemporaryTables()
		) {
			// https://bugs.mysql.com/bug.php?id=10327
			$this->markTestSkipped( 'MySQL bug 10327 - can\'t reopen temporary tables' );
		}

		$this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
		$this->overrideMwServices();

		if ( isset( $params['ucuserids'] ) ) {
			$params['ucuserids'] = implode( '|', array_map( 'User::idFromName', $params['ucuserids'] ) );
		}
		if ( isset( $params['ucuser'] ) ) {
			$params['ucuser'] = implode( '|', $params['ucuser'] );
		}

		$sort = 'rsort';
		if ( $reverse ) {
			$params['ucdir'] = 'newer';
			$sort = 'sort';
		}

		$params += [
			'action' => 'query',
			'list' => 'usercontribs',
			'ucprop' => 'ids',
		];

		$apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
		$this->assertArrayNotHasKey( 'continue', $apiResult[0] );
		$this->assertArrayHasKey( 'query', $apiResult[0] );
		$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );

		$count = 0;
		$ids = [];
		foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
			$count++;
			$ids[$page['user']][] = $page['revid'];
		}
		$this->assertSame( $revs, $count, 'Expected number of revisions' );
		foreach ( $ids as $user => $revids ) {
			$sorted = $revids;
			call_user_func_array( $sort, [ &$sorted ] );
			$this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
		}

		for ( $limit = 1; $limit < $revs; $limit++ ) {
			$continue = [];
			$count = 0;
			$batchedIds = [];
			while ( $continue !== null ) {
				$apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
				$this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
				$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
					"Batching with limit $limit" );
				$continue = isset( $apiResult[0]['continue'] ) ? $apiResult[0]['continue'] : null;
				foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
					$count++;
					$batchedIds[$page['user']][] = $page['revid'];
				}
				$this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
			}
			$this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
		}
	}

	public static function provideSorting() {
		$users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
		$users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
		$ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];

		foreach (
			[
				'old' => MIGRATION_OLD,
				'write both' => MIGRATION_WRITE_BOTH,
				'write new' => MIGRATION_WRITE_NEW,
				'new' => MIGRATION_NEW,
			] as $stageName => $stage
		) {
			foreach ( [ false, true ] as $reverse ) {
				$name = $stageName . ( $reverse ? ', reverse' : '' );
				yield "Named users, $name" => [ $stage, [ 'ucuser' => $users ], $reverse, 9 ];
				yield "Named users including a no-edit user, $name" => [
					$stage, [ 'ucuser' => $users2 ], $reverse, 6
				];
				yield "IP users, $name" => [ $stage, [ 'ucuser' => $ips ], $reverse, 9 ];
				yield "All users, $name" => [
					$stage, [ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
				];
				yield "User IDs, $name" => [ $stage, [ 'ucuserids' => $users ], $reverse, 9 ];
				yield "Users by prefix, $name" => [ $stage, [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
				yield "IPs by prefix, $name" => [ $stage, [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
			}
		}
	}

	/**
	 * @dataProvider provideInterwikiUser
	 * @param int $stage One of the MIGRATION_* constants for $wgActorTableSchemaMigrationStage
	 */
	public function testInterwikiUser( $stage ) {
		$this->setMwGlobals( 'wgActorTableSchemaMigrationStage', $stage );
		$this->overrideMwServices();

		$params = [
			'action' => 'query',
			'list' => 'usercontribs',
			'ucuser' => 'IW>' . __CLASS__,
			'ucprop' => 'ids',
			'uclimit' => 'max',
		];

		$apiResult = $this->doApiRequest( $params );
		$this->assertArrayNotHasKey( 'continue', $apiResult[0] );
		$this->assertArrayHasKey( 'query', $apiResult[0] );
		$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );

		$count = 0;
		$ids = [];
		foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
			$count++;
			$this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
			$ids[] = $page['revid'];
		}
		$this->assertSame( 3, $count, 'Expected number of revisions' );
		$sorted = $ids;
		rsort( $sorted );
		$this->assertSame( $sorted, $ids, "IDs are sorted" );
	}

	public static function provideInterwikiUser() {
		return [
			'old' => [ MIGRATION_OLD ],
			'write both' => [ MIGRATION_WRITE_BOTH ],
			'write new' => [ MIGRATION_WRITE_NEW ],
			'new' => [ MIGRATION_NEW ],
		];
	}

}