4 class XmlSelectTest
extends MediaWikiTestCase
{
7 protected function setUp() {
9 $this->setMwGlobals( array(
10 'wgWellFormedXml' => true,
12 $this->select
= new XmlSelect();
15 protected function tearDown() {
20 ### START OF TESTS ###
22 public function testConstructWithoutParameters() {
23 $this->assertEquals( '<select></select>', $this->select
->getHTML() );
27 * Parameters are $name (false), $id (false), $default (false)
28 * @dataProvider provideConstructionParameters
30 public function testConstructParameters( $name, $id, $default, $expected ) {
31 $this->select
= new XmlSelect( $name, $id, $default );
32 $this->assertEquals( $expected, $this->select
->getHTML() );
36 * Provide parameters for testConstructParameters() which use three
38 * - $name (default: false)
39 * - $id (default: false)
40 * - $default (default: false)
41 * Provides a fourth parameters representing the expected HTML output
44 public static function provideConstructionParameters() {
47 * Values are set following a 3-bit Gray code where two successive
48 * values differ by only one value.
49 * See http://en.wikipedia.org/wiki/Gray_code
52 array( false, false, false, '<select></select>' ),
53 array( false, false, 'foo', '<select></select>' ),
54 array( false, 'id', 'foo', '<select id="id"></select>' ),
55 array( false, 'id', false, '<select id="id"></select>' ),
56 array( 'name', 'id', false, '<select name="name" id="id"></select>' ),
57 array( 'name', 'id', 'foo', '<select name="name" id="id"></select>' ),
58 array( 'name', false, 'foo', '<select name="name"></select>' ),
59 array( 'name', false, false, '<select name="name"></select>' ),
63 # Begin XmlSelect::addOption() similar to Xml::option
64 public function testAddOption() {
65 $this->select
->addOption( 'foo' );
66 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select
->getHTML() );
69 public function testAddOptionWithDefault() {
70 $this->select
->addOption( 'foo', true );
71 $this->assertEquals( '<select><option value="1">foo</option></select>', $this->select
->getHTML() );
74 public function testAddOptionWithFalse() {
75 $this->select
->addOption( 'foo', false );
76 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select
->getHTML() );
79 public function testAddOptionWithValueZero() {
80 $this->select
->addOption( 'foo', 0 );
81 $this->assertEquals( '<select><option value="0">foo</option></select>', $this->select
->getHTML() );
84 # End XmlSelect::addOption() similar to Xml::option
86 public function testSetDefault() {
87 $this->select
->setDefault( 'bar1' );
88 $this->select
->addOption( 'foo1' );
89 $this->select
->addOption( 'bar1' );
90 $this->select
->addOption( 'foo2' );
92 '<select><option value="foo1">foo1</option>' . "\n" .
93 '<option value="bar1" selected="">bar1</option>' . "\n" .
94 '<option value="foo2">foo2</option></select>', $this->select
->getHTML() );
98 * Adding default later on should set the correct selection or
100 * To handle this, we need to render the options in getHtml()
102 public function testSetDefaultAfterAddingOptions() {
103 $this->select
->addOption( 'foo1' );
104 $this->select
->addOption( 'bar1' );
105 $this->select
->addOption( 'foo2' );
106 $this->select
->setDefault( 'bar1' ); # setting default after adding options
108 '<select><option value="foo1">foo1</option>' . "\n" .
109 '<option value="bar1" selected="">bar1</option>' . "\n" .
110 '<option value="foo2">foo2</option></select>', $this->select
->getHTML() );
113 public function testGetAttributes() {
114 # create some attributes
115 $this->select
->setAttribute( 'dummy', 0x777 );
116 $this->select
->setAttribute( 'string', 'euro €' );
117 $this->select
->setAttribute( 1911, 'razor' );
119 # verify we can retrieve them
121 $this->select
->getAttribute( 'dummy' ),
125 $this->select
->getAttribute( 'string' ),
129 $this->select
->getAttribute( 1911 ),
133 # inexistant keys should give us 'null'
135 $this->select
->getAttribute( 'I DO NOT EXIT' ),
139 # verify string / integer
141 $this->select
->getAttribute( '1911' ),
145 $this->select
->getAttribute( 'dummy' ),