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 ) { protected function getForm( $params = [], WebRequest $request = null ) {
$params['class'] = HTMLFancyCaptchaField::class;
$form = new HTMLForm( [ 'captchaWord' => $params ] );
if ( $request ) { if ( $request ) {
$context = new DerivativeContext( RequestContext::getMain() ); $context = new DerivativeContext( RequestContext::getMain() );
$context->setRequest( $request ); $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->setTitle( Title::newFromText( 'Foo' ) );
$form->prepareForm(); $form->prepareForm();
return $form; return $form;

View file

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