tests: Pass context to HTMLForm constructor

Change-Id: I8777b2b2d6a87695f6517f259f31a177356ac8f5
This commit is contained in:
Bartosz Dziewoński 2022-05-15 23:35:03 +02:00
parent 58d604af5b
commit 5880031d1c
2 changed files with 13 additions and 11 deletions

View file

@ -30,13 +30,14 @@ class HTMLFancyCaptchaFieldTest extends PHPUnit\Framework\TestCase {
}
protected function getForm( $params = [], WebRequest $request = null ) {
$params['class'] = HTMLFancyCaptchaField::class;
$form = new HTMLForm( [ 'captchaWord' => $params ] );
if ( $request ) {
$context = new DerivativeContext( RequestContext::getMain() );
$context->setRequest( $request );
$form->setContext( $context );
} else {
$context = RequestContext::getMain();
}
$params['class'] = HTMLFancyCaptchaField::class;
$form = new HTMLForm( [ 'captchaWord' => $params ], $context );
$form->setTitle( Title::newFromText( 'Foo' ) );
$form->prepareForm();
return $form;

View file

@ -5,25 +5,26 @@
*/
class HTMLReCaptchaNoCaptchaFieldTest extends PHPUnit\Framework\TestCase {
public function testSubmit() {
$request = new FauxRequest( [
'foo' => 'abc',
'g-recaptcha-response' => 'def',
], true );
$context = new DerivativeContext( RequestContext::getMain() );
$context->setRequest( $request );
$form = new HTMLForm( [
'foo' => [
'class' => HTMLReCaptchaNoCaptchaField::class,
'key' => '123',
],
] );
$request = new FauxRequest( [
'foo' => 'abc',
'g-recaptcha-response' => 'def',
], true );
], $context );
$mockClosure = $this->getMockBuilder( stdClass::class )
->addMethods( [ '__invoke' ] )->getMock();
$mockClosure->expects( $this->once() )->method( '__invoke' )
->with( [ 'foo' => 'def' ] )->willReturn( true );
$context = new DerivativeContext( RequestContext::getMain() );
$context->setRequest( $request );
$form->setTitle( Title::newFromText( 'Title' ) );
$form->setContext( $context );
$form->setSubmitCallback( $mockClosure );
$form->prepareForm();
$form->trySubmit();