2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
5 private static $oldLang;
6 private static $oldContLang;
7 private static $oldNamespaces;
9 public function setUp() {
10 global $wgLang, $wgContLang, $wgLanguageCode;
12 self
::$oldLang = $wgLang;
13 self
::$oldContLang = $wgContLang;
15 $wgLanguageCode = 'en';
16 $wgContLang = $wgLang = Language
::factory( $wgLanguageCode );
18 // Hardcode namespaces during test runs,
19 // so that html output based on existing namespaces
20 // can be properly evaluated.
21 self
::$oldNamespaces = $wgContLang->namespaceNames
;
22 $wgContLang->namespaceNames
= array(
34 9 => 'MediaWiki_talk',
36 11 => 'Template_talk',
42 public function tearDown() {
43 global $wgLang, $wgContLang, $wgLanguageCode;
44 $wgLang = self
::$oldLang;
45 $wgContLang = self
::$oldContLang;
46 $wgLanguageCode = $wgContLang->getCode();
47 $wgContLang->namespaceNames
= self
::$oldNamespaces;
50 public function testExpandAttributesSkipsNullAndFalse() {
54 Html
::expandAttributes( array( 'foo' => null ) ),
55 'skip keys with null value'
58 Html
::expandAttributes( array( 'foo' => false ) ),
59 'skip keys with false value'
61 $this->AssertNotEmpty(
62 Html
::expandAttributes( array( 'foo' => '' ) ),
63 'keep keys with an empty string'
67 public function testExpandAttributesForBooleans() {
71 Html
::expandAttributes( array( 'selected' => false ) ),
72 'Boolean attributes do not generates output when value is false'
76 Html
::expandAttributes( array( 'selected' => null ) ),
77 'Boolean attributes do not generates output when value is null'
81 $wgHtml5 ?
' selected=""' : ' selected="selected"',
82 Html
::expandAttributes( array( 'selected' => true ) ),
83 'Boolean attributes skip value output'
86 $wgHtml5 ?
' selected=""' : ' selected="selected"',
87 Html
::expandAttributes( array( 'selected' ) ),
88 'Boolean attributes (ex: selected) do not need a value'
93 * Test for Html::expandAttributes()
94 * Please note it output a string prefixed with a space!
96 public function testExpandAttributesVariousExpansions() {
100 Html
::expandAttributes( array( 'empty_string' => '' ) ),
101 'Value with an empty string'
105 Html
::expandAttributes( array( 'key' => 'value' ) ),
110 Html
::expandAttributes( array( 'one' => 1 ) ),
111 'Value is a numeric one'
115 Html
::expandAttributes( array( 'zero' => 0 ) ),
116 'Value is a numeric zero'
121 * Html::expandAttributes has special features for HTML
122 * attributes that use space separated lists and also
123 * allows arrays to be used as values.
125 public function testExpandAttributesListValueAttributes() {
128 ' class="redundant spaces here"',
129 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
130 'Normalization should strip redundant spaces'
134 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
135 'Normalization should remove duplicates in string-lists'
137 ### "EMPTY" ARRAY VALUES
140 Html
::expandAttributes( array( 'class' => array() ) ),
141 'Value with an empty array'
145 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
146 'Array with null, empty string and spaces'
148 ### NON-EMPTY ARRAY VALUES
151 Html
::expandAttributes( array( 'class' => array(
158 'Normalization should remove duplicates in the array'
162 Html
::expandAttributes( array( 'class' => array(
168 'Normalization should remove duplicates in string-lists in the array'
173 * Test feature added by r96188, let pass attributes values as
174 * a PHP array. Restricted to class,rel, accesskey.
176 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
178 ' class="booltrue one"',
179 Html
::expandAttributes( array( 'class' => array(
183 # Method use isset() internally, make sure we do discard
184 # attributes values which have been assigned well known values
186 'boolfalse' => false,
194 * How do we handle duplicate keys in HTML attributes expansion?
195 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
196 * The later will take precedence.
198 * Feature added by r96188
200 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
203 Html
::expandAttributes( array( 'class' => array(
211 function testNamespaceSelector() {
213 '<select id="namespace" name="namespace">
214 <option value="0">(Main)</option>
215 <option value="1">Talk</option>
216 <option value="2">User</option>
217 <option value="3">User talk</option>
218 <option value="4">MyWiki</option>
219 <option value="5">MyWiki Talk</option>
220 <option value="6">File</option>
221 <option value="7">File talk</option>
222 <option value="8">MediaWiki</option>
223 <option value="9">MediaWiki talk</option>
224 <option value="10">Template</option>
225 <option value="11">Template talk</option>
226 <option value="100">Custom</option>
227 <option value="101">Custom talk</option>
229 Html
::namespaceSelector(),
230 'Basic namespace selector without custom options'
233 '<label for="mw-test-namespace">Select a namespace:</label> <select id="mw-test-namespace" name="wpNamespace">
234 <option value="all">all</option>
235 <option value="0">(Main)</option>
236 <option value="1">Talk</option>
237 <option value="2" selected="">User</option>
238 <option value="3">User talk</option>
239 <option value="4">MyWiki</option>
240 <option value="5">MyWiki Talk</option>
241 <option value="6">File</option>
242 <option value="7">File talk</option>
243 <option value="8">MediaWiki</option>
244 <option value="9">MediaWiki talk</option>
245 <option value="10">Template</option>
246 <option value="11">Template talk</option>
247 <option value="100">Custom</option>
248 <option value="101">Custom talk</option>
250 Html
::namespaceSelector(
251 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
252 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
254 'Basic namespace selector with custom values'