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