MediaWiki  1.33.0
CaptchaTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ScopedCallback;
4 use Wikimedia\TestingAccessWrapper;
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 
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
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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:306
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
CaptchaTest\provideCanSkipCaptchaUserright
provideCanSkipCaptchaUserright()
Definition: CaptchaTest.php:150
CaptchaTest\testNamespaceTriggersOverwrite
testNamespaceTriggersOverwrite( $trigger, $expected)
provideNamespaceOverwrites
Definition: CaptchaTest.php:47
CaptchaTest\testCanSkipCaptchaMailconfirmed
testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail, $userIsMailConfirmed, $expected)
Definition: CaptchaTest.php:164
CaptchaTest\tearDown
tearDown()
Definition: CaptchaTest.php:14
CaptchaTest\provideAttributeSet
provideAttributeSet()
Definition: CaptchaTest.php:109
CaptchaTest\testCanSkipCaptchaIPWhitelisted
testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected)
Definition: CaptchaTest.php:193
CaptchaTest\$hold
ScopedCallback[] $hold
Definition: CaptchaTest.php:12
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
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:98
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
CaptchaTest\testCaptchaTriggersAttributeSetTrue
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
provideAttributeSet
Definition: CaptchaTest.php:103
CaptchaTest\provideNamespaceOverwrites
provideNamespaceOverwrites()
Definition: CaptchaTest.php:63
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:709
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
CaptchaTest\provideSimpleTriggersCaptcha
provideSimpleTriggersCaptcha()
Definition: CaptchaTest.php:33
CaptchaTest
SimpleCaptcha.
Definition: CaptchaTest.php:9
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
CaptchaTest\testCaptchaTriggersAttributeGetsOverwritten
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
provideAttributeOverwritten
Definition: CaptchaTest.php:119
$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:2636
CaptchaTest\testCanSkipCaptchaUserright
testCanSkipCaptchaUserright( $userIsAllowed, $expected)
provideCanSkipCaptchaUserright
Definition: CaptchaTest.php:140
$value
$value
Definition: styleTest.css.php:49
CaptchaTest\testTriggersCaptcha
testTriggersCaptcha( $action, $expectedResult)
provideSimpleTriggersCaptcha
Definition: CaptchaTest.php:23
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:430
CaptchaTest\provideCanSkipCaptchaIPWhitelisted
provideCanSkipCaptchaIPWhitelisted()
Definition: CaptchaTest.php:209
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:70
$keys
$keys
Definition: testCompression.php:67
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:1985
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:130
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6