Debian Codegraph @

extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\Context\RequestContext;
5 use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers;
6 use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha;
7 use MediaWiki\Extension\ConfirmEdit\Store\CaptchaCacheStore;
8 use MediaWiki\Extension\ConfirmEdit\Store\CaptchaStore;
9 use MediaWiki\Registration\ExtensionRegistry;
10 use MediaWiki\Request\FauxRequest;
11 use MediaWiki\Request\WebRequest;
12 use MediaWiki\Title\Title;
13 use MediaWiki\User\User;
14 use Wikimedia\ScopedCallback;
15 use Wikimedia\TestingAccessWrapper;
16
17 /**
18 * @covers \MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha
19 */
20 class CaptchaTest extends MediaWikiIntegrationTestCase {
21 private const TEST_CAPTCHA_INDEX = 127;
22
23 /** @var ScopedCallback[] */
24 private $hold = [];
25
26 protected function setUp(): void {
27 parent::setUp();
28
29 // Clear any handlers of the ConfirmEditTriggersCaptcha hook for this test, as in CI their additional
30 // checks may cause the tests to fail (such as those from IPReputation).
31 $this->clearHook( 'ConfirmEditTriggersCaptcha' );
32 }
33
34 public function tearDown(): void {
35 // Destroy any ScopedCallbacks being held
36 $this->hold = [];
37 parent::tearDown();
38 }
39
40 /**
41 * @dataProvider providePassCaptchaLimitedFromRequest
42 *
43 * @param string|null $captchaId
44 * @param string|null $captchaWord
45 * @param bool $passed
46 * @return void
47 */
48 public function testPassCaptchaLimitedFromRequest(
49 ?string $captchaId,
50 ?string $captchaWord,
51 bool $passed
52 ): void {
53 $this->setMwGlobals( 'wgCaptchaStorageClass', CaptchaCacheStore::class );
54 CaptchaStore::unsetInstanceForTests();
55
56 $params = [ 'index' => self::TEST_CAPTCHA_INDEX, 'question' => '5+3', 'answer' => 8 ];
57
58 $captcha = new SimpleCaptcha();
59 $captcha->storeCaptcha( $params );
60
61 $request = new FauxRequest( array_filter( [
62 'wpCaptchaId' => $captchaId,
63 'wpCaptchaWord' => $captchaWord,
64 ] ) );
65
66 $user = $this->createMock( User::class );
67 $user->expects( $passed ? $this->once() : $this->exactly( 2 ) )
68 ->method( 'pingLimiter' )
69 ->willReturnMap( [
70 [ 'badcaptcha', 0, false ],
71 [ 'badcaptcha', 1, false ]
72 ] );
73
74 $this->assertSame( $passed, $captcha->passCaptchaLimitedFromRequest( $request, $user ) );
75 }
76
77 public function providePassCaptchaLimitedFromRequest(): iterable {
78 yield 'new captcha session' => [ null, null, false ];
79 yield 'missing captcha ID' => [ null, '8', false ];
80 yield 'mismatched captcha ID' => [ '129', '8', false ];
81 yield 'missing answer' => [ (string)self::TEST_CAPTCHA_INDEX, null, false ];
82 yield 'wrong answer' => [ (string)self::TEST_CAPTCHA_INDEX, '7', false ];
83 yield 'correct answer' => [ (string)self::TEST_CAPTCHA_INDEX, '8', true ];
84 }
85
86 /**
87 * @dataProvider provideSimpleTriggersCaptcha
88 */
89 public function testTriggersCaptcha( $action, $expectedResult ) {
90 $captcha = new SimpleCaptcha();
91 $this->overrideConfigValue( 'CaptchaTriggers', [
92 $action => $expectedResult,
93 ] );
94 $this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) );
95 }
96
97 public static function provideSimpleTriggersCaptcha() {
98 $data = [];
99 $captchaTriggers = new ReflectionClass( CaptchaTriggers::class );
100 $constants = $captchaTriggers->getConstants();
101 foreach ( $constants as $const ) {
102 $data[] = [ $const, true ];
103 $data[] = [ $const, false ];
104 }
105 return $data;
106 }
107
108 /**
109 * @dataProvider provideBooleans
110 */
111 public function testNamespaceTriggersOverwrite( bool $expected ) {
112 $trigger = 'edit';
113 $captcha = new SimpleCaptcha();
114 $this->overrideConfigValues( [
115 'CaptchaTriggers' => [
116 $trigger => !$expected,
117 ],
118 'CaptchaTriggersOnNamespace' => [
119 0 => [
120 $trigger => $expected,
121 ],
122 ],
123 ] );
124 $title = Title::newFromText( 'Main' );
125 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
126 }
127
128 private function setCaptchaTriggersAttribute( $trigger, $value ) {
129 // Avoid clobbering captcha triggers registered by other extensions
130 $this->overrideConfigValue( 'CaptchaTriggers', $GLOBALS['wgCaptchaTriggers'] );
131
132 $this->hold[] = ExtensionRegistry::getInstance()->setAttributeForTest(
133 'CaptchaTriggers', [ $trigger => $value ]
134 );
135 }
136
137 /**
138 * @dataProvider provideBooleans
139 */
140 public function testCaptchaTriggersAttributeSetTrue( bool $value ) {
141 $trigger = 'test';
142 $this->setCaptchaTriggersAttribute( $trigger, $value );
143 $captcha = new SimpleCaptcha();
144 $this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
145 }
146
147 /**
148 * @dataProvider provideBooleans
149 */
150 public function testCaptchaTriggersAttributeGetsOverwritten( bool $expected ) {
151 $trigger = 'edit';
152 $this->overrideConfigValue( 'CaptchaTriggers', [ $trigger => $expected ] );
153 $this->setCaptchaTriggersAttribute( $trigger, !$expected );
154 $captcha = new SimpleCaptcha();
155 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
156 }
157
158 /**
159 * @dataProvider provideBooleans
160 */
161 public function testCanSkipCaptchaUserright( bool $userIsAllowed ) {
162 $testObject = new SimpleCaptcha();
163 $user = $this->createMock( User::class );
164 $user->method( 'isAllowed' )->willReturn( $userIsAllowed );
165
166 $actual = $testObject->canSkipCaptcha( $user, RequestContext::getMain()->getConfig() );
167
168 $this->assertEquals( $userIsAllowed, $actual );
169 }
170
171 public static function provideBooleans() {
172 yield [ true ];
173 yield [ false ];
174 }
175
176 /**
177 * @dataProvider provideCanSkipCaptchaMailconfirmed
178 */
179 public function testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail,
180 $userIsMailConfirmed, $expected ) {
181 $testObject = new SimpleCaptcha();
182 $user = $this->createMock( User::class );
183 $user->method( 'isEmailConfirmed' )->willReturn( $userIsMailConfirmed );
184 $config = new HashConfig( [ 'AllowConfirmedEmail' => $allowUserConfirmEmail ] );
185
186 $actual = $testObject->canSkipCaptcha( $user, $config );
187
188 $this->assertEquals( $expected, $actual );
189 }
190
191 public static function provideCanSkipCaptchaMailconfirmed() {
192 return [
193 [ false, false, false ],
194 [ false, true, false ],
195 [ true, false, false ],
196 [ true, true, true ],
197 ];
198 }
199
200 /**
201 * @dataProvider provideCanSkipCaptchaBypassIPList
202 */
203 public function testCanSkipCaptchaBypassIP( $requestIP, $list, $expected ) {
204 $testObject = new SimpleCaptcha();
205 $config = new HashConfig( [ 'AllowConfirmedEmail' => false ] );
206 $request = $this->createMock( WebRequest::class );
207 $request->method( 'getIP' )->willReturn( $requestIP );
208
209 $this->setMwGlobals( [
210 'wgRequest' => $request,
211 ] );
212 $this->overrideConfigValue( 'CaptchaBypassIPs', $list );
213
214 $actual = $testObject->canSkipCaptcha( RequestContext::getMain()->getUser(), $config );
215
216 $this->assertEquals( $expected, $actual );
217 }
218
219 public static function provideCanSkipCaptchaBypassIPList() {
220 return ( [
221 [ '127.0.0.1', [ '127.0.0.1', '127.0.0.2' ], true ],
222 [ '127.0.0.1', [], false ]
223 ]
224 );
225 }
226
227 public function testCanSkipCaptchaSystemUser(): void {
228 $testObject = new SimpleCaptcha();
229 $config = new HashConfig( [ 'AllowConfirmedEmail' => false ] );
230 $user = $this->createConfiguredMock( User::class, [ 'isSystemUser' => true ] );
231
232 $actual = $testObject->canSkipCaptcha( $user, $config );
233
234 $this->assertTrue( $actual );
235 }
236
237 public function testTriggersCaptchaReturnsEarlyIfCaptchaSolved() {
238 $this->overrideConfigValue( 'CaptchaTriggers', [
239 'edit' => true,
240 ] );
241 $testObject = new SimpleCaptcha();
242 /** @var SimpleCaptcha $wrapper */
243 $wrapper = TestingAccessWrapper::newFromObject( $testObject );
244 $wrapper->captchaSolved = true;
245 $this->assertFalse( $testObject->triggersCaptcha( 'edit' ), 'CAPTCHA is not triggered if already solved' );
246 }
247
248 public function testForceShowCaptcha() {
249 $this->overrideConfigValue( 'CaptchaTriggers', [
250 'edit' => false,
251 ] );
252 $testObject = new SimpleCaptcha();
253 $this->assertFalse(
254 $testObject->triggersCaptcha( 'edit' ), 'CAPTCHA is not triggered by edit action in this configuration'
255 );
256 $testObject->setForceShowCaptcha( true );
257 $this->assertTrue( $testObject->triggersCaptcha( 'edit' ), 'Force showing a CAPTCHA if flag is set' );
258 }
259 }