MediaWiki REL1_33
CaptchaTest.php
Go to the documentation of this file.
1<?php
2
5
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$GLOBALS['IP']
SimpleCaptcha.
testNamespaceTriggersOverwrite( $trigger, $expected)
provideNamespaceOverwrites
provideNamespaceOverwrites()
provideCanSkipCaptchaIPWhitelisted()
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
provideAttributeOverwritten
provideCanSkipCaptchaUserright()
testCanSkipCaptchaUserright( $userIsAllowed, $expected)
provideCanSkipCaptchaUserright
testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail, $userIsMailConfirmed, $expected)
setCaptchaTriggersAttribute( $trigger, $value)
testTriggersCaptcha( $action, $expectedResult)
provideSimpleTriggersCaptcha
provideAttributeOverwritten()
provideCanSkipCaptchaMailconfirmed()
ScopedCallback[] $hold
provideSimpleTriggersCaptcha()
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
provideAttributeSet
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2843
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2004
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)