Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / htmlform / HTMLAutoCompleteSelectFieldTest.php
blob33e3a257d245c264506e604354830e77dcda8a49
1 <?php
2 /**
3 * Unit tests for HTMLAutoCompleteSelectField
5 * @covers HTMLAutoCompleteSelectField
6 */
7 class HtmlAutoCompleteSelectFieldTest extends MediaWikiTestCase {
9 public $options = [
10 'Bulgaria' => 'BGR',
11 'Burkina Faso' => 'BFA',
12 'Burundi' => 'BDI',
15 /**
16 * Verify that attempting to instantiate an HTMLAutoCompleteSelectField
17 * without providing any autocomplete options causes an exception to be
18 * thrown.
20 * @expectedException MWException
21 * @expectedExceptionMessage called without any autocompletions
23 function testMissingAutocompletions() {
24 new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test' ] );
27 /**
28 * Verify that the autocomplete options are correctly encoded as
29 * the 'data-autocomplete' attribute of the field.
31 * @covers HTMLAutoCompleteSelectField::getAttributes
33 function testGetAttributes() {
34 $field = new HTMLAutoCompleteSelectField( [
35 'fieldname' => 'Test',
36 'autocomplete' => $this->options,
37 ] );
39 $attributes = $field->getAttributes( [] );
40 $this->assertEquals( array_keys( $this->options ),
41 FormatJson::decode( $attributes['data-autocomplete'] ),
42 "The 'data-autocomplete' attribute encodes autocomplete option keys as a JSON array."
46 /**
47 * Test that the optional select dropdown is included or excluded based on
48 * the presence or absence of the 'options' parameter.
50 function testOptionalSelectElement() {
51 $params = [
52 'fieldname' => 'Test',
53 'autocomplete-data' => $this->options,
54 'options' => $this->options,
57 $field = new HTMLAutoCompleteSelectField( $params );
58 $html = $field->getInputHTML( false );
59 $this->assertRegExp( '/select/', $html,
60 "When the 'options' parameter is set, the HTML includes a <select>" );
62 unset( $params['options'] );
63 $field = new HTMLAutoCompleteSelectField( $params );
64 $html = $field->getInputHTML( false );
65 $this->assertNotRegExp( '/select/', $html,
66 "When the 'options' parameter is not set, the HTML does not include a <select>" );