| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\EditPage\Constraint\IEditConstraint; |
| 22 |
use MediaWiki\EditPage\Constraint\SpamRegexConstraint; |
| 23 |
use MediaWiki\EditPage\SpamChecker; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use Psr\Log\LogLevel; |
| 26 |
use Psr\Log\NullLogger; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class SpamRegexConstraintTest extends MediaWikiUnitTestCase { |
| 36 |
use EditConstraintTestTrait; |
| 37 |
|
| 38 |
public function testPass() { |
| 39 |
$summary = __METHOD__ . '-summary'; |
| 40 |
$sectionHeading = __METHOD__ . '-section-heading'; |
| 41 |
$text = __METHOD__ . '-text'; |
| 42 |
|
| 43 |
$spamChecker = $this->createMock( SpamChecker::class ); |
| 44 |
$spamChecker->expects( $this->once() ) |
| 45 |
->method( 'checkSummary' ) |
| 46 |
->with( $summary ) |
| 47 |
->willReturn( false ); |
| 48 |
$spamChecker->expects( $this->exactly( 2 ) ) |
| 49 |
->method( 'checkContent' ) |
| 50 |
->willReturnMap( [ |
| 51 |
[ $sectionHeading, false ], |
| 52 |
[ $text, false ], |
| 53 |
] ); |
| 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 |
$sectionHeading, |
| 66 |
$text, |
| 67 |
'Request-IP', |
| 68 |
$title |
| 69 |
); |
| 70 |
$this->assertConstraintPassed( $constraint ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testFailure() { |
| 74 |
$summary = __METHOD__ . '-summary'; |
| 75 |
$sectionHeading = __METHOD__ . '-section-heading'; |
| 76 |
$text = __METHOD__ . '-text'; |
| 77 |
$matchingText = __METHOD__ . '-match'; |
| 78 |
|
| 79 |
$spamChecker = $this->createMock( SpamChecker::class ); |
| 80 |
$spamChecker->expects( $this->once() ) |
| 81 |
->method( 'checkSummary' ) |
| 82 |
->with( $summary ) |
| 83 |
->willReturn( $matchingText ); |
| 84 |
|
| 85 |
$prefixedDBKey = 'PrefixedDBKeyGoesHere'; |
| 86 |
$title = $this->createMock( Title::class ); |
| 87 |
$title->expects( $this->once() ) |
| 88 |
->method( 'getPrefixedDBkey' ) |
| 89 |
->willReturn( $prefixedDBKey ); |
| 90 |
|
| 91 |
$logger = new TestLogger( true ); |
| 92 |
|
| 93 |
$constraint = new SpamRegexConstraint( |
| 94 |
$logger, |
| 95 |
$spamChecker, |
| 96 |
$summary, |
| 97 |
$sectionHeading, |
| 98 |
$text, |
| 99 |
'Request-IP', |
| 100 |
$title |
| 101 |
); |
| 102 |
$this->assertConstraintFailed( $constraint, IEditConstraint::AS_SPAM_ERROR ); |
| 103 |
|
| 104 |
$this->assertSame( [ |
| 105 |
[ |
| 106 |
LogLevel::DEBUG, |
| 107 |
'{ip} spam regex hit [[{title}]]: "{match}"' |
| 108 |
], |
| 109 |
], $logger->getBuffer() ); |
| 110 |
$logger->clearBuffer(); |
| 111 |
|
| 112 |
$this->assertSame( |
| 113 |
$matchingText, |
| 114 |
$constraint->getMatch() |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
} |