MediaWiki  1.34.0
SpamBlacklistTest.php
Go to the documentation of this file.
1 <?php
2 
8 class SpamBlacklistTest extends MediaWikiTestCase {
12  protected $spamFilter;
13 
24  protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
25 
35  protected $whitelist = [ 'a5b\.sytes\.net' ];
36 
37  public function spamProvider() {
38  return [
39  'no spam' => [
40  [ 'https://example.com' ],
41  false,
42  ],
43  'revision with spam, with additional non-spam' => [
44  [ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
45  [ '01bags.com' ],
46  ],
47 
48  'revision with spam using full width stop normalization' => [
49  [ 'http://01bags.com' ],
50  [ '01bags.com' ],
51  ],
52 
53  'revision with domain blacklisted as spam, but subdomain whitelisted' => [
54  [ 'http://a5b.sytes.net' ],
55  false,
56  ],
57  ];
58  }
59 
63  public function testSpam( $links, $expected ) {
64  $returnValue = $this->spamFilter->filter( $links, Title::newMainPage() );
65  $this->assertEquals( $expected, $returnValue );
66  }
67 
68  protected function setUp() {
69  parent::setUp();
70 
71  // create spam filter
72  $this->spamFilter = new SpamBlacklist;
73 
74  $this->setMwGlobals( 'wgBlacklistSettings', [
75  'files' => [],
76  ] );
77 
78  \MessageCache::singleton()->enable();
79  $this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) );
80  $this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) );
81 
82  // That only works if the spam blacklist is really reset
83  $instance = BaseBlacklist::getInstance( 'spam' );
84  $reflProp = new \ReflectionProperty( $instance, 'regexes' );
85  $reflProp->setAccessible( true );
86  $reflProp->setValue( $instance, false );
87  }
88 
89  protected function tearDown() {
90  \MessageCache::singleton()->disable();
91  parent::tearDown();
92  }
93 }
BaseBlacklist\getInstance
static getInstance( $type)
Returns an instance of the given blacklist.
Definition: BaseBlacklist.php:118
SpamBlacklist
Definition: SpamBlacklist.php:6
SpamBlacklistTest\setUp
setUp()
Definition: SpamBlacklistTest.php:68
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:649
SpamBlacklistTest\testSpam
testSpam( $links, $expected)
@dataProvider spamProvider
Definition: SpamBlacklistTest.php:63
SpamBlacklistTest\$spamFilter
SpamBlacklist $spamFilter
Definition: SpamBlacklistTest.php:12
MessageCache\singleton
static singleton()
Get the singleton instance of this class.
Definition: MessageCache.php:114
SpamBlacklistTest
@group SpamBlacklist @group Database @covers SpamBlacklist
Definition: SpamBlacklistTest.php:8
SpamBlacklistTest\$blacklist
array $blacklist
Spam blacklist regexes.
Definition: SpamBlacklistTest.php:24
SpamBlacklistTest\$whitelist
array $whitelist
Spam whitelist regexes.
Definition: SpamBlacklistTest.php:35
SpamBlacklistTest\spamProvider
spamProvider()
Definition: SpamBlacklistTest.php:37
SpamBlacklistTest\tearDown
tearDown()
Definition: SpamBlacklistTest.php:89