Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/107568


Change subject: Tests for SpamRegex & SpamBlacklist integration
......................................................................

Tests for SpamRegex & SpamBlacklist integration

Change-Id: I1643b8d47ad7ac68f55a60d210b99f6b6d259774
---
A tests/SpamBlacklistTest.php
A tests/SpamRegexTest.php
2 files changed, 156 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/68/107568/1

diff --git a/tests/SpamBlacklistTest.php b/tests/SpamBlacklistTest.php
new file mode 100644
index 0000000..d2f49fb
--- /dev/null
+++ b/tests/SpamBlacklistTest.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Flow\Tests;
+
+use Flow\SpamFilter\SpamBlacklist;
+use Flow\Model\PostRevision;
+use Title;
+
+class SpamBlacklistTest extends PostRevisionTestCase {
+       /**
+        * @var SpamBlacklist
+        */
+       protected $spamFilter;
+
+       /**
+        * We'll change the test $wgMemc in setUp - this will hold the original
+        * $wgMemc, to restore on tearDown.
+        *
+        * @return BagOStuff
+        */
+       protected $originalCache;
+
+       public function spamProvider() {
+               return array(
+                       array(
+                               // default new topic title revision - no spam
+                               $this->generateObject(),
+                               null,
+                               true
+                       ),
+                       array(
+                               // revision with spam
+                               $this->generateObject( array( 'rev_content' => 
'http://01bags.com', 'rev_flags' => 'html' ) ),
+                               null,
+                               false
+                       ),
+                       array(
+                               // revision with domain blacklisted as spam, 
but subdomain whitelisted
+                               $this->generateObject( array( 'rev_content' => 
'http://a5b.syte\.net', 'rev_flags' => 'html' ) ),
+                               null,
+                               true
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider spamProvider
+        */
+       public function testSpam( PostRevision $newRevision, PostRevision 
$oldRevision = null, $expected ) {
+               if ( !$this->spamFilter->enabled() ) {
+                       $this->markTestSkipped( 'SpamRegex not enabled' );
+               }
+
+               $title = Title::newFromText( 'UTPage' );
+
+               $status = $this->spamFilter->validate( $newRevision, 
$oldRevision, $title );
+               $this->assertEquals( $status->isOK(), $expected );
+       }
+
+       protected function setUp() {
+               parent::setUp();
+
+               // create spam filter
+               $this->spamFilter = new SpamBlacklist;
+
+               // alter $wgMemc & write empty shared blacklist, to prevent an 
attempt
+               // to fetch spam blacklist over network
+               global $wgMemc, $wgDBname;
+               $this->originalCache = $wgMemc;
+               $wgMemc = new \HashBagOStuff;
+               $wgMemc->set( "$wgDBname:spam_blacklist_regexes", array() );
+
+               /*
+                * Examples taken from:
+                *
+                * @see http://meta.wikimedia.org/wiki/Spam_blacklist
+                * @see http://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist
+                * @see http://en.wikipedia.org/wiki/MediaWiki:Spam-whitelist
+                */
+               $blacklist = array( '\b01bags\.com\b', 'sytes\.net' );
+               $whitelist = array( 'a5b\.sytes\.net' );
+
+               // local spam lists are read from spam-blacklist & 
spam-whitelist
+               // messages, so change them for this test
+               $msgCache = \MessageCache::singleton();
+               $msgCache->enable();
+               $msgCache->replace( 'Spam-blacklist', implode( "\n", $blacklist 
) );
+               $msgCache->replace( 'Spam-whitelist', implode( "\n", $whitelist 
) );
+       }
+
+       protected function tearDown() {
+               global $wgMemc;
+               $wgMemc = $this->originalCache;
+
+               // we don't have to restore the original messages, disable() 
will make
+               // sure they're ignored
+               $msgCache = \MessageCache::singleton();
+               $msgCache->disable();
+       }
+}
diff --git a/tests/SpamRegexTest.php b/tests/SpamRegexTest.php
new file mode 100644
index 0000000..178ffa3
--- /dev/null
+++ b/tests/SpamRegexTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Flow\Tests;
+
+use Flow\SpamFilter\SpamRegex;
+use Flow\Model\PostRevision;
+use Title;
+
+class SpamRegexTest extends PostRevisionTestCase {
+       /**
+        * @var SpamRegex
+        */
+       protected $spamFilter;
+
+       public function spamProvider() {
+               return array(
+                       array(
+                               // default new topic title revision - no spam
+                               $this->generateObject(),
+                               null,
+                               true
+                       ),
+                       array(
+                               // revision with spam
+                               $this->generateObject( array( 'rev_content' => 
'http://spam', 'rev_flags' => 'html' ) ),
+                               null,
+                               false
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider spamProvider
+        */
+       public function testSpam( PostRevision $newRevision, PostRevision 
$oldRevision = null, $expected ) {
+               if ( !$this->spamFilter->enabled() ) {
+                       $this->markTestSkipped( 'SpamRegex not enabled' );
+               }
+
+               $title = Title::newFromText( 'UTPage' );
+
+               $status = $this->spamFilter->validate( $newRevision, 
$oldRevision, $title );
+               $this->assertEquals( $status->isOK(), $expected );
+       }
+
+       protected function setUp() {
+               parent::setUp();
+
+               // create spam filter
+               $this->spamFilter = new SpamRegex;
+
+               // create a dummy filter
+               global $wgSpamRegex;
+               $wgSpamRegex = array( '/http:\/\/spam/' );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/107568
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1643b8d47ad7ac68f55a60d210b99f6b6d259774
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to