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();
. "\n"
. Html::closeElement( 'select' );
if ( isset( $params['label'] ) ) {
- $ret = Xml::label( $params['label'], $selectAttribs['name'] ) . ' ' . $ret;
+ $ret = Xml::label( $params['label'], $selectAttribs['id'] ) . ' ' . $ret;
}
return $ret;
}
class HtmlTest extends MediaWikiTestCase {
private static $oldLang;
private static $oldContLang;
+ private static $oldNamespaces;
public function setUp() {
global $wgLang, $wgContLang, $wgLanguageCode;
$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() {
$wgLang = self::$oldLang;
$wgContLang = self::$oldContLang;
$wgLanguageCode = $wgContLang->getCode();
+ $wgContLang->namespaceNames = self::$oldNamespaces;
}
public function testExpandAttributesSkipsNullAndFalse() {
)))
);
}
+
+ 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> <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'
+ );
+ }
}