2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
5 private static $oldLang;
6 private static $oldContLang;
7 private static $oldLanguageCode;
8 private static $oldNamespaces;
10 public function setUp() {
11 global $wgLang, $wgContLang, $wgLanguageCode;
13 self
::$oldLang = $wgLang;
14 self
::$oldContLang = $wgContLang;
15 self
::$oldNamespaces = $wgContLang->namespaceNames
;
16 self
::$oldLanguageCode = $wgLanguageCode;
18 $wgLanguageCode = 'en';
19 $wgContLang = $wgLang = Language
::factory( $wgLanguageCode );
21 // Hardcode namespaces during test runs,
22 // so that html output based on existing namespaces
23 // can be properly evaluated.
24 $wgContLang->namespaceNames
= array(
36 9 => 'MediaWiki_talk',
38 11 => 'Template_talk',
44 public function tearDown() {
45 global $wgLang, $wgContLang, $wgLanguageCode;
47 $wgContLang->namespaceNames
= self
::$oldNamespaces;
48 $wgLang = self
::$oldLang;
49 $wgContLang = self
::$oldContLang;
50 $wgLanguageCode = self
::$oldLanguageCode;
53 public function testExpandAttributesSkipsNullAndFalse() {
57 Html
::expandAttributes( array( 'foo' => null ) ),
58 'skip keys with null value'
61 Html
::expandAttributes( array( 'foo' => false ) ),
62 'skip keys with false value'
64 $this->AssertNotEmpty(
65 Html
::expandAttributes( array( 'foo' => '' ) ),
66 'keep keys with an empty string'
70 public function testExpandAttributesForBooleans() {
74 Html
::expandAttributes( array( 'selected' => false ) ),
75 'Boolean attributes do not generates output when value is false'
79 Html
::expandAttributes( array( 'selected' => null ) ),
80 'Boolean attributes do not generates output when value is null'
84 $wgHtml5 ?
' selected=""' : ' selected="selected"',
85 Html
::expandAttributes( array( 'selected' => true ) ),
86 'Boolean attributes skip value output'
89 $wgHtml5 ?
' selected=""' : ' selected="selected"',
90 Html
::expandAttributes( array( 'selected' ) ),
91 'Boolean attributes (ex: selected) do not need a value'
96 * Test for Html::expandAttributes()
97 * Please note it output a string prefixed with a space!
99 public function testExpandAttributesVariousExpansions() {
103 Html
::expandAttributes( array( 'empty_string' => '' ) ),
104 'Value with an empty string'
108 Html
::expandAttributes( array( 'key' => 'value' ) ),
113 Html
::expandAttributes( array( 'one' => 1 ) ),
114 'Value is a numeric one'
118 Html
::expandAttributes( array( 'zero' => 0 ) ),
119 'Value is a numeric zero'
124 * Html::expandAttributes has special features for HTML
125 * attributes that use space separated lists and also
126 * allows arrays to be used as values.
128 public function testExpandAttributesListValueAttributes() {
131 ' class="redundant spaces here"',
132 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
133 'Normalization should strip redundant spaces'
137 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
138 'Normalization should remove duplicates in string-lists'
140 ### "EMPTY" ARRAY VALUES
143 Html
::expandAttributes( array( 'class' => array() ) ),
144 'Value with an empty array'
148 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
149 'Array with null, empty string and spaces'
151 ### NON-EMPTY ARRAY VALUES
154 Html
::expandAttributes( array( 'class' => array(
161 'Normalization should remove duplicates in the array'
165 Html
::expandAttributes( array( 'class' => array(
171 'Normalization should remove duplicates in string-lists in the array'
176 * Test feature added by r96188, let pass attributes values as
177 * a PHP array. Restricted to class,rel, accesskey.
179 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
181 ' class="booltrue one"',
182 Html
::expandAttributes( array( 'class' => array(
186 # Method use isset() internally, make sure we do discard
187 # attributes values which have been assigned well known values
189 'boolfalse' => false,
197 * How do we handle duplicate keys in HTML attributes expansion?
198 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
199 * The later will take precedence.
201 * Feature added by r96188
203 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
206 Html
::expandAttributes( array( 'class' => array(
214 function testNamespaceSelector() {
216 '<select id="namespace" name="namespace">
217 <option value="0">(Main)</option>
218 <option value="1">Talk</option>
219 <option value="2">User</option>
220 <option value="3">User talk</option>
221 <option value="4">MyWiki</option>
222 <option value="5">MyWiki Talk</option>
223 <option value="6">File</option>
224 <option value="7">File talk</option>
225 <option value="8">MediaWiki</option>
226 <option value="9">MediaWiki talk</option>
227 <option value="10">Template</option>
228 <option value="11">Template talk</option>
229 <option value="100">Custom</option>
230 <option value="101">Custom talk</option>
232 Html
::namespaceSelector(),
233 'Basic namespace selector without custom options'
236 '<label for="mw-test-namespace">Select a namespace:</label> <select id="mw-test-namespace" name="wpNamespace">
237 <option value="all">all</option>
238 <option value="0">(Main)</option>
239 <option value="1">Talk</option>
240 <option value="2" selected="">User</option>
241 <option value="3">User talk</option>
242 <option value="4">MyWiki</option>
243 <option value="5">MyWiki Talk</option>
244 <option value="6">File</option>
245 <option value="7">File talk</option>
246 <option value="8">MediaWiki</option>
247 <option value="9">MediaWiki talk</option>
248 <option value="10">Template</option>
249 <option value="11">Template talk</option>
250 <option value="100">Custom</option>
251 <option value="101">Custom talk</option>
253 Html
::namespaceSelector(
254 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
255 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
257 'Basic namespace selector with custom values'