Add tests for several methods of Xml
[lhc/web/wiklou.git] / tests / phpunit / includes / XmlTest.php
index 6c059ec..d26f3ec 100644 (file)
@@ -34,6 +34,11 @@ class XmlTest extends MediaWikiTestCase {
                ] );
        }
 
+       protected function tearDown() {
+               Language::factory( 'en' )->resetNamespaces();
+               parent::tearDown();
+       }
+
        /**
         * @covers Xml::expandAttributes
         */
@@ -136,6 +141,57 @@ class XmlTest extends MediaWikiTestCase {
                $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
        }
 
+       public function provideMonthSelector() {
+               global $wgLang;
+
+               $header = '<select name="month" id="month" class="mw-month-selector">';
+               $header2 = '<select name="month" id="monthSelector" class="mw-month-selector">';
+               $monthsString = '';
+               for ( $i = 1; $i < 13; $i++ ) {
+                       $monthName = $wgLang->getMonthName( $i );
+                       $monthsString .= "<option value=\"{$i}\">{$monthName}</option>";
+                       if ( $i !== 12 ) {
+                               $monthsString .= "\n";
+                       }
+               }
+               $monthsString2 = str_replace(
+                       '<option value="12">December</option>',
+                       '<option value="12" selected="">December</option>',
+                       $monthsString
+               );
+               $end = '</select>';
+
+               $allMonths = "<option value=\"AllMonths\">all</option>\n";
+               return [
+                       [ $header . $monthsString . $end, '', null, 'month' ],
+                       [ $header . $monthsString2 . $end, 12, null, 'month' ],
+                       [ $header2 . $monthsString . $end, '', null, 'monthSelector' ],
+                       [ $header . $allMonths . $monthsString . $end, '', 'AllMonths', 'month' ],
+
+               ];
+       }
+
+       /**
+        * @covers Xml::monthSelector
+        * @dataProvider provideMonthSelector
+        */
+       public function testMonthSelector( $expected, $selected, $allmonths, $id ) {
+               $this->assertEquals(
+                       $expected,
+                       Xml::monthSelector( $selected, $allmonths, $id )
+               );
+       }
+
+       /**
+        * @covers Xml::span
+        */
+       public function testSpan() {
+               $this->assertEquals(
+                       '<span class="foo" id="testSpan">element</span>',
+                       Xml::span( 'element', 'foo', [ 'id' => 'testSpan' ] )
+               );
+       }
+
        /**
         * @covers Xml::dateMenu
         */
@@ -403,12 +459,15 @@ class XmlTest extends MediaWikiTestCase {
         */
        public function testListDropDown() {
                $this->assertEquals(
-                       '<select id="test-name" name="test-name" class="test-css" tabindex="2">' . "\n" .
-                               '<option value="other">other reasons</option>' .
-                               '<optgroup label="Foo"><option value="Foo 1">Foo 1</option>' .
-                               '<option value="Example" selected="">Example</option>' .
-                               '</optgroup><optgroup label="Bar">' .
-                               '<option value="Bar 1">Bar 1</option></optgroup>' . "\n" .
+                       '<select name="test-name" id="test-name" class="test-css" tabindex="2">' .
+                               '<option value="other">other reasons</option>' . "\n" .
+                               '<optgroup label="Foo">' .
+                               '<option value="Foo 1">Foo 1</option>' . "\n" .
+                               '<option value="Example" selected="">Example</option>' . "\n" .
+                               '</optgroup>' . "\n" .
+                               '<optgroup label="Bar">' .
+                               '<option value="Bar 1">Bar 1</option>' . "\n" .
+                               '</optgroup>' .
                                '</select>',
                        Xml::listDropDown(
                                // name
@@ -426,4 +485,133 @@ class XmlTest extends MediaWikiTestCase {
                        )
                );
        }
+
+       /**
+        * @covers Xml::listDropDownOptions
+        */
+       public function testListDropDownOptions() {
+               $this->assertEquals(
+                       [
+                               'other reasons' => 'other',
+                               'Foo' => [
+                                       'Foo 1' => 'Foo 1',
+                                       'Example' => 'Example',
+                               ],
+                               'Bar' => [
+                                       'Bar 1' => 'Bar 1',
+                               ],
+                       ],
+                       Xml::listDropDownOptions(
+                               "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
+                               [ 'other' => 'other reasons' ]
+                       )
+               );
+       }
+
+       /**
+        * @covers Xml::listDropDownOptionsOoui
+        */
+       public function testListDropDownOptionsOoui() {
+               $this->assertEquals(
+                       [
+                               [ 'data' => 'other', 'label' => 'other reasons' ],
+                               [ 'optgroup' => 'Foo' ],
+                               [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
+                               [ 'data' => 'Example', 'label' => 'Example' ],
+                               [ 'optgroup' => 'Bar' ],
+                               [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
+                       ],
+                       Xml::listDropDownOptionsOoui( [
+                               'other reasons' => 'other',
+                               'Foo' => [
+                                       'Foo 1' => 'Foo 1',
+                                       'Example' => 'Example',
+                               ],
+                               'Bar' => [
+                                       'Bar 1' => 'Bar 1',
+                               ],
+                       ] )
+               );
+       }
+
+       /**
+        * @covers Xml::fieldset
+        */
+       public function testFieldset() {
+               $this->assertEquals(
+                       "<fieldset>\n",
+                       Xml::fieldset(),
+                       'Opening tag'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n",
+                       Xml::fieldset( false ),
+                       'Opening tag (false means no legend)'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n",
+                       Xml::fieldset( '' ),
+                       'Opening tag (empty string also means no legend)'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n<legend>Foo</legend>\n",
+                       Xml::fieldset( 'Foo' ),
+                       'Opening tag with legend'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n<legend>Foo</legend>\nBar\n</fieldset>\n",
+                       Xml::fieldset( 'Foo', 'Bar' ),
+                       'Entire element with legend'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n<legend>Foo</legend>\n",
+                       Xml::fieldset( 'Foo', false ),
+                       'Opening tag with legend (false means no content and no closing tag)'
+               );
+               $this->assertEquals(
+                       "<fieldset>\n<legend>Foo</legend>\n\n</fieldset>\n",
+                       Xml::fieldset( 'Foo', '' ),
+                       'Entire element with legend but no content (empty string generates a closing tag)'
+               );
+               $this->assertEquals(
+                       "<fieldset class=\"bar\">\n<legend>Foo</legend>\nBar\n</fieldset>\n",
+                       Xml::fieldset( 'Foo', 'Bar', [ 'class' => 'bar' ] ),
+                       'Opening tag with legend and attributes'
+               );
+               $this->assertEquals(
+                       "<fieldset class=\"bar\">\n<legend>Foo</legend>\n",
+                       Xml::fieldset( 'Foo', false, [ 'class' => 'bar' ] ),
+                       'Entire element with legend and attributes'
+               );
+       }
+
+       /**
+        * @covers Xml::testBuildTable
+        */
+       public function testBuildTable() {
+               $firstRow = [ 'foo', 'bar' ];
+               $secondRow = [ 'Berlin', 'Tehran' ];
+               $headers = [ 'header1', 'header2' ];
+               $expected = '<table id="testTable"><thead id="testTable"><th>header1</th>' .
+                       '<th>header2</th></thead><tr><td>foo</td><td>bar</td></tr><tr><td>Berlin</td>' .
+                       '<td>Tehran</td></tr></table>';
+               $this->assertEquals(
+                       $expected,
+                       Xml::buildTable(
+                               [ $firstRow, $secondRow ],
+                               [ 'id' => 'testTable' ],
+                               $headers
+                       )
+               );
+       }
+
+       /**
+        * @covers Xml::testBuildTableRow
+        */
+       public function testBuildTableRow() {
+               $this->assertEquals(
+                       '<tr id="testRow"><td>foo</td><td>bar</td></tr>',
+                       Xml::buildTableRow( [ 'id' => 'testRow' ], [ 'foo', 'bar' ] )
+               );
+       }
 }