MediaWiki  1.32.5
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 
130  public function provideCanSkipCaptchaUserright() {
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 }
CaptchaTest\provideCanSkipCaptchaMailconfirmed
provideCanSkipCaptchaMailconfirmed()
Definition: CaptchaTest.php:157
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
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:280
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
CaptchaTest\provideCanSkipCaptchaUserright
provideCanSkipCaptchaUserright()
Definition: CaptchaTest.php:130
CaptchaTest\testNamespaceTriggersOverwrite
testNamespaceTriggersOverwrite( $trigger, $expected)
provideNamespaceOverwrites
Definition: CaptchaTest.php:34
CaptchaTest\testCanSkipCaptchaMailconfirmed
testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail, $userIsMailConfirmed, $expected)
Definition: CaptchaTest.php:144
CaptchaTest\provideAttributeSet
provideAttributeSet()
Definition: CaptchaTest.php:89
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:14
CaptchaTest\testCanSkipCaptchaIPWhitelisted
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
Definition: CaptchaTest.php:173
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
CaptchaTest\testCaptchaTriggersAttributeSetTrue
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
provideAttributeSet
Definition: CaptchaTest.php:83
CaptchaTest\provideNamespaceOverwrites
provideNamespaceOverwrites()
Definition: CaptchaTest.php:50
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
CaptchaTest\provideSimpleTriggersCaptcha
provideSimpleTriggersCaptcha()
Definition: CaptchaTest.php:20
CaptchaTest
SimpleCaptcha.
Definition: CaptchaTest.php:6
CaptchaTest\testCaptchaTriggersAttributeGetsOverwritten
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
provideAttributeOverwritten
Definition: CaptchaTest.php:99
$request
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:2683
CaptchaTest\testCanSkipCaptchaUserright
testCanSkipCaptchaUserright( $userIsAllowed, $expected)
provideCanSkipCaptchaUserright
Definition: CaptchaTest.php:120
$value
$value
Definition: styleTest.css.php:49
CaptchaTest\testTriggersCaptcha
testTriggersCaptcha( $action, $expectedResult)
provideSimpleTriggersCaptcha
Definition: CaptchaTest.php:10
SimpleCaptcha
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
Definition: SimpleCaptcha.php:8
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:434
CaptchaTest\provideCanSkipCaptchaIPWhitelisted
provideCanSkipCaptchaIPWhitelisted()
Definition: CaptchaTest.php:189
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
CaptchaTest\setCaptchaTriggersAttribute
setCaptchaTriggersAttribute( $trigger, $value)
Definition: CaptchaTest.php:57
true
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:2044
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
CaptchaTest\provideAttributeOverwritten
provideAttributeOverwritten()
Definition: CaptchaTest.php:110