Debian Codegraph @

tests/phpunit/unit/includes/editpage/Constraint/SpamRegexConstraintTest.php

raw symbols SCIP
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
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 * Tests the SpamRegexContraint
30 *
31 * @author DannyS712
32 *
33 * @covers \MediaWiki\EditPage\Constraint\SpamRegexConstraint
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 }