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
|
|
|
|
*/
|
2021-10-11 23:04:44 +00:00
|
|
|
|
class SpamBlacklistTest extends MediaWikiIntegrationTestCase {
|
2017-05-01 22:33:21 +00:00
|
|
|
|
/**
|
|
|
|
|
* @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 ) {
|
2020-01-04 11:16:36 +00:00
|
|
|
|
$returnValue = $this->spamFilter->filter(
|
|
|
|
|
$links,
|
|
|
|
|
Title::newMainPage(),
|
|
|
|
|
$this->createMock( User::class )
|
|
|
|
|
);
|
2017-05-01 22:33:21 +00:00
|
|
|
|
$this->assertEquals( $expected, $returnValue );
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 13:13:34 +00:00
|
|
|
|
public function spamEditProvider() {
|
|
|
|
|
return [
|
|
|
|
|
'no spam' => [
|
|
|
|
|
'https://example.com',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
'revision with spam, with additional non-spam' => [
|
|
|
|
|
"https://foo.com\nhttp://01bags.com\nhttp://bar.com'",
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
'revision with domain blacklisted as spam, but subdomain whitelisted' => [
|
|
|
|
|
'http://a5b.sytes.net',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider spamEditProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testSpamEdit( $text, $ok ) {
|
|
|
|
|
$fields = [
|
|
|
|
|
'wpTextbox1' => $text,
|
|
|
|
|
'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
|
|
|
|
|
'wpRecreate' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$req = new FauxRequest( $fields, true );
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$title = $page->getTitle();
|
|
|
|
|
|
|
|
|
|
$articleContext = new RequestContext;
|
|
|
|
|
$articleContext->setRequest( $req );
|
|
|
|
|
$articleContext->setWikiPage( $page );
|
|
|
|
|
$articleContext->setUser( $this->getTestUser()->getUser() );
|
|
|
|
|
|
|
|
|
|
$article = new Article( $title );
|
|
|
|
|
$ep = new EditPage( $article );
|
|
|
|
|
$ep->setContextTitle( $title );
|
|
|
|
|
|
|
|
|
|
$ep->importFormData( $req );
|
|
|
|
|
|
|
|
|
|
$status = $ep->attemptSave( $result );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $ok, $status->isOK() );
|
|
|
|
|
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$this->assertTrue( $status->hasMessage( 'spam-blacklisted-link' ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 22:37:22 +00:00
|
|
|
|
protected function setUp(): void {
|
2017-05-01 22:33:21 +00:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
|
$this->setMwGlobals( 'wgBlacklistSettings', [
|
|
|
|
|
'files' => [],
|
|
|
|
|
] );
|
2017-05-01 22:33:21 +00:00
|
|
|
|
|
2020-09-29 16:53:36 +00:00
|
|
|
|
BaseBlacklist::clearInstanceCache();
|
|
|
|
|
|
|
|
|
|
// create spam filter
|
|
|
|
|
$this->spamFilter = new SpamBlacklist;
|
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 22:37:22 +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();
|
|
|
|
|
}
|
|
|
|
|
}
|