Merge "(bug 44943) Cleanup of API:Account creation documentation."
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlSelectTest.php
1 <?php
2
3 // TODO
4 class XmlSelectTest extends MediaWikiTestCase {
5 protected $select;
6
7 protected function setUp() {
8 parent::setUp();
9 $this->setMwGlobals( array(
10 'wgHtml5' => true,
11 'wgWellFormedXml' => true,
12 ) );
13 $this->select = new XmlSelect();
14 }
15
16 protected function tearDown() {
17 parent::tearDown();
18 $this->select = null;
19 }
20
21 ### START OF TESTS ###
22
23 public function testConstructWithoutParameters() {
24 $this->assertEquals( '<select></select>', $this->select->getHTML() );
25 }
26
27 /**
28 * Parameters are $name (false), $id (false), $default (false)
29 * @dataProvider provideConstructionParameters
30 */
31 public function testConstructParameters( $name, $id, $default, $expected ) {
32 $this->select = new XmlSelect( $name, $id, $default );
33 $this->assertEquals( $expected, $this->select->getHTML() );
34 }
35
36 /**
37 * Provide parameters for testConstructParameters() which use three
38 * parameters:
39 * - $name (default: false)
40 * - $id (default: false)
41 * - $default (default: false)
42 * Provides a fourth parameters representing the expected HTML output
43 *
44 */
45 public static function provideConstructionParameters() {
46 return array(
47 /**
48 * Values are set following a 3-bit Gray code where two successive
49 * values differ by only one value.
50 * See http://en.wikipedia.org/wiki/Gray_code
51 */
52 # $name $id $default
53 array( false, false, false, '<select></select>' ),
54 array( false, false, 'foo', '<select></select>' ),
55 array( false, 'id', 'foo', '<select id="id"></select>' ),
56 array( false, 'id', false, '<select id="id"></select>' ),
57 array( 'name', 'id', false, '<select name="name" id="id"></select>' ),
58 array( 'name', 'id', 'foo', '<select name="name" id="id"></select>' ),
59 array( 'name', false, 'foo', '<select name="name"></select>' ),
60 array( 'name', false, false, '<select name="name"></select>' ),
61 );
62 }
63
64 # Begin XmlSelect::addOption() similar to Xml::option
65 public function testAddOption() {
66 $this->select->addOption( 'foo' );
67 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
68 }
69
70 public function testAddOptionWithDefault() {
71 $this->select->addOption( 'foo', true );
72 $this->assertEquals( '<select><option value="1">foo</option></select>', $this->select->getHTML() );
73 }
74
75 public function testAddOptionWithFalse() {
76 $this->select->addOption( 'foo', false );
77 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
78 }
79
80 public function testAddOptionWithValueZero() {
81 $this->select->addOption( 'foo', 0 );
82 $this->assertEquals( '<select><option value="0">foo</option></select>', $this->select->getHTML() );
83 }
84
85 # End XmlSelect::addOption() similar to Xml::option
86
87 public function testSetDefault() {
88 $this->select->setDefault( 'bar1' );
89 $this->select->addOption( 'foo1' );
90 $this->select->addOption( 'bar1' );
91 $this->select->addOption( 'foo2' );
92 $this->assertEquals(
93 '<select><option value="foo1">foo1</option>' . "\n" .
94 '<option value="bar1" selected="">bar1</option>' . "\n" .
95 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
96 }
97
98 /**
99 * Adding default later on should set the correct selection or
100 * raise an exception.
101 * To handle this, we need to render the options in getHtml()
102 */
103 public function testSetDefaultAfterAddingOptions() {
104 $this->select->addOption( 'foo1' );
105 $this->select->addOption( 'bar1' );
106 $this->select->addOption( 'foo2' );
107 $this->select->setDefault( 'bar1' ); # setting default after adding options
108 $this->assertEquals(
109 '<select><option value="foo1">foo1</option>' . "\n" .
110 '<option value="bar1" selected="">bar1</option>' . "\n" .
111 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
112 }
113
114 public function testGetAttributes() {
115 # create some attributes
116 $this->select->setAttribute( 'dummy', 0x777 );
117 $this->select->setAttribute( 'string', 'euro €' );
118 $this->select->setAttribute( 1911, 'razor' );
119
120 # verify we can retrieve them
121 $this->assertEquals(
122 $this->select->getAttribute( 'dummy' ),
123 0x777
124 );
125 $this->assertEquals(
126 $this->select->getAttribute( 'string' ),
127 'euro €'
128 );
129 $this->assertEquals(
130 $this->select->getAttribute( 1911 ),
131 'razor'
132 );
133
134 # inexistant keys should give us 'null'
135 $this->assertEquals(
136 $this->select->getAttribute( 'I DO NOT EXIT' ),
137 null
138 );
139
140 # verify string / integer
141 $this->assertEquals(
142 $this->select->getAttribute( '1911' ),
143 'razor'
144 );
145 $this->assertEquals(
146 $this->select->getAttribute( 'dummy' ),
147 0x777
148 );
149 }
150 }