]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / htmlform / HTMLAutoCompleteSelectFieldTest.php
1 <?php
2 /**
3  * Unit tests for HTMLAutoCompleteSelectField
4  *
5  * @covers HTMLAutoCompleteSelectField
6  */
7 class HtmlAutoCompleteSelectFieldTest extends MediaWikiTestCase {
8
9         public $options = [
10                 'Bulgaria'     => 'BGR',
11                 'Burkina Faso' => 'BFA',
12                 'Burundi'      => 'BDI',
13         ];
14
15         /**
16          * Verify that attempting to instantiate an HTMLAutoCompleteSelectField
17          * without providing any autocomplete options causes an exception to be
18          * thrown.
19          *
20          * @expectedException        MWException
21          * @expectedExceptionMessage called without any autocompletions
22          */
23         function testMissingAutocompletions() {
24                 new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test' ] );
25         }
26
27         /**
28          * Verify that the autocomplete options are correctly encoded as
29          * the 'data-autocomplete' attribute of the field.
30          *
31          * @covers HTMLAutoCompleteSelectField::getAttributes
32          */
33         function testGetAttributes() {
34                 $field = new HTMLAutoCompleteSelectField( [
35                         'fieldname'    => 'Test',
36                         'autocomplete' => $this->options,
37                 ] );
38
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."
43                 );
44         }
45
46         /**
47          * Test that the optional select dropdown is included or excluded based on
48          * the presence or absence of the 'options' parameter.
49          */
50         function testOptionalSelectElement() {
51                 $params = [
52                         'fieldname'         => 'Test',
53                         'autocomplete-data' => $this->options,
54                         'options'           => $this->options,
55                 ];
56
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>" );
61
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>" );
67         }
68 }