Merge "Bump wikimedia/parsoid to 0.21.0-a11"
[mediawiki.git] / tests / phpunit / maintenance / DeleteAutoPatrolLogsTest.php
blob5045cac2cdf4bc04c2567be7c36ca546bedb44a7
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use DeleteAutoPatrolLogs;
7 /**
8 * @group Database
9 * @covers \DeleteAutoPatrolLogs
11 class DeleteAutoPatrolLogsTest extends MaintenanceBaseTestCase {
13 public function getMaintenanceClass() {
14 return DeleteAutoPatrolLogs::class;
17 protected function setUp(): void {
18 parent::setUp();
20 $this->insertLoggingData();
23 private function insertLoggingData() {
24 $dbw = $this->getDb();
25 $logs = [];
27 $comment = \MediaWiki\MediaWikiServices::getInstance()->getCommentStore()
28 ->createComment( $dbw, '' );
30 // Manual patrolling
31 $logs[] = [
32 'log_type' => 'patrol',
33 'log_action' => 'patrol',
34 'log_actor' => 7251,
35 'log_params' => '',
36 'log_timestamp' => $dbw->timestamp( '20041223210426' ),
37 'log_namespace' => NS_MAIN,
38 'log_title' => 'DeleteAutoPatrolLogs',
39 'log_comment_id' => $comment->id,
42 // Autopatrol #1
43 $logs[] = [
44 'log_type' => 'patrol',
45 'log_action' => 'autopatrol',
46 'log_actor' => 7252,
47 'log_params' => '',
48 'log_timestamp' => $dbw->timestamp( '20051223210426' ),
49 'log_namespace' => NS_MAIN,
50 'log_title' => 'DeleteAutoPatrolLogs',
51 'log_comment_id' => $comment->id,
54 // Block
55 $logs[] = [
56 'log_type' => 'block',
57 'log_action' => 'block',
58 'log_actor' => 7253,
59 'log_params' => '',
60 'log_timestamp' => $dbw->timestamp( '20061223210426' ),
61 'log_namespace' => NS_MAIN,
62 'log_title' => 'DeleteAutoPatrolLogs',
63 'log_comment_id' => $comment->id,
66 // Very old/ invalid patrol
67 $logs[] = [
68 'log_type' => 'patrol',
69 'log_action' => 'patrol',
70 'log_actor' => 7253,
71 'log_params' => 'nanana',
72 'log_timestamp' => $dbw->timestamp( '20061223210426' ),
73 'log_namespace' => NS_MAIN,
74 'log_title' => 'DeleteAutoPatrolLogs',
75 'log_comment_id' => $comment->id,
78 // Autopatrol #2
79 $logs[] = [
80 'log_type' => 'patrol',
81 'log_action' => 'autopatrol',
82 'log_actor' => 7254,
83 'log_params' => '',
84 'log_timestamp' => $dbw->timestamp( '20071223210426' ),
85 'log_namespace' => NS_MAIN,
86 'log_title' => 'DeleteAutoPatrolLogs',
87 'log_comment_id' => $comment->id,
90 // Autopatrol #3 old way
91 $logs[] = [
92 'log_type' => 'patrol',
93 'log_action' => 'patrol',
94 'log_actor' => 7255,
95 'log_params' => serialize( [ '6::auto' => true ] ),
96 'log_timestamp' => $dbw->timestamp( '20081223210426' ),
97 'log_namespace' => NS_MAIN,
98 'log_title' => 'DeleteAutoPatrolLogs',
99 'log_comment_id' => $comment->id,
102 // Manual patrol #2 old way
103 $logs[] = [
104 'log_type' => 'patrol',
105 'log_action' => 'patrol',
106 'log_actor' => 7256,
107 'log_params' => serialize( [ '6::auto' => false ] ),
108 'log_timestamp' => $dbw->timestamp( '20091223210426' ),
109 'log_namespace' => NS_MAIN,
110 'log_title' => 'DeleteAutoPatrolLogs',
111 'log_comment_id' => $comment->id,
114 // Autopatrol #4 very old way
115 $logs[] = [
116 'log_type' => 'patrol',
117 'log_action' => 'patrol',
118 'log_actor' => 7257,
119 'log_params' => "9227851\n0\n1",
120 'log_timestamp' => $dbw->timestamp( '20081223210426' ),
121 'log_namespace' => NS_MAIN,
122 'log_title' => 'DeleteAutoPatrolLogs',
123 'log_comment_id' => $comment->id,
126 // Manual patrol #3 very old way
127 $logs[] = [
128 'log_type' => 'patrol',
129 'log_action' => 'patrol',
130 'log_actor' => 7258,
131 'log_params' => "9227851\n0\n0",
132 'log_timestamp' => $dbw->timestamp( '20091223210426' ),
133 'log_namespace' => NS_MAIN,
134 'log_title' => 'DeleteAutoPatrolLogs',
135 'log_comment_id' => $comment->id,
138 $dbw->newInsertQueryBuilder()
139 ->insertInto( 'logging' )
140 ->rows( $logs )
141 ->caller( __METHOD__ )
142 ->execute();
145 public static function runProvider() {
146 $allRows = [
147 (object)[
148 'log_type' => 'patrol',
149 'log_action' => 'patrol',
150 'log_actor' => '7251',
152 (object)[
153 'log_type' => 'patrol',
154 'log_action' => 'autopatrol',
155 'log_actor' => '7252',
157 (object)[
158 'log_type' => 'block',
159 'log_action' => 'block',
160 'log_actor' => '7253',
162 (object)[
163 'log_type' => 'patrol',
164 'log_action' => 'patrol',
165 'log_actor' => '7253',
167 (object)[
168 'log_type' => 'patrol',
169 'log_action' => 'autopatrol',
170 'log_actor' => '7254',
172 (object)[
173 'log_type' => 'patrol',
174 'log_action' => 'patrol',
175 'log_actor' => '7255',
177 (object)[
178 'log_type' => 'patrol',
179 'log_action' => 'patrol',
180 'log_actor' => '7256',
182 (object)[
183 'log_type' => 'patrol',
184 'log_action' => 'patrol',
185 'log_actor' => '7257',
187 (object)[
188 'log_type' => 'patrol',
189 'log_action' => 'patrol',
190 'log_actor' => '7258',
194 $cases = [
195 'dry run' => [
196 $allRows,
197 [ '--sleep', '0', '--dry-run', '-q' ]
199 'basic run' => [
201 $allRows[0],
202 $allRows[2],
203 $allRows[3],
204 $allRows[5],
205 $allRows[6],
206 $allRows[7],
207 $allRows[8],
209 [ '--sleep', '0', '-q' ]
211 'run with before' => [
213 $allRows[0],
214 $allRows[2],
215 $allRows[3],
216 $allRows[4],
217 $allRows[5],
218 $allRows[6],
219 $allRows[7],
220 $allRows[8],
222 [ '--sleep', '0', '--before', '20060123210426', '-q' ]
224 'run with check-old' => [
226 $allRows[0],
227 $allRows[1],
228 $allRows[2],
229 $allRows[3],
230 $allRows[4],
231 $allRows[6],
232 $allRows[8],
234 [ '--sleep', '0', '--check-old', '-q' ]
238 foreach ( $cases as $key => $case ) {
239 yield $key . '-batch-size-1' => [
240 $case[0],
241 array_merge( $case[1], [ '--batch-size', '1' ] )
243 yield $key . '-batch-size-5' => [
244 $case[0],
245 array_merge( $case[1], [ '--batch-size', '5' ] )
247 yield $key . '-batch-size-1000' => [
248 $case[0],
249 array_merge( $case[1], [ '--batch-size', '1000' ] )
255 * @dataProvider runProvider
257 public function testRun( $expected, $args ) {
258 $this->maintenance->loadWithArgv( $args );
260 $this->maintenance->execute();
262 $remainingLogs = $this->getDb()->newSelectQueryBuilder()
263 ->select( [ 'log_type', 'log_action', 'log_actor' ] )
264 ->from( 'logging' )
265 ->orderBy( 'log_id' )
266 ->caller( __METHOD__ )->fetchResultSet();
268 $this->assertEquals( $expected, iterator_to_array( $remainingLogs, false ) );
271 public function testFromId() {
272 $fromId = $this->getDb()->newSelectQueryBuilder()
273 ->select( 'log_id' )
274 ->from( 'logging' )
275 ->where( [ 'log_params' => 'nanana' ] )
276 ->fetchField();
278 $this->maintenance->loadWithArgv( [ '--sleep', '0', '--from-id', strval( $fromId ), '-q' ] );
280 $this->maintenance->execute();
282 $remainingLogs = $this->getDb()->newSelectQueryBuilder()
283 ->select( [ 'log_type', 'log_action', 'log_actor' ] )
284 ->from( 'logging' )
285 ->orderBy( 'log_id' )
286 ->caller( __METHOD__ )->fetchResultSet();
288 $deleted = [
289 'log_type' => 'patrol',
290 'log_action' => 'autopatrol',
291 'log_actor' => '7254',
293 $notDeleted = [
294 'log_type' => 'patrol',
295 'log_action' => 'autopatrol',
296 'log_actor' => '7252',
299 $remainingLogs = array_map(
300 static function ( $val ) {
301 return (array)$val;
303 iterator_to_array( $remainingLogs, false )
306 $this->assertNotContains( $deleted, $remainingLogs );
307 $this->assertContains( $notDeleted, $remainingLogs );