7cd98d881655b7ca6d79703e0c29861470f62fce
[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 private static $oldContLang;
7
8 public function setUp() {
9 global $wgLang, $wgContLang, $wgLanguageCode;
10
11 self::$oldLang = $wgLang;
12 self::$oldContLang = $wgContLang;
13
14 $wgLanguageCode = 'en';
15 $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
16 }
17
18 public function tearDown() {
19 global $wgLang, $wgContLang, $wgLanguageCode;
20 $wgLang = self::$oldLang;
21 $wgContLang = self::$oldContLang;
22 $wgLanguageCode = $wgContLang->getCode();
23 }
24
25 public function testExpandAttributesSkipsNullAndFalse() {
26
27 ### EMPTY ########
28 $this->AssertEmpty(
29 Html::expandAttributes( array( 'foo'=>null) ),
30 'skip keys with null value'
31 );
32 $this->AssertEmpty(
33 Html::expandAttributes( array( 'foo'=>false) ),
34 'skip keys with false value'
35 );
36 $this->AssertNotEmpty(
37 Html::expandAttributes( array( 'foo'=>'') ),
38 'keep keys with an empty string'
39 );
40 }
41
42 public function testExpandAttributesForBooleans() {
43 global $wgHtml5;
44 $this->AssertEquals(
45 '',
46 Html::expandAttributes( array( 'selected'=>false) ),
47 'Boolean attributes do not generates output when value is false'
48 );
49 $this->AssertEquals(
50 '',
51 Html::expandAttributes( array( 'selected'=>null) ),
52 'Boolean attributes do not generates output when value is null'
53 );
54
55 $this->AssertEquals(
56 $wgHtml5 ? ' selected=""' : ' selected="selected"',
57 Html::expandAttributes( array( 'selected'=>true ) ),
58 'Boolean attributes skip value output'
59 );
60 $this->AssertEquals(
61 $wgHtml5 ? ' selected=""' : ' selected="selected"',
62 Html::expandAttributes( array( 'selected' ) ),
63 'Boolean attributes (ex: selected) do not need a value'
64 );
65 }
66
67 /**
68 * Test for Html::expandAttributes()
69 * Please note it output a string prefixed with a space!
70 */
71 public function testExpandAttributesVariousExpansions() {
72 ### NOT EMPTY ####
73 $this->AssertEquals(
74 ' empty_string=""',
75 Html::expandAttributes( array( 'empty_string'=>'') ),
76 'Value with an empty string'
77 );
78 $this->AssertEquals(
79 ' key="value"',
80 Html::expandAttributes( array( 'key'=>'value') ),
81 'Value is a string'
82 );
83 $this->AssertEquals(
84 ' one="1"',
85 Html::expandAttributes( array( 'one'=>1) ),
86 'Value is a numeric one'
87 );
88 $this->AssertEquals(
89 ' zero="0"',
90 Html::expandAttributes( array( 'zero'=>0) ),
91 'Value is a numeric zero'
92 );
93 }
94 }