summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SpamBlacklist/tests
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/SpamBlacklist/tests
first commit
Diffstat (limited to 'www/wiki/extensions/SpamBlacklist/tests')
-rw-r--r--www/wiki/extensions/SpamBlacklist/tests/phan/config.php19
-rw-r--r--www/wiki/extensions/SpamBlacklist/tests/phpunit/BaseBlacklistTest.php51
-rw-r--r--www/wiki/extensions/SpamBlacklist/tests/phpunit/SpamBlacklistTest.php91
3 files changed, 161 insertions, 0 deletions
diff --git a/www/wiki/extensions/SpamBlacklist/tests/phan/config.php b/www/wiki/extensions/SpamBlacklist/tests/phan/config.php
new file mode 100644
index 00000000..467f0c27
--- /dev/null
+++ b/www/wiki/extensions/SpamBlacklist/tests/phan/config.php
@@ -0,0 +1,19 @@
+<?php
+
+$cfg = require __DIR__ . '/../../vendor/mediawiki/mediawiki-phan-config/src/config.php';
+$cfg['directory_list'] = array_merge(
+ $cfg['directory_list'],
+ [
+ './../../extensions/CheckUser',
+ './../../extensions/EventLogging',
+ ]
+);
+$cfg['exclude_analysis_directory_list'] = array_merge(
+ $cfg['exclude_analysis_directory_list'],
+ [
+ './../../extensions/CheckUser',
+ './../../extensions/EventLogging',
+ ]
+);
+
+return $cfg;
diff --git a/www/wiki/extensions/SpamBlacklist/tests/phpunit/BaseBlacklistTest.php b/www/wiki/extensions/SpamBlacklist/tests/phpunit/BaseBlacklistTest.php
new file mode 100644
index 00000000..634a266d
--- /dev/null
+++ b/www/wiki/extensions/SpamBlacklist/tests/phpunit/BaseBlacklistTest.php
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * @covers BaseBlacklist
+ */
+class BaseBlacklistTest extends MediaWikiTestCase {
+
+ /**
+ * @return array
+ */
+ public static function provideGetTypeFromTitle() {
+ return [
+ [ 'MediaWiki:Spam-blacklist', 'spam' ],
+ [ 'MediaWiki:Spam-whitelist', 'spam' ],
+ [ 'MediaWiki:Email-whitelist', 'email' ],
+ [ 'MediaWiki:Email-blacklist', 'email' ],
+ [ 'MediaWiki:A random page', false ],
+ [ 'Another random page', false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetTypeFromTitle
+ * @param string $title
+ * @param string|bool $expected
+ */
+ public function testGetTypeFromTitle( $title, $expected ) {
+ $title = Title::newFromText( $title );
+ $output = BaseBlacklist::getTypeFromTitle( $title );
+ $this->assertEquals( $expected, $output );
+ }
+}
diff --git a/www/wiki/extensions/SpamBlacklist/tests/phpunit/SpamBlacklistTest.php b/www/wiki/extensions/SpamBlacklist/tests/phpunit/SpamBlacklistTest.php
new file mode 100644
index 00000000..7558cfda
--- /dev/null
+++ b/www/wiki/extensions/SpamBlacklist/tests/phpunit/SpamBlacklistTest.php
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * @group SpamBlacklist
+ */
+class SpamBlacklistTest extends MediaWikiTestCase {
+ /**
+ * @var SpamBlacklist
+ */
+ protected $spamFilter;
+
+ /**
+ * Spam blacklist regexes. Examples taken from:
+ *
+ * @see http://meta.wikimedia.org/wiki/Spam_blacklist
+ * @see http://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist
+ *
+ * via Flow extension
+ *
+ * @var array
+ */
+ protected $blacklist = [ '\b01bags\.com\b', 'sytes\.net' ];
+
+ /**
+ * Spam whitelist regexes. Examples taken from:
+ *
+ * @see http://en.wikipedia.org/wiki/MediaWiki:Spam-whitelist
+ *
+ * via Flow extension
+ *
+ * @var array
+ */
+ protected $whitelist = [ 'a5b\.sytes\.net' ];
+
+ public function spamProvider() {
+ return [
+ 'no spam' => [
+ [ 'https://example.com' ],
+ false,
+ ],
+ 'revision with spam, with additional non-spam' => [
+ [ 'https://foo.com', 'http://01bags.com', 'http://bar.com' ],
+ [ '01bags.com' ],
+ ],
+
+ 'revision with spam using full width stop normalization' => [
+ [ 'http://01bags.com' ],
+ [ '01bags.com' ],
+ ],
+
+ 'revision with domain blacklisted as spam, but subdomain whitelisted' => [
+ [ 'http://a5b.sytes.net' ],
+ false,
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider spamProvider
+ */
+ public function testSpam( $links, $expected ) {
+ $returnValue = $this->spamFilter->filter( $links, Title::newMainPage() );
+ $this->assertEquals( $expected, $returnValue );
+ }
+
+ protected function setUp() {
+ parent::setUp();
+
+ // create spam filter
+ $this->spamFilter = new SpamBlacklist;
+
+ $this->setMwGlobals( 'wgBlacklistSettings', [
+ 'files' => [],
+ ] );
+
+ \MessageCache::singleton()->enable();
+ $this->insertPage( 'MediaWiki:Spam-blacklist', implode( "\n", $this->blacklist ) );
+ $this->insertPage( 'MediaWiki:Spam-whitelist', implode( "\n", $this->whitelist ) );
+
+ // That only works if the spam blacklist is really reset
+ $instance = BaseBlacklist::getInstance( 'spam' );
+ $reflProp = new \ReflectionProperty( $instance, 'regexes' );
+ $reflProp->setAccessible( true );
+ $reflProp->setValue( $instance, false );
+ }
+
+ protected function tearDown() {
+ \MessageCache::singleton()->disable();
+ parent::tearDown();
+ }
+}