tests for Html::testExpandAttributes()
[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 ### FIXME: maybe they should just output 'selected'
51 $this->AssertEquals(
52 ' selected=""',
53 Html::expandAttributes( array( 'selected'=>true ) ),
54 'Boolean attributes skip value output'
55 );
56 $this->AssertEquals(
57 ' selected=""',
58 Html::expandAttributes( array( 'selected' ) ),
59 'Boolean attributes (ex: selected) do not need a value'
60 );
61 }
62
63 /**
64 * Test for Html::expandAttributes()
65 * Please note it output a string prefixed with a space!
66 */
67 public function testExpandAttributesVariousExpansions() {
68 ### NOT EMPTY ####
69 $this->AssertEquals(
70 ' empty_string=""',
71 Html::expandAttributes( array( 'empty_string'=>'') ),
72 'Value with an empty string'
73 );
74 $this->AssertEquals(
75 ' key="value"',
76 Html::expandAttributes( array( 'key'=>'value') ),
77 'Value is a string'
78 );
79 $this->AssertEquals(
80 ' one="1"',
81 Html::expandAttributes( array( 'one'=>1) ),
82 'Value is a numeric one'
83 );
84 $this->AssertEquals(
85 ' zero="0"',
86 Html::expandAttributes( array( 'zero'=>0) ),
87 'Value is a numeric zero'
88 );
89 }
90 }