wiki.techinc.nl/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php
Brian Wolff 07cdef809c Deprecate creating HTMLFormFields without reference to parent form
Currently it is documented that mParent in HTMLFormField may be null.
This can happen if the form element is constructed manually via
new, instead of the normal way via HTMLForm methods.

As it stands, much of the code assumes that mParent is always set
despite the documentation. Lets mark creating form fields without
parent set as deprecated. The current situation seems like a
recipe for bugs, and after the deprecation period this would allow
us to simplify some of the HTMLFormField code.

Bug: T326456
Change-Id: Ica0740049f0a3e8ec764903c5b71825e4d628a3f
Depends-On: I15a39605e3eec8a5c265c4a331039fa906eda036
2023-01-08 18:20:16 -08:00

55 lines
1.8 KiB
PHP

<?php
/**
* @covers HTMLAutoCompleteSelectField
*/
class HTMLAutoCompleteSelectFieldTest extends MediaWikiIntegrationTestCase {
public $options = [
'Bulgaria' => 'BGR',
'Burkina Faso' => 'BFA',
'Burundi' => 'BDI',
];
/**
* Verify that attempting to instantiate an HTMLAutoCompleteSelectField
* without providing any autocomplete options causes an exception to be
* thrown.
*/
public function testMissingAutocompletions() {
$this->expectException( MWException::class );
$this->expectExceptionMessage( "called without any autocompletions" );
$htmlForm = $this->createMock( HTMLForm::class );
new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test', 'parent' => $htmlForm ] );
}
/**
* Test that the optional select dropdown is included or excluded based on
* the presence or absence of the 'options' parameter.
*/
public function testOptionalSelectElement() {
$htmlForm = $this->createMock( HTMLForm::class );
$htmlForm->method( 'msg' )
->willReturnCallback( static function ( ...$args ) {
return call_user_func_array( 'wfMessage', $args );
} );
$params = [
'fieldname' => 'Test',
'autocomplete-data' => $this->options,
'options' => $this->options,
'parent' => $htmlForm
];
$field = new HTMLAutoCompleteSelectField( $params );
$html = $field->getInputHTML( false );
$this->assertMatchesRegularExpression( '/select/', $html,
"When the 'options' parameter is set, the HTML includes a <select>" );
unset( $params['options'] );
$field = new HTMLAutoCompleteSelectField( $params );
$html = $field->getInputHTML( false );
$this->assertDoesNotMatchRegularExpression( '/select/', $html,
"When the 'options' parameter is not set, the HTML does not include a <select>" );
}
}