Follow up r91419. A boolean attribute can be both the empty string and its name ...
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2 /** tests for includes/Html.php */
3
4 class HtmlTest extends MediaWikiTestCase {
5 private static $oldLang;
6
7 public function setUp() {
8 global $wgLang, $wgLanguageCode;
9
10 self::$oldLang = $wgLang;
11 $wgLanguageCode = 'en';
12 $wgLang = Language::factory( $wgLanguageCode );
13 }
14
15 public function tearDown() {
16 global $wgLang, $wgLanguageCode;
17 $wgLang = self::$oldLang;
18 $wgLanguageCode = $wgLang->getCode();
19 }
20
21 public function testExpandAttributesSkipsNullAndFalse() {
22
23 ### EMPTY ########
24 $this->AssertEmpty(
25 Html::expandAttributes( array( 'foo'=>null) ),
26 'skip keys with null value'
27 );
28 $this->AssertEmpty(
29 Html::expandAttributes( array( 'foo'=>false) ),
30 'skip keys with false value'
31 );
32 $this->AssertNotEmpty(
33 Html::expandAttributes( array( 'foo'=>'') ),
34 'keep keys with an empty string'
35 );
36 }
37
38 public function testExpandAttributesForBooleans() {
39 $this->AssertEquals(
40 '',
41 Html::expandAttributes( array( 'selected'=>false) ),
42 'Boolean attributes do not generates output when value is false'
43 );
44 $this->AssertEquals(
45 '',
46 Html::expandAttributes( array( 'selected'=>null) ),
47 'Boolean attributes do not generates output when value is null'
48 );
49
50 $this->AssertEquals(
51 ' selected="selected"',
52 Html::expandAttributes( array( 'selected'=>true ) ),
53 'Boolean attributes skip value output'
54 );
55 $this->AssertEquals(
56 ' selected="selected"',
57 Html::expandAttributes( array( 'selected' ) ),
58 'Boolean attributes (ex: selected) do not need a value'
59 );
60 }
61
62 /**
63 * Test for Html::expandAttributes()
64 * Please note it output a string prefixed with a space!
65 */
66 public function testExpandAttributesVariousExpansions() {
67 ### NOT EMPTY ####
68 $this->AssertEquals(
69 ' empty_string=""',
70 Html::expandAttributes( array( 'empty_string'=>'') ),
71 'Value with an empty string'
72 );
73 $this->AssertEquals(
74 ' key="value"',
75 Html::expandAttributes( array( 'key'=>'value') ),
76 'Value is a string'
77 );
78 $this->AssertEquals(
79 ' one="1"',
80 Html::expandAttributes( array( 'one'=>1) ),
81 'Value is a numeric one'
82 );
83 $this->AssertEquals(
84 ' zero="0"',
85 Html::expandAttributes( array( 'zero'=>0) ),
86 'Value is a numeric zero'
87 );
88 }
89 }