Merge "Use the request object provided in User::setCookies"
[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(
76 '<select><option value="foo">foo</option></select>',
77 $this->select->getHTML()
78 );
79 }
80
81 /**
82 * @covers XmlSelect::addOption
83 */
84 public function testAddOptionWithDefault() {
85 $this->select->addOption( 'foo', true );
86 $this->assertEquals(
87 '<select><option value="1">foo</option></select>',
88 $this->select->getHTML()
89 );
90 }
91
92 /**
93 * @covers XmlSelect::addOption
94 */
95 public function testAddOptionWithFalse() {
96 $this->select->addOption( 'foo', false );
97 $this->assertEquals(
98 '<select><option value="foo">foo</option></select>',
99 $this->select->getHTML()
100 );
101 }
102
103 /**
104 * @covers XmlSelect::addOption
105 */
106 public function testAddOptionWithValueZero() {
107 $this->select->addOption( 'foo', 0 );
108 $this->assertEquals(
109 '<select><option value="0">foo</option></select>',
110 $this->select->getHTML()
111 );
112 }
113
114 /**
115 * @covers XmlSelect::setDefault
116 */
117 public function testSetDefault() {
118 $this->select->setDefault( 'bar1' );
119 $this->select->addOption( 'foo1' );
120 $this->select->addOption( 'bar1' );
121 $this->select->addOption( 'foo2' );
122 $this->assertEquals(
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() );
126 }
127
128 /**
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
133 */
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
139 $this->assertEquals(
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() );
143 }
144
145 /**
146 * @covers XmlSelect::setAttribute
147 * @covers XmlSelect::getAttribute
148 */
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' );
154
155 # verify we can retrieve them
156 $this->assertEquals(
157 $this->select->getAttribute( 'dummy' ),
158 0x777
159 );
160 $this->assertEquals(
161 $this->select->getAttribute( 'string' ),
162 'euro €'
163 );
164 $this->assertEquals(
165 $this->select->getAttribute( 1911 ),
166 'razor'
167 );
168
169 # inexistent keys should give us 'null'
170 $this->assertEquals(
171 $this->select->getAttribute( 'I DO NOT EXIT' ),
172 null
173 );
174
175 # verify string / integer
176 $this->assertEquals(
177 $this->select->getAttribute( '1911' ),
178 'razor'
179 );
180 $this->assertEquals(
181 $this->select->getAttribute( 'dummy' ),
182 0x777
183 );
184 }
185 }