MediaWiki REL1_31
SpamBlacklistTest.php
Go to the documentation of this file.
1<?php
2
10 protected $spamFilter;
11
22 protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
23
33 protected $whitelist = [ 'a5b\.sytes\.net' ];
34
35 public function spamProvider() {
36 return [
37 'no spam' => [
38 [ 'https://example.com' ],
39 false,
40 ],
41 'revision with spam, with additional non-spam' => [
42 [ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
43 [ '01bags.com' ],
44 ],
45
46 'revision with spam using full width stop normalization' => [
47 [ 'http://01bags.com' ],
48 [ '01bags.com' ],
49 ],
50
51 'revision with domain blacklisted as spam, but subdomain whitelisted' => [
52 [ 'http://a5b.sytes.net' ],
53 false,
54 ],
55 ];
56 }
57
61 public function testSpam( $links, $expected ) {
62 $returnValue = $this->spamFilter->filter( $links, Title::newMainPage() );
63 $this->assertEquals( $expected, $returnValue );
64 }
65
66 protected function setUp() {
67 parent::setUp();
68
69 // create spam filter
70 $this->spamFilter = new SpamBlacklist;
71
72 $this->setMwGlobals( 'wgBlacklistSettings', [
73 'files' => [],
74 ] );
75
76 \MessageCache::singleton()->enable();
77 $this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) );
78 $this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) );
79
80 // That only works if the spam blacklist is really reset
81 $instance = BaseBlacklist::getInstance( 'spam' );
82 $reflProp = new \ReflectionProperty( $instance, 'regexes' );
83 $reflProp->setAccessible( true );
84 $reflProp->setValue( $instance, false );
85 }
86
87 protected function tearDown() {
88 \MessageCache::singleton()->disable();
89 parent::tearDown();
90 }
91}
static getInstance( $type)
Returns an instance of the given blacklist.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null)
Insert a new page.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
testSpam( $links, $expected)
spamProvider
array $whitelist
Spam whitelist regexes.
array $blacklist
Spam blacklist regexes.
SpamBlacklist $spamFilter