6 class XmlSelectTest
extends MediaWikiTestCase
{
13 protected function setUp() {
15 $this->setMwGlobals( array(
16 'wgWellFormedXml' => true,
18 $this->select
= new XmlSelect();
21 protected function tearDown() {
27 * @covers XmlSelect::__construct
29 public function testConstructWithoutParameters() {
30 $this->assertEquals( '<select></select>', $this->select
->getHTML() );
34 * Parameters are $name (false), $id (false), $default (false)
35 * @dataProvider provideConstructionParameters
36 * @covers XmlSelect::__construct
38 public function testConstructParameters( $name, $id, $default, $expected ) {
39 $this->select
= new XmlSelect( $name, $id, $default );
40 $this->assertEquals( $expected, $this->select
->getHTML() );
44 * Provide parameters for testConstructParameters() which use three
46 * - $name (default: false)
47 * - $id (default: false)
48 * - $default (default: false)
49 * Provides a fourth parameters representing the expected HTML output
51 public static function provideConstructionParameters() {
54 * Values are set following a 3-bit Gray code where two successive
55 * values differ by only one value.
56 * See http://en.wikipedia.org/wiki/Gray_code
59 array( false, false, false, '<select></select>' ),
60 array( false, false, 'foo', '<select></select>' ),
61 array( false, 'id', 'foo', '<select id="id"></select>' ),
62 array( false, 'id', false, '<select id="id"></select>' ),
63 array( 'name', 'id', false, '<select name="name" id="id"></select>' ),
64 array( 'name', 'id', 'foo', '<select name="name" id="id"></select>' ),
65 array( 'name', false, 'foo', '<select name="name"></select>' ),
66 array( 'name', false, false, '<select name="name"></select>' ),
71 * @covers XmlSelect::addOption
73 public function testAddOption() {
74 $this->select
->addOption( 'foo' );
76 '<select><option value="foo">foo</option></select>',
77 $this->select
->getHTML()
82 * @covers XmlSelect::addOption
84 public function testAddOptionWithDefault() {
85 $this->select
->addOption( 'foo', true );
87 '<select><option value="1">foo</option></select>',
88 $this->select
->getHTML()
93 * @covers XmlSelect::addOption
95 public function testAddOptionWithFalse() {
96 $this->select
->addOption( 'foo', false );
98 '<select><option value="foo">foo</option></select>',
99 $this->select
->getHTML()
104 * @covers XmlSelect::addOption
106 public function testAddOptionWithValueZero() {
107 $this->select
->addOption( 'foo', 0 );
109 '<select><option value="0">foo</option></select>',
110 $this->select
->getHTML()
115 * @covers XmlSelect::setDefault
117 public function testSetDefault() {
118 $this->select
->setDefault( 'bar1' );
119 $this->select
->addOption( 'foo1' );
120 $this->select
->addOption( 'bar1' );
121 $this->select
->addOption( 'foo2' );
123 '<select><option value="foo1">foo1</option>' . "\n" .
124 '<option value="bar1" selected="">bar1</option>' . "\n" .
125 '<option value="foo2">foo2</option></select>', $this->select
->getHTML() );
129 * Adding default later on should set the correct selection or
130 * raise an exception.
131 * To handle this, we need to render the options in getHtml()
132 * @covers XmlSelect::setDefault
134 public function testSetDefaultAfterAddingOptions() {
135 $this->select
->addOption( 'foo1' );
136 $this->select
->addOption( 'bar1' );
137 $this->select
->addOption( 'foo2' );
138 $this->select
->setDefault( 'bar1' ); # setting default after adding options
140 '<select><option value="foo1">foo1</option>' . "\n" .
141 '<option value="bar1" selected="">bar1</option>' . "\n" .
142 '<option value="foo2">foo2</option></select>', $this->select
->getHTML() );
146 * @covers XmlSelect::setAttribute
147 * @covers XmlSelect::getAttribute
149 public function testGetAttributes() {
150 # create some attributes
151 $this->select
->setAttribute( 'dummy', 0x777 );
152 $this->select
->setAttribute( 'string', 'euro €' );
153 $this->select
->setAttribute( 1911, 'razor' );
155 # verify we can retrieve them
157 $this->select
->getAttribute( 'dummy' ),
161 $this->select
->getAttribute( 'string' ),
165 $this->select
->getAttribute( 1911 ),
169 # inexistent keys should give us 'null'
171 $this->select
->getAttribute( 'I DO NOT EXIT' ),
175 # verify string / integer
177 $this->select
->getAttribute( '1911' ),
181 $this->select
->getAttribute( 'dummy' ),