HTMLAutoCompleteSelectFieldTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Unit tests for HTMLAutoCompleteSelectField
  4. *
  5. * @covers HTMLAutoCompleteSelectField
  6. */
  7. class HTMLAutoCompleteSelectFieldTest extends MediaWikiTestCase {
  8. public $options = [
  9. 'Bulgaria' => 'BGR',
  10. 'Burkina Faso' => 'BFA',
  11. 'Burundi' => 'BDI',
  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. * Verify that the autocomplete options are correctly encoded as
  26. * the 'data-autocomplete' attribute of the field.
  27. *
  28. * @covers HTMLAutoCompleteSelectField::getAttributes
  29. */
  30. function testGetAttributes() {
  31. $field = new HTMLAutoCompleteSelectField( [
  32. 'fieldname' => 'Test',
  33. 'autocomplete' => $this->options,
  34. ] );
  35. $attributes = $field->getAttributes( [] );
  36. $this->assertEquals( array_keys( $this->options ),
  37. FormatJson::decode( $attributes['data-autocomplete'] ),
  38. "The 'data-autocomplete' attribute encodes autocomplete option keys as a JSON array."
  39. );
  40. }
  41. /**
  42. * Test that the optional select dropdown is included or excluded based on
  43. * the presence or absence of the 'options' parameter.
  44. */
  45. function testOptionalSelectElement() {
  46. $params = [
  47. 'fieldname' => 'Test',
  48. 'autocomplete-data' => $this->options,
  49. 'options' => $this->options,
  50. ];
  51. $field = new HTMLAutoCompleteSelectField( $params );
  52. $html = $field->getInputHTML( false );
  53. $this->assertRegExp( '/select/', $html,
  54. "When the 'options' parameter is set, the HTML includes a <select>" );
  55. unset( $params['options'] );
  56. $field = new HTMLAutoCompleteSelectField( $params );
  57. $html = $field->getInputHTML( false );
  58. $this->assertNotRegExp( '/select/', $html,
  59. "When the 'options' parameter is not set, the HTML does not include a <select>" );
  60. }
  61. }