| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\DerivativeContext; |
| 4 |
use MediaWiki\Context\RequestContext; |
| 5 |
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\HTMLFancyCaptchaField; |
| 6 |
use MediaWiki\HTMLForm\HTMLForm; |
| 7 |
use MediaWiki\Request\FauxRequest; |
| 8 |
use MediaWiki\Request\WebRequest; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class HTMLFancyCaptchaFieldTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
public function setUp(): void { |
| 18 |
parent::setUp(); |
| 19 |
|
| 20 |
$this->mergeMwGlobalArrayValue( |
| 21 |
'wgAutoloadClasses', |
| 22 |
[ |
| 23 |
'MediaWiki\\Extension\\ConfirmEdit\\FancyCaptcha\\HTMLFancyCaptchaField' |
| 24 |
=> __DIR__ . '/../../FancyCaptcha/includes/HTMLFancyCaptchaField.php' |
| 25 |
] |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
public function testGetHTML() { |
| 30 |
$html = $this->getForm( [ 'imageUrl' => 'https://example.com/' ] )->getHTML( false ); |
| 31 |
$this->assertMatchesRegularExpression( '/"fancycaptcha-image"/', $html ); |
| 32 |
$this->assertMatchesRegularExpression( '#src="https://example.com/"#', $html ); |
| 33 |
$this->assertDoesNotMatchRegularExpression( '/"mw-createacct-captcha-assisted"/', $html ); |
| 34 |
|
| 35 |
$html = $this->getForm( [ 'imageUrl' => '', 'showCreateHelp' => true ] )->getHTML( false ); |
| 36 |
$this->assertMatchesRegularExpression( '/"mw-createacct-captcha-assisted"/', $html ); |
| 37 |
|
| 38 |
$html = $this->getForm( [ 'imageUrl' => '', 'label' => 'FooBarBaz' ] )->getHTML( false ); |
| 39 |
$this->assertMatchesRegularExpression( '/FooBarBaz/', $html ); |
| 40 |
} |
| 41 |
|
| 42 |
public function testValue() { |
| 43 |
$mockClosure = $this->getMockBuilder( stdClass::class ) |
| 44 |
->addMethods( [ '__invoke' ] )->getMock(); |
| 45 |
$request = new FauxRequest( [ 'wpcaptchaWord' => 'abc' ], true ); |
| 46 |
$form = $this->getForm( [ 'imageUrl' => 'https://example.com/' ], $request ); |
| 47 |
$form->setSubmitCallback( $mockClosure ); |
| 48 |
|
| 49 |
$mockClosure->expects( $this->once() )->method( '__invoke' ) |
| 50 |
->with( [ 'captchaWord' => 'abc' ] )->willReturn( true ); |
| 51 |
$form->trySubmit(); |
| 52 |
} |
| 53 |
|
| 54 |
protected function getForm( $params = [], ?WebRequest $request = null ) { |
| 55 |
if ( $request ) { |
| 56 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 57 |
$context->setRequest( $request ); |
| 58 |
} else { |
| 59 |
$context = RequestContext::getMain(); |
| 60 |
} |
| 61 |
$params['class'] = HTMLFancyCaptchaField::class; |
| 62 |
$form = new HTMLForm( [ 'captchaWord' => $params ], $context ); |
| 63 |
$form->setTitle( Title::newFromText( 'Foo' ) ); |
| 64 |
$form->prepareForm(); |
| 65 |
return $form; |
| 66 |
} |
| 67 |
} |