2016-04-25 20:58:18 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-08 16:53:12 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\HTMLFancyCaptchaField;
|
2023-05-19 10:23:59 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2023-12-10 23:07:55 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-19 04:14:21 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-04-08 16:53:12 +00:00
|
|
|
|
2018-01-23 23:59:08 +00:00
|
|
|
/**
|
2022-04-08 16:53:12 +00:00
|
|
|
* @covers \MediaWiki\Extension\ConfirmEdit\FancyCaptcha\HTMLFancyCaptchaField
|
2023-08-04 00:10:41 +00:00
|
|
|
* @group Database
|
2018-01-23 23:59:08 +00:00
|
|
|
*/
|
2022-10-07 12:18:40 +00:00
|
|
|
class HTMLFancyCaptchaFieldTest extends MediaWikiIntegrationTestCase {
|
2024-01-17 04:46:58 +00:00
|
|
|
|
|
|
|
public function setUp(): void {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->mergeMwGlobalArrayValue(
|
|
|
|
'wgAutoloadClasses',
|
|
|
|
[
|
|
|
|
'MediaWiki\\Extension\\ConfirmEdit\\FancyCaptcha\\HTMLFancyCaptchaField'
|
|
|
|
=> __DIR__ . '/../../FancyCaptcha/includes/HTMLFancyCaptchaField.php'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
public function testGetHTML() {
|
|
|
|
$html = $this->getForm( [ 'imageUrl' => 'https://example.com/' ] )->getHTML( false );
|
2022-10-07 12:18:40 +00:00
|
|
|
$this->assertMatchesRegularExpression( '/"fancycaptcha-image"/', $html );
|
|
|
|
$this->assertMatchesRegularExpression( '#src="https://example.com/"#', $html );
|
2022-10-07 14:51:53 +00:00
|
|
|
$this->assertDoesNotMatchRegularExpression( '/"mw-createacct-captcha-assisted"/', $html );
|
2016-04-25 20:58:18 +00:00
|
|
|
|
|
|
|
$html = $this->getForm( [ 'imageUrl' => '', 'showCreateHelp' => true ] )->getHTML( false );
|
2022-10-07 12:18:40 +00:00
|
|
|
$this->assertMatchesRegularExpression( '/"mw-createacct-captcha-assisted"/', $html );
|
2016-04-25 20:58:18 +00:00
|
|
|
|
|
|
|
$html = $this->getForm( [ 'imageUrl' => '', 'label' => 'FooBarBaz' ] )->getHTML( false );
|
2022-10-07 12:18:40 +00:00
|
|
|
$this->assertMatchesRegularExpression( '/FooBarBaz/', $html );
|
2016-04-25 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValue() {
|
2018-10-09 12:09:10 +00:00
|
|
|
$mockClosure = $this->getMockBuilder( stdClass::class )
|
2021-05-03 07:10:28 +00:00
|
|
|
->addMethods( [ '__invoke' ] )->getMock();
|
2016-04-25 20:58:18 +00:00
|
|
|
$request = new FauxRequest( [ 'wpcaptchaWord' => 'abc' ], true );
|
|
|
|
$form = $this->getForm( [ 'imageUrl' => 'https://example.com/' ], $request );
|
|
|
|
$form->setSubmitCallback( $mockClosure );
|
|
|
|
|
|
|
|
$mockClosure->expects( $this->once() )->method( '__invoke' )
|
|
|
|
->with( [ 'captchaWord' => 'abc' ] )->willReturn( true );
|
|
|
|
$form->trySubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getForm( $params = [], WebRequest $request = null ) {
|
|
|
|
if ( $request ) {
|
|
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
|
|
$context->setRequest( $request );
|
2022-05-15 21:35:03 +00:00
|
|
|
} else {
|
|
|
|
$context = RequestContext::getMain();
|
2016-04-25 20:58:18 +00:00
|
|
|
}
|
2022-05-15 21:35:03 +00:00
|
|
|
$params['class'] = HTMLFancyCaptchaField::class;
|
|
|
|
$form = new HTMLForm( [ 'captchaWord' => $params ], $context );
|
2016-04-25 20:58:18 +00:00
|
|
|
$form->setTitle( Title::newFromText( 'Foo' ) );
|
|
|
|
$form->prepareForm();
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|