summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/XmlSelectTest.php
blob: 2407c15171cae36026a7062d38b5c05541da8a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

// TODO
class XmlSelectTest extends MediaWikiTestCase {
	protected $select;

	protected function setUp() {
		$this->select = new XmlSelect();
	}
	protected function tearDown() {
		$this->select = null;
	}

	### START OF TESTS ###

	public function testConstructWithoutParameters() {
		$this->assertEquals( '<select></select>', $this->select->getHTML() );
	}

	/**
	 * Parameters are $name (false), $id (false), $default (false)
	 * @dataProvider provideConstructionParameters
	 */
	public function testConstructParameters( $name, $id, $default, $expected ) {
		$this->select = new XmlSelect( $name, $id, $default );
		$this->assertEquals( $expected, $this->select->getHTML() );
	}

	/**
	 * Provide parameters for testConstructParameters() which use three
	 * parameters:
	 *  - $name    (default: false)
	 *  - $id      (default: false)
	 *  - $default (default: false)
	 * Provides a fourth parameters representing the expected HTML output
	 *
	 */
	public function provideConstructionParameters() {
		return array(
			/**
			 * Values are set following a 3-bit Gray code where two successive
			 * values differ by only one value.
			 * See http://en.wikipedia.org/wiki/Gray_code
			 */
			#      $name   $id    $default
			array( false , false, false,  '<select></select>' ),
			array( false , false, 'foo',  '<select></select>' ),
			array( false , 'id' , 'foo',  '<select id="id"></select>' ),
			array( false , 'id' , false,  '<select id="id"></select>' ),
			array( 'name', 'id' , false,  '<select name="name" id="id"></select>' ),
			array( 'name', 'id' , 'foo',  '<select name="name" id="id"></select>' ),
			array( 'name', false, 'foo',  '<select name="name"></select>' ),
			array( 'name', false, false,  '<select name="name"></select>' ),
		);
	}

	# Begin XmlSelect::addOption() similar to Xml::option
	public function testAddOption() {
		$this->select->addOption( 'foo' );
		$this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
	}
	public function testAddOptionWithDefault() {
		$this->select->addOption( 'foo', true );
		$this->assertEquals( '<select><option value="1">foo</option></select>', $this->select->getHTML() );
	}
	public function testAddOptionWithFalse() {
		$this->select->addOption( 'foo', false );
		$this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
	}
	public function testAddOptionWithValueZero() {
		$this->select->addOption( 'foo', 0 );
		$this->assertEquals( '<select><option value="0">foo</option></select>', $this->select->getHTML() );
	}
	# End XmlSelect::addOption() similar to Xml::option

	public function testSetDefault() {
		$this->select->setDefault( 'bar1' );
		$this->select->addOption( 'foo1' );
		$this->select->addOption( 'bar1' );
		$this->select->addOption( 'foo2' );
		$this->assertEquals(
'<select><option value="foo1">foo1</option>' . "\n" .
'<option value="bar1" selected="">bar1</option>' . "\n" .
'<option value="foo2">foo2</option></select>', $this->select->getHTML() );
	}

	/**
	 * Adding default later on should set the correct selection or
	 * raise an exception.
	 * To handle this, we need to render the options in getHtml()
	 */
	public function testSetDefaultAfterAddingOptions() {
		$this->select->addOption( 'foo1' );
		$this->select->addOption( 'bar1' );
		$this->select->addOption( 'foo2' );
		$this->select->setDefault( 'bar1' ); # setting default after adding options
		$this->assertEquals(
'<select><option value="foo1">foo1</option>' . "\n" .
'<option value="bar1" selected="">bar1</option>' . "\n" .
'<option value="foo2">foo2</option></select>', $this->select->getHTML() );
	}

	public function testGetAttributes() {
		# create some attributes
		$this->select->setAttribute( 'dummy', 0x777 );
		$this->select->setAttribute( 'string', 'euro €' );
		$this->select->setAttribute( 1911, 'razor' );

		# verify we can retrieve them
		$this->assertEquals(
			$this->select->getAttribute( 'dummy' ),
			0x777
		);
		$this->assertEquals(
			$this->select->getAttribute( 'string' ),
			'euro €'
		);
		$this->assertEquals(
			$this->select->getAttribute( 1911 ),
			'razor'
		);

		# inexistant keys should give us 'null'
		$this->assertEquals(
			$this->select->getAttribute( 'I DO NOT EXIT' ),
			null
		);

		# verify string / integer
		$this->assertEquals(
			$this->select->getAttribute( '1911' ),
			'razor'	
		);
		$this->assertEquals(
			$this->select->getAttribute( 'dummy' ),
			0x777
		);
	}
}