[Html] Unit test + bugfix Html::namespaceSelector
authorKrinkle <krinkle@users.mediawiki.org>
Wed, 25 Jan 2012 03:25:54 +0000 (03:25 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Wed, 25 Jan 2012 03:25:54 +0000 (03:25 +0000)
* Previously it was passing $selectAttribs['name'] to Xml::label, which uses its value for the <label for=""> attribute. This works as long as $selectAttribs['id'] and $selectAttribs['name'] match, but when they don't it fails. <label for=""> always corresponds with <{input,text area,select} id=""> in browsers, never with "name".
* Make name/id match eachother by default to avoid backwards compatibility breakages (they used to match in the Xml class method as well)
* Add HtmlTest.php entries similar to the ones in XmlTest
* Fix E_NOTICE about $params['selected'], default to ''

-- Follows-up r109974, r109698, r109990
-- Bug originally introduced in r41425
-- XmlTest.php still runs successfully
-- HtmlTest.php runs successfully

includes/Html.php
tests/phpunit/includes/HtmlTest.php
tests/phpunit/includes/XmlTest.php

index eaa244f..1b324d3 100644 (file)
@@ -713,17 +713,21 @@ class Html {
                global $wgContLang;
 
                $selectAttribs = $selectAttribs + array(
-                       'id' => 'mw-namespaceselect',
+                       'id' => 'namespace',
                        'name' => 'namespace',
                );
                ksort( $selectAttribs );
 
-               // If string only contains digits, convert to clean int. Selected could also
-               // be "all" or "" etc. which needs to be left untouched.
-               // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
-               // and returns false for already clean ints. Use regex instead..
-               if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
-                       $params['selected'] = intval( $params['selected'] );
+               if ( isset( $params['selected'] ) ) {
+                       // If string only contains digits, convert to clean int. Selected could also
+                       // be "all" or "" etc. which needs to be left untouched.
+                       // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
+                       // and returns false for already clean ints. Use regex instead..
+                       if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
+                               $params['selected'] = intval( $params['selected'] );
+                       }
+               } else {
+                       $params['selected'] = '';
                }
 
                $options = array();
@@ -749,7 +753,7 @@ class Html {
                        . "\n"
                        . Html::closeElement( 'select' );
                if ( isset( $params['label'] ) ) {
-                       $ret = Xml::label( $params['label'], $selectAttribs['name'] ) . '&#160;' . $ret;
+                       $ret = Xml::label( $params['label'], $selectAttribs['id'] ) . '&#160;' . $ret;
                }
                return $ret;
        }
index a517a1c..8de7272 100644 (file)
@@ -4,6 +4,7 @@
 class HtmlTest extends MediaWikiTestCase {
        private static $oldLang;
        private static $oldContLang;
+       private static $oldNamespaces;
 
        public function setUp() {
                global $wgLang, $wgContLang, $wgLanguageCode;
@@ -13,6 +14,29 @@ class HtmlTest extends MediaWikiTestCase {
                
                $wgLanguageCode = 'en';
                $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
+
+               // Hardcode namespaces during test runs,
+               // so that html output based on existing namespaces
+               // can be properly evaluated.
+               self::$oldNamespaces = $wgContLang->namespaceNames;
+               $wgContLang->namespaceNames = array(
+                       -2 => 'Media',
+                       -1 => 'Special',
+                       0  => '',
+                       1  => 'Talk',
+                       2  => 'User',
+                       3  => 'User_talk',
+                       4  => 'MyWiki',
+                       5  => 'MyWiki_Talk',
+                       6  => 'File',
+                       7  => 'File_talk',
+                       8  => 'MediaWiki',
+                       9  => 'MediaWiki_talk',
+                       10  => 'Template',
+                       11  => 'Template_talk',
+                       100  => 'Custom',
+                       101  => 'Custom_talk',
+               );
        }
        
        public function tearDown() {
@@ -20,6 +44,7 @@ class HtmlTest extends MediaWikiTestCase {
                $wgLang = self::$oldLang;
                $wgContLang = self::$oldContLang;
                $wgLanguageCode = $wgContLang->getCode();
+               $wgContLang->namespaceNames = self::$oldNamespaces;
        }
 
        public function testExpandAttributesSkipsNullAndFalse() {
@@ -182,4 +207,51 @@ class HtmlTest extends MediaWikiTestCase {
                        )))
                );
        }
+
+       function testNamespaceSelector() {
+               $this->assertEquals(
+                       '<select id="namespace" name="namespace">
+<option value="0">(Main)</option>
+<option value="1">Talk</option>
+<option value="2">User</option>
+<option value="3">User talk</option>
+<option value="4">MyWiki</option>
+<option value="5">MyWiki Talk</option>
+<option value="6">File</option>
+<option value="7">File talk</option>
+<option value="8">MediaWiki</option>
+<option value="9">MediaWiki talk</option>
+<option value="10">Template</option>
+<option value="11">Template talk</option>
+<option value="100">Custom</option>
+<option value="101">Custom talk</option>
+</select>',
+                       Html::namespaceSelector(),
+                       'Basic namespace selector without custom options'
+               );
+               $this->assertEquals(
+                       '<label for="mw-test-namespace">Select a namespace:</label>&#160;<select id="mw-test-namespace" name="wpNamespace">
+<option value="all">all</option>
+<option value="0">(Main)</option>
+<option value="1">Talk</option>
+<option value="2" selected="">User</option>
+<option value="3">User talk</option>
+<option value="4">MyWiki</option>
+<option value="5">MyWiki Talk</option>
+<option value="6">File</option>
+<option value="7">File talk</option>
+<option value="8">MediaWiki</option>
+<option value="9">MediaWiki talk</option>
+<option value="10">Template</option>
+<option value="11">Template talk</option>
+<option value="100">Custom</option>
+<option value="101">Custom talk</option>
+</select>',
+                       Html::namespaceSelector(
+                               array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
+                               array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
+                       ),
+                       'Basic namespace selector with custom values'
+               );
+       }
 }
index 9d75616..ea9bd7e 100644 (file)
@@ -214,7 +214,7 @@ class XmlTest extends MediaWikiTestCase {
                        'Basic namespace selector without custom options'
                );
                $this->assertEquals(
-                       '<label for="myname">Select a namespace:</label>&#160;<select class="namespaceselector" id="namespace" name="myname">
+                       '<label for="namespace">Select a namespace:</label>&#160;<select class="namespaceselector" id="namespace" name="myname">
 <option value="all">all</option>
 <option value="0">(Main)</option>
 <option value="1">Talk</option>