jenkins-bot has submitted this change and it was merged.

Change subject: Test for AbuseFilter integration
......................................................................


Test for AbuseFilter integration

Change-Id: I9934e4c71256254b66490d4df58359e8f7d5ccbc
---
A tests/AbuseFilterTest.php
M tests/SpamBlacklistTest.php
M tests/SpamRegexTest.php
3 files changed, 138 insertions(+), 8 deletions(-)

Approvals:
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/AbuseFilterTest.php b/tests/AbuseFilterTest.php
new file mode 100644
index 0000000..64302f8
--- /dev/null
+++ b/tests/AbuseFilterTest.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace Flow\Tests;
+
+use Flow\SpamFilter\AbuseFilter;
+use Flow\Model\PostRevision;
+use Title;
+use User;
+
+/**
+ * @group Database
+ */
+class AbuseFilterTest extends PostRevisionTestCase {
+       /**
+        * @var AbuseFilter
+        */
+       protected $spamFilter;
+
+       /**
+        * @var array
+        */
+       protected $tablesUsed = array( 'abuse_filter', 'abuse_filter_action', 
'abuse_filter_history', 'abuse_filter_log' );
+
+       protected $filters = array(
+               // no CSS screen hijack
+               '(new_wikitext rlike 
"position\s*:\s*(fixed|absolute)|style\s*=\s*\"[a-z0-9:;\s]*&|z-index\s*:\s*\d|\|([4-9]\d{3}|\d{5,})px")'
 => 'disallow',
+       );
+
+       public function spamProvider() {
+               return array(
+                       array(
+                               // default new topic title revision - no spam
+                               $this->generateObject(),
+                               null,
+                               true
+                       ),
+                       array(
+                               // revision with spam
+                               // 
https://www.mediawiki.org/w/index.php?title=Talk:Sandbox&workflow=050bbdd07b64a1c028b2782bcb087b42#flow-post-050bbdd07b70a1c028b2782bcb087b42
+                               $this->generateObject( array( 'rev_content' => 
'<div style="background: yellow; position: fixed; top: 0px; left: 0px; width: 
3000px; height: 3000px; z-index: 1111;">test</div>', 'rev_flags' => 'html' ) ),
+                               null,
+                               false
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider spamProvider
+        */
+       public function testSpam( PostRevision $newRevision, PostRevision 
$oldRevision = null, $expected ) {
+               $title = Title::newFromText( 'UTPage' );
+
+               $status = $this->spamFilter->validate( $newRevision, 
$oldRevision, $title );
+               $this->assertEquals( $status->isOK(), $expected );
+       }
+
+       protected function setUp() {
+               parent::setUp();
+
+               global $wgFlowAbuseFilterGroup,
+                       $wgFlowAbuseFilterEmergencyDisableThreshold,
+                       $wgFlowAbuseFilterEmergencyDisableCount,
+                       $wgFlowAbuseFilterEmergencyDisableAge;
+
+               $user = User::newFromName( 'UTSysop' );
+
+               $this->spamFilter = new AbuseFilter( $user, 
$wgFlowAbuseFilterGroup );
+               if ( !$this->spamFilter->enabled() ) {
+                       $this->markTestSkipped( 'AbuseFilter not enabled' );
+               }
+
+               $this->spamFilter->setup( array(
+                       'threshold' => 
$wgFlowAbuseFilterEmergencyDisableThreshold,
+                       'count' => $wgFlowAbuseFilterEmergencyDisableCount,
+                       'age' => $wgFlowAbuseFilterEmergencyDisableAge,
+               ) );
+
+               foreach ( $this->filters as $pattern => $action ) {
+                       $this->createFilter( $pattern, $action );
+               }
+       }
+
+       protected function tearDown() {
+               foreach ( $this->tablesUsed as $table ) {
+                       $this->db->delete( $table, '*', __METHOD__ );
+               }
+       }
+
+       /**
+        * Inserts a filter into stub database.
+        *
+        * @param string $pattern
+        * @param string[optional] $action
+        */
+       protected function createFilter( $pattern, $action = 'disallow' ) {
+               global $wgFlowAbuseFilterGroup;
+               $user = User::newFromName( 'UTSysop' );
+
+               $this->db->replace(
+                       'abuse_filter',
+                       array( 'af_id' ),
+                       array(
+//                             'af_id',
+                               'af_pattern' => $pattern,
+                               'af_user' => $user->getId(),
+                               'af_user_text' => $user->getName(),
+                               'af_timestamp' => wfTimestampNow(),
+                               'af_enabled' => 1,
+                               'af_comments' => null,
+                               'af_public_comments' => 'Test filter',
+                               'af_hidden' => 0,
+                               'af_hit_count' => 0,
+                               'af_throttled' => 0,
+                               'af_deleted' => 0,
+                               'af_actions' => $action,
+                               'af_group' => $wgFlowAbuseFilterGroup,
+                       ),
+                       __METHOD__
+               );
+
+               $this->db->replace(
+                       'abuse_filter_action',
+                       array( 'afa_filter' ),
+                       array(
+                               'afa_filter' => $this->db->insertId(),
+                               'afa_consequence' => $action,
+                               'afa_parameters' => '',
+                       ),
+                       __METHOD__
+               );
+       }
+}
diff --git a/tests/SpamBlacklistTest.php b/tests/SpamBlacklistTest.php
index 9981e06..c94e013 100644
--- a/tests/SpamBlacklistTest.php
+++ b/tests/SpamBlacklistTest.php
@@ -52,10 +52,6 @@
         * @dataProvider spamProvider
         */
        public function testSpam( PostRevision $newRevision, PostRevision 
$oldRevision = null, $expected ) {
-               if ( !$this->spamFilter->enabled() ) {
-                       $this->markTestSkipped( 'SpamBlacklist not enabled' );
-               }
-
                $title = Title::newFromText( 'UTPage' );
 
                $status = $this->spamFilter->validate( $newRevision, 
$oldRevision, $title );
@@ -67,6 +63,9 @@
 
                // create spam filter
                $this->spamFilter = new SpamBlacklist;
+               if ( !$this->spamFilter->enabled() ) {
+                       $this->markTestSkipped( 'SpamBlacklist not enabled' );
+               }
 
                // alter $wgMemc & write empty shared blacklist, to prevent an 
attempt
                // to fetch spam blacklist over network
diff --git a/tests/SpamRegexTest.php b/tests/SpamRegexTest.php
index 178ffa3..e065248 100644
--- a/tests/SpamRegexTest.php
+++ b/tests/SpamRegexTest.php
@@ -33,10 +33,6 @@
         * @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 );
@@ -48,6 +44,9 @@
 
                // create spam filter
                $this->spamFilter = new SpamRegex;
+               if ( !$this->spamFilter->enabled() ) {
+                       $this->markTestSkipped( 'SpamRegex not enabled' );
+               }
 
                // create a dummy filter
                global $wgSpamRegex;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9934e4c71256254b66490d4df58359e8f7d5ccbc
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <jenkins-...@wikimedia.org>

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

Reply via email to