MediaWiki REL1_32
CaptchaTest.php
Go to the documentation of this file.
1<?php
2
10 public function testTriggersCaptcha( $action, $expectedResult ) {
11 $captcha = new SimpleCaptcha();
12 $this->setMwGlobals( [
13 'wgCaptchaTriggers' => [
14 $action => $expectedResult,
15 ]
16 ] );
17 $this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) );
18 }
19
20 public function provideSimpleTriggersCaptcha() {
21 $data = [];
22 $captchaTriggers = new ReflectionClass( CaptchaTriggers::class );
23 $constants = $captchaTriggers->getConstants();
24 foreach ( $constants as $const ) {
25 $data[] = [ $const, true ];
26 $data[] = [ $const, false ];
27 }
28 return $data;
29 }
30
34 public function testNamespaceTriggersOverwrite( $trigger, $expected ) {
35 $captcha = new SimpleCaptcha();
36 $this->setMwGlobals( [
37 'wgCaptchaTriggers' => [
38 $trigger => !$expected,
39 ],
40 'wgCaptchaTriggersOnNamespace' => [
41 0 => [
42 $trigger => $expected,
43 ],
44 ],
45 ] );
46 $title = Title::newFromText( 'Main' );
47 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
48 }
49
50 public function provideNamespaceOverwrites() {
51 return [
52 [ 'edit', true ],
53 [ 'edit', false ],
54 ];
55 }
56
57 private function setCaptchaTriggersAttribute( $trigger, $value ) {
58 $info = [
59 'globals' => [],
60 'callbacks' => [],
61 'defines' => [],
62 'credits' => [],
63 'attributes' => [
64 'CaptchaTriggers' => [
65 $trigger => $value
66 ]
67 ],
68 'autoloaderPaths' => []
69 ];
70 $registry = new ExtensionRegistry();
71 $class = new ReflectionClass( 'ExtensionRegistry' );
72 $instanceProperty = $class->getProperty( 'instance' );
73 $instanceProperty->setAccessible( true );
74 $instanceProperty->setValue( $registry );
75 $method = $class->getMethod( 'exportExtractedData' );
76 $method->setAccessible( true );
77 $method->invokeArgs( $registry, [ $info ] );
78 }
79
83 public function testCaptchaTriggersAttributeSetTrue( $trigger, $value ) {
84 $this->setCaptchaTriggersAttribute( $trigger, $value );
85 $captcha = new SimpleCaptcha();
86 $this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
87 }
88
89 public function provideAttributeSet() {
90 return [
91 [ 'test', true ],
92 [ 'test', false ],
93 ];
94 }
95
99 public function testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected ) {
100 $this->setMwGlobals( [
101 'wgCaptchaTriggers' => [
102 $trigger => $expected
103 ]
104 ] );
105 $this->setCaptchaTriggersAttribute( $trigger, !$expected );
106 $captcha = new SimpleCaptcha();
107 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
108 }
109
110 public function provideAttributeOverwritten() {
111 return [
112 [ 'edit', true ],
113 [ 'edit', false ],
114 ];
115 }
116
120 public function testCanSkipCaptchaUserright( $userIsAllowed, $expected ) {
121 $testObject = new SimpleCaptcha();
122 $user = $this->getMock( User::class );
123 $user->method( 'isAllowed' )->willReturn( $userIsAllowed );
124
125 $actual = $testObject->canSkipCaptcha( $user, RequestContext::getMain()->getConfig() );
126
127 $this->assertEquals( $expected, $actual );
128 }
129
131 return [
132 [ true, true ],
133 [ false, false ]
134 ];
135 }
136
144 public function testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail,
145 $userIsMailConfirmed, $expected ) {
146 $testObject = new SimpleCaptcha();
147 $user = $this->getMock( User::class );
148 $user->method( 'isEmailConfirmed' )->willReturn( $userIsMailConfirmed );
149 $config = $this->getMock( Config::class );
150 $config->method( 'get' )->willReturn( $allowUserConfirmEmail );
151
152 $actual = $testObject->canSkipCaptcha( $user, $config );
153
154 $this->assertEquals( $expected, $actual );
155 }
156
158 return [
159 [ false, false, false ],
160 [ false, true, false ],
161 [ true, false, false ],
162 [ true, true, true ],
163 ];
164 }
165
173 public function testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected ) {
174 $testObject = new SimpleCaptcha();
175 $config = $this->getMock( Config::class );
176 $request = $this->getMock( WebRequest::class );
177 $request->method( 'getIP' )->willReturn( $requestIP );
178
179 $this->setMwGlobals( [
180 'wgRequest' => $request,
181 'wgCaptchaWhitelistIP' => $IPWhitelist
182 ] );
183
184 $actual = $testObject->canSkipCaptcha( RequestContext::getMain()->getUser(), $config );
185
186 $this->assertEquals( $expected, $actual );
187 }
188
190 return ( [
191 [ '127.0.0.1', [ '127.0.0.1', '127.0.0.2' ], true ],
192 [ '127.0.0.1', [], false ]
193 ]
194 );
195 }
196}
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()
provideSimpleTriggersCaptcha()
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
provideAttributeSet
ExtensionRegistry class.
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:2880
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:2055
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187