2014-08-26 19:31:26 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @covers HTMLAutoCompleteSelectField
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class HTMLAutoCompleteSelectFieldTest extends MediaWikiIntegrationTestCase {
|
2014-08-26 19:31:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
public $options = [
|
2014-08-26 19:31:26 +00:00
|
|
|
'Bulgaria' => 'BGR',
|
|
|
|
|
'Burkina Faso' => 'BFA',
|
|
|
|
|
'Burundi' => 'BDI',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-26 19:31:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify that attempting to instantiate an HTMLAutoCompleteSelectField
|
|
|
|
|
* without providing any autocomplete options causes an exception to be
|
|
|
|
|
* thrown.
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function testMissingAutocompletions() {
|
2023-01-22 00:55:59 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectExceptionMessage( "called without any autocompletions" );
|
2023-01-07 07:48:03 +00:00
|
|
|
|
|
|
|
|
$htmlForm = $this->createMock( HTMLForm::class );
|
|
|
|
|
new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test', 'parent' => $htmlForm ] );
|
2014-08-26 19:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that the optional select dropdown is included or excluded based on
|
|
|
|
|
* the presence or absence of the 'options' parameter.
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function testOptionalSelectElement() {
|
2023-01-07 07:48:03 +00:00
|
|
|
$htmlForm = $this->createMock( HTMLForm::class );
|
2023-01-10 20:33:47 +00:00
|
|
|
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' );
|
2023-01-07 07:48:03 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2016-11-17 18:39:23 +00:00
|
|
|
'fieldname' => 'Test',
|
|
|
|
|
'autocomplete-data' => $this->options,
|
|
|
|
|
'options' => $this->options,
|
2023-01-07 07:48:03 +00:00
|
|
|
'parent' => $htmlForm
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-26 19:31:26 +00:00
|
|
|
|
|
|
|
|
$field = new HTMLAutoCompleteSelectField( $params );
|
|
|
|
|
$html = $field->getInputHTML( false );
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertMatchesRegularExpression( '/select/', $html,
|
2014-08-26 19:31:26 +00:00
|
|
|
"When the 'options' parameter is set, the HTML includes a <select>" );
|
|
|
|
|
|
|
|
|
|
unset( $params['options'] );
|
|
|
|
|
$field = new HTMLAutoCompleteSelectField( $params );
|
|
|
|
|
$html = $field->getInputHTML( false );
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertDoesNotMatchRegularExpression( '/select/', $html,
|
2014-08-26 19:31:26 +00:00
|
|
|
"When the 'options' parameter is not set, the HTML does not include a <select>" );
|
|
|
|
|
}
|
|
|
|
|
}
|