| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\HTMLForm\Field; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\HTMLForm\Field\HTMLAutoCompleteSelectField; |
| 7 |
use MediaWiki\HTMLForm\HTMLForm; |
| 8 |
use MediaWikiIntegrationTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class HTMLAutoCompleteSelectFieldTest extends MediaWikiIntegrationTestCase { |
| 14 |
|
| 15 |
private const OPTIONS = [ |
| 16 |
'Bulgaria' => 'BGR', |
| 17 |
'Burkina Faso' => 'BFA', |
| 18 |
'Burundi' => 'BDI', |
| 19 |
]; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function testMissingAutocompletions() { |
| 27 |
$this->expectException( InvalidArgumentException::class ); |
| 28 |
$this->expectExceptionMessage( "called without any autocompletions" ); |
| 29 |
|
| 30 |
$htmlForm = $this->createMock( HTMLForm::class ); |
| 31 |
new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test', 'parent' => $htmlForm ] ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function testOptionalSelectElement() { |
| 39 |
$htmlForm = $this->createMock( HTMLForm::class ); |
| 40 |
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' ); |
| 41 |
|
| 42 |
$params = [ |
| 43 |
'fieldname' => 'Test', |
| 44 |
'autocomplete-data' => self::OPTIONS, |
| 45 |
'options' => self::OPTIONS, |
| 46 |
'parent' => $htmlForm |
| 47 |
]; |
| 48 |
|
| 49 |
$field = new HTMLAutoCompleteSelectField( $params ); |
| 50 |
$html = $field->getInputHTML( false ); |
| 51 |
$this->assertMatchesRegularExpression( '/select/', $html, |
| 52 |
"When the 'options' parameter is set, the HTML includes a <select>" ); |
| 53 |
|
| 54 |
unset( $params['options'] ); |
| 55 |
$field = new HTMLAutoCompleteSelectField( $params ); |
| 56 |
$html = $field->getInputHTML( false ); |
| 57 |
$this->assertDoesNotMatchRegularExpression( '/select/', $html, |
| 58 |
"When the 'options' parameter is not set, the HTML does not include a <select>" ); |
| 59 |
} |
| 60 |
} |