MediaWiki  1.34.0
CaptchaTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ScopedCallback;
4 use Wikimedia\TestingAccessWrapper;
5 
9 class 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 
150  public function provideCanSkipCaptchaUserright() {
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 }
CaptchaTest\provideCanSkipCaptchaMailconfirmed
provideCanSkipCaptchaMailconfirmed()
Definition: CaptchaTest.php:177
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
CaptchaTest\provideCanSkipCaptchaUserright
provideCanSkipCaptchaUserright()
Definition: CaptchaTest.php:150
CaptchaTest\testNamespaceTriggersOverwrite
testNamespaceTriggersOverwrite( $trigger, $expected)
@dataProvider provideNamespaceOverwrites
Definition: CaptchaTest.php:47
CaptchaTest\testCanSkipCaptchaMailconfirmed
testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail, $userIsMailConfirmed, $expected)
Definition: CaptchaTest.php:164
CaptchaTest\tearDown
tearDown()
Definition: CaptchaTest.php:14
true
return true
Definition: router.php:92
CaptchaTest\provideAttributeSet
provideAttributeSet()
Definition: CaptchaTest.php:109
getUser
getUser()
CaptchaTest\testCanSkipCaptchaIPWhitelisted
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
Definition: CaptchaTest.php:193
CaptchaTest\$hold
ScopedCallback[] $hold
Definition: CaptchaTest.php:12
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:106
CaptchaTest\testCaptchaTriggersAttributeSetTrue
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
@dataProvider provideAttributeSet
Definition: CaptchaTest.php:103
CaptchaTest\provideNamespaceOverwrites
provideNamespaceOverwrites()
Definition: CaptchaTest.php:63
CaptchaTest\provideSimpleTriggersCaptcha
provideSimpleTriggersCaptcha()
Definition: CaptchaTest.php:33
CaptchaTest
@covers SimpleCaptcha
Definition: CaptchaTest.php:9
$title
$title
Definition: testCompression.php:34
CaptchaTest\testCaptchaTriggersAttributeGetsOverwritten
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
@dataProvider provideAttributeOverwritten
Definition: CaptchaTest.php:119
CaptchaTest\testCanSkipCaptchaUserright
testCanSkipCaptchaUserright( $userIsAllowed, $expected)
@dataProvider provideCanSkipCaptchaUserright
Definition: CaptchaTest.php:140
CaptchaTest\testTriggersCaptcha
testTriggersCaptcha( $action, $expectedResult)
@dataProvider provideSimpleTriggersCaptcha
Definition: CaptchaTest.php:23
SimpleCaptcha
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
Definition: SimpleCaptcha.php:9
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
CaptchaTest\provideCanSkipCaptchaIPWhitelisted
provideCanSkipCaptchaIPWhitelisted()
Definition: CaptchaTest.php:209
CaptchaTest\setCaptchaTriggersAttribute
setCaptchaTriggersAttribute( $trigger, $value)
Definition: CaptchaTest.php:70
$keys
$keys
Definition: testCompression.php:67
CaptchaTest\provideAttributeOverwritten
provideAttributeOverwritten()
Definition: CaptchaTest.php:130
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6