| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\EditPage\EditPage; |
| 5 |
use MediaWiki\Extension\SpamBlacklist\BaseBlacklist; |
| 6 |
use MediaWiki\Extension\SpamBlacklist\SpamBlacklist; |
| 7 |
use MediaWiki\MediaWikiServices; |
| 8 |
use MediaWiki\Request\FauxRequest; |
| 9 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class SpamBlacklistTest extends MediaWikiIntegrationTestCase { |
| 18 |
|
| 19 |
use TempUserTestTrait; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
protected $spamFilter; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ]; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected $whitelist = [ 'a5b\.sytes\.net' ]; |
| 48 |
|
| 49 |
public static function spamProvider() { |
| 50 |
return [ |
| 51 |
'no spam' => [ |
| 52 |
[ 'https://example.com' ], |
| 53 |
false, |
| 54 |
], |
| 55 |
'revision with spam, with additional non-spam' => [ |
| 56 |
[ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ], |
| 57 |
[ '01bags.com' ], |
| 58 |
], |
| 59 |
|
| 60 |
'revision with spam using full width stop normalization' => [ |
| 61 |
[ 'http://01bags.com' ], |
| 62 |
[ '01bags.com' ], |
| 63 |
], |
| 64 |
|
| 65 |
'revision with domain blacklisted as spam, but subdomain whitelisted' => [ |
| 66 |
[ 'http://a5b.sytes.net' ], |
| 67 |
false, |
| 68 |
], |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function testSpamTempAccounts( $links, $expected ) { |
| 76 |
$this->enableAutoCreateTempUser(); |
| 77 |
$this->prepareGlobals(); |
| 78 |
$tempUserCreator = MediaWikiServices::getInstance()->getTempUserCreator(); |
| 79 |
$user = $tempUserCreator->create( |
| 80 |
null, |
| 81 |
new FauxRequest() |
| 82 |
)->getUser(); |
| 83 |
$returnValue = $this->spamFilter->filter( |
| 84 |
$links, |
| 85 |
Title::newMainPage(), |
| 86 |
$user |
| 87 |
); |
| 88 |
$this->assertEquals( $expected, $returnValue ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testSpamAnonEditing( $links, $expected ) { |
| 95 |
$this->disableAutoCreateTempUser(); |
| 96 |
$this->prepareGlobals(); |
| 97 |
|
| 98 |
$userFactory = $this->getServiceContainer()->getUserFactory(); |
| 99 |
$user = $userFactory->newAnonymous(); |
| 100 |
$returnValue = $this->spamFilter->filter( |
| 101 |
$links, |
| 102 |
Title::newMainPage(), |
| 103 |
$user |
| 104 |
); |
| 105 |
$this->assertEquals( $expected, $returnValue ); |
| 106 |
} |
| 107 |
|
| 108 |
public static function spamEditProvider() { |
| 109 |
return [ |
| 110 |
'no spam' => [ |
| 111 |
'https://example.com', |
| 112 |
true, |
| 113 |
], |
| 114 |
'revision with spam, with additional non-spam' => [ |
| 115 |
"https://foo.com\nhttp://01bags.com\nhttp://bar.com'", |
| 116 |
false, |
| 117 |
], |
| 118 |
|
| 119 |
'revision with domain blacklisted as spam, but subdomain whitelisted' => [ |
| 120 |
'http://a5b.sytes.net', |
| 121 |
true, |
| 122 |
], |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function testSpamEdit( $text, $ok ) { |
| 130 |
$this->prepareGlobals(); |
| 131 |
$fields = [ |
| 132 |
'wpTextbox1' => $text, |
| 133 |
'wpUnicodeCheck' => EditPage::UNICODE_CHECK, |
| 134 |
'wpRecreate' => true, |
| 135 |
]; |
| 136 |
|
| 137 |
$req = new FauxRequest( $fields, true ); |
| 138 |
|
| 139 |
$page = $this->getNonexistingTestPage( __METHOD__ ); |
| 140 |
$title = $page->getTitle(); |
| 141 |
|
| 142 |
$articleContext = new RequestContext; |
| 143 |
$articleContext->setRequest( $req ); |
| 144 |
$articleContext->setWikiPage( $page ); |
| 145 |
$articleContext->setUser( $this->getTestUser()->getUser() ); |
| 146 |
|
| 147 |
$article = new Article( $title ); |
| 148 |
$ep = new EditPage( $article ); |
| 149 |
$ep->setContextTitle( $title ); |
| 150 |
$ep->getContext()->setUser( $articleContext->getUser() ); |
| 151 |
|
| 152 |
$ep->importFormData( $req ); |
| 153 |
|
| 154 |
$status = $ep->attemptSave( $result ); |
| 155 |
|
| 156 |
if ( $ok ) { |
| 157 |
$this->assertStatusOK( $status ); |
| 158 |
} else { |
| 159 |
$this->assertStatusError( 'spam-blacklisted-link', $status ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
private function prepareGlobals(): void { |
| 164 |
$this->overrideConfigValue( 'BlacklistSettings', [ |
| 165 |
'files' => [], |
| 166 |
] ); |
| 167 |
|
| 168 |
BaseBlacklist::clearInstanceCache(); |
| 169 |
|
| 170 |
|
| 171 |
$this->spamFilter = new SpamBlacklist; |
| 172 |
|
| 173 |
MediaWikiServices::getInstance()->getMessageCache()->enable(); |
| 174 |
$this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) ); |
| 175 |
$this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) ); |
| 176 |
|
| 177 |
|
| 178 |
$instance = BaseBlacklist::getInstance( 'spam' ); |
| 179 |
$reflProp = new ReflectionProperty( $instance, 'regexes' ); |
| 180 |
$reflProp->setValue( $instance, false ); |
| 181 |
} |
| 182 |
|
| 183 |
protected function tearDown(): void { |
| 184 |
MediaWikiServices::getInstance()->getMessageCache()->disable(); |
| 185 |
parent::tearDown(); |
| 186 |
} |
| 187 |
} |