MediaWiki  1.34.0
HTMLFancyCaptchaFieldTest.php
Go to the documentation of this file.
1 <?php
2 
6 class HTMLFancyCaptchaFieldTest extends PHPUnit\Framework\TestCase {
7  public function testGetHTML() {
8  $html = $this->getForm( [ 'imageUrl' => 'https://example.com/' ] )->getHTML( false );
9  $this->assertRegExp( '/"fancycaptcha-image"/', $html );
10  $this->assertRegExp( '#src="https://example.com/"#', $html );
11  $this->assertNotRegExp( '/"mw-createacct-captcha-assisted"/', $html );
12 
13  $html = $this->getForm( [ 'imageUrl' => '', 'showCreateHelp' => true ] )->getHTML( false );
14  $this->assertRegExp( '/"mw-createacct-captcha-assisted"/', $html );
15 
16  $html = $this->getForm( [ 'imageUrl' => '', 'label' => 'FooBarBaz' ] )->getHTML( false );
17  $this->assertRegExp( '/FooBarBaz/', $html );
18  }
19 
20  public function testValue() {
21  $mockClosure = $this->getMockBuilder( stdClass::class )
22  ->setMethods( [ '__invoke' ] )->getMock();
23  $request = new FauxRequest( [ 'wpcaptchaWord' => 'abc' ], true );
24  $form = $this->getForm( [ 'imageUrl' => 'https://example.com/' ], $request );
25  $form->setSubmitCallback( $mockClosure );
26 
27  $mockClosure->expects( $this->once() )->method( '__invoke' )
28  ->with( [ 'captchaWord' => 'abc' ] )->willReturn( true );
29  $form->trySubmit();
30  }
31 
32  protected function getForm( $params = [], WebRequest $request = null ) {
33  $params['class'] = HTMLFancyCaptchaField::class;
34  $form = new HTMLForm( [ 'captchaWord' => $params ] );
35  if ( $request ) {
37  $context->setRequest( $request );
38  $form->setContext( $context );
39  }
40  $form->setTitle( Title::newFromText( 'Foo' ) );
41  $form->prepareForm();
42  return $form;
43  }
44 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
HTMLFancyCaptchaFieldTest\testValue
testValue()
Definition: HTMLFancyCaptchaFieldTest.php:20
HTMLFancyCaptchaFieldTest\getForm
getForm( $params=[], WebRequest $request=null)
Definition: HTMLFancyCaptchaFieldTest.php:32
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
HTMLFancyCaptchaFieldTest
@covers HTMLFancyCaptchaField
Definition: HTMLFancyCaptchaFieldTest.php:6
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
$context
$context
Definition: load.php:45
HTMLFancyCaptchaFieldTest\testGetHTML
testGetHTML()
Definition: HTMLFancyCaptchaFieldTest.php:7
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131