Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / XmlSelectTest.php
blob0e03add4df8e11befe7754ef36a08324dcabcf24
1 <?php
3 /**
4 * @group Xml
5 */
6 class XmlSelectTest extends MediaWikiTestCase {
8 /**
9 * @var XmlSelect
11 protected $select;
13 protected function setUp() {
14 parent::setUp();
15 $this->setMwGlobals( array(
16 'wgWellFormedXml' => true,
17 ) );
18 $this->select = new XmlSelect();
21 protected function tearDown() {
22 parent::tearDown();
23 $this->select = null;
26 /**
27 * @covers XmlSelect::__construct
29 public function testConstructWithoutParameters() {
30 $this->assertEquals( '<select></select>', $this->select->getHTML() );
33 /**
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() );
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
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
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>' ),
70 /**
71 * @covers XmlSelect::addOption
73 public function testAddOption() {
74 $this->select->addOption( 'foo' );
75 $this->assertEquals(
76 '<select><option value="foo">foo</option></select>',
77 $this->select->getHTML()
81 /**
82 * @covers XmlSelect::addOption
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()
92 /**
93 * @covers XmlSelect::addOption
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()
104 * @covers XmlSelect::addOption
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()
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' );
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() );
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
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() );
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
156 $this->assertEquals(
157 $this->select->getAttribute( 'dummy' ),
158 0x777
160 $this->assertEquals(
161 $this->select->getAttribute( 'string' ),
162 'euro €'
164 $this->assertEquals(
165 $this->select->getAttribute( 1911 ),
166 'razor'
169 # inexistent keys should give us 'null'
170 $this->assertEquals(
171 $this->select->getAttribute( 'I DO NOT EXIT' ),
172 null
175 # verify string / integer
176 $this->assertEquals(
177 $this->select->getAttribute( '1911' ),
178 'razor'
180 $this->assertEquals(
181 $this->select->getAttribute( 'dummy' ),
182 0x777