2017-08-14 15:39:20 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-08 16:40:15 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers;
|
|
|
|
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha;
|
2019-01-14 18:33:28 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
2018-01-23 23:59:08 +00:00
|
|
|
/**
|
2022-04-08 16:40:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha
|
2018-01-23 23:59:08 +00:00
|
|
|
*/
|
2021-10-11 22:36:53 +00:00
|
|
|
class CaptchaTest extends MediaWikiIntegrationTestCase {
|
2019-01-14 18:33:28 +00:00
|
|
|
|
|
|
|
/** @var ScopedCallback[] */
|
|
|
|
private $hold = [];
|
|
|
|
|
2021-07-22 06:38:52 +00:00
|
|
|
public function tearDown(): void {
|
2019-01-14 18:33:28 +00:00
|
|
|
// Destroy any ScopedCallbacks being held
|
|
|
|
$this->hold = [];
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:39:20 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSimpleTriggersCaptcha
|
|
|
|
*/
|
|
|
|
public function testTriggersCaptcha( $action, $expectedResult ) {
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgCaptchaTriggers' => [
|
|
|
|
$action => $expectedResult,
|
|
|
|
]
|
|
|
|
] );
|
|
|
|
$this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideSimpleTriggersCaptcha() {
|
2017-08-14 15:39:20 +00:00
|
|
|
$data = [];
|
|
|
|
$captchaTriggers = new ReflectionClass( CaptchaTriggers::class );
|
|
|
|
$constants = $captchaTriggers->getConstants();
|
|
|
|
foreach ( $constants as $const ) {
|
|
|
|
$data[] = [ $const, true ];
|
|
|
|
$data[] = [ $const, false ];
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideNamespaceOverwrites
|
|
|
|
*/
|
|
|
|
public function testNamespaceTriggersOverwrite( $trigger, $expected ) {
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgCaptchaTriggers' => [
|
|
|
|
$trigger => !$expected,
|
|
|
|
],
|
|
|
|
'wgCaptchaTriggersOnNamespace' => [
|
|
|
|
0 => [
|
|
|
|
$trigger => $expected,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
] );
|
|
|
|
$title = Title::newFromText( 'Main' );
|
|
|
|
$this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideNamespaceOverwrites() {
|
2017-08-14 15:39:20 +00:00
|
|
|
return [
|
|
|
|
[ 'edit', true ],
|
|
|
|
[ 'edit', false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setCaptchaTriggersAttribute( $trigger, $value ) {
|
2022-03-10 17:36:34 +00:00
|
|
|
// Avoid clobbering captcha triggers registered by other extensions
|
|
|
|
$this->setMwGlobals( 'wgCaptchaTriggers', $GLOBALS['wgCaptchaTriggers'] );
|
2018-10-09 14:13:57 +00:00
|
|
|
|
2019-01-14 18:33:28 +00:00
|
|
|
$this->hold[] = ExtensionRegistry::getInstance()->setAttributeForTest(
|
|
|
|
'CaptchaTriggers', [ $trigger => $value ]
|
|
|
|
);
|
2017-08-14 15:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAttributeSet
|
|
|
|
*/
|
|
|
|
public function testCaptchaTriggersAttributeSetTrue( $trigger, $value ) {
|
|
|
|
$this->setCaptchaTriggersAttribute( $trigger, $value );
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideAttributeSet() {
|
2017-08-14 15:39:20 +00:00
|
|
|
return [
|
|
|
|
[ 'test', true ],
|
|
|
|
[ 'test', false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideAttributeOverwritten
|
|
|
|
*/
|
|
|
|
public function testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected ) {
|
2022-03-10 17:36:34 +00:00
|
|
|
$this->setMwGlobals( 'wgCaptchaTriggers', [ $trigger => $expected ] );
|
2017-08-14 15:39:20 +00:00
|
|
|
$this->setCaptchaTriggersAttribute( $trigger, !$expected );
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideAttributeOverwritten() {
|
2017-08-14 15:39:20 +00:00
|
|
|
return [
|
|
|
|
[ 'edit', true ],
|
|
|
|
[ 'edit', false ],
|
|
|
|
];
|
|
|
|
}
|
2018-05-18 18:29:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCanSkipCaptchaUserright
|
|
|
|
*/
|
|
|
|
public function testCanSkipCaptchaUserright( $userIsAllowed, $expected ) {
|
|
|
|
$testObject = new SimpleCaptcha();
|
2019-10-13 09:33:58 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2018-05-18 18:29:00 +00:00
|
|
|
$user->method( 'isAllowed' )->willReturn( $userIsAllowed );
|
|
|
|
|
|
|
|
$actual = $testObject->canSkipCaptcha( $user, RequestContext::getMain()->getConfig() );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideCanSkipCaptchaUserright() {
|
2018-05-18 18:29:00 +00:00
|
|
|
return [
|
|
|
|
[ true, true ],
|
|
|
|
[ false, false ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCanSkipCaptchaMailconfirmed
|
|
|
|
*/
|
|
|
|
public function testCanSkipCaptchaMailconfirmed( $allowUserConfirmEmail,
|
|
|
|
$userIsMailConfirmed, $expected ) {
|
|
|
|
$testObject = new SimpleCaptcha();
|
2019-10-13 09:33:58 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2018-05-18 18:29:00 +00:00
|
|
|
$user->method( 'isEmailConfirmed' )->willReturn( $userIsMailConfirmed );
|
2019-10-13 09:33:58 +00:00
|
|
|
$config = $this->createMock( Config::class );
|
2018-05-18 18:29:00 +00:00
|
|
|
$config->method( 'get' )->willReturn( $allowUserConfirmEmail );
|
|
|
|
|
|
|
|
$actual = $testObject->canSkipCaptcha( $user, $config );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideCanSkipCaptchaMailconfirmed() {
|
2018-05-18 18:29:00 +00:00
|
|
|
return [
|
|
|
|
[ false, false, false ],
|
|
|
|
[ false, true, false ],
|
|
|
|
[ true, false, false ],
|
|
|
|
[ true, true, true ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCanSkipCaptchaIPWhitelisted
|
|
|
|
*/
|
|
|
|
public function testCanSkipCaptchaIPWhitelisted( $requestIP, $IPWhitelist, $expected ) {
|
|
|
|
$testObject = new SimpleCaptcha();
|
2019-10-13 09:33:58 +00:00
|
|
|
$config = $this->createMock( Config::class );
|
|
|
|
$request = $this->createMock( WebRequest::class );
|
2018-05-18 18:29:00 +00:00
|
|
|
$request->method( 'getIP' )->willReturn( $requestIP );
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgRequest' => $request,
|
|
|
|
'wgCaptchaWhitelistIP' => $IPWhitelist
|
|
|
|
] );
|
|
|
|
|
|
|
|
$actual = $testObject->canSkipCaptcha( RequestContext::getMain()->getUser(), $config );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2023-05-20 09:59:50 +00:00
|
|
|
public static function provideCanSkipCaptchaIPWhitelisted() {
|
2018-05-18 18:29:00 +00:00
|
|
|
return ( [
|
|
|
|
[ '127.0.0.1', [ '127.0.0.1', '127.0.0.2' ], true ],
|
|
|
|
[ '127.0.0.1', [], false ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2017-08-14 15:39:20 +00:00
|
|
|
}
|