mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/SpamBlacklist
synced 2024-11-24 07:04:04 +00:00
da1af447e2
There are some usages outside of SpamBlacklist that must be fixed. After doing that, the signature should be updated to make the user non-optional. Note: I've changed the signature because external callers only pass the first two parameters. Hence, it's easier to change it now, add a User parameter to the callers and then make it non-optional, than having to pass $preserveLog and $mode in all callers and then swapping the order (as that would break the world). Change-Id: I0714eb9dbc6af3c775ab7a81cb4b59e687183f77
102 lines
2.3 KiB
PHP
102 lines
2.3 KiB
PHP
<?php
|
||
|
||
use MediaWiki\MediaWikiServices;
|
||
|
||
/**
|
||
* @group SpamBlacklist
|
||
* @group Database
|
||
* @covers SpamBlacklist
|
||
*/
|
||
class SpamBlacklistTest extends MediaWikiTestCase {
|
||
/**
|
||
* @var SpamBlacklist
|
||
*/
|
||
protected $spamFilter;
|
||
|
||
/**
|
||
* Spam blacklist regexes. Examples taken from:
|
||
*
|
||
* @see https://meta.wikimedia.org/wiki/Spam_blacklist
|
||
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist
|
||
*
|
||
* via Flow extension
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
|
||
|
||
/**
|
||
* Spam whitelist regexes. Examples taken from:
|
||
*
|
||
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-whitelist
|
||
*
|
||
* via Flow extension
|
||
*
|
||
* @var array
|
||
*/
|
||
protected $whitelist = [ 'a5b\.sytes\.net' ];
|
||
|
||
public function spamProvider() {
|
||
return [
|
||
'no spam' => [
|
||
[ 'https://example.com' ],
|
||
false,
|
||
],
|
||
'revision with spam, with additional non-spam' => [
|
||
[ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
|
||
[ '01bags.com' ],
|
||
],
|
||
|
||
'revision with spam using full width stop normalization' => [
|
||
[ 'http://01bags.com' ],
|
||
[ '01bags.com' ],
|
||
],
|
||
|
||
'revision with domain blacklisted as spam, but subdomain whitelisted' => [
|
||
[ 'http://a5b.sytes.net' ],
|
||
false,
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @dataProvider spamProvider
|
||
*/
|
||
public function testSpam( $links, $expected ) {
|
||
$returnValue = $this->spamFilter->filter(
|
||
$links,
|
||
Title::newMainPage(),
|
||
$this->createMock( User::class )
|
||
);
|
||
$this->assertEquals( $expected, $returnValue );
|
||
}
|
||
|
||
protected function setUp() : void {
|
||
parent::setUp();
|
||
|
||
$this->setMwGlobals( 'wgBlacklistSettings', [
|
||
'files' => [],
|
||
] );
|
||
|
||
BaseBlacklist::clearInstanceCache();
|
||
|
||
// create spam filter
|
||
$this->spamFilter = new SpamBlacklist;
|
||
|
||
MediaWikiServices::getInstance()->getMessageCache()->enable();
|
||
$this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) );
|
||
$this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) );
|
||
|
||
// That only works if the spam blacklist is really reset
|
||
$instance = BaseBlacklist::getInstance( 'spam' );
|
||
$reflProp = new \ReflectionProperty( $instance, 'regexes' );
|
||
$reflProp->setAccessible( true );
|
||
$reflProp->setValue( $instance, false );
|
||
}
|
||
|
||
protected function tearDown() : void {
|
||
MediaWikiServices::getInstance()->getMessageCache()->disable();
|
||
parent::tearDown();
|
||
}
|
||
}
|