Merge "Simplify watchlist edit mode handling"
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlSelectTest.php
1 <?php
2
3 /**
4 * @group Xml
5 */
6 class XmlSelectTest extends MediaWikiTestCase {
7
8 /**
9 * @var XmlSelect
10 */
11 protected $select;
12
13 protected function setUp() {
14 parent::setUp();
15 $this->setMwGlobals( array(
16 'wgWellFormedXml' => true,
17 ) );
18 $this->select = new XmlSelect();
19 }
20
21 protected function tearDown() {
22 parent::tearDown();
23 $this->select = null;
24 }
25
26 /**
27 * @covers XmlSelect::__construct
28 */
29 public function testConstructWithoutParameters() {
30 $this->assertEquals( '<select></select>', $this->select->getHTML() );
31 }
32
33 /**
34 * Parameters are $name (false), $id (false), $default (false)
35 * @dataProvider provideConstructionParameters
36 * @covers XmlSelect::__construct
37 */
38 public function testConstructParameters( $name, $id, $default, $expected ) {
39 $this->select = new XmlSelect( $name, $id, $default );
40 $this->assertEquals( $expected, $this->select->getHTML() );
41 }
42
43 /**
44 * Provide parameters for testConstructParameters() which use three
45 * parameters:
46 * - $name (default: false)
47 * - $id (default: false)
48 * - $default (default: false)
49 * Provides a fourth parameters representing the expected HTML output
50 */
51 public static function provideConstructionParameters() {
52 return array(
53 /**
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
57 */
58 # $name $id $default
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>' ),
67 );
68 }
69
70 /**
71 * @covers XmlSelect::addOption
72 */
73 public function testAddOption() {
74 $this->select->addOption( 'foo' );
75 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
76 }
77
78 /**
79 * @covers XmlSelect::addOption
80 */
81 public function testAddOptionWithDefault() {
82 $this->select->addOption( 'foo', true );
83 $this->assertEquals( '<select><option value="1">foo</option></select>', $this->select->getHTML() );
84 }
85
86 /**
87 * @covers XmlSelect::addOption
88 */
89 public function testAddOptionWithFalse() {
90 $this->select->addOption( 'foo', false );
91 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
92 }
93
94 /**
95 * @covers XmlSelect::addOption
96 */
97 public function testAddOptionWithValueZero() {
98 $this->select->addOption( 'foo', 0 );
99 $this->assertEquals( '<select><option value="0">foo</option></select>', $this->select->getHTML() );
100 }
101
102 /**
103 * @covers XmlSelect::setDefault
104 */
105 public function testSetDefault() {
106 $this->select->setDefault( 'bar1' );
107 $this->select->addOption( 'foo1' );
108 $this->select->addOption( 'bar1' );
109 $this->select->addOption( 'foo2' );
110 $this->assertEquals(
111 '<select><option value="foo1">foo1</option>' . "\n" .
112 '<option value="bar1" selected="">bar1</option>' . "\n" .
113 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
114 }
115
116 /**
117 * Adding default later on should set the correct selection or
118 * raise an exception.
119 * To handle this, we need to render the options in getHtml()
120 * @covers XmlSelect::setDefault
121 */
122 public function testSetDefaultAfterAddingOptions() {
123 $this->select->addOption( 'foo1' );
124 $this->select->addOption( 'bar1' );
125 $this->select->addOption( 'foo2' );
126 $this->select->setDefault( 'bar1' ); # setting default after adding options
127 $this->assertEquals(
128 '<select><option value="foo1">foo1</option>' . "\n" .
129 '<option value="bar1" selected="">bar1</option>' . "\n" .
130 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
131 }
132
133 /**
134 * @covers XmlSelect::setAttribute
135 * @covers XmlSelect::getAttribute
136 */
137 public function testGetAttributes() {
138 # create some attributes
139 $this->select->setAttribute( 'dummy', 0x777 );
140 $this->select->setAttribute( 'string', 'euro €' );
141 $this->select->setAttribute( 1911, 'razor' );
142
143 # verify we can retrieve them
144 $this->assertEquals(
145 $this->select->getAttribute( 'dummy' ),
146 0x777
147 );
148 $this->assertEquals(
149 $this->select->getAttribute( 'string' ),
150 'euro €'
151 );
152 $this->assertEquals(
153 $this->select->getAttribute( 1911 ),
154 'razor'
155 );
156
157 # inexistant keys should give us 'null'
158 $this->assertEquals(
159 $this->select->getAttribute( 'I DO NOT EXIT' ),
160 null
161 );
162
163 # verify string / integer
164 $this->assertEquals(
165 $this->select->getAttribute( '1911' ),
166 'razor'
167 );
168 $this->assertEquals(
169 $this->select->getAttribute( 'dummy' ),
170 0x777
171 );
172 }
173 }