2017-08-14 15:39:20 +00:00
|
|
|
<?php
|
|
|
|
|
2024-09-13 10:57:46 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2024-06-08 21:46:45 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2022-04-08 16:40:15 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers;
|
|
|
|
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha;
|
2024-10-19 21:34:41 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2023-12-10 23:07:55 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-19 04:14:21 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-10 23:07:55 +00:00
|
|
|
use MediaWiki\User\User;
|
2019-01-14 18:33:28 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2024-05-15 08:23:46 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2019-01-14 18:33:28 +00:00
|
|
|
|
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();
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaTriggers', [
|
|
|
|
$action => $expectedResult,
|
2017-08-14 15:39:20 +00:00
|
|
|
] );
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-13 11:02:24 +00:00
|
|
|
* @dataProvider provideBooleans
|
2017-08-14 15:39:20 +00:00
|
|
|
*/
|
2024-09-13 11:02:24 +00:00
|
|
|
public function testNamespaceTriggersOverwrite( bool $expected ) {
|
|
|
|
$trigger = 'edit';
|
2017-08-14 15:39:20 +00:00
|
|
|
$captcha = new SimpleCaptcha();
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'CaptchaTriggers' => [
|
2017-08-14 15:39:20 +00:00
|
|
|
$trigger => !$expected,
|
|
|
|
],
|
2024-07-22 10:30:27 +00:00
|
|
|
'CaptchaTriggersOnNamespace' => [
|
2017-08-14 15:39:20 +00:00
|
|
|
0 => [
|
|
|
|
$trigger => $expected,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
] );
|
|
|
|
$title = Title::newFromText( 'Main' );
|
|
|
|
$this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setCaptchaTriggersAttribute( $trigger, $value ) {
|
2022-03-10 17:36:34 +00:00
|
|
|
// Avoid clobbering captcha triggers registered by other extensions
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaTriggers', $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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-13 11:02:24 +00:00
|
|
|
* @dataProvider provideBooleans
|
2017-08-14 15:39:20 +00:00
|
|
|
*/
|
2024-09-13 11:02:24 +00:00
|
|
|
public function testCaptchaTriggersAttributeSetTrue( bool $value ) {
|
|
|
|
$trigger = 'test';
|
2017-08-14 15:39:20 +00:00
|
|
|
$this->setCaptchaTriggersAttribute( $trigger, $value );
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-13 11:02:24 +00:00
|
|
|
* @dataProvider provideBooleans
|
2017-08-14 15:39:20 +00:00
|
|
|
*/
|
2024-09-13 11:02:24 +00:00
|
|
|
public function testCaptchaTriggersAttributeGetsOverwritten( bool $expected ) {
|
|
|
|
$trigger = 'edit';
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaTriggers', [ $trigger => $expected ] );
|
2017-08-14 15:39:20 +00:00
|
|
|
$this->setCaptchaTriggersAttribute( $trigger, !$expected );
|
|
|
|
$captcha = new SimpleCaptcha();
|
|
|
|
$this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:29:00 +00:00
|
|
|
/**
|
2024-09-13 11:02:24 +00:00
|
|
|
* @dataProvider provideBooleans
|
2018-05-18 18:29:00 +00:00
|
|
|
*/
|
2024-09-13 11:02:24 +00:00
|
|
|
public function testCanSkipCaptchaUserright( bool $userIsAllowed ) {
|
2018-05-18 18:29:00 +00:00
|
|
|
$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() );
|
|
|
|
|
2024-09-13 11:02:24 +00:00
|
|
|
$this->assertEquals( $userIsAllowed, $actual );
|
2018-05-18 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 11:02:24 +00:00
|
|
|
public static function provideBooleans() {
|
|
|
|
yield [ true ];
|
|
|
|
yield [ false ];
|
2018-05-18 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|
2024-09-13 10:57:46 +00:00
|
|
|
$config = new HashConfig( [ 'AllowConfirmedEmail' => $allowUserConfirmEmail ] );
|
2018-05-18 18:29:00 +00:00
|
|
|
|
|
|
|
$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();
|
2024-09-13 10:57:46 +00:00
|
|
|
$config = new HashConfig( [ 'AllowConfirmedEmail' => false ] );
|
2019-10-13 09:33:58 +00:00
|
|
|
$request = $this->createMock( WebRequest::class );
|
2018-05-18 18:29:00 +00:00
|
|
|
$request->method( 'getIP' )->willReturn( $requestIP );
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgRequest' => $request,
|
|
|
|
] );
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaWhitelistIP', $IPWhitelist );
|
2018-05-18 18:29:00 +00:00
|
|
|
|
|
|
|
$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 ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2024-05-15 08:23:46 +00:00
|
|
|
|
|
|
|
public function testTriggersCaptchaReturnsEarlyIfCaptchaSolved() {
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaTriggers', [
|
|
|
|
'edit' => true,
|
2024-05-15 08:23:46 +00:00
|
|
|
] );
|
|
|
|
$testObject = new SimpleCaptcha();
|
2024-09-13 10:51:48 +00:00
|
|
|
/** @var SimpleCaptcha $wrapper */
|
2024-05-15 08:23:46 +00:00
|
|
|
$wrapper = TestingAccessWrapper::newFromObject( $testObject );
|
|
|
|
$wrapper->captchaSolved = true;
|
2024-09-13 10:51:48 +00:00
|
|
|
$this->assertFalse( $testObject->triggersCaptcha( 'edit' ), 'CAPTCHA is not triggered if already solved' );
|
2024-05-15 08:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testForceShowCaptcha() {
|
2024-07-22 10:30:27 +00:00
|
|
|
$this->overrideConfigValue( 'CaptchaTriggers', [
|
|
|
|
'edit' => false,
|
2024-05-15 08:23:46 +00:00
|
|
|
] );
|
|
|
|
$testObject = new SimpleCaptcha();
|
|
|
|
$this->assertFalse(
|
2024-09-13 10:51:48 +00:00
|
|
|
$testObject->triggersCaptcha( 'edit' ), 'CAPTCHA is not triggered by edit action in this configuration'
|
2024-05-15 08:23:46 +00:00
|
|
|
);
|
2024-09-13 10:51:48 +00:00
|
|
|
$testObject->setForceShowCaptcha( true );
|
|
|
|
$this->assertTrue( $testObject->triggersCaptcha( 'edit' ), 'Force showing a CAPTCHA if flag is set' );
|
2024-05-15 08:23:46 +00:00
|
|
|
}
|
2017-08-14 15:39:20 +00:00
|
|
|
}
|