| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\BlockUser; |
| 4 |
use MediaWiki\Content\Content; |
| 5 |
use MediaWiki\Content\ContentHandler; |
| 6 |
use MediaWiki\Content\JsonContent; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Context\RequestContext; |
| 9 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices; |
| 10 |
use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
| 11 |
use MediaWiki\Extension\AbuseFilter\FilterRunnerFactory; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Hooks\Handlers\FilteredActionsHandler; |
| 13 |
use MediaWiki\Extension\AbuseFilter\Parser\AFPData; |
| 14 |
use MediaWiki\Json\FormatJson; |
| 15 |
use MediaWiki\MainConfigNames; |
| 16 |
use MediaWiki\MediaWikiServices; |
| 17 |
use MediaWiki\Status\Status; |
| 18 |
use MediaWiki\Storage\PageEditStash; |
| 19 |
use MediaWiki\Title\Title; |
| 20 |
use MediaWiki\User\User; |
| 21 |
use MediaWiki\WikiMap\WikiMap; |
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 24 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 25 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 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 |
class AbuseFilterConsequencesTest extends MediaWikiIntegrationTestCase { |
| 78 |
use AbuseFilterCreateAccountTestTrait; |
| 79 |
use AbuseFilterUploadTestTrait; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private $user; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private const FILTERS = [ |
| 89 |
1 => [ |
| 90 |
'af_pattern' => 'added_lines irlike "foo"', |
| 91 |
'af_public_comments' => 'Mock filter for edit', |
| 92 |
'actions' => [ |
| 93 |
'warn' => [ |
| 94 |
'abusefilter-my-warning' |
| 95 |
], |
| 96 |
'tag' => [ |
| 97 |
'filtertag' |
| 98 |
] |
| 99 |
] |
| 100 |
], |
| 101 |
2 => [ |
| 102 |
'af_pattern' => 'action = "move" & moved_to_title contains "test" & moved_to_title === moved_to_text', |
| 103 |
'af_public_comments' => 'Mock filter for move', |
| 104 |
'af_hidden' => Flags::FILTER_HIDDEN, |
| 105 |
'actions' => [ |
| 106 |
'disallow' => [], |
| 107 |
'block' => [ |
| 108 |
'blocktalk', |
| 109 |
'8 hours', |
| 110 |
'infinity' |
| 111 |
] |
| 112 |
] |
| 113 |
], |
| 114 |
3 => [ |
| 115 |
'af_pattern' => 'action = "delete" & "test" in lcase(page_prefixedtitle) & page_prefixedtitle === article_prefixedtext', |
| 116 |
'af_public_comments' => 'Mock filter for delete', |
| 117 |
'af_global' => 1, |
| 118 |
'actions' => [ |
| 119 |
'degroup' => [] |
| 120 |
] |
| 121 |
], |
| 122 |
5 => [ |
| 123 |
|
| 124 |
'af_pattern' => 'user_name == "UTSysop"', |
| 125 |
'af_public_comments' => 'Mock filter', |
| 126 |
'actions' => [ |
| 127 |
'tag' => [ |
| 128 |
'firstTag', |
| 129 |
'secondTag' |
| 130 |
] |
| 131 |
] |
| 132 |
], |
| 133 |
6 => [ |
| 134 |
'af_pattern' => 'edit_delta === 7', |
| 135 |
'af_public_comments' => 'Mock filter with edit_delta', |
| 136 |
'af_hidden' => Flags::FILTER_HIDDEN, |
| 137 |
'af_global' => 1, |
| 138 |
'actions' => [ |
| 139 |
'disallow' => [ |
| 140 |
'abusefilter-disallowed-really' |
| 141 |
] |
| 142 |
] |
| 143 |
], |
| 144 |
7 => [ |
| 145 |
'af_pattern' => 'timestamp === string(timestamp)', |
| 146 |
'af_public_comments' => 'Mock filter with timestamp', |
| 147 |
'actions' => [ |
| 148 |
'degroup' => [] |
| 149 |
] |
| 150 |
], |
| 151 |
8 => [ |
| 152 |
'af_pattern' => 'added_lines_pst irlike "\\[\\[Link\\|Link\\]\\]"', |
| 153 |
'af_public_comments' => 'Mock filter with pst', |
| 154 |
'actions' => [ |
| 155 |
'disallow' => [], |
| 156 |
'block' => [ |
| 157 |
'NoTalkBlockSet', |
| 158 |
'4 hours', |
| 159 |
'4 hours' |
| 160 |
] |
| 161 |
] |
| 162 |
], |
| 163 |
9 => [ |
| 164 |
'af_pattern' => 'new_size > old_size', |
| 165 |
'af_public_comments' => 'Mock filter with size', |
| 166 |
'af_hidden' => Flags::FILTER_HIDDEN, |
| 167 |
'actions' => [ |
| 168 |
'disallow' => [], |
| 169 |
'block' => [ |
| 170 |
'blocktalk', |
| 171 |
'3 hours', |
| 172 |
'3 hours' |
| 173 |
] |
| 174 |
] |
| 175 |
], |
| 176 |
10 => [ |
| 177 |
'af_pattern' => '1 == 1', |
| 178 |
'af_public_comments' => 'Mock throttled filter', |
| 179 |
'af_hidden' => Flags::FILTER_HIDDEN, |
| 180 |
'af_throttled' => 1, |
| 181 |
'actions' => [ |
| 182 |
'tag' => [ |
| 183 |
'testTag' |
| 184 |
], |
| 185 |
'block' => [ |
| 186 |
'blocktalk', |
| 187 |
'infinity', |
| 188 |
'infinity' |
| 189 |
] |
| 190 |
] |
| 191 |
], |
| 192 |
11 => [ |
| 193 |
'af_pattern' => '1 == 1', |
| 194 |
'af_public_comments' => 'Catch-all filter which throttles', |
| 195 |
'actions' => [ |
| 196 |
'throttle' => [ |
| 197 |
11, |
| 198 |
'1,3600', |
| 199 |
'site' |
| 200 |
], |
| 201 |
'disallow' => [] |
| 202 |
] |
| 203 |
], |
| 204 |
12 => [ |
| 205 |
'af_pattern' => 'page_title == user_name & user_name === page_title', |
| 206 |
'af_public_comments' => 'Mock filter for userpage', |
| 207 |
'actions' => [ |
| 208 |
'disallow' => [], |
| 209 |
'block' => [ |
| 210 |
'blocktalk', |
| 211 |
'8 hours', |
| 212 |
'1 day' |
| 213 |
], |
| 214 |
'degroup' => [] |
| 215 |
] |
| 216 |
], |
| 217 |
13 => [ |
| 218 |
'af_pattern' => '2 == 2', |
| 219 |
'af_public_comments' => 'Another throttled mock filter', |
| 220 |
'af_throttled' => 1, |
| 221 |
'actions' => [ |
| 222 |
'block' => [ |
| 223 |
'blocktalk', |
| 224 |
'8 hours', |
| 225 |
'1 day' |
| 226 |
], |
| 227 |
'degroup' => [] |
| 228 |
] |
| 229 |
], |
| 230 |
14 => [ |
| 231 |
'af_pattern' => '5/int(article_text) == 3', |
| 232 |
'af_public_comments' => 'Filter with a possible division by zero', |
| 233 |
'actions' => [ |
| 234 |
'disallow' => [] |
| 235 |
] |
| 236 |
], |
| 237 |
15 => [ |
| 238 |
'af_pattern' => 'action contains "createaccount"', |
| 239 |
'af_public_comments' => 'Catch-all for account creations', |
| 240 |
'af_hidden' => Flags::FILTER_HIDDEN, |
| 241 |
'actions' => [ |
| 242 |
'disallow' => [] |
| 243 |
] |
| 244 |
], |
| 245 |
18 => [ |
| 246 |
'af_pattern' => '1 == 1', |
| 247 |
'af_public_comments' => 'Global filter', |
| 248 |
'af_global' => 1, |
| 249 |
'actions' => [ |
| 250 |
'warn' => [ |
| 251 |
'abusefilter-warning' |
| 252 |
], |
| 253 |
'disallow' => [] |
| 254 |
] |
| 255 |
], |
| 256 |
19 => [ |
| 257 |
|
| 258 |
'af_pattern' => 'user_name === "UTSysop"', |
| 259 |
'af_public_comments' => 'Another global filter', |
| 260 |
'af_global' => 1, |
| 261 |
'actions' => [ |
| 262 |
'tag' => [ |
| 263 |
'globalTag' |
| 264 |
] |
| 265 |
] |
| 266 |
], |
| 267 |
20 => [ |
| 268 |
'af_pattern' => 'page_title === "Cellar door"', |
| 269 |
'af_public_comments' => 'Yet another global filter', |
| 270 |
'af_global' => 1, |
| 271 |
'actions' => [ |
| 272 |
'disallow' => [], |
| 273 |
] |
| 274 |
], |
| 275 |
21 => [ |
| 276 |
'af_pattern' => '1==1', |
| 277 |
'af_public_comments' => 'Dangerous filter', |
| 278 |
'actions' => [ |
| 279 |
'blockautopromote' => [] |
| 280 |
] |
| 281 |
], |
| 282 |
22 => [ |
| 283 |
'af_pattern' => 'action contains "upload" & "Block me" in added_lines & file_size > 0 & ' . |
| 284 |
'file_mime contains "/" & file_width + file_height > 0 & summary !== ""', |
| 285 |
'af_public_comments' => 'Filter for uploads', |
| 286 |
'actions' => [ |
| 287 |
'warn' => [ |
| 288 |
'abusefilter-random-upload' |
| 289 |
], |
| 290 |
'blockautopromote' => [] |
| 291 |
] |
| 292 |
], |
| 293 |
23 => [ |
| 294 |
'af_pattern' => '1 === 1', |
| 295 |
'af_public_comments' => 'Catch-all for warning + disallow', |
| 296 |
'actions' => [ |
| 297 |
'warn' => [ |
| 298 |
'abusefilter-my-warning' |
| 299 |
], |
| 300 |
'disallow' => [ |
| 301 |
'abusefilter-my-disallow' |
| 302 |
] |
| 303 |
] |
| 304 |
], |
| 305 |
24 => [ |
| 306 |
|
| 307 |
'af_pattern' => '1 == 1', |
| 308 |
'af_public_comments' => 'Always matches, no actions', |
| 309 |
'actions' => [] |
| 310 |
], |
| 311 |
]; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
protected function setUp(): void { |
| 318 |
parent::setUp(); |
| 319 |
|
| 320 |
$this->user = $this->getTestSysop()->getUser(); |
| 321 |
|
| 322 |
|
| 323 |
ConvertibleTimestamp::setFakeTime( time() ); |
| 324 |
|
| 325 |
|
| 326 |
$this->overrideConfigValues( [ |
| 327 |
|
| 328 |
MainConfigNames::PageCreationLog => false, |
| 329 |
'AbuseFilterActions' => [ |
| 330 |
'throttle' => true, |
| 331 |
'warn' => true, |
| 332 |
'disallow' => true, |
| 333 |
'blockautopromote' => true, |
| 334 |
'block' => true, |
| 335 |
'rangeblock' => true, |
| 336 |
'degroup' => true, |
| 337 |
'tag' => true |
| 338 |
], |
| 339 |
] ); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
protected function tearDown(): void { |
| 346 |
$this->clearUploads(); |
| 347 |
parent::tearDown(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
private function createFilters( $ids ) { |
| 356 |
global $wgAbuseFilterActions; |
| 357 |
|
| 358 |
$dbw = $this->getDb(); |
| 359 |
$defaultRowSection = [ |
| 360 |
'af_actor' => 1, |
| 361 |
'af_timestamp' => $dbw->timestamp(), |
| 362 |
'af_group' => 'default', |
| 363 |
'af_comments' => '', |
| 364 |
'af_hit_count' => 0, |
| 365 |
'af_enabled' => 1, |
| 366 |
'af_hidden' => Flags::FILTER_PUBLIC, |
| 367 |
'af_throttled' => 0, |
| 368 |
'af_deleted' => 0, |
| 369 |
'af_global' => 0 |
| 370 |
]; |
| 371 |
|
| 372 |
$filterRows = []; |
| 373 |
$actionsRows = []; |
| 374 |
foreach ( $ids as $id ) { |
| 375 |
$filter = self::FILTERS[$id] + $defaultRowSection; |
| 376 |
$actions = $filter['actions']; |
| 377 |
unset( $filter['actions'] ); |
| 378 |
$filter[ 'af_actions' ] = implode( ',', array_keys( $actions ) ); |
| 379 |
$filter[ 'af_id' ] = $id; |
| 380 |
|
| 381 |
ksort( $filter ); |
| 382 |
$filterRows[] = $filter; |
| 383 |
|
| 384 |
foreach ( array_filter( $wgAbuseFilterActions ) as $action => $_ ) { |
| 385 |
if ( isset( $actions[$action] ) ) { |
| 386 |
$parameters = $actions[$action]; |
| 387 |
|
| 388 |
$thisRow = [ |
| 389 |
'afa_filter' => $id, |
| 390 |
'afa_consequence' => $action, |
| 391 |
'afa_parameters' => implode( "\n", $parameters ) |
| 392 |
]; |
| 393 |
$actionsRows[] = $thisRow; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
$dbw->newInsertQueryBuilder() |
| 399 |
->insertInto( 'abuse_filter' ) |
| 400 |
->rows( $filterRows ) |
| 401 |
->caller( __METHOD__ ) |
| 402 |
->execute(); |
| 403 |
if ( $actionsRows ) { |
| 404 |
$dbw->newInsertQueryBuilder() |
| 405 |
->insertInto( 'abuse_filter_action' ) |
| 406 |
->rows( $actionsRows ) |
| 407 |
->caller( __METHOD__ ) |
| 408 |
->execute(); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
private function stashEdit( Title $title, Content $content, string $summary ) { |
| 419 |
$services = $this->getServiceContainer(); |
| 420 |
$pageUpdater = $services->getWikiPageFactory()->newFromTitle( $title )->newPageUpdater( $this->user ); |
| 421 |
return $services->getPageEditStash()->parseAndCache( |
| 422 |
$pageUpdater, |
| 423 |
$content, |
| 424 |
$this->user, |
| 425 |
$summary |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
private function doEdit( Title $title, $oldText, $newText, $summary, $fromStash = null ) { |
| 439 |
$services = $this->getServiceContainer(); |
| 440 |
$page = $services->getWikiPageFactory()->newFromTitle( $title ); |
| 441 |
if ( !$page->exists() ) { |
| 442 |
$status = $this->editPage( |
| 443 |
$page, |
| 444 |
$oldText, |
| 445 |
__METHOD__ . ' page creation' |
| 446 |
); |
| 447 |
if ( !$status->isGood() ) { |
| 448 |
throw new RuntimeException( "Could not create test page. $status" ); |
| 449 |
} |
| 450 |
$page->clear(); |
| 451 |
$title->resetArticleID( -1 ); |
| 452 |
} |
| 453 |
|
| 454 |
if ( $fromStash !== null ) { |
| 455 |
|
| 456 |
$stashText = $newText; |
| 457 |
if ( $fromStash === false ) { |
| 458 |
|
| 459 |
$stashText = md5( uniqid( rand(), true ) ); |
| 460 |
} |
| 461 |
$stashContent = $stashText instanceof Content ? $stashText : new WikitextContent( $stashText ); |
| 462 |
$stashResult = $this->stashEdit( $title, $stashContent, $summary ); |
| 463 |
if ( $stashResult !== PageEditStash::ERROR_NONE ) { |
| 464 |
throw new RuntimeException( "The edit cannot be stashed, got the following error: $stashResult" ); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
$content = $newText instanceof Content |
| 469 |
? $newText |
| 470 |
: ContentHandler::makeContent( $newText, $title ); |
| 471 |
$status = Status::newGood(); |
| 472 |
$context = RequestContext::getMain(); |
| 473 |
$context->setUser( $this->user ); |
| 474 |
$context->setWikiPage( $page ); |
| 475 |
|
| 476 |
$hooksHandler = new FilteredActionsHandler( |
| 477 |
$services->getStatsdDataFactory(), |
| 478 |
AbuseFilterServices::getFilterRunnerFactory(), |
| 479 |
AbuseFilterServices::getVariableGeneratorFactory(), |
| 480 |
AbuseFilterServices::getEditRevUpdater(), |
| 481 |
AbuseFilterServices::getBlockedDomainFilter(), |
| 482 |
$services->getPermissionManager() |
| 483 |
); |
| 484 |
$hooksHandler->onEditFilterMergedContent( $context, $content, $status, $summary, |
| 485 |
$this->user, false ); |
| 486 |
|
| 487 |
if ( $status->isGood() ) { |
| 488 |
|
| 489 |
$this->editPage( |
| 490 |
$page, |
| 491 |
$newText, |
| 492 |
$summary, |
| 493 |
NS_MAIN, |
| 494 |
$this->user |
| 495 |
); |
| 496 |
$page->clear(); |
| 497 |
$title->resetArticleID( -1 ); |
| 498 |
} |
| 499 |
|
| 500 |
return $status; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
private function doAction( $params ) { |
| 510 |
$target = Title::newFromTextThrow( $params['target'] ); |
| 511 |
|
| 512 |
switch ( $params['action'] ) { |
| 513 |
case 'edit': |
| 514 |
$status = $this->doEdit( $target, $params['oldText'], $params['newText'], $params['summary'] ); |
| 515 |
break; |
| 516 |
case 'stashedit': |
| 517 |
$stashStatus = $params['stashType'] === 'hit'; |
| 518 |
$status = $this->doEdit( |
| 519 |
$target, |
| 520 |
$params['oldText'], |
| 521 |
$params['newText'], |
| 522 |
$params['summary'], |
| 523 |
$stashStatus |
| 524 |
); |
| 525 |
break; |
| 526 |
case 'move': |
| 527 |
|
| 528 |
$this->getExistingTestPage( $target ); |
| 529 |
$newTitle = isset( $params['newTitle'] ) |
| 530 |
? Title::newFromTextThrow( $params['newTitle'] ) |
| 531 |
: $this->getNonExistingTestPage()->getTitle(); |
| 532 |
$status = $this->getServiceContainer() |
| 533 |
->getMovePageFactory() |
| 534 |
->newMovePage( $target, $newTitle ) |
| 535 |
->move( $this->user, 'AbuseFilter move test', false ); |
| 536 |
break; |
| 537 |
case 'delete': |
| 538 |
$page = $this->getExistingTestPage( $target ); |
| 539 |
$status = $page->doDeleteArticleReal( |
| 540 |
'Testing deletion in AbuseFilter', |
| 541 |
$this->user |
| 542 |
); |
| 543 |
break; |
| 544 |
case 'createaccount': |
| 545 |
$status = $this->createAccount( $params['username'] ); |
| 546 |
break; |
| 547 |
case 'upload': |
| 548 |
[ $status, $this->clearPath ] = $this->doUpload( |
| 549 |
$this->user, |
| 550 |
$params['target'], |
| 551 |
$params['newText'] ?? 'AbuseFilter test upload', |
| 552 |
$params['summary'] ?? 'Test' |
| 553 |
); |
| 554 |
break; |
| 555 |
default: |
| 556 |
throw new UnexpectedValueException( 'Unrecognized action ' . $params['action'] ); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
$this->user->clearInstanceCache(); |
| 562 |
|
| 563 |
return $status; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private function doActions( $actionsParams ) { |
| 571 |
$ret = []; |
| 572 |
foreach ( $actionsParams as $params ) { |
| 573 |
$ret[] = $this->doAction( $params ); |
| 574 |
} |
| 575 |
return $ret; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
private function getActionTags( $actionParams ) { |
| 585 |
$dbw = $this->getDb(); |
| 586 |
$title = Title::newFromTextThrow( $actionParams['target'] ); |
| 587 |
$store = $this->getServiceContainer()->getChangeTagsStore(); |
| 588 |
if ( $actionParams['action'] === 'edit' || $actionParams['action'] === 'stashedit' ) { |
| 589 |
return $store->getTags( $dbw, null, $title->getLatestRevID() ); |
| 590 |
} |
| 591 |
|
| 592 |
$logType = $actionParams['action'] === 'createaccount' ? 'newusers' : $actionParams['action']; |
| 593 |
$logAction = $logType === 'newusers' ? 'create2' : $logType; |
| 594 |
$id = $dbw->newSelectQueryBuilder() |
| 595 |
->select( 'log_id' ) |
| 596 |
->from( 'logging' ) |
| 597 |
->where( [ |
| 598 |
'log_namespace' => $title->getNamespace(), |
| 599 |
'log_title' => $title->getDBkey(), |
| 600 |
'log_type' => $logType, |
| 601 |
'log_action' => $logAction |
| 602 |
] ) |
| 603 |
->orderBy( 'log_id', SelectQueryBuilder::SORT_DESC ) |
| 604 |
->caller( __METHOD__ ) |
| 605 |
->fetchField(); |
| 606 |
if ( !$id ) { |
| 607 |
$this->fail( 'Could not find the action in the logging table.' ); |
| 608 |
} |
| 609 |
return $store->getTags( $dbw, null, null, (int)$id ); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
private function checkConsequences( $result, $actionParams, $expectedConsequences ) { |
| 622 |
$expectedErrors = []; |
| 623 |
$testErrorMessage = false; |
| 624 |
foreach ( $expectedConsequences as $consequence => $ids ) { |
| 625 |
foreach ( $ids as $id ) { |
| 626 |
$params = self::FILTERS[$id]['actions'][$consequence]; |
| 627 |
switch ( $consequence ) { |
| 628 |
case 'warn': |
| 629 |
|
| 630 |
$expectedErrors[] = $params[0] ?? 'abusefilter-warning'; |
| 631 |
break; |
| 632 |
case 'disallow': |
| 633 |
|
| 634 |
$expectedErrors[] = $params[0] ?? 'abusefilter-disallowed'; |
| 635 |
break; |
| 636 |
case 'block': |
| 637 |
|
| 638 |
|
| 639 |
$userBlock = $this->user->getBlock( false ); |
| 640 |
|
| 641 |
if ( !$userBlock ) { |
| 642 |
$testErrorMessage = "User isn't blocked."; |
| 643 |
break; |
| 644 |
} |
| 645 |
|
| 646 |
$shouldPreventTalkEdit = $params[0] === 'blocktalk'; |
| 647 |
$edittalkCheck = $userBlock->appliesToUsertalk( $this->user->getTalkPage() ) === |
| 648 |
$shouldPreventTalkEdit; |
| 649 |
if ( !$edittalkCheck ) { |
| 650 |
$testErrorMessage = 'The expected block option "edittalk" options does not ' . |
| 651 |
'match the actual one.'; |
| 652 |
break; |
| 653 |
} |
| 654 |
|
| 655 |
$expectedExpiry = BlockUser::parseExpiryInput( $params[2] ); |
| 656 |
$actualExpiry = $userBlock->getExpiry(); |
| 657 |
if ( !wfIsInfinity( $actualExpiry ) ) { |
| 658 |
$actualExpiry = wfTimestamp( TS_MW, $actualExpiry ); |
| 659 |
} |
| 660 |
if ( $expectedExpiry === false || $expectedExpiry !== $actualExpiry ) { |
| 661 |
$testErrorMessage = "The expected block expiry ($expectedExpiry) does not " . |
| 662 |
"match the actual one ($actualExpiry)."; |
| 663 |
break; |
| 664 |
} |
| 665 |
|
| 666 |
$expectedErrors[] = 'abusefilter-blocked-display'; |
| 667 |
break; |
| 668 |
case 'degroup': |
| 669 |
|
| 670 |
$expectedErrors[] = 'abusefilter-degrouped'; |
| 671 |
$ugm = MediaWikiServices::getInstance()->getUserGroupManager(); |
| 672 |
$groupCheck = !in_array( 'sysop', $ugm->getUserEffectiveGroups( $this->user ) ); |
| 673 |
if ( !$groupCheck ) { |
| 674 |
$testErrorMessage = 'The user was not degrouped.'; |
| 675 |
} |
| 676 |
break; |
| 677 |
case 'tag': |
| 678 |
|
| 679 |
$appliedTags = $this->getActionTags( $actionParams ); |
| 680 |
$tagCheck = count( array_diff( $params, $appliedTags ) ) === 0; |
| 681 |
if ( !$tagCheck ) { |
| 682 |
$expectedTags = implode( ', ', $params ); |
| 683 |
$actualTags = implode( ', ', $appliedTags ); |
| 684 |
|
| 685 |
$testErrorMessage = "Expected the action to have the following tags: $expectedTags. " . |
| 686 |
"Got the following instead: $actualTags."; |
| 687 |
} |
| 688 |
break; |
| 689 |
case 'throttle': |
| 690 |
throw new UnexpectedValueException( 'Use self::testThrottleConsequence to test throttling' ); |
| 691 |
case 'blockautopromote': |
| 692 |
|
| 693 |
$expectedErrors[] = 'abusefilter-autopromote-blocked'; |
| 694 |
$value = AbuseFilterServices::getBlockAutopromoteStore() |
| 695 |
->getAutoPromoteBlockStatus( $this->user ); |
| 696 |
if ( !$value ) { |
| 697 |
$testErrorMessage = "The key for blocking autopromotion wasn't set."; |
| 698 |
} |
| 699 |
break; |
| 700 |
default: |
| 701 |
throw new UnexpectedValueException( "Consequence not recognized: $consequence." ); |
| 702 |
} |
| 703 |
|
| 704 |
if ( $testErrorMessage ) { |
| 705 |
$this->fail( $testErrorMessage ); |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
$actual = []; |
| 711 |
foreach ( $result->getMessages() as $msg ) { |
| 712 |
if ( strpos( $msg->getKey(), 'abusefilter' ) !== false ) { |
| 713 |
$actual[] = $msg->getKey(); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
sort( $expectedErrors ); |
| 718 |
sort( $actual ); |
| 719 |
return [ $expectedErrors, $actual ]; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
private function assertAbuseLog( $actionParams, $expectedConsequences ): void { |
| 727 |
$consequencesByFilter = []; |
| 728 |
foreach ( $expectedConsequences as $consequence => $ids ) { |
| 729 |
foreach ( $ids as $id ) { |
| 730 |
$consequencesByFilter[$id][] = $consequence; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
foreach ( $consequencesByFilter as $filter => $consequences ) { |
| 735 |
$action = $actionParams['action']; |
| 736 |
if ( $action === 'stashedit' ) { |
| 737 |
$action = 'edit'; |
| 738 |
} |
| 739 |
$title = Title::newFromTextThrow( $actionParams['target'] ); |
| 740 |
|
| 741 |
$row = $this->getDb()->newSelectQueryBuilder() |
| 742 |
->select( '*' ) |
| 743 |
->from( 'abuse_filter_log' ) |
| 744 |
->where( [ |
| 745 |
'afl_filter_id' => $filter, |
| 746 |
'afl_global' => 0, |
| 747 |
] ) |
| 748 |
->orderBy( 'afl_id', SelectQueryBuilder::SORT_DESC ) |
| 749 |
->caller( __METHOD__ ) |
| 750 |
->fetchRow(); |
| 751 |
$this->assertNotFalse( $row ); |
| 752 |
|
| 753 |
$dumpStr = FormatJson::encode( $row ); |
| 754 |
$this->assertSame( $action, $row->afl_action, $dumpStr ); |
| 755 |
$this->assertNull( $row->afl_wiki, $dumpStr ); |
| 756 |
if ( $action === 'createaccount' ) { |
| 757 |
$this->assertSame( $actionParams['username'], $row->afl_user_text, $dumpStr ); |
| 758 |
$this->assertSame( -1, (int)$row->afl_namespace, $dumpStr ); |
| 759 |
} else { |
| 760 |
$this->assertSame( $this->user->getName(), $row->afl_user_text, $dumpStr ); |
| 761 |
$this->assertSame( $title->getNamespace(), (int)$row->afl_namespace, $dumpStr ); |
| 762 |
$this->assertSame( $title->getDBkey(), $row->afl_title, $dumpStr ); |
| 763 |
} |
| 764 |
if ( |
| 765 |
in_array( 'disallow', $consequences ) |
| 766 |
&& array_intersect( $consequences, [ 'block', 'blockautopromote', 'degroup' ] ) |
| 767 |
) { |
| 768 |
$consequences = array_diff( $consequences, [ 'disallow' ] ); |
| 769 |
} |
| 770 |
$this->assertArrayEquals( |
| 771 |
$consequences, |
| 772 |
explode( ',', $row->afl_actions ), |
| 773 |
false, |
| 774 |
false, |
| 775 |
$dumpStr |
| 776 |
); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
public function testFilterConsequences( $createIds, $actionParams, $expectedConsequences ) { |
| 790 |
$this->createFilters( $createIds ); |
| 791 |
$result = $this->doAction( $actionParams ); |
| 792 |
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $expectedConsequences ); |
| 793 |
|
| 794 |
$this->assertEquals( |
| 795 |
$expected, |
| 796 |
$actual, |
| 797 |
'The error messages obtained by performing the action do not match.' |
| 798 |
); |
| 799 |
|
| 800 |
$this->assertAbuseLog( $actionParams, $expectedConsequences ); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
public static function provideFilters() { |
| 814 |
|
| 815 |
$username = 'UTSysop'; |
| 816 |
return [ |
| 817 |
'Basic test for "edit" action' => [ |
| 818 |
[ 1, 2 ], |
| 819 |
[ |
| 820 |
'action' => 'edit', |
| 821 |
'target' => 'Test page', |
| 822 |
'oldText' => 'Some old text for the test.', |
| 823 |
'newText' => 'I like foo', |
| 824 |
'summary' => 'Test AbuseFilter for edit action.' |
| 825 |
], |
| 826 |
[ 'warn' => [ 1 ] ] |
| 827 |
], |
| 828 |
'Basic test for "move" action' => [ |
| 829 |
[ 2 ], |
| 830 |
[ |
| 831 |
'action' => 'move', |
| 832 |
'target' => 'Test page', |
| 833 |
'newTitle' => 'Another test page' |
| 834 |
], |
| 835 |
[ 'block' => [ 2 ] ] |
| 836 |
], |
| 837 |
'Basic test for "delete" action' => [ |
| 838 |
[ 2, 3 ], |
| 839 |
[ |
| 840 |
'action' => 'delete', |
| 841 |
'target' => 'Test page' |
| 842 |
], |
| 843 |
[ 'degroup' => [ 3 ] ] |
| 844 |
], |
| 845 |
'Basic test for "createaccount", no consequences.' => [ |
| 846 |
[ 1, 2, 3 ], |
| 847 |
[ |
| 848 |
'action' => 'createaccount', |
| 849 |
'target' => 'User:AnotherUser', |
| 850 |
'username' => 'AnotherUser' |
| 851 |
], |
| 852 |
[] |
| 853 |
], |
| 854 |
'Basic test for "createaccount", disallowed.' => [ |
| 855 |
[ 15 ], |
| 856 |
[ |
| 857 |
'action' => 'createaccount', |
| 858 |
'target' => 'User:AnotherUser', |
| 859 |
'username' => 'AnotherUser' |
| 860 |
], |
| 861 |
[ 'disallow' => [ 15 ] ] |
| 862 |
], |
| 863 |
'Check that all tags are applied' => [ |
| 864 |
[ 5 ], |
| 865 |
[ |
| 866 |
'action' => 'edit', |
| 867 |
'target' => "User:$username", |
| 868 |
'oldText' => 'Hey.', |
| 869 |
'newText' => 'I am a very nice user, really!', |
| 870 |
'summary' => '' |
| 871 |
], |
| 872 |
[ 'tag' => [ 5 ] ] |
| 873 |
], |
| 874 |
'Check that degroup and block are executed together' => [ |
| 875 |
[ 2, 3, 7, 8 ], |
| 876 |
[ |
| 877 |
'action' => 'edit', |
| 878 |
'target' => 'Link', |
| 879 |
'oldText' => 'What is a link?', |
| 880 |
'newText' => 'A link is something like this: [[Link|]].', |
| 881 |
'summary' => 'Explaining' |
| 882 |
], |
| 883 |
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ] |
| 884 |
], |
| 885 |
'Check that the block duration is the longer one' => [ |
| 886 |
[ 8, 9 ], |
| 887 |
[ |
| 888 |
'action' => 'edit', |
| 889 |
'target' => 'Whatever', |
| 890 |
'oldText' => 'Whatever is whatever', |
| 891 |
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]', |
| 892 |
'summary' => 'Whatever' |
| 893 |
], |
| 894 |
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ] |
| 895 |
], |
| 896 |
'Check that throttled filters only execute "safe" actions' => [ |
| 897 |
[ 10 ], |
| 898 |
[ |
| 899 |
'action' => 'edit', |
| 900 |
'target' => 'Buffalo', |
| 901 |
'oldText' => 'Buffalo', |
| 902 |
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.', |
| 903 |
'summary' => 'Buffalo!' |
| 904 |
], |
| 905 |
[ 'tag' => [ 10 ] ] |
| 906 |
], |
| 907 |
'Check that degroup and block are both executed and degroup warning is shown twice' => [ |
| 908 |
[ 1, 3, 7, 12 ], |
| 909 |
[ |
| 910 |
'action' => 'edit', |
| 911 |
'target' => "User:$username", |
| 912 |
'oldText' => '', |
| 913 |
'newText' => 'A couple of lines about me...', |
| 914 |
'summary' => 'My user page' |
| 915 |
], |
| 916 |
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ] |
| 917 |
], |
| 918 |
'Check that every throttled filter only executes "safe" actions' => [ |
| 919 |
[ 10, 13 ], |
| 920 |
[ |
| 921 |
'action' => 'edit', |
| 922 |
'target' => 'Tyger! Tyger! Burning bright', |
| 923 |
'oldText' => 'In the forests of the night', |
| 924 |
'newText' => 'What immortal hand or eye', |
| 925 |
'summary' => 'Could frame thy fearful symmetry?' |
| 926 |
], |
| 927 |
[ 'tag' => [ 10 ] ] |
| 928 |
], |
| 929 |
'Check that runtime exceptions (division by zero) are correctly handled' => [ |
| 930 |
[ 14 ], |
| 931 |
[ |
| 932 |
'action' => 'edit', |
| 933 |
'target' => '0', |
| 934 |
'oldText' => 'Old text', |
| 935 |
'newText' => 'New text', |
| 936 |
'summary' => 'Some summary' |
| 937 |
], |
| 938 |
[] |
| 939 |
], |
| 940 |
'Test for blockautopromote action.' => [ |
| 941 |
[ 21 ], |
| 942 |
[ |
| 943 |
'action' => 'edit', |
| 944 |
'target' => 'Rainbow', |
| 945 |
'oldText' => '', |
| 946 |
'newText' => '...', |
| 947 |
'summary' => '' |
| 948 |
], |
| 949 |
[ 'blockautopromote' => [ 21 ] ], |
| 950 |
], |
| 951 |
'No consequences' => [ |
| 952 |
[ 8 ], |
| 953 |
[ |
| 954 |
'action' => 'edit', |
| 955 |
'target' => 'Anything', |
| 956 |
'oldText' => 'Bar', |
| 957 |
'newText' => 'Foo', |
| 958 |
'summary' => '' |
| 959 |
], |
| 960 |
[] |
| 961 |
], |
| 962 |
'Test upload action' => [ |
| 963 |
[ 5 ], |
| 964 |
[ |
| 965 |
'action' => 'upload', |
| 966 |
'target' => 'File:MyFile.svg', |
| 967 |
], |
| 968 |
[ 'tag' => [ 5 ] ] |
| 969 |
], |
| 970 |
'Test upload action 2' => [ |
| 971 |
[ 22 ], |
| 972 |
[ |
| 973 |
'action' => 'upload', |
| 974 |
'target' => 'File:MyFile.svg', |
| 975 |
'newText' => 'Block me please!', |
| 976 |
'summary' => 'Asking to be blocked' |
| 977 |
], |
| 978 |
[ 'warn' => [ 22 ] ] |
| 979 |
], |
| 980 |
]; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
private function checkThrottleConsequence( $results ) { |
| 990 |
$finalRes = array_pop( $results ); |
| 991 |
foreach ( $results as $result ) { |
| 992 |
if ( !$result->isGood() ) { |
| 993 |
$this->fail( 'Only the last actions should have triggered a filter; the other ones ' . |
| 994 |
'should have been allowed.' ); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
return $finalRes; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
public function testThrottle( $createIds, $actionsParams, $consequences ) { |
| 1011 |
$this->createFilters( $createIds ); |
| 1012 |
$results = $this->doActions( $actionsParams ); |
| 1013 |
$res = $this->checkThrottleConsequence( $results ); |
| 1014 |
$lastParams = array_pop( $actionsParams ); |
| 1015 |
[ $expected, $actual ] = $this->checkConsequences( $res, $lastParams, $consequences ); |
| 1016 |
|
| 1017 |
$this->assertEquals( |
| 1018 |
$expected, |
| 1019 |
$actual, |
| 1020 |
'The error messages obtained by performing the action do not match.' |
| 1021 |
); |
| 1022 |
|
| 1023 |
$this->assertAbuseLog( $lastParams, $consequences ); |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
public static function provideThrottleFilters() { |
| 1039 |
return [ |
| 1040 |
'Basic test for throttling edits' => [ |
| 1041 |
[ 11 ], |
| 1042 |
[ |
| 1043 |
[ |
| 1044 |
'action' => 'edit', |
| 1045 |
'target' => 'Throttle', |
| 1046 |
'oldText' => 'What is throttle?', |
| 1047 |
'newText' => 'Throttle is something that should happen...', |
| 1048 |
'summary' => 'Throttle' |
| 1049 |
], |
| 1050 |
[ |
| 1051 |
'action' => 'edit', |
| 1052 |
'target' => 'Throttle', |
| 1053 |
'oldText' => 'Throttle is something that should happen...', |
| 1054 |
'newText' => '... Right now!', |
| 1055 |
'summary' => 'Throttle' |
| 1056 |
] |
| 1057 |
], |
| 1058 |
[ 'disallow' => [ 11 ] ] |
| 1059 |
], |
| 1060 |
'Basic test for throttling "move"' => [ |
| 1061 |
[ 11 ], |
| 1062 |
[ |
| 1063 |
[ |
| 1064 |
'action' => 'move', |
| 1065 |
'target' => 'Throttle test', |
| 1066 |
'newTitle' => 'Another throttle test' |
| 1067 |
], |
| 1068 |
[ |
| 1069 |
'action' => 'move', |
| 1070 |
'target' => 'Another throttle test', |
| 1071 |
'newTitle' => 'Yet another throttle test' |
| 1072 |
], |
| 1073 |
], |
| 1074 |
[ 'disallow' => [ 11 ] ] |
| 1075 |
], |
| 1076 |
'Basic test for throttling "delete"' => [ |
| 1077 |
[ 11 ], |
| 1078 |
[ |
| 1079 |
[ |
| 1080 |
'action' => 'delete', |
| 1081 |
'target' => 'Test page' |
| 1082 |
], |
| 1083 |
[ |
| 1084 |
'action' => 'delete', |
| 1085 |
'target' => 'Test page' |
| 1086 |
] |
| 1087 |
], |
| 1088 |
[ 'disallow' => [ 11 ] ] |
| 1089 |
], |
| 1090 |
'Basic test for throttling "createaccount"' => [ |
| 1091 |
[ 11 ], |
| 1092 |
[ |
| 1093 |
[ |
| 1094 |
'action' => 'createaccount', |
| 1095 |
'target' => 'User:AnotherUser', |
| 1096 |
'username' => 'AnotherUser' |
| 1097 |
], |
| 1098 |
[ |
| 1099 |
'action' => 'createaccount', |
| 1100 |
'target' => 'User:YetAnotherUser', |
| 1101 |
'username' => 'YetAnotherUser' |
| 1102 |
] |
| 1103 |
], |
| 1104 |
[ 'disallow' => [ 11 ] ] |
| 1105 |
] |
| 1106 |
]; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
public function testWarn( $createIds, $actionParams, $warnIDs, $consequences ) { |
| 1121 |
$this->createFilters( $createIds ); |
| 1122 |
$params = [ $actionParams, $actionParams ]; |
| 1123 |
[ $warnedStatus, $finalStatus ] = $this->doActions( $params ); |
| 1124 |
|
| 1125 |
[ $expectedWarn, $actualWarn ] = $this->checkConsequences( |
| 1126 |
$warnedStatus, |
| 1127 |
$actionParams, |
| 1128 |
[ 'warn' => $warnIDs ] |
| 1129 |
); |
| 1130 |
|
| 1131 |
$this->assertSame( |
| 1132 |
$expectedWarn, |
| 1133 |
$actualWarn, |
| 1134 |
'The error messages for the first action do not match.' |
| 1135 |
); |
| 1136 |
|
| 1137 |
[ $expectedFinal, $actualFinal ] = $this->checkConsequences( |
| 1138 |
$finalStatus, |
| 1139 |
$actionParams, |
| 1140 |
$consequences |
| 1141 |
); |
| 1142 |
|
| 1143 |
$this->assertSame( |
| 1144 |
$expectedFinal, |
| 1145 |
$actualFinal, |
| 1146 |
'The error messages for the second action do not match.' |
| 1147 |
); |
| 1148 |
|
| 1149 |
$this->assertAbuseLog( $actionParams, $consequences ); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
public static function provideWarnFilters() { |
| 1164 |
return [ |
| 1165 |
'Basic test for warning and then tag' => [ |
| 1166 |
[ 1 ], |
| 1167 |
[ |
| 1168 |
'action' => 'edit', |
| 1169 |
'target' => 'Foo', |
| 1170 |
'oldText' => 'Neutral text', |
| 1171 |
'newText' => 'First foo version', |
| 1172 |
'summary' => '' |
| 1173 |
], |
| 1174 |
[ 1 ], |
| 1175 |
[ 'tag' => [ 1 ] ], |
| 1176 |
], |
| 1177 |
'Basic test for warning on "move"' => [ |
| 1178 |
[ 23 ], |
| 1179 |
[ |
| 1180 |
'action' => 'move', |
| 1181 |
'target' => 'Test warn', |
| 1182 |
'newTitle' => 'Another warn test' |
| 1183 |
], |
| 1184 |
[ 23 ], |
| 1185 |
[ 'disallow' => [ 23 ] ] |
| 1186 |
], |
| 1187 |
'Basic test for warning on "delete"' => [ |
| 1188 |
[ 23 ], |
| 1189 |
[ |
| 1190 |
'action' => 'delete', |
| 1191 |
'target' => 'Warned' |
| 1192 |
], |
| 1193 |
[ 23 ], |
| 1194 |
[ 'disallow' => [ 23 ] ] |
| 1195 |
], |
| 1196 |
'Basic test for warning on "createaccount"' => [ |
| 1197 |
[ 23 ], |
| 1198 |
[ |
| 1199 |
'action' => 'createaccount', |
| 1200 |
'target' => 'User:AnotherWarnedUser', |
| 1201 |
'username' => 'AnotherWarnedUser' |
| 1202 |
], |
| 1203 |
[ 23 ], |
| 1204 |
[ 'disallow' => [ 23 ] ] |
| 1205 |
] |
| 1206 |
]; |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
public function testVarDump( $createIds, $actionParams, $usedVars ) { |
| 1218 |
$this->createFilters( $createIds ); |
| 1219 |
|
| 1220 |
$this->doAction( $actionParams ); |
| 1221 |
|
| 1222 |
|
| 1223 |
$row = $this->getDb()->newSelectQueryBuilder() |
| 1224 |
->select( 'afl_var_dump, afl_ip' ) |
| 1225 |
->from( 'abuse_filter_log' ) |
| 1226 |
->orderBy( 'afl_timestamp', SelectQueryBuilder::SORT_DESC ) |
| 1227 |
->caller( __METHOD__ ) |
| 1228 |
->fetchRow(); |
| 1229 |
|
| 1230 |
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row )->getVars(); |
| 1231 |
|
| 1232 |
$interestingVars = array_intersect_key( $vars, array_fill_keys( $usedVars, true ) ); |
| 1233 |
|
| 1234 |
sort( $usedVars ); |
| 1235 |
ksort( $interestingVars ); |
| 1236 |
$this->assertEquals( |
| 1237 |
$usedVars, |
| 1238 |
array_keys( $interestingVars ), |
| 1239 |
"The saved variables aren't the expected ones." |
| 1240 |
); |
| 1241 |
$this->assertContainsOnlyInstancesOf( |
| 1242 |
AFPData::class, |
| 1243 |
$interestingVars, |
| 1244 |
'Some variables have not been computed.' |
| 1245 |
); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
public static function provideFiltersAndVariables() { |
| 1254 |
return [ |
| 1255 |
[ |
| 1256 |
[ 1, 2 ], |
| 1257 |
[ |
| 1258 |
'action' => 'edit', |
| 1259 |
'target' => 'Test page', |
| 1260 |
'oldText' => 'Some old text for the test.', |
| 1261 |
'newText' => 'I like foo', |
| 1262 |
'summary' => 'Test AbuseFilter for edit action.' |
| 1263 |
], |
| 1264 |
[ 'added_lines', 'action' ] |
| 1265 |
], |
| 1266 |
[ |
| 1267 |
[ 1, 2 ], |
| 1268 |
[ |
| 1269 |
'action' => 'stashedit', |
| 1270 |
'target' => 'Test page', |
| 1271 |
'oldText' => 'Some old text for the test.', |
| 1272 |
'newText' => 'I like foo', |
| 1273 |
'summary' => 'Test AbuseFilter for edit action.', |
| 1274 |
'stashType' => 'hit' |
| 1275 |
], |
| 1276 |
[ 'added_lines', 'action' ] |
| 1277 |
], |
| 1278 |
[ |
| 1279 |
[ 1, 2 ], |
| 1280 |
[ |
| 1281 |
'action' => 'stashedit', |
| 1282 |
'target' => 'Test page', |
| 1283 |
'oldText' => 'Some old text for the test.', |
| 1284 |
'newText' => 'I like foo', |
| 1285 |
'summary' => 'Test AbuseFilter for edit action.', |
| 1286 |
'stashType' => 'miss' |
| 1287 |
], |
| 1288 |
[ 'added_lines', 'action' ] |
| 1289 |
], |
| 1290 |
[ |
| 1291 |
[ 2 ], |
| 1292 |
[ |
| 1293 |
'action' => 'move', |
| 1294 |
'target' => 'Test page', |
| 1295 |
'newTitle' => 'Another test page' |
| 1296 |
], |
| 1297 |
[ 'action', 'moved_to_title' ] |
| 1298 |
], |
| 1299 |
[ |
| 1300 |
[ 2, 3 ], |
| 1301 |
[ |
| 1302 |
'action' => 'delete', |
| 1303 |
'target' => 'Test page' |
| 1304 |
], |
| 1305 |
[ 'action', 'page_prefixedtitle' ] |
| 1306 |
], |
| 1307 |
[ |
| 1308 |
[ 15 ], |
| 1309 |
[ |
| 1310 |
'action' => 'createaccount', |
| 1311 |
'target' => 'User:AnotherUser', |
| 1312 |
'username' => 'AnotherUser' |
| 1313 |
], |
| 1314 |
[ 'action' ] |
| 1315 |
], |
| 1316 |
]; |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
public function testStashedEdit( $type, $createIds, $actionParams, $consequences ) { |
| 1331 |
if ( $type !== 'hit' && $type !== 'miss' ) { |
| 1332 |
throw new InvalidArgumentException( '$type must be either "hit" or "miss"' ); |
| 1333 |
} |
| 1334 |
|
| 1335 |
$actionParams['action'] = 'stashedit'; |
| 1336 |
$actionParams['stashType'] = $type; |
| 1337 |
|
| 1338 |
$loggerMock = $this->createMock( LoggerInterface::class ); |
| 1339 |
$loggerCalls = []; |
| 1340 |
$loggerMock->method( 'debug' ) |
| 1341 |
->willReturnCallback( static function ( $msg, $args ) use ( &$loggerCalls ) { |
| 1342 |
if ( isset( $args['logtype'] ) ) { |
| 1343 |
$loggerCalls[] = $args['logtype']; |
| 1344 |
} |
| 1345 |
} ); |
| 1346 |
$this->setLogger( 'StashEdit', $loggerMock ); |
| 1347 |
|
| 1348 |
$this->createFilters( $createIds ); |
| 1349 |
$result = $this->doAction( $actionParams ); |
| 1350 |
|
| 1351 |
|
| 1352 |
if ( !in_array( 'store', $loggerCalls, true ) ) { |
| 1353 |
$this->fail( 'Did not store the edit in cache as expected for a stashed edit.' ); |
| 1354 |
} elseif ( !in_array( $type, $loggerCalls, true ) ) { |
| 1355 |
$this->fail( "Did not $type the cache as expected for a stashed edit." ); |
| 1356 |
} |
| 1357 |
|
| 1358 |
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $consequences ); |
| 1359 |
|
| 1360 |
$this->assertEquals( |
| 1361 |
$expected, |
| 1362 |
$actual, |
| 1363 |
'The error messages obtained by performing the action do not match.' |
| 1364 |
); |
| 1365 |
|
| 1366 |
$this->assertAbuseLog( $actionParams, $consequences ); |
| 1367 |
} |
| 1368 |
|
| 1369 |
public static function provideStashedEdits() { |
| 1370 |
|
| 1371 |
$username = 'UTSysop'; |
| 1372 |
$sets = [ |
| 1373 |
[ |
| 1374 |
[ 1, 2 ], |
| 1375 |
[ |
| 1376 |
'target' => 'Test page', |
| 1377 |
'oldText' => 'Some old text for the test.', |
| 1378 |
'newText' => 'I like foo', |
| 1379 |
'summary' => 'Test AbuseFilter for edit action.' |
| 1380 |
], |
| 1381 |
[ 'warn' => [ 1 ] ] |
| 1382 |
], |
| 1383 |
[ |
| 1384 |
[ 5 ], |
| 1385 |
[ |
| 1386 |
'target' => "User:$username", |
| 1387 |
'oldText' => 'Hey.', |
| 1388 |
'newText' => 'I am a very nice user, really!', |
| 1389 |
'summary' => '' |
| 1390 |
], |
| 1391 |
[ 'tag' => [ 5 ] ] |
| 1392 |
], |
| 1393 |
[ |
| 1394 |
[ 6 ], |
| 1395 |
[ |
| 1396 |
'target' => 'Help:Help', |
| 1397 |
'oldText' => 'Some help.', |
| 1398 |
'newText' => 'Some help for you', |
| 1399 |
'summary' => 'Help! I need somebody' |
| 1400 |
], |
| 1401 |
[ 'disallow' => [ 6 ] ] |
| 1402 |
], |
| 1403 |
[ |
| 1404 |
[ 2, 3, 7, 8 ], |
| 1405 |
[ |
| 1406 |
'target' => 'Link', |
| 1407 |
'oldText' => 'What is a link?', |
| 1408 |
'newText' => 'A link is something like this: [[Link|]].', |
| 1409 |
'summary' => 'Explaining' |
| 1410 |
], |
| 1411 |
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ] |
| 1412 |
], |
| 1413 |
[ |
| 1414 |
[ 8, 9 ], |
| 1415 |
[ |
| 1416 |
'target' => 'Whatever', |
| 1417 |
'oldText' => 'Whatever is whatever', |
| 1418 |
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]', |
| 1419 |
'summary' => 'Whatever' |
| 1420 |
], |
| 1421 |
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ] |
| 1422 |
], |
| 1423 |
[ |
| 1424 |
[ 10 ], |
| 1425 |
[ |
| 1426 |
'target' => 'Buffalo', |
| 1427 |
'oldText' => 'Buffalo', |
| 1428 |
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.', |
| 1429 |
'summary' => 'Buffalo!' |
| 1430 |
], |
| 1431 |
[ 'tag' => [ 10 ] ] |
| 1432 |
], |
| 1433 |
[ |
| 1434 |
[ 1, 3, 7, 12 ], |
| 1435 |
[ |
| 1436 |
'target' => "User:$username", |
| 1437 |
'oldText' => '', |
| 1438 |
'newText' => 'A couple of lines about me...', |
| 1439 |
'summary' => 'My user page' |
| 1440 |
], |
| 1441 |
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ] |
| 1442 |
], |
| 1443 |
[ |
| 1444 |
[ 10, 13 ], |
| 1445 |
[ |
| 1446 |
'target' => 'Tyger! Tyger! Burning bright', |
| 1447 |
'oldText' => 'In the forests of the night', |
| 1448 |
'newText' => 'What immortal hand or eye', |
| 1449 |
'summary' => 'Could frame thy fearful symmetry?' |
| 1450 |
], |
| 1451 |
[ 'tag' => [ 10 ] ] |
| 1452 |
], |
| 1453 |
[ |
| 1454 |
[ 14 ], |
| 1455 |
[ |
| 1456 |
'target' => '0', |
| 1457 |
'oldText' => 'Old text', |
| 1458 |
'newText' => 'New text', |
| 1459 |
'summary' => 'Some summary' |
| 1460 |
], |
| 1461 |
[] |
| 1462 |
], |
| 1463 |
]; |
| 1464 |
|
| 1465 |
foreach ( $sets as $set ) { |
| 1466 |
|
| 1467 |
yield [ 'miss', ...$set ]; |
| 1468 |
yield [ 'hit', ...$set ]; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
public function testGlobalFilters( $createIds, $actionParams, $consequences ) { |
| 1485 |
$this->overrideConfigValues( [ |
| 1486 |
'AbuseFilterCentralDB' => WikiMap::getCurrentWikiId(), |
| 1487 |
'AbuseFilterIsCentral' => false, |
| 1488 |
] ); |
| 1489 |
$this->createFilters( $createIds ); |
| 1490 |
|
| 1491 |
AbuseFilterServices::getFilterLookup( $this->getServiceContainer() )->hideLocalFiltersForTesting(); |
| 1492 |
$result = $this->doAction( $actionParams ); |
| 1493 |
|
| 1494 |
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $consequences ); |
| 1495 |
|
| 1496 |
|
| 1497 |
$this->assertEquals( |
| 1498 |
$expected, |
| 1499 |
$actual, |
| 1500 |
'The error messages obtained by performing the action do not match.' |
| 1501 |
); |
| 1502 |
|
| 1503 |
|
| 1504 |
$dbr = $this->getDb(); |
| 1505 |
$loggedFilters = $dbr->newSelectQueryBuilder() |
| 1506 |
->select( 'afl_filter_id' ) |
| 1507 |
->from( 'abuse_filter_log' ) |
| 1508 |
->where( $dbr->expr( 'afl_wiki', '!=', null ) ) |
| 1509 |
->caller( __METHOD__ ) |
| 1510 |
->fetchFieldValues(); |
| 1511 |
|
| 1512 |
|
| 1513 |
$this->assertEquals( |
| 1514 |
$createIds, |
| 1515 |
$loggedFilters, |
| 1516 |
'Some filter hits were not logged in the external DB.' |
| 1517 |
); |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
public static function provideGlobalFilters() { |
| 1526 |
return [ |
| 1527 |
[ |
| 1528 |
[ 18 ], |
| 1529 |
[ |
| 1530 |
'action' => 'edit', |
| 1531 |
'target' => 'Global', |
| 1532 |
'oldText' => 'Old text', |
| 1533 |
'newText' => 'New text', |
| 1534 |
'summary' => '' |
| 1535 |
], |
| 1536 |
[ 'warn' => [ 18 ] ] |
| 1537 |
], |
| 1538 |
[ |
| 1539 |
[ 19 ], |
| 1540 |
[ |
| 1541 |
'action' => 'edit', |
| 1542 |
'target' => 'A global page', |
| 1543 |
'oldText' => 'Foo', |
| 1544 |
'newText' => 'Bar', |
| 1545 |
'summary' => 'Baz' |
| 1546 |
], |
| 1547 |
[ 'tag' => [ 19 ] ] |
| 1548 |
], |
| 1549 |
[ |
| 1550 |
[ 18 ], |
| 1551 |
[ |
| 1552 |
'action' => 'move', |
| 1553 |
'target' => 'Cellar door', |
| 1554 |
'newTitle' => 'Attic door' |
| 1555 |
], |
| 1556 |
[ 'warn' => [ 18 ] ] |
| 1557 |
], |
| 1558 |
[ |
| 1559 |
[ 19, 20 ], |
| 1560 |
[ |
| 1561 |
'action' => 'delete', |
| 1562 |
'target' => 'Cellar door', |
| 1563 |
], |
| 1564 |
[ 'disallow' => [ 20 ] ] |
| 1565 |
], |
| 1566 |
[ |
| 1567 |
[ 19 ], |
| 1568 |
[ |
| 1569 |
'action' => 'stashedit', |
| 1570 |
'target' => 'Cellar door', |
| 1571 |
'oldText' => '', |
| 1572 |
'newText' => 'Too many doors', |
| 1573 |
'summary' => '', |
| 1574 |
'stashType' => 'hit' |
| 1575 |
], |
| 1576 |
[ 'tag' => [ 19 ] ] |
| 1577 |
], |
| 1578 |
[ |
| 1579 |
[ 18 ], |
| 1580 |
[ |
| 1581 |
'action' => 'createaccount', |
| 1582 |
'target' => 'User:AbuseFilterGlobalUser', |
| 1583 |
'username' => 'AbuseFilterGlobalUser' |
| 1584 |
], |
| 1585 |
[ 'warn' => [ 18 ] ] |
| 1586 |
] |
| 1587 |
]; |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
public function testRevIdSet() { |
| 1597 |
|
| 1598 |
$this->createFilters( [ 24 ] ); |
| 1599 |
|
| 1600 |
$targetTitle = Title::makeTitle( NS_MAIN, 'TestRevIdSet' ); |
| 1601 |
$startingRevId = $targetTitle->getLatestRevID( IDBAccessObject::READ_LATEST ); |
| 1602 |
|
| 1603 |
$this->doEdit( $targetTitle, 'Old text', 'New text', 'Summary' ); |
| 1604 |
$latestRevId = $targetTitle->getLatestRevID( IDBAccessObject::READ_LATEST ); |
| 1605 |
|
| 1606 |
$this->assertNotSame( |
| 1607 |
$startingRevId, |
| 1608 |
$latestRevId, |
| 1609 |
'Edit should have been properly saved' |
| 1610 |
); |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
$filterHit = $this->getDb()->newSelectQueryBuilder() |
| 1616 |
->select( '*' ) |
| 1617 |
->from( 'abuse_filter_log' ) |
| 1618 |
->where( [ 'afl_filter_id' => 24 ] ) |
| 1619 |
->orderBy( 'afl_id', SelectQueryBuilder::SORT_DESC ) |
| 1620 |
->caller( __METHOD__ ) |
| 1621 |
->fetchRow(); |
| 1622 |
|
| 1623 |
|
| 1624 |
$filterHitStr = FormatJson::encode( $filterHit ); |
| 1625 |
$this->assertSame( |
| 1626 |
$latestRevId, |
| 1627 |
(int)( $filterHit->afl_rev_id ), |
| 1628 |
"AbuseLog entry updated with the revision id (full filter hit: $filterHitStr)" |
| 1629 |
); |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
public function testNullEdit() { |
| 1637 |
$this->setService( |
| 1638 |
FilterRunnerFactory::SERVICE_NAME, |
| 1639 |
$this->createNoOpMock( FilterRunnerFactory::class ) |
| 1640 |
); |
| 1641 |
|
| 1642 |
$this->createFilters( [ 24 ] ); |
| 1643 |
|
| 1644 |
$targetTitle = Title::makeTitle( NS_MAIN, 'TestNullEdit' ); |
| 1645 |
$text = 'Some text'; |
| 1646 |
$status = $this->doEdit( $targetTitle, $text, $text, 'Summary' ); |
| 1647 |
$this->assertStatusGood( $status ); |
| 1648 |
} |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
public function testContentModelChange() { |
| 1655 |
|
| 1656 |
$this->createFilters( [ 23 ] ); |
| 1657 |
|
| 1658 |
$targetTitle = Title::makeTitle( NS_MAIN, 'TestContentModelChange' ); |
| 1659 |
|
| 1660 |
$text = FormatJson::encode( [ 'key' => 'value' ] ); |
| 1661 |
$oldContent = new JsonContent( $text ); |
| 1662 |
$newContent = new WikitextContent( $text ); |
| 1663 |
|
| 1664 |
$status = $this->doEdit( $targetTitle, $oldContent, $newContent, 'Summary' ); |
| 1665 |
$this->assertStatusNotOK( $status ); |
| 1666 |
} |
| 1667 |
} |