1<?php 2/** 3 * This program is free software; you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation; either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write to the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 * http://www.gnu.org/copyleft/gpl.html 17 * 18 * @file 19 */ 20 21use MediaWiki\EditPage\Constraint\IEditConstraint; 22use MediaWiki\EditPage\Constraint\SpamRegexConstraint; 23use MediaWiki\EditPage\SpamChecker; 24use Psr\Log\LogLevel; 25use Psr\Log\NullLogger; 26 27/** 28 * Tests the SpamRegexContraint 29 * 30 * @author DannyS712 31 * 32 * @covers \MediaWiki\EditPage\Constraint\SpamRegexConstraint 33 */ 34class SpamRegexConstraintTest extends MediaWikiUnitTestCase { 35 use EditConstraintTestTrait; 36 37 public function testPass() { 38 $summary = __METHOD__ . '-summary'; 39 $sectionHeading = __METHOD__ . '-section-heading'; 40 $text = __METHOD__ . '-text'; 41 42 $spamChecker = $this->createMock( SpamChecker::class ); 43 $spamChecker->expects( $this->once() ) 44 ->method( 'checkSummary' ) 45 ->with( $summary ) 46 ->willReturn( false ); 47 $spamChecker->expects( $this->exactly( 2 ) ) 48 ->method( 'checkContent' ) 49 ->withConsecutive( 50 [ $sectionHeading ], 51 [ $text ] 52 ) 53 ->willReturn( false ); 54 55 $title = $this->createMock( Title::class ); 56 $title->expects( $this->never() ) 57 ->method( 'getPrefixedDBkey' ); 58 59 $logger = new NullLogger(); 60 61 $constraint = new SpamRegexConstraint( 62 $logger, 63 $spamChecker, 64 $summary, 65 'new', 66 $sectionHeading, 67 $text, 68 'Request-IP', 69 $title 70 ); 71 $this->assertConstraintPassed( $constraint ); 72 } 73 74 public function testFailure() { 75 $summary = __METHOD__ . '-summary'; 76 $sectionHeading = __METHOD__ . '-section-heading'; 77 $text = __METHOD__ . '-text'; 78 $matchingText = __METHOD__ . '-match'; 79 80 $spamChecker = $this->createMock( SpamChecker::class ); 81 $spamChecker->expects( $this->once() ) 82 ->method( 'checkSummary' ) 83 ->with( $summary ) 84 ->willReturn( $matchingText ); 85 86 $prefixedDBKey = 'PrefixedDBKeyGoesHere'; 87 $title = $this->createMock( Title::class ); 88 $title->expects( $this->once() ) 89 ->method( 'getPrefixedDBkey' ) 90 ->willReturn( $prefixedDBKey ); 91 92 $logger = new TestLogger( true ); 93 94 $constraint = new SpamRegexConstraint( 95 $logger, 96 $spamChecker, 97 $summary, 98 '', 99 $sectionHeading, 100 $text, 101 'Request-IP', 102 $title 103 ); 104 $this->assertConstraintFailed( $constraint, IEditConstraint::AS_SPAM_ERROR ); 105 106 $this->assertSame( [ 107 [ 108 LogLevel::DEBUG, 109 '{ip} spam regex hit [[{title}]]: "{match}"' 110 ], 111 ], $logger->getBuffer() ); 112 $logger->clearBuffer(); 113 114 $this->assertSame( 115 $matchingText, 116 $constraint->getMatch() 117 ); 118 } 119 120} 121