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() {
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectException( MWException::class );
|
|
|
|
|
$this->expectExceptionMessage( "called without any autocompletions" );
|
2016-02-17 09:09:32 +00:00
|
|
|
new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test' ] );
|
2014-08-26 19:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify that the autocomplete options are correctly encoded as
|
|
|
|
|
* the 'data-autocomplete' attribute of the field.
|
|
|
|
|
*
|
|
|
|
|
* @covers HTMLAutoCompleteSelectField::getAttributes
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function testGetAttributes() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$field = new HTMLAutoCompleteSelectField( [
|
2014-08-26 19:31:26 +00:00
|
|
|
'fieldname' => 'Test',
|
|
|
|
|
'autocomplete' => $this->options,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-08-26 19:31:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$attributes = $field->getAttributes( [] );
|
2014-08-26 19:31:26 +00:00
|
|
|
$this->assertEquals( array_keys( $this->options ),
|
|
|
|
|
FormatJson::decode( $attributes['data-autocomplete'] ),
|
|
|
|
|
"The 'data-autocomplete' attribute encodes autocomplete option keys as a JSON array."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
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,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-26 19:31:26 +00:00
|
|
|
|
|
|
|
|
$field = new HTMLAutoCompleteSelectField( $params );
|
|
|
|
|
$html = $field->getInputHTML( false );
|
|
|
|
|
$this->assertRegExp( '/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->assertNotRegExp( '/select/', $html,
|
|
|
|
|
"When the 'options' parameter is not set, the HTML does not include a <select>" );
|
|
|
|
|
}
|
|
|
|
|
}
|