2018-07-08 16:06:19 +00:00
|
|
|
<?php
|
2018-08-23 18:53:55 +00:00
|
|
|
|
2021-02-05 22:11:55 +00:00
|
|
|
use MediaWiki\Block\BlockUser;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\Content\JsonContent;
|
|
|
|
use MediaWiki\Context\RequestContext;
|
2020-09-20 22:31:41 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2024-05-16 10:40:53 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Flags;
|
2022-07-15 08:44:16 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterRunnerFactory;
|
2021-08-14 14:03:08 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\Handlers\FilteredActionsHandler;
|
2020-12-03 22:22:43 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\AFPData;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2024-07-30 15:03:40 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2018-08-23 18:53:55 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2019-06-23 17:19:28 +00:00
|
|
|
use MediaWiki\Storage\PageEditStash;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Title\Title;
|
|
|
|
use MediaWiki\User\User;
|
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2021-02-06 15:39:11 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2024-04-30 18:21:20 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2021-02-05 22:11:55 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2018-08-23 18:53:55 +00:00
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
/**
|
|
|
|
* Complete tests where filters are saved, actions are executed and the right
|
|
|
|
* consequences are expected to be taken
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
|
|
|
* @group AbuseFilterConsequences
|
|
|
|
* @group Database
|
2018-12-27 19:27:25 +00:00
|
|
|
* @group Large
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
2020-11-27 14:49:41 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\FilterRunner
|
2021-08-14 14:03:08 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Hooks\Handlers\FilteredActionsHandler
|
2024-04-17 00:44:40 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator
|
2021-01-03 11:12:16 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseFilterPreAuthenticationProvider
|
2020-11-26 15:23:22 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagger
|
2020-10-21 14:18:08 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\BlockAutopromoteStore
|
2020-12-18 14:05:33 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Block
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\BlockAutopromote
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\BlockingConsequence
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Consequence
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Degroup
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Disallow
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\RangeBlock
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Tag
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Throttle
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Warn
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesLookup
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesExecutor
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2021-10-11 21:32:14 +00:00
|
|
|
class AbuseFilterConsequencesTest extends MediaWikiIntegrationTestCase {
|
2020-09-16 09:15:38 +00:00
|
|
|
use AbuseFilterCreateAccountTestTrait;
|
|
|
|
use AbuseFilterUploadTestTrait;
|
|
|
|
|
2018-08-23 18:53:55 +00:00
|
|
|
/**
|
|
|
|
* @var User The user performing actions
|
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
private $user;
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2019-02-23 09:01:57 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength
|
2021-01-15 21:09:52 +00:00
|
|
|
/** @var array Filters that may be created, their key is the ID. */
|
2023-06-23 10:28:06 +00:00
|
|
|
private const FILTERS = [
|
2018-07-08 16:06:19 +00:00
|
|
|
1 => [
|
|
|
|
'af_pattern' => 'added_lines irlike "foo"',
|
|
|
|
'af_public_comments' => 'Mock filter for edit',
|
|
|
|
'actions' => [
|
|
|
|
'warn' => [
|
|
|
|
'abusefilter-my-warning'
|
|
|
|
],
|
|
|
|
'tag' => [
|
|
|
|
'filtertag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
2 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'action = "move" & moved_to_title contains "test" & moved_to_title === moved_to_text',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for move',
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_HIDDEN,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'infinity'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
3 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'action = "delete" & "test" in lcase(page_prefixedtitle) & page_prefixedtitle === article_prefixedtext',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for delete',
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_global' => 1,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
5 => [
|
2023-08-18 13:00:49 +00:00
|
|
|
// XXX Need to hardcode UTSysop here because this is a constant
|
|
|
|
'af_pattern' => 'user_name == "UTSysop"',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter',
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'firstTag',
|
|
|
|
'secondTag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
6 => [
|
|
|
|
'af_pattern' => 'edit_delta === 7',
|
|
|
|
'af_public_comments' => 'Mock filter with edit_delta',
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_HIDDEN,
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_global' => 1,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
2018-10-12 08:51:45 +00:00
|
|
|
'disallow' => [
|
|
|
|
'abusefilter-disallowed-really'
|
|
|
|
]
|
2018-07-08 16:06:19 +00:00
|
|
|
]
|
|
|
|
],
|
|
|
|
7 => [
|
2022-06-25 12:14:44 +00:00
|
|
|
'af_pattern' => 'timestamp === string(timestamp)',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter with timestamp',
|
|
|
|
'actions' => [
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
8 => [
|
|
|
|
'af_pattern' => 'added_lines_pst irlike "\\[\\[Link\\|Link\\]\\]"',
|
|
|
|
'af_public_comments' => 'Mock filter with pst',
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'NoTalkBlockSet',
|
|
|
|
'4 hours',
|
|
|
|
'4 hours'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
9 => [
|
|
|
|
'af_pattern' => 'new_size > old_size',
|
|
|
|
'af_public_comments' => 'Mock filter with size',
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_HIDDEN,
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'3 hours',
|
|
|
|
'3 hours'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
10 => [
|
|
|
|
'af_pattern' => '1 == 1',
|
|
|
|
'af_public_comments' => 'Mock throttled filter',
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_HIDDEN,
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_throttled' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'testTag'
|
|
|
|
],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'infinity',
|
|
|
|
'infinity'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
11 => [
|
|
|
|
'af_pattern' => '1 == 1',
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_public_comments' => 'Catch-all filter which throttles',
|
2018-07-08 16:06:19 +00:00
|
|
|
'actions' => [
|
|
|
|
'throttle' => [
|
|
|
|
11,
|
|
|
|
'1,3600',
|
2018-12-27 19:27:25 +00:00
|
|
|
'site'
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
|
|
|
'disallow' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
12 => [
|
2018-08-21 14:46:18 +00:00
|
|
|
'af_pattern' => 'page_title == user_name & user_name === page_title',
|
2018-07-08 16:06:19 +00:00
|
|
|
'af_public_comments' => 'Mock filter for userpage',
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'1 day'
|
|
|
|
],
|
|
|
|
'degroup' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
13 => [
|
|
|
|
'af_pattern' => '2 == 2',
|
|
|
|
'af_public_comments' => 'Another throttled mock filter',
|
|
|
|
'af_throttled' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'block' => [
|
|
|
|
'blocktalk',
|
|
|
|
'8 hours',
|
|
|
|
'1 day'
|
|
|
|
],
|
|
|
|
'degroup' => []
|
|
|
|
]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
|
|
|
14 => [
|
|
|
|
'af_pattern' => '5/int(article_text) == 3',
|
|
|
|
'af_public_comments' => 'Filter with a possible division by zero',
|
2018-12-27 19:27:25 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
15 => [
|
|
|
|
'af_pattern' => 'action contains "createaccount"',
|
|
|
|
'af_public_comments' => 'Catch-all for account creations',
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_HIDDEN,
|
2018-08-20 14:24:23 +00:00
|
|
|
'actions' => [
|
|
|
|
'disallow' => []
|
|
|
|
]
|
2018-08-23 18:53:55 +00:00
|
|
|
],
|
2018-08-24 14:22:58 +00:00
|
|
|
18 => [
|
2018-08-25 12:44:01 +00:00
|
|
|
'af_pattern' => '1 == 1',
|
|
|
|
'af_public_comments' => 'Global filter',
|
|
|
|
'af_global' => 1,
|
2018-08-24 14:22:58 +00:00
|
|
|
'actions' => [
|
2018-08-25 12:44:01 +00:00
|
|
|
'warn' => [
|
|
|
|
'abusefilter-warning'
|
|
|
|
],
|
|
|
|
'disallow' => []
|
2018-08-24 14:22:58 +00:00
|
|
|
]
|
|
|
|
],
|
2018-08-25 12:44:01 +00:00
|
|
|
19 => [
|
2023-08-18 13:00:49 +00:00
|
|
|
// XXX Need to hardcode UTSysop here because this is a constant
|
|
|
|
'af_pattern' => 'user_name === "UTSysop"',
|
2018-08-25 12:44:01 +00:00
|
|
|
'af_public_comments' => 'Another global filter',
|
|
|
|
'af_global' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'tag' => [
|
|
|
|
'globalTag'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
20 => [
|
|
|
|
'af_pattern' => 'page_title === "Cellar door"',
|
|
|
|
'af_public_comments' => 'Yet another global filter',
|
|
|
|
'af_global' => 1,
|
|
|
|
'actions' => [
|
|
|
|
'disallow' => [],
|
|
|
|
]
|
2018-07-16 12:10:36 +00:00
|
|
|
],
|
|
|
|
21 => [
|
|
|
|
'af_pattern' => '1==1',
|
|
|
|
'af_public_comments' => 'Dangerous filter',
|
|
|
|
'actions' => [
|
|
|
|
'blockautopromote' => []
|
|
|
|
]
|
2020-09-15 13:51:43 +00:00
|
|
|
],
|
|
|
|
22 => [
|
|
|
|
'af_pattern' => 'action contains "upload" & "Block me" in added_lines & file_size > 0 & ' .
|
|
|
|
'file_mime contains "/" & file_width + file_height > 0 & summary !== ""',
|
|
|
|
'af_public_comments' => 'Filter for uploads',
|
|
|
|
'actions' => [
|
|
|
|
'warn' => [
|
|
|
|
'abusefilter-random-upload'
|
|
|
|
],
|
|
|
|
'blockautopromote' => []
|
|
|
|
]
|
2018-09-26 11:38:51 +00:00
|
|
|
],
|
|
|
|
23 => [
|
|
|
|
'af_pattern' => '1 === 1',
|
|
|
|
'af_public_comments' => 'Catch-all for warning + disallow',
|
|
|
|
'actions' => [
|
|
|
|
'warn' => [
|
|
|
|
'abusefilter-my-warning'
|
|
|
|
],
|
|
|
|
'disallow' => [
|
|
|
|
'abusefilter-my-disallow'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
2021-07-04 06:25:06 +00:00
|
|
|
24 => [
|
|
|
|
// We used this for testRevIdSet()
|
|
|
|
'af_pattern' => '1 == 1',
|
|
|
|
'af_public_comments' => 'Always matches, no actions',
|
|
|
|
'actions' => []
|
|
|
|
],
|
2018-07-08 16:06:19 +00:00
|
|
|
];
|
2019-02-23 09:01:57 +00:00
|
|
|
// phpcs:enable Generic.Files.LineLength
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* @inheritDoc
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
protected function setUp(): void {
|
2018-07-08 16:06:19 +00:00
|
|
|
parent::setUp();
|
2022-07-15 08:44:16 +00:00
|
|
|
// Ensure that our user is a sysop
|
2023-08-18 13:00:49 +00:00
|
|
|
$this->user = $this->getTestSysop()->getUser();
|
2018-08-25 12:44:01 +00:00
|
|
|
|
2021-02-05 22:11:55 +00:00
|
|
|
// Pin time to avoid time shifts on relative block duration
|
|
|
|
ConvertibleTimestamp::setFakeTime( time() );
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
// Make sure that the config we're using is the one we're expecting
|
2024-07-30 15:03:40 +00:00
|
|
|
$this->overrideConfigValues( [
|
2018-12-27 19:27:25 +00:00
|
|
|
// Exclude noisy creation log
|
2024-07-30 15:03:40 +00:00
|
|
|
MainConfigNames::PageCreationLog => false,
|
|
|
|
'AbuseFilterActions' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
'throttle' => true,
|
|
|
|
'warn' => true,
|
|
|
|
'disallow' => true,
|
|
|
|
'blockautopromote' => true,
|
|
|
|
'block' => true,
|
|
|
|
'rangeblock' => true,
|
|
|
|
'degroup' => true,
|
|
|
|
'tag' => true
|
2018-08-25 12:44:01 +00:00
|
|
|
],
|
2018-07-08 16:06:19 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2019-05-24 14:54:29 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
protected function tearDown(): void {
|
2020-09-16 09:15:38 +00:00
|
|
|
$this->clearUploads();
|
2019-05-24 14:54:29 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
/**
|
2023-06-23 10:28:06 +00:00
|
|
|
* Creates new filters with the given ids, referred to self::FILTERS
|
2018-12-27 19:27:25 +00:00
|
|
|
*
|
|
|
|
* @param int[] $ids IDs of the filters to create
|
|
|
|
*/
|
2023-08-18 13:00:49 +00:00
|
|
|
private function createFilters( $ids ) {
|
2018-12-27 19:27:25 +00:00
|
|
|
global $wgAbuseFilterActions;
|
2022-08-31 22:57:58 +00:00
|
|
|
|
|
|
|
$dbw = $this->getDb();
|
2018-12-27 19:27:25 +00:00
|
|
|
$defaultRowSection = [
|
2018-03-08 21:16:25 +00:00
|
|
|
'af_actor' => 1,
|
2022-08-31 22:57:58 +00:00
|
|
|
'af_timestamp' => $dbw->timestamp(),
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_group' => 'default',
|
|
|
|
'af_comments' => '',
|
|
|
|
'af_hit_count' => 0,
|
|
|
|
'af_enabled' => 1,
|
2024-05-16 10:40:53 +00:00
|
|
|
'af_hidden' => Flags::FILTER_PUBLIC,
|
2018-12-27 19:27:25 +00:00
|
|
|
'af_throttled' => 0,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_global' => 0
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $ids as $id ) {
|
2023-06-23 10:28:06 +00:00
|
|
|
$filter = self::FILTERS[$id] + $defaultRowSection;
|
2018-12-27 19:27:25 +00:00
|
|
|
$actions = $filter['actions'];
|
|
|
|
unset( $filter['actions'] );
|
|
|
|
$filter[ 'af_actions' ] = implode( ',', array_keys( $actions ) );
|
|
|
|
$filter[ 'af_id' ] = $id;
|
|
|
|
|
2024-04-02 18:26:25 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
->insertInto( 'abuse_filter' )
|
|
|
|
->row( $filter )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2018-12-27 19:27:25 +00:00
|
|
|
|
|
|
|
$actionsRows = [];
|
|
|
|
foreach ( array_filter( $wgAbuseFilterActions ) as $action => $_ ) {
|
|
|
|
if ( isset( $actions[$action] ) ) {
|
|
|
|
$parameters = $actions[$action];
|
|
|
|
|
|
|
|
$thisRow = [
|
|
|
|
'afa_filter' => $id,
|
|
|
|
'afa_consequence' => $action,
|
|
|
|
'afa_parameters' => implode( "\n", $parameters )
|
|
|
|
];
|
|
|
|
$actionsRows[] = $thisRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 18:26:25 +00:00
|
|
|
if ( $actionsRows ) {
|
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
->insertInto( 'abuse_filter_action' )
|
|
|
|
->rows( $actionsRows )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
|
|
|
}
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:53:55 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title Title of the page to edit
|
2022-07-15 08:44:16 +00:00
|
|
|
* @param Content $content The new content of the page
|
2018-08-23 18:53:55 +00:00
|
|
|
* @param string $summary The summary of the edit
|
|
|
|
* @return string The status of the operation, as returned by the API.
|
|
|
|
*/
|
2022-07-15 08:44:16 +00:00
|
|
|
private function stashEdit( Title $title, Content $content, string $summary ) {
|
2021-12-16 22:51:24 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2023-08-18 13:00:49 +00:00
|
|
|
$pageUpdater = $services->getWikiPageFactory()->newFromTitle( $title )->newPageUpdater( $this->user );
|
2021-12-16 22:51:24 +00:00
|
|
|
return $services->getPageEditStash()->parseAndCache(
|
2023-08-18 13:00:49 +00:00
|
|
|
$pageUpdater,
|
2022-07-15 08:44:16 +00:00
|
|
|
$content,
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user,
|
2019-06-23 17:19:28 +00:00
|
|
|
$summary
|
|
|
|
);
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title Title of the page to edit
|
2022-07-15 08:44:16 +00:00
|
|
|
* @param Content|string $oldText Old content of the page
|
|
|
|
* @param Content|string $newText The new content of the page
|
2018-07-08 16:06:19 +00:00
|
|
|
* @param string $summary The summary of the edit
|
2018-08-23 18:53:55 +00:00
|
|
|
* @param bool|null $fromStash Whether to stash the edit. Null means no stashing, false means
|
|
|
|
* stash the edit but don't reuse it for saving, true means stash and reuse.
|
2018-07-08 16:06:19 +00:00
|
|
|
* @return Status
|
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
private function doEdit( Title $title, $oldText, $newText, $summary, $fromStash = null ) {
|
2021-12-16 22:51:24 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
$page = $services->getWikiPageFactory()->newFromTitle( $title );
|
2018-12-27 19:27:25 +00:00
|
|
|
if ( !$page->exists() ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$status = $this->editPage(
|
2021-07-04 06:25:06 +00:00
|
|
|
$page,
|
2019-08-26 13:01:09 +00:00
|
|
|
$oldText,
|
2022-06-28 20:46:45 +00:00
|
|
|
__METHOD__ . ' page creation'
|
2019-08-26 13:01:09 +00:00
|
|
|
);
|
|
|
|
if ( !$status->isGood() ) {
|
2023-08-18 13:00:49 +00:00
|
|
|
throw new RuntimeException( "Could not create test page. $status" );
|
2019-08-26 13:01:09 +00:00
|
|
|
}
|
2022-07-15 08:44:16 +00:00
|
|
|
$page->clear();
|
2019-08-26 13:01:09 +00:00
|
|
|
$title->resetArticleID( -1 );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-08-23 18:53:55 +00:00
|
|
|
if ( $fromStash !== null ) {
|
|
|
|
// If we want to save from stash, submit the same text
|
|
|
|
$stashText = $newText;
|
|
|
|
if ( $fromStash === false ) {
|
|
|
|
// Otherwise, stash some random text which won't match the actual edit
|
|
|
|
$stashText = md5( uniqid( rand(), true ) );
|
|
|
|
}
|
2022-07-15 08:44:16 +00:00
|
|
|
$stashContent = $stashText instanceof Content ? $stashText : new WikitextContent( $stashText );
|
|
|
|
$stashResult = $this->stashEdit( $title, $stashContent, $summary );
|
2019-06-23 17:19:28 +00:00
|
|
|
if ( $stashResult !== PageEditStash::ERROR_NONE ) {
|
2023-06-07 15:41:20 +00:00
|
|
|
throw new RuntimeException( "The edit cannot be stashed, got the following error: $stashResult" );
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2022-07-15 08:44:16 +00:00
|
|
|
$content = $newText instanceof Content
|
|
|
|
? $newText
|
|
|
|
: ContentHandler::makeContent( $newText, $title );
|
2019-08-26 13:01:09 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
$context->setUser( $this->user );
|
2021-07-04 06:25:06 +00:00
|
|
|
$context->setWikiPage( $page );
|
2019-08-26 13:01:09 +00:00
|
|
|
|
2021-08-14 14:03:08 +00:00
|
|
|
$hooksHandler = new FilteredActionsHandler(
|
2021-12-16 22:51:24 +00:00
|
|
|
$services->getStatsdDataFactory(),
|
2021-08-14 14:03:08 +00:00
|
|
|
AbuseFilterServices::getFilterRunnerFactory(),
|
|
|
|
AbuseFilterServices::getVariableGeneratorFactory(),
|
2023-05-24 20:09:59 +00:00
|
|
|
AbuseFilterServices::getEditRevUpdater(),
|
2023-08-06 11:44:16 +00:00
|
|
|
AbuseFilterServices::getBlockedDomainFilter(),
|
2023-06-08 19:26:33 +00:00
|
|
|
$services->getPermissionManager()
|
2021-08-14 14:03:08 +00:00
|
|
|
);
|
|
|
|
$hooksHandler->onEditFilterMergedContent( $context, $content, $status, $summary,
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user, false );
|
|
|
|
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
// Edit the page in case the test will expect for it to exist
|
|
|
|
$this->editPage(
|
2021-07-04 06:25:06 +00:00
|
|
|
$page,
|
2019-08-26 13:01:09 +00:00
|
|
|
$newText,
|
|
|
|
$summary,
|
2022-06-28 20:46:45 +00:00
|
|
|
NS_MAIN,
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user
|
|
|
|
);
|
2022-07-15 08:44:16 +00:00
|
|
|
$page->clear();
|
|
|
|
$title->resetArticleID( -1 );
|
2019-08-26 13:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an action to filter
|
|
|
|
*
|
|
|
|
* @param array $params Parameters of the action
|
2018-12-27 19:27:25 +00:00
|
|
|
* @return Status
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function doAction( $params ) {
|
2022-07-15 08:44:16 +00:00
|
|
|
$target = Title::newFromTextThrow( $params['target'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
switch ( $params['action'] ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
case 'edit':
|
2018-12-27 19:27:25 +00:00
|
|
|
$status = $this->doEdit( $target, $params['oldText'], $params['newText'], $params['summary'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
2018-08-23 18:53:55 +00:00
|
|
|
case 'stashedit':
|
|
|
|
$stashStatus = $params['stashType'] === 'hit';
|
|
|
|
$status = $this->doEdit(
|
|
|
|
$target,
|
|
|
|
$params['oldText'],
|
|
|
|
$params['newText'],
|
|
|
|
$params['summary'],
|
|
|
|
$stashStatus
|
|
|
|
);
|
|
|
|
break;
|
2018-07-08 16:06:19 +00:00
|
|
|
case 'move':
|
2019-08-26 13:01:09 +00:00
|
|
|
// Ensure that the page exists
|
|
|
|
$this->getExistingTestPage( $target );
|
2019-04-17 13:16:58 +00:00
|
|
|
$newTitle = isset( $params['newTitle'] )
|
2022-07-15 08:44:16 +00:00
|
|
|
? Title::newFromTextThrow( $params['newTitle'] )
|
2019-04-17 13:16:58 +00:00
|
|
|
: $this->getNonExistingTestPage()->getTitle();
|
2021-08-10 20:06:39 +00:00
|
|
|
$status = $this->getServiceContainer()
|
|
|
|
->getMovePageFactory()
|
|
|
|
->newMovePage( $target, $newTitle )
|
|
|
|
->move( $this->user, 'AbuseFilter move test', false );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'delete':
|
2019-08-26 13:01:09 +00:00
|
|
|
$page = $this->getExistingTestPage( $target );
|
2020-03-19 00:44:26 +00:00
|
|
|
$status = $page->doDeleteArticleReal(
|
|
|
|
'Testing deletion in AbuseFilter',
|
|
|
|
$this->user
|
|
|
|
);
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'createaccount':
|
2020-09-16 09:15:38 +00:00
|
|
|
$status = $this->createAccount( $params['username'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
2020-09-15 13:51:43 +00:00
|
|
|
case 'upload':
|
2020-09-16 09:15:38 +00:00
|
|
|
[ $status, $this->clearPath ] = $this->doUpload(
|
|
|
|
$this->user,
|
2020-09-15 13:51:43 +00:00
|
|
|
$params['target'],
|
|
|
|
$params['newText'] ?? 'AbuseFilter test upload',
|
|
|
|
$params['summary'] ?? 'Test'
|
|
|
|
);
|
|
|
|
break;
|
2018-12-27 19:27:25 +00:00
|
|
|
default:
|
2018-08-23 18:53:55 +00:00
|
|
|
throw new UnexpectedValueException( 'Unrecognized action ' . $params['action'] );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear cache since we'll need to retrieve some fresh data about the user
|
|
|
|
// like blocks and groups later when checking expected values
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->user->clearInstanceCache();
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* @param array[] $actionsParams Arrays of parameters for every action
|
|
|
|
* @return Status[]
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function doActions( $actionsParams ) {
|
|
|
|
$ret = [];
|
|
|
|
foreach ( $actionsParams as $params ) {
|
|
|
|
$ret[] = $this->doAction( $params );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
2018-12-27 19:27:25 +00:00
|
|
|
return $ret;
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* Helper function to retrieve change tags applied to an edit or log entry
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
2018-12-27 19:27:25 +00:00
|
|
|
* @param array $actionParams As given by the data provider
|
|
|
|
* @return string[] The applied tags
|
2018-07-08 16:06:19 +00:00
|
|
|
*/
|
2018-12-27 19:27:25 +00:00
|
|
|
private function getActionTags( $actionParams ) {
|
2022-08-31 22:57:58 +00:00
|
|
|
$dbw = $this->getDb();
|
2022-07-15 08:44:16 +00:00
|
|
|
$title = Title::newFromTextThrow( $actionParams['target'] );
|
2024-03-29 10:08:41 +00:00
|
|
|
$store = $this->getServiceContainer()->getChangeTagsStore();
|
2018-08-23 18:53:55 +00:00
|
|
|
if ( $actionParams['action'] === 'edit' || $actionParams['action'] === 'stashedit' ) {
|
2024-03-29 10:08:41 +00:00
|
|
|
return $store->getTags( $dbw, null, $title->getLatestRevID() );
|
2018-08-20 14:24:23 +00:00
|
|
|
}
|
2018-12-27 19:27:25 +00:00
|
|
|
|
2020-02-22 13:50:43 +00:00
|
|
|
$logType = $actionParams['action'] === 'createaccount' ? 'newusers' : $actionParams['action'];
|
2020-09-16 09:15:38 +00:00
|
|
|
$logAction = $logType === 'newusers' ? 'create2' : $logType;
|
2024-04-30 18:21:20 +00:00
|
|
|
$id = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( 'log_id' )
|
|
|
|
->from( 'logging' )
|
|
|
|
->where( [
|
2022-07-15 08:44:16 +00:00
|
|
|
'log_namespace' => $title->getNamespace(),
|
2020-02-22 13:50:43 +00:00
|
|
|
'log_title' => $title->getDBkey(),
|
|
|
|
'log_type' => $logType,
|
|
|
|
'log_action' => $logAction
|
2024-04-30 18:21:20 +00:00
|
|
|
] )
|
|
|
|
->orderBy( 'log_id', SelectQueryBuilder::SORT_DESC )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchField();
|
2020-02-22 13:50:43 +00:00
|
|
|
if ( !$id ) {
|
|
|
|
$this->fail( 'Could not find the action in the logging table.' );
|
2018-08-20 14:24:23 +00:00
|
|
|
}
|
2024-03-29 10:08:41 +00:00
|
|
|
return $store->getTags( $dbw, null, null, (int)$id );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
/**
|
2019-02-23 09:01:57 +00:00
|
|
|
* Checks that consequences are effectively taken and builds an array of expected and actual
|
|
|
|
* consequences which can be compared.
|
2018-12-27 19:27:25 +00:00
|
|
|
*
|
|
|
|
* @param Status $result As returned by self::doAction
|
|
|
|
* @param array $actionParams As it's given by data providers
|
2022-03-05 17:00:09 +00:00
|
|
|
* @param array $expectedConsequences
|
2018-12-27 19:27:25 +00:00
|
|
|
* @return array [ expected consequences, actual consequences ]
|
|
|
|
*/
|
2022-03-05 17:00:09 +00:00
|
|
|
private function checkConsequences( $result, $actionParams, $expectedConsequences ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
$expectedErrors = [];
|
|
|
|
$testErrorMessage = false;
|
2022-03-05 17:00:09 +00:00
|
|
|
foreach ( $expectedConsequences as $consequence => $ids ) {
|
2018-07-08 16:06:19 +00:00
|
|
|
foreach ( $ids as $id ) {
|
2023-06-23 10:28:06 +00:00
|
|
|
$params = self::FILTERS[$id]['actions'][$consequence];
|
2018-07-08 16:06:19 +00:00
|
|
|
switch ( $consequence ) {
|
|
|
|
case 'warn':
|
|
|
|
// Aborts the hook with the warning message as error.
|
2022-03-05 17:00:09 +00:00
|
|
|
$expectedErrors[] = $params[0] ?? 'abusefilter-warning';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'disallow':
|
2018-10-12 08:51:45 +00:00
|
|
|
// Aborts the hook with the disallow message error.
|
2022-03-05 17:00:09 +00:00
|
|
|
$expectedErrors[] = $params[0] ?? 'abusefilter-disallowed';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'block':
|
|
|
|
// Aborts the hook with 'abusefilter-blocked-display' error. Should block
|
|
|
|
// the user with expected duration and options.
|
2019-08-26 13:01:09 +00:00
|
|
|
$userBlock = $this->user->getBlock( false );
|
2018-07-08 16:06:19 +00:00
|
|
|
|
|
|
|
if ( !$userBlock ) {
|
|
|
|
$testErrorMessage = "User isn't blocked.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$shouldPreventTalkEdit = $params[0] === 'blocktalk';
|
2019-08-26 13:01:09 +00:00
|
|
|
$edittalkCheck = $userBlock->appliesToUsertalk( $this->user->getTalkPage() ) ===
|
2019-02-23 09:01:57 +00:00
|
|
|
$shouldPreventTalkEdit;
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$edittalkCheck ) {
|
|
|
|
$testErrorMessage = 'The expected block option "edittalk" options does not ' .
|
|
|
|
'match the actual one.';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:11:55 +00:00
|
|
|
$expectedExpiry = BlockUser::parseExpiryInput( $params[2] );
|
|
|
|
$actualExpiry = $userBlock->getExpiry();
|
|
|
|
if ( !wfIsInfinity( $actualExpiry ) ) {
|
|
|
|
$actualExpiry = wfTimestamp( TS_MW, $actualExpiry );
|
|
|
|
}
|
|
|
|
if ( $expectedExpiry === false || $expectedExpiry !== $actualExpiry ) {
|
2018-08-16 17:08:13 +00:00
|
|
|
$testErrorMessage = "The expected block expiry ($expectedExpiry) does not " .
|
|
|
|
"match the actual one ($actualExpiry).";
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:00:09 +00:00
|
|
|
$expectedErrors[] = 'abusefilter-blocked-display';
|
2018-07-08 16:06:19 +00:00
|
|
|
break;
|
|
|
|
case 'degroup':
|
|
|
|
// Aborts the hook with 'abusefilter-degrouped' error and degroups the user.
|
2022-03-05 17:00:09 +00:00
|
|
|
$expectedErrors[] = 'abusefilter-degrouped';
|
2021-05-06 13:35:35 +00:00
|
|
|
$ugm = MediaWikiServices::getInstance()->getUserGroupManager();
|
|
|
|
$groupCheck = !in_array( 'sysop', $ugm->getUserEffectiveGroups( $this->user ) );
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$groupCheck ) {
|
|
|
|
$testErrorMessage = 'The user was not degrouped.';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'tag':
|
2019-08-26 13:01:09 +00:00
|
|
|
// Only adds tags, to be retrieved in change_tag table.
|
2018-12-27 19:27:25 +00:00
|
|
|
$appliedTags = $this->getActionTags( $actionParams );
|
2018-11-27 17:56:02 +00:00
|
|
|
$tagCheck = count( array_diff( $params, $appliedTags ) ) === 0;
|
2018-07-08 16:06:19 +00:00
|
|
|
if ( !$tagCheck ) {
|
2018-12-27 19:27:25 +00:00
|
|
|
$expectedTags = implode( ', ', $params );
|
|
|
|
$actualTags = implode( ', ', $appliedTags );
|
2018-07-08 16:06:19 +00:00
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
$testErrorMessage = "Expected the action to have the following tags: $expectedTags. " .
|
2018-07-08 16:06:19 +00:00
|
|
|
"Got the following instead: $actualTags.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'throttle':
|
2018-12-27 19:27:25 +00:00
|
|
|
throw new UnexpectedValueException( 'Use self::testThrottleConsequence to test throttling' );
|
2018-07-16 12:10:36 +00:00
|
|
|
case 'blockautopromote':
|
|
|
|
// Aborts the hook with 'abusefilter-autopromote-blocked' error and prevent promotion.
|
2022-03-05 17:00:09 +00:00
|
|
|
$expectedErrors[] = 'abusefilter-autopromote-blocked';
|
2020-10-21 14:18:08 +00:00
|
|
|
$value = AbuseFilterServices::getBlockAutopromoteStore()
|
|
|
|
->getAutoPromoteBlockStatus( $this->user );
|
2019-07-06 23:35:03 +00:00
|
|
|
if ( !$value ) {
|
2018-07-16 12:10:36 +00:00
|
|
|
$testErrorMessage = "The key for blocking autopromotion wasn't set.";
|
|
|
|
}
|
|
|
|
break;
|
2018-12-27 19:27:25 +00:00
|
|
|
default:
|
2019-08-26 13:01:09 +00:00
|
|
|
throw new UnexpectedValueException( "Consequence not recognized: $consequence." );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $testErrorMessage ) {
|
2018-12-08 15:29:46 +00:00
|
|
|
$this->fail( $testErrorMessage );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$actual = [];
|
2024-04-12 20:48:59 +00:00
|
|
|
foreach ( $result->getMessages() as $msg ) {
|
|
|
|
if ( strpos( $msg->getKey(), 'abusefilter' ) !== false ) {
|
|
|
|
$actual[] = $msg->getKey();
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:00:09 +00:00
|
|
|
sort( $expectedErrors );
|
2018-07-16 12:10:36 +00:00
|
|
|
sort( $actual );
|
2022-03-05 17:00:09 +00:00
|
|
|
return [ $expectedErrors, $actual ];
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 14:02:56 +00:00
|
|
|
/**
|
|
|
|
* @param array $actionParams
|
|
|
|
* @param array $expectedConsequences
|
|
|
|
*/
|
|
|
|
private function assertAbuseLog( $actionParams, $expectedConsequences ): void {
|
|
|
|
$consequencesByFilter = [];
|
|
|
|
foreach ( $expectedConsequences as $consequence => $ids ) {
|
|
|
|
foreach ( $ids as $id ) {
|
|
|
|
$consequencesByFilter[$id][] = $consequence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $consequencesByFilter as $filter => $consequences ) {
|
|
|
|
$action = $actionParams['action'];
|
|
|
|
if ( $action === 'stashedit' ) {
|
|
|
|
$action = 'edit';
|
|
|
|
}
|
|
|
|
$title = Title::newFromTextThrow( $actionParams['target'] );
|
|
|
|
|
2024-04-30 18:21:20 +00:00
|
|
|
$row = $this->getDb()->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'abuse_filter_log' )
|
|
|
|
->where( [
|
2022-07-15 14:02:56 +00:00
|
|
|
'afl_filter_id' => $filter,
|
|
|
|
'afl_global' => 0,
|
2024-04-30 18:21:20 +00:00
|
|
|
] )
|
|
|
|
->orderBy( 'afl_id', SelectQueryBuilder::SORT_DESC )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRow();
|
2022-07-15 14:02:56 +00:00
|
|
|
$this->assertNotFalse( $row );
|
|
|
|
|
|
|
|
$dumpStr = FormatJson::encode( $row );
|
|
|
|
$this->assertSame( $action, $row->afl_action, $dumpStr );
|
|
|
|
$this->assertNull( $row->afl_wiki, $dumpStr );
|
|
|
|
if ( $action === 'createaccount' ) {
|
|
|
|
$this->assertSame( $actionParams['username'], $row->afl_user_text, $dumpStr );
|
|
|
|
$this->assertSame( -1, (int)$row->afl_namespace, $dumpStr );
|
|
|
|
} else {
|
2023-08-18 13:00:49 +00:00
|
|
|
$this->assertSame( $this->user->getName(), $row->afl_user_text, $dumpStr );
|
2022-07-15 14:02:56 +00:00
|
|
|
$this->assertSame( $title->getNamespace(), (int)$row->afl_namespace, $dumpStr );
|
|
|
|
$this->assertSame( $title->getDBkey(), $row->afl_title, $dumpStr );
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
in_array( 'disallow', $consequences )
|
|
|
|
&& array_intersect( $consequences, [ 'block', 'blockautopromote', 'degroup' ] )
|
|
|
|
) {
|
|
|
|
$consequences = array_diff( $consequences, [ 'disallow' ] );
|
|
|
|
}
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
$consequences,
|
|
|
|
explode( ',', $row->afl_actions ),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
$dumpStr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:27:25 +00:00
|
|
|
/**
|
|
|
|
* Creates new filters, execute an action and check the consequences
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
2022-03-05 17:00:09 +00:00
|
|
|
* @param array $expectedConsequences The consequences we're expecting
|
2018-12-27 19:27:25 +00:00
|
|
|
* @dataProvider provideFilters
|
2022-07-15 14:02:56 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseLogger
|
2018-12-27 19:27:25 +00:00
|
|
|
*/
|
2022-03-05 17:00:09 +00:00
|
|
|
public function testFilterConsequences( $createIds, $actionParams, $expectedConsequences ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-27 19:27:25 +00:00
|
|
|
$result = $this->doAction( $actionParams );
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $expectedConsequences );
|
2018-12-27 19:27:25 +00:00
|
|
|
|
2018-07-08 16:06:19 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-07-08 16:06:19 +00:00
|
|
|
);
|
2022-07-15 14:02:56 +00:00
|
|
|
|
|
|
|
$this->assertAbuseLog( $actionParams, $expectedConsequences );
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 19:27:25 +00:00
|
|
|
* Data provider for testFilterConsequences. For every test case, we pass
|
2023-06-23 10:28:06 +00:00
|
|
|
* - an array with the IDs of the filters to be created (listed in self::FILTERS),
|
2018-08-20 14:24:23 +00:00
|
|
|
* - an array with details of the action to execute in order to trigger the filters,
|
|
|
|
* - an array of expected consequences of the form
|
|
|
|
* [ 'consequence name' => [ IDs of the filter to take its parameters from ] ]
|
|
|
|
* Such IDs may be more than one if we have a warning that is shown twice.
|
2018-07-08 16:06:19 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideFilters() {
|
2023-08-18 13:00:49 +00:00
|
|
|
// XXX Need to hardcode the username of $this->user here.
|
|
|
|
$username = 'UTSysop';
|
2018-07-08 16:06:19 +00:00
|
|
|
return [
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "edit" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'warn' => [ 1 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "move" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
2018-07-08 16:06:19 +00:00
|
|
|
'newTitle' => 'Another test page'
|
|
|
|
],
|
2022-03-05 17:00:09 +00:00
|
|
|
[ 'block' => [ 2 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-08 15:29:46 +00:00
|
|
|
'Basic test for "delete" action' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2, 3 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'degroup' => [ 3 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Basic test for "createaccount", no consequences.' => [
|
2021-01-09 23:26:29 +00:00
|
|
|
[ 1, 2, 3 ],
|
2018-07-08 16:06:19 +00:00
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
2018-07-08 16:06:19 +00:00
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Basic test for "createaccount", disallowed.' => [
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 15 ] ]
|
|
|
|
],
|
|
|
|
'Check that all tags are applied' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 5 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
2023-08-18 13:00:49 +00:00
|
|
|
'target' => "User:$username",
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 5 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that degroup and block are executed together' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Link',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that the block duration is the longer one' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 8, 9 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Whatever',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Whatever is whatever',
|
|
|
|
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]',
|
|
|
|
'summary' => 'Whatever'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that throttled filters only execute "safe" actions' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 10 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Buffalo',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'Buffalo',
|
|
|
|
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.',
|
|
|
|
'summary' => 'Buffalo!'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 10 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that degroup and block are both executed and degroup warning is shown twice' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 1, 3, 7, 12 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
2023-08-18 13:00:49 +00:00
|
|
|
'target' => "User:$username",
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'A couple of lines about me...',
|
|
|
|
'summary' => 'My user page'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ]
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that every throttled filter only executes "safe" actions' => [
|
2018-07-08 16:06:19 +00:00
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
2018-07-08 16:06:19 +00:00
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[ 'tag' => [ 10 ] ]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
'Check that runtime exceptions (division by zero) are correctly handled' => [
|
2018-08-20 14:24:23 +00:00
|
|
|
[ 14 ],
|
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => '0',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => 'Some summary'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-07-16 12:10:36 +00:00
|
|
|
'Test for blockautopromote action.' => [
|
|
|
|
[ 21 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Rainbow',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => '...',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'blockautopromote' => [ 21 ] ],
|
|
|
|
],
|
2022-07-15 14:02:56 +00:00
|
|
|
'No consequences' => [
|
|
|
|
[ 8 ],
|
2018-08-20 14:24:23 +00:00
|
|
|
[
|
2018-12-27 19:27:25 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Anything',
|
2018-08-20 14:24:23 +00:00
|
|
|
'oldText' => 'Bar',
|
|
|
|
'newText' => 'Foo',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
[]
|
2018-08-20 14:24:23 +00:00
|
|
|
],
|
2020-09-15 13:51:43 +00:00
|
|
|
'Test upload action' => [
|
|
|
|
[ 5 ],
|
|
|
|
[
|
|
|
|
'action' => 'upload',
|
2022-07-15 08:44:16 +00:00
|
|
|
'target' => 'File:MyFile.svg',
|
2020-09-15 13:51:43 +00:00
|
|
|
],
|
|
|
|
[ 'tag' => [ 5 ] ]
|
|
|
|
],
|
|
|
|
'Test upload action 2' => [
|
|
|
|
[ 22 ],
|
|
|
|
[
|
|
|
|
'action' => 'upload',
|
2022-07-15 08:44:16 +00:00
|
|
|
'target' => 'File:MyFile.svg',
|
2020-09-15 13:51:43 +00:00
|
|
|
'newText' => 'Block me please!',
|
|
|
|
'summary' => 'Asking to be blocked'
|
|
|
|
],
|
|
|
|
[ 'warn' => [ 22 ] ]
|
|
|
|
],
|
2018-12-27 19:27:25 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check an array of results from self::doAction to ensure that all but the last actions have been
|
|
|
|
* executed (i.e. no errors).
|
|
|
|
* @param Status[] $results As returned by self::doActions
|
|
|
|
* @return Status The Status of the last action, to be later checked with self::checkConsequences
|
|
|
|
*/
|
|
|
|
private function checkThrottleConsequence( $results ) {
|
|
|
|
$finalRes = array_pop( $results );
|
|
|
|
foreach ( $results as $result ) {
|
|
|
|
if ( !$result->isGood() ) {
|
2019-02-23 09:01:57 +00:00
|
|
|
$this->fail( 'Only the last actions should have triggered a filter; the other ones ' .
|
2021-02-06 15:39:11 +00:00
|
|
|
'should have been allowed.' );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $finalRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like self::testFilterConsequences but for throttle, which deserves a special treatment
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array[] $actionsParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideThrottleFilters
|
2022-07-15 14:02:56 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseLogger
|
2018-12-27 19:27:25 +00:00
|
|
|
*/
|
|
|
|
public function testThrottle( $createIds, $actionsParams, $consequences ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2021-07-29 14:12:06 +00:00
|
|
|
$results = $this->doActions( $actionsParams );
|
2018-12-27 19:27:25 +00:00
|
|
|
$res = $this->checkThrottleConsequence( $results );
|
|
|
|
$lastParams = array_pop( $actionsParams );
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expected, $actual ] = $this->checkConsequences( $res, $lastParams, $consequences );
|
2018-12-27 19:27:25 +00:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-12-27 19:27:25 +00:00
|
|
|
);
|
2022-07-15 14:02:56 +00:00
|
|
|
|
|
|
|
$this->assertAbuseLog( $lastParams, $consequences );
|
2018-12-27 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testThrottle. For every test case, we pass
|
2023-06-23 10:28:06 +00:00
|
|
|
* - an array with the IDs of the filters to be created (listed in self::FILTERS),
|
2019-02-23 09:01:57 +00:00
|
|
|
* - an array of array, where every sub-array holds the details of the action to execute in
|
|
|
|
* order to trigger the filters, each one like in self::provideFilters
|
2018-12-27 19:27:25 +00:00
|
|
|
* - an array of expected consequences for the last action (i.e. after throttling) of the form
|
|
|
|
* [ 'consequence name' => [ IDs of the filter to take its parameters from ] ]
|
|
|
|
* Such IDs may be more than one if we have a warning that is shown twice.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideThrottleFilters() {
|
2018-12-27 19:27:25 +00:00
|
|
|
return [
|
|
|
|
'Basic test for throttling edits' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Throttle',
|
|
|
|
'oldText' => 'What is throttle?',
|
|
|
|
'newText' => 'Throttle is something that should happen...',
|
|
|
|
'summary' => 'Throttle'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Throttle',
|
|
|
|
'oldText' => 'Throttle is something that should happen...',
|
|
|
|
'newText' => '... Right now!',
|
|
|
|
'summary' => 'Throttle'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "move"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Throttle test',
|
|
|
|
'newTitle' => 'Another throttle test'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Another throttle test',
|
|
|
|
'newTitle' => 'Yet another throttle test'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "delete"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for throttling "createaccount"' => [
|
|
|
|
[ 11 ],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:YetAnotherUser',
|
|
|
|
'username' => 'YetAnotherUser'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 11 ] ]
|
2018-09-26 11:38:51 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like self::testFilterConsequences but for warn, which deserves a special treatment.
|
|
|
|
* Data provider passes parameters for a single action, which we repeat twice
|
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array[] $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param int[] $warnIDs IDs of the filters which will warn us
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideWarnFilters
|
2022-07-15 14:02:56 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseLogger
|
2018-09-26 11:38:51 +00:00
|
|
|
*/
|
|
|
|
public function testWarn( $createIds, $actionParams, $warnIDs, $consequences ) {
|
|
|
|
$this->createFilters( $createIds );
|
|
|
|
$params = [ $actionParams, $actionParams ];
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $warnedStatus, $finalStatus ] = $this->doActions( $params );
|
2018-09-26 11:38:51 +00:00
|
|
|
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expectedWarn, $actualWarn ] = $this->checkConsequences(
|
2018-09-26 11:38:51 +00:00
|
|
|
$warnedStatus,
|
|
|
|
$actionParams,
|
|
|
|
[ 'warn' => $warnIDs ]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedWarn,
|
|
|
|
$actualWarn,
|
|
|
|
'The error messages for the first action do not match.'
|
|
|
|
);
|
|
|
|
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expectedFinal, $actualFinal ] = $this->checkConsequences(
|
2018-09-26 11:38:51 +00:00
|
|
|
$finalStatus,
|
|
|
|
$actionParams,
|
|
|
|
$consequences
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedFinal,
|
|
|
|
$actualFinal,
|
|
|
|
'The error messages for the second action do not match.'
|
|
|
|
);
|
2022-07-15 14:02:56 +00:00
|
|
|
|
|
|
|
$this->assertAbuseLog( $actionParams, $consequences );
|
2018-09-26 11:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testWarn. For every test case, we pass
|
2023-06-23 10:28:06 +00:00
|
|
|
* - an array with the IDs of the filters to be created (listed in self::FILTERS),
|
2018-09-26 11:38:51 +00:00
|
|
|
* - an array with action parameters, like in self::provideFilters. This will be executed twice.
|
|
|
|
* - an array of IDs of the filter which should give a warning
|
|
|
|
* - an array of expected consequences for the last action (i.e. after throttling) of the form
|
|
|
|
* [ 'consequence name' => [ IDs of the filter to take its parameters from ] ]
|
|
|
|
* Such IDs may be more than one if we have a warning that is shown twice.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideWarnFilters() {
|
2018-09-26 11:38:51 +00:00
|
|
|
return [
|
|
|
|
'Basic test for warning and then tag' => [
|
|
|
|
[ 1 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Foo',
|
|
|
|
'oldText' => 'Neutral text',
|
|
|
|
'newText' => 'First foo version',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 1 ],
|
|
|
|
[ 'tag' => [ 1 ] ],
|
2018-07-08 16:06:19 +00:00
|
|
|
],
|
2018-09-26 11:38:51 +00:00
|
|
|
'Basic test for warning on "move"' => [
|
|
|
|
[ 23 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test warn',
|
|
|
|
'newTitle' => 'Another warn test'
|
|
|
|
],
|
|
|
|
[ 23 ],
|
|
|
|
[ 'disallow' => [ 23 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for warning on "delete"' => [
|
|
|
|
[ 23 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Warned'
|
|
|
|
],
|
|
|
|
[ 23 ],
|
|
|
|
[ 'disallow' => [ 23 ] ]
|
|
|
|
],
|
|
|
|
'Basic test for warning on "createaccount"' => [
|
|
|
|
[ 23 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherWarnedUser',
|
|
|
|
'username' => 'AnotherWarnedUser'
|
|
|
|
],
|
|
|
|
[ 23 ],
|
|
|
|
[ 'disallow' => [ 23 ] ]
|
|
|
|
]
|
2018-07-08 16:06:19 +00:00
|
|
|
];
|
|
|
|
}
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param string[] $usedVars The variables effectively computed by filters in $createIds.
|
|
|
|
* We'll search these in the stored dump.
|
2021-01-02 13:41:31 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore
|
2018-12-28 16:01:19 +00:00
|
|
|
* @dataProvider provideFiltersAndVariables
|
|
|
|
*/
|
|
|
|
public function testVarDump( $createIds, $actionParams, $usedVars ) {
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-12-28 16:01:19 +00:00
|
|
|
// We don't care about consequences here
|
2018-08-23 18:53:55 +00:00
|
|
|
$this->doAction( $actionParams );
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
// We just take a dump from a single filters, as they're all identical for the same action
|
2024-03-22 13:40:06 +00:00
|
|
|
$row = $this->getDb()->newSelectQueryBuilder()
|
|
|
|
->select( 'afl_var_dump, afl_ip' )
|
2024-04-30 18:21:20 +00:00
|
|
|
->from( 'abuse_filter_log' )
|
|
|
|
->orderBy( 'afl_timestamp', SelectQueryBuilder::SORT_DESC )
|
|
|
|
->caller( __METHOD__ )
|
2024-03-22 13:40:06 +00:00
|
|
|
->fetchRow();
|
2018-12-28 16:01:19 +00:00
|
|
|
|
2024-03-22 13:40:06 +00:00
|
|
|
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row )->getVars();
|
2018-12-28 16:01:19 +00:00
|
|
|
|
|
|
|
$interestingVars = array_intersect_key( $vars, array_fill_keys( $usedVars, true ) );
|
|
|
|
|
|
|
|
sort( $usedVars );
|
|
|
|
ksort( $interestingVars );
|
|
|
|
$this->assertEquals(
|
|
|
|
$usedVars,
|
|
|
|
array_keys( $interestingVars ),
|
|
|
|
"The saved variables aren't the expected ones."
|
|
|
|
);
|
|
|
|
$this->assertContainsOnlyInstancesOf(
|
|
|
|
AFPData::class,
|
|
|
|
$interestingVars,
|
|
|
|
'Some variables have not been computed.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testVarDump
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideFiltersAndVariables() {
|
2018-12-28 16:01:19 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
2018-08-23 18:53:55 +00:00
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.',
|
|
|
|
'stashType' => 'miss'
|
|
|
|
],
|
|
|
|
[ 'added_lines', 'action' ]
|
|
|
|
],
|
2018-12-28 16:01:19 +00:00
|
|
|
[
|
|
|
|
[ 2 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Test page',
|
|
|
|
'newTitle' => 'Another test page'
|
|
|
|
],
|
|
|
|
[ 'action', 'moved_to_title' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Test page'
|
|
|
|
],
|
|
|
|
[ 'action', 'page_prefixedtitle' ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 15 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
|
|
|
'target' => 'User:AnotherUser',
|
|
|
|
'username' => 'AnotherUser'
|
|
|
|
],
|
|
|
|
[ 'action' ]
|
|
|
|
],
|
2018-08-23 18:53:55 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as testFilterConsequences but only for stashed edits
|
|
|
|
*
|
|
|
|
* @param string $type Either "hit" or "miss". The former saves the edit from stash, the second
|
|
|
|
* stashes the edit but doesn't reuse it.
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideStashedEdits
|
2022-07-15 14:02:56 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseLogger
|
2018-08-23 18:53:55 +00:00
|
|
|
*/
|
|
|
|
public function testStashedEdit( $type, $createIds, $actionParams, $consequences ) {
|
|
|
|
if ( $type !== 'hit' && $type !== 'miss' ) {
|
|
|
|
throw new InvalidArgumentException( '$type must be either "hit" or "miss"' );
|
|
|
|
}
|
|
|
|
// Add some info in actionParams identical for all tests
|
|
|
|
$actionParams['action'] = 'stashedit';
|
|
|
|
$actionParams['stashType'] = $type;
|
|
|
|
|
2021-02-06 15:39:11 +00:00
|
|
|
$loggerMock = $this->createMock( LoggerInterface::class );
|
|
|
|
$loggerCalls = [];
|
|
|
|
$loggerMock->method( 'debug' )
|
2021-04-30 18:54:17 +00:00
|
|
|
->willReturnCallback( static function ( $msg, $args ) use ( &$loggerCalls ) {
|
2021-02-06 15:39:11 +00:00
|
|
|
if ( isset( $args['logtype'] ) ) {
|
|
|
|
$loggerCalls[] = $args['logtype'];
|
|
|
|
}
|
|
|
|
} );
|
2018-08-23 18:53:55 +00:00
|
|
|
$this->setLogger( 'StashEdit', $loggerMock );
|
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-08-23 18:53:55 +00:00
|
|
|
$result = $this->doAction( $actionParams );
|
|
|
|
|
|
|
|
// Check that we stored the edit and then hit/missed the cache
|
2021-02-06 15:39:11 +00:00
|
|
|
if ( !in_array( 'store', $loggerCalls, true ) ) {
|
2018-08-23 18:53:55 +00:00
|
|
|
$this->fail( 'Did not store the edit in cache as expected for a stashed edit.' );
|
2021-02-06 15:39:11 +00:00
|
|
|
} elseif ( !in_array( $type, $loggerCalls, true ) ) {
|
2018-08-23 18:53:55 +00:00
|
|
|
$this->fail( "Did not $type the cache as expected for a stashed edit." );
|
|
|
|
}
|
|
|
|
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $consequences );
|
2018-08-23 18:53:55 +00:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-08-23 18:53:55 +00:00
|
|
|
);
|
2022-07-15 14:02:56 +00:00
|
|
|
|
|
|
|
$this->assertAbuseLog( $actionParams, $consequences );
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideStashedEdits() {
|
2023-08-18 13:00:49 +00:00
|
|
|
// XXX Need to hardcode the username of $this->user here.
|
|
|
|
$username = 'UTSysop';
|
2018-08-23 18:53:55 +00:00
|
|
|
$sets = [
|
|
|
|
[
|
|
|
|
[ 1, 2 ],
|
|
|
|
[
|
|
|
|
'target' => 'Test page',
|
|
|
|
'oldText' => 'Some old text for the test.',
|
|
|
|
'newText' => 'I like foo',
|
|
|
|
'summary' => 'Test AbuseFilter for edit action.'
|
|
|
|
],
|
|
|
|
[ 'warn' => [ 1 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 5 ],
|
|
|
|
[
|
2023-08-18 13:00:49 +00:00
|
|
|
'target' => "User:$username",
|
2018-08-23 18:53:55 +00:00
|
|
|
'oldText' => 'Hey.',
|
|
|
|
'newText' => 'I am a very nice user, really!',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 5 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 6 ],
|
|
|
|
[
|
|
|
|
'target' => 'Help:Help',
|
|
|
|
'oldText' => 'Some help.',
|
|
|
|
'newText' => 'Some help for you',
|
|
|
|
'summary' => 'Help! I need somebody'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 6 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 2, 3, 7, 8 ],
|
|
|
|
[
|
|
|
|
'target' => 'Link',
|
|
|
|
'oldText' => 'What is a link?',
|
|
|
|
'newText' => 'A link is something like this: [[Link|]].',
|
|
|
|
'summary' => 'Explaining'
|
|
|
|
],
|
|
|
|
[ 'degroup' => [ 7 ], 'block' => [ 8 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 8, 9 ],
|
|
|
|
[
|
|
|
|
'target' => 'Whatever',
|
|
|
|
'oldText' => 'Whatever is whatever',
|
|
|
|
'newText' => 'Whatever is whatever, whatever it is. BTW, here is a [[Link|]]',
|
|
|
|
'summary' => 'Whatever'
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 8 ], 'block' => [ 8 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10 ],
|
|
|
|
[
|
|
|
|
'target' => 'Buffalo',
|
|
|
|
'oldText' => 'Buffalo',
|
|
|
|
'newText' => 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.',
|
|
|
|
'summary' => 'Buffalo!'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 10 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 1, 3, 7, 12 ],
|
|
|
|
[
|
2023-08-18 13:00:49 +00:00
|
|
|
'target' => "User:$username",
|
2018-08-23 18:53:55 +00:00
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'A couple of lines about me...',
|
|
|
|
'summary' => 'My user page'
|
|
|
|
],
|
|
|
|
[ 'block' => [ 12 ], 'degroup' => [ 7, 12 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 10, 13 ],
|
|
|
|
[
|
|
|
|
'target' => 'Tyger! Tyger! Burning bright',
|
|
|
|
'oldText' => 'In the forests of the night',
|
|
|
|
'newText' => 'What immortal hand or eye',
|
|
|
|
'summary' => 'Could frame thy fearful symmetry?'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 10 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 14 ],
|
|
|
|
[
|
|
|
|
'target' => '0',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => 'Some summary'
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
],
|
2018-12-28 16:01:19 +00:00
|
|
|
];
|
2018-08-23 18:53:55 +00:00
|
|
|
|
|
|
|
foreach ( $sets as $set ) {
|
2019-06-14 00:08:41 +00:00
|
|
|
// Test both successfully saving a stashed edit and stashing the edit but re-executing filters
|
2024-04-11 12:01:25 +00:00
|
|
|
yield [ 'miss', ...$set ];
|
|
|
|
yield [ 'hit', ...$set ];
|
2018-08-23 18:53:55 +00:00
|
|
|
}
|
2018-12-28 16:01:19 +00:00
|
|
|
}
|
2018-08-24 14:22:58 +00:00
|
|
|
|
2018-08-25 12:44:01 +00:00
|
|
|
/**
|
|
|
|
* Tests for global filters, defined on a central wiki and executed on another (e.g. a filter
|
2023-08-18 13:00:49 +00:00
|
|
|
* defined on meta but triggered on another wiki using meta's global filters).
|
|
|
|
* For simplicity, this test creates filters in the local database but makes the extension believe that it's
|
|
|
|
* actually external.
|
2018-08-25 12:44:01 +00:00
|
|
|
*
|
|
|
|
* @param int[] $createIds IDs of the filters to create
|
|
|
|
* @param array $actionParams Details of the action we need to execute to trigger filters
|
|
|
|
* @param array $consequences The consequences we're expecting
|
|
|
|
* @dataProvider provideGlobalFilters
|
2022-07-15 14:02:56 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\AbuseLogger
|
2018-08-25 12:44:01 +00:00
|
|
|
*/
|
|
|
|
public function testGlobalFilters( $createIds, $actionParams, $consequences ) {
|
2024-07-30 15:03:40 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'AbuseFilterCentralDB' => WikiMap::getCurrentWikiId(),
|
|
|
|
'AbuseFilterIsCentral' => false,
|
2020-05-17 15:38:20 +00:00
|
|
|
] );
|
2023-08-18 13:00:49 +00:00
|
|
|
$this->createFilters( $createIds );
|
2018-08-25 12:44:01 +00:00
|
|
|
|
2023-08-18 13:00:49 +00:00
|
|
|
AbuseFilterServices::getFilterLookup( $this->getServiceContainer() )->hideLocalFiltersForTesting();
|
2018-08-25 12:44:01 +00:00
|
|
|
$result = $this->doAction( $actionParams );
|
|
|
|
|
2024-03-16 18:52:48 +00:00
|
|
|
[ $expected, $actual ] = $this->checkConsequences( $result, $actionParams, $consequences );
|
2018-08-25 12:44:01 +00:00
|
|
|
|
2023-08-18 13:00:49 +00:00
|
|
|
// First check that the filters work as expected
|
2018-08-25 12:44:01 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual,
|
2019-08-26 13:01:09 +00:00
|
|
|
'The error messages obtained by performing the action do not match.'
|
2018-08-25 12:44:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the hits were logged on the "external" DB
|
2024-04-30 18:21:20 +00:00
|
|
|
$dbr = $this->getDb();
|
|
|
|
$loggedFilters = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( 'afl_filter_id' )
|
|
|
|
->from( 'abuse_filter_log' )
|
|
|
|
->where( $dbr->expr( 'afl_wiki', '!=', null ) )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchFieldValues();
|
2019-02-06 16:07:01 +00:00
|
|
|
|
|
|
|
// Use assertEquals because selectFieldValues returns an array of strings
|
2018-08-25 12:44:01 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$createIds,
|
2018-09-11 16:57:49 +00:00
|
|
|
$loggedFilters,
|
2018-08-25 12:44:01 +00:00
|
|
|
'Some filter hits were not logged in the external DB.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testGlobalFilters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-20 19:44:55 +00:00
|
|
|
public static function provideGlobalFilters() {
|
2018-08-25 12:44:01 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'Global',
|
|
|
|
'oldText' => 'Old text',
|
|
|
|
'newText' => 'New text',
|
|
|
|
'summary' => ''
|
|
|
|
],
|
2022-03-05 17:00:09 +00:00
|
|
|
[ 'warn' => [ 18 ] ]
|
2018-08-25 12:44:01 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19 ],
|
|
|
|
[
|
|
|
|
'action' => 'edit',
|
|
|
|
'target' => 'A global page',
|
|
|
|
'oldText' => 'Foo',
|
|
|
|
'newText' => 'Bar',
|
|
|
|
'summary' => 'Baz'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 19 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'move',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
'newTitle' => 'Attic door'
|
|
|
|
],
|
|
|
|
[ 'warn' => [ 18 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19, 20 ],
|
|
|
|
[
|
|
|
|
'action' => 'delete',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
],
|
|
|
|
[ 'disallow' => [ 20 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 19 ],
|
|
|
|
[
|
|
|
|
'action' => 'stashedit',
|
|
|
|
'target' => 'Cellar door',
|
|
|
|
'oldText' => '',
|
|
|
|
'newText' => 'Too many doors',
|
|
|
|
'summary' => '',
|
|
|
|
'stashType' => 'hit'
|
|
|
|
],
|
|
|
|
[ 'tag' => [ 19 ] ]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ 18 ],
|
|
|
|
[
|
|
|
|
'action' => 'createaccount',
|
2019-08-26 13:01:09 +00:00
|
|
|
'target' => 'User:AbuseFilterGlobalUser',
|
|
|
|
'username' => 'AbuseFilterGlobalUser'
|
2018-08-25 12:44:01 +00:00
|
|
|
],
|
|
|
|
[ 'warn' => [ 18 ] ]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2021-07-04 06:25:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that after an edit is saved, if a filter was hit the afl_rev_id is updated
|
|
|
|
* to reflect the new edit
|
|
|
|
*
|
|
|
|
* Regression tests for T286140
|
|
|
|
*/
|
|
|
|
public function testRevIdSet() {
|
|
|
|
// Filter 24 has no actions and always matches
|
|
|
|
$this->createFilters( [ 24 ] );
|
|
|
|
|
2022-06-28 20:46:45 +00:00
|
|
|
$targetTitle = Title::makeTitle( NS_MAIN, 'TestRevIdSet' );
|
2024-01-26 13:56:58 +00:00
|
|
|
$startingRevId = $targetTitle->getLatestRevID( IDBAccessObject::READ_LATEST );
|
2021-07-04 06:25:06 +00:00
|
|
|
|
|
|
|
$this->doEdit( $targetTitle, 'Old text', 'New text', 'Summary' );
|
2024-01-26 13:56:58 +00:00
|
|
|
$latestRevId = $targetTitle->getLatestRevID( IDBAccessObject::READ_LATEST );
|
2021-07-04 06:25:06 +00:00
|
|
|
|
|
|
|
$this->assertNotSame(
|
|
|
|
$startingRevId,
|
|
|
|
$latestRevId,
|
|
|
|
'Edit should have been properly saved'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check the database for the filter hit
|
|
|
|
// We don't have an easy way to retrieve the afl_id for this relevant hit,
|
|
|
|
// so instead find the latest row for this filter
|
2024-04-30 18:21:20 +00:00
|
|
|
$filterHit = $this->getDb()->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'abuse_filter_log' )
|
|
|
|
->where( [ 'afl_filter_id' => 24 ] )
|
|
|
|
->orderBy( 'afl_id', SelectQueryBuilder::SORT_DESC )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRow();
|
2021-07-04 06:25:06 +00:00
|
|
|
|
|
|
|
// Helpful for debugging
|
|
|
|
$filterHitStr = FormatJson::encode( $filterHit );
|
|
|
|
$this->assertSame(
|
|
|
|
$latestRevId,
|
|
|
|
(int)( $filterHit->afl_rev_id ),
|
|
|
|
"AbuseLog entry updated with the revision id (full filter hit: $filterHitStr)"
|
|
|
|
);
|
|
|
|
}
|
2022-07-15 08:44:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a null edit.
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator
|
|
|
|
*/
|
|
|
|
public function testNullEdit() {
|
|
|
|
$this->setService(
|
|
|
|
FilterRunnerFactory::SERVICE_NAME,
|
|
|
|
$this->createNoOpMock( FilterRunnerFactory::class )
|
|
|
|
);
|
|
|
|
// Filter 24 has no actions and always matches
|
|
|
|
$this->createFilters( [ 24 ] );
|
|
|
|
|
|
|
|
$targetTitle = Title::makeTitle( NS_MAIN, 'TestNullEdit' );
|
|
|
|
$text = 'Some text';
|
|
|
|
$status = $this->doEdit( $targetTitle, $text, $text, 'Summary' );
|
|
|
|
$this->assertStatusGood( $status );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a content model change.
|
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator
|
|
|
|
*/
|
|
|
|
public function testContentModelChange() {
|
|
|
|
// Filter 23 always matches and disables
|
|
|
|
$this->createFilters( [ 23 ] );
|
|
|
|
|
|
|
|
$targetTitle = Title::makeTitle( NS_MAIN, 'TestContentModelChange' );
|
|
|
|
|
|
|
|
$text = FormatJson::encode( [ 'key' => 'value' ] );
|
|
|
|
$oldContent = new JsonContent( $text );
|
|
|
|
$newContent = new WikitextContent( $text );
|
|
|
|
|
|
|
|
$status = $this->doEdit( $targetTitle, $oldContent, $newContent, 'Summary' );
|
|
|
|
$this->assertStatusNotOK( $status );
|
|
|
|
}
|
2018-07-08 16:06:19 +00:00
|
|
|
}
|