2017-05-01 22:33:21 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2020-03-14 13:08:25 +00:00
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2017-05-01 22:33:21 +00:00
|
|
|
|
/**
|
2017-07-06 00:58:33 +00:00
|
|
|
|
* @group SpamBlacklist
|
2018-09-14 19:56:24 +00:00
|
|
|
|
* @group Database
|
2018-08-24 10:00:45 +00:00
|
|
|
|
* @covers SpamBlacklist
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*/
|
|
|
|
|
class SpamBlacklistTest extends MediaWikiTestCase {
|
|
|
|
|
/**
|
|
|
|
|
* @var SpamBlacklist
|
|
|
|
|
*/
|
|
|
|
|
protected $spamFilter;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-06-29 17:39:54 +00:00
|
|
|
|
* Spam blacklist regexes. Examples taken from:
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*
|
2019-06-01 10:20:17 +00:00
|
|
|
|
* @see https://meta.wikimedia.org/wiki/Spam_blacklist
|
|
|
|
|
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist
|
2017-06-29 17:39:54 +00:00
|
|
|
|
*
|
|
|
|
|
* via Flow extension
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spam whitelist regexes. Examples taken from:
|
|
|
|
|
*
|
2019-06-01 10:20:17 +00:00
|
|
|
|
* @see https://en.wikipedia.org/wiki/MediaWiki:Spam-whitelist
|
2017-05-01 22:33:21 +00:00
|
|
|
|
*
|
|
|
|
|
* via Flow extension
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2017-06-29 17:39:54 +00:00
|
|
|
|
protected $whitelist = [ 'a5b\.sytes\.net' ];
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
|
|
|
|
public function spamProvider() {
|
2017-06-06 16:29:27 +00:00
|
|
|
|
return [
|
|
|
|
|
'no spam' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'https://example.com' ],
|
|
|
|
|
false,
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
|
|
|
|
'revision with spam, with additional non-spam' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
|
|
|
|
|
[ '01bags.com' ],
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
'revision with spam using full width stop normalization' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'http://01bags.com' ],
|
|
|
|
|
[ '01bags.com' ],
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
'revision with domain blacklisted as spam, but subdomain whitelisted' => [
|
2017-05-01 22:33:21 +00:00
|
|
|
|
[ 'http://a5b.sytes.net' ],
|
|
|
|
|
false,
|
2017-06-06 16:29:27 +00:00
|
|
|
|
],
|
|
|
|
|
];
|
2017-05-01 22:33:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider spamProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testSpam( $links, $expected ) {
|
|
|
|
|
$returnValue = $this->spamFilter->filter( $links, Title::newMainPage() );
|
|
|
|
|
$this->assertEquals( $expected, $returnValue );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 18:58:00 +00:00
|
|
|
|
protected function setUp() : void {
|
2017-05-01 22:33:21 +00:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
// create spam filter
|
|
|
|
|
$this->spamFilter = new SpamBlacklist;
|
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
$this->setMwGlobals( 'wgBlacklistSettings', [
|
|
|
|
|
'files' => [],
|
|
|
|
|
] );
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2020-03-14 13:08:25 +00:00
|
|
|
|
MediaWikiServices::getInstance()->getMessageCache()->enable();
|
2017-05-01 22:33:21 +00:00
|
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 18:58:00 +00:00
|
|
|
|
protected function tearDown() : void {
|
2020-03-14 13:08:25 +00:00
|
|
|
|
MediaWikiServices::getInstance()->getMessageCache()->disable();
|
2017-05-01 22:33:21 +00:00
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
}
|