MediaWiki REL1_34
CaptchaTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\ScopedCallback;
4use Wikimedia\TestingAccessWrapper;
5
9class CaptchaTest extends MediaWikiTestCase {
10
12 private $hold = [];
13
14 public function tearDown() {
15 // Destroy any ScopedCallbacks being held
16 $this->hold = [];
17 parent::tearDown();
18 }
19
23 public function testTriggersCaptcha( $action, $expectedResult ) {
24 $captcha = new SimpleCaptcha();
25 $this->setMwGlobals( [
26 'wgCaptchaTriggers' => [
27 $action => $expectedResult,
28 ]
29 ] );
30 $this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) );
31 }
32
33 public function provideSimpleTriggersCaptcha() {
34 $data = [];
35 $captchaTriggers = new ReflectionClass( CaptchaTriggers::class );
36 $constants = $captchaTriggers->getConstants();
37 foreach ( $constants as $const ) {
38 $data[] = [ $const, true ];
39 $data[] = [ $const, false ];
40 }
41 return $data;
42 }
43
47 public function testNamespaceTriggersOverwrite( $trigger, $expected ) {
48 $captcha = new SimpleCaptcha();
49 $this->setMwGlobals( [
50 'wgCaptchaTriggers' => [
51 $trigger => !$expected,
52 ],
53 'wgCaptchaTriggersOnNamespace' => [
54 0 => [
55 $trigger => $expected,
56 ],
57 ],
58 ] );
59 $title = Title::newFromText( 'Main' );
60 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
61 }
62
63 public function provideNamespaceOverwrites() {
64 return [
65 [ 'edit', true ],
66 [ 'edit', false ],
67 ];
68 }
69
70 private function setCaptchaTriggersAttribute( $trigger, $value ) {
71 // XXX This is really hacky, but is needed to stop extensions from
72 // being clobbered in subsequent tests. This should be fixed properly
73 // by making extension registration happen in services instead of
74 // globals.
75 $keys =
76 TestingAccessWrapper::newFromClass( ExtensionProcessor::class )->globalSettings;
77 $globalsToStash = [];
78 foreach ( $keys as $key ) {
79 $globalsToStash["wg$key"] = $GLOBALS["wg$key"];
80 }
81 $this->setMwGlobals( $globalsToStash );
82
83 $info = [
84 'globals' => [],
85 'callbacks' => [],
86 'defines' => [],
87 'credits' => [],
88 'attributes' => [
89 'CaptchaTriggers' => [
90 $trigger => $value
91 ]
92 ],
93 'autoloaderPaths' => []
94 ];
95 $this->hold[] = ExtensionRegistry::getInstance()->setAttributeForTest(
96 'CaptchaTriggers', [ $trigger => $value ]
97 );
98 }
99
103 public function testCaptchaTriggersAttributeSetTrue( $trigger, $value ) {
104 $this->setCaptchaTriggersAttribute( $trigger, $value );
105 $captcha = new SimpleCaptcha();
106 $this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
107 }
108
109 public function provideAttributeSet() {
110 return [
111 [ 'test', true ],
112 [ 'test', false ],
113 ];
114 }
115
119 public function testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected ) {
120 $this->setMwGlobals( [
121 'wgCaptchaTriggers' => [
122 $trigger => $expected
123 ]
124 ] );
125 $this->setCaptchaTriggersAttribute( $trigger, !$expected );
126 $captcha = new SimpleCaptcha();
127 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
128 }
129
130 public function provideAttributeOverwritten() {
131 return [
132 [ 'edit', true ],
133 [ 'edit', false ],
134 ];
135 }
136
140 public function testCanSkipCaptchaUserright( $userIsAllowed, $expected ) {
141 $testObject = new SimpleCaptcha();
142 $user = $this->getMock( User::class );
143 $user->method( 'isAllowed' )->willReturn( $userIsAllowed );
144
145 $actual = $testObject->canSkipCaptcha( $user, RequestContext::getMain()->getConfig() );
146
147 $this->assertEquals( $expected, $actual );
148 }
149
151 return [
152 [ true, true ],
153 [ false, false ]
154 ];
155 }
156
164 public function testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail,
165 $userIsMailConfirmed, $expected ) {
166 $testObject = new SimpleCaptcha();
167 $user = $this->getMock( User::class );
168 $user->method( 'isEmailConfirmed' )->willReturn( $userIsMailConfirmed );
169 $config = $this->getMock( Config::class );
170 $config->method( 'get' )->willReturn( $allowUserConfirmEmail );
171
172 $actual = $testObject->canSkipCaptcha( $user, $config );
173
174 $this->assertEquals( $expected, $actual );
175 }
176
178 return [
179 [ false, false, false ],
180 [ false, true, false ],
181 [ true, false, false ],
182 [ true, true, true ],
183 ];
184 }
185
193 public function testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected ) {
194 $testObject = new SimpleCaptcha();
195 $config = $this->getMock( Config::class );
196 $request = $this->getMock( WebRequest::class );
197 $request->method( 'getIP' )->willReturn( $requestIP );
198
199 $this->setMwGlobals( [
200 'wgRequest' => $request,
201 'wgCaptchaWhitelistIP' => $IPWhitelist
202 ] );
203
204 $actual = $testObject->canSkipCaptcha( RequestContext::getMain()->getUser(), $config );
205
206 $this->assertEquals( $expected, $actual );
207 }
208
210 return ( [
211 [ '127.0.0.1', [ '127.0.0.1', '127.0.0.2' ], true ],
212 [ '127.0.0.1', [], false ]
213 ]
214 );
215 }
216}
getUser()
$GLOBALS['IP']
@covers SimpleCaptcha
testNamespaceTriggersOverwrite( $trigger, $expected)
@dataProvider provideNamespaceOverwrites
provideNamespaceOverwrites()
provideCanSkipCaptchaIPWhitelisted()
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
@dataProvider provideAttributeOverwritten
provideCanSkipCaptchaUserright()
testCanSkipCaptchaUserright( $userIsAllowed, $expected)
@dataProvider provideCanSkipCaptchaUserright
testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail, $userIsMailConfirmed, $expected)
setCaptchaTriggersAttribute( $trigger, $value)
testTriggersCaptcha( $action, $expectedResult)
@dataProvider provideSimpleTriggersCaptcha
provideAttributeOverwritten()
provideCanSkipCaptchaMailconfirmed()
ScopedCallback[] $hold
provideSimpleTriggersCaptcha()
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
@dataProvider provideAttributeSet
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
return true
Definition router.php:94