r109993 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109992‎ | r109993 | r109994 >
Date:03:25, 25 January 2012
Author:krinkle
Status:resolved (Comments)
Tags:core 
Comment:
[Html] Unit test + bugfix Html::namespaceSelector
* 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
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/HtmlTest.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/XmlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/XmlTest.php
@@ -214,7 +214,7 @@
215215 'Basic namespace selector without custom options'
216216 );
217217 $this->assertEquals(
218 - '<label for="myname">Select a namespace:</label>&#160;<select class="namespaceselector" id="namespace" name="myname">
 218+ '<label for="namespace">Select a namespace:</label>&#160;<select class="namespaceselector" id="namespace" name="myname">
219219 <option value="all">all</option>
220220 <option value="0">(Main)</option>
221221 <option value="1">Talk</option>
Index: trunk/phase3/tests/phpunit/includes/HtmlTest.php
@@ -4,6 +4,7 @@
55 class HtmlTest extends MediaWikiTestCase {
66 private static $oldLang;
77 private static $oldContLang;
 8+ private static $oldNamespaces;
89
910 public function setUp() {
1011 global $wgLang, $wgContLang, $wgLanguageCode;
@@ -13,6 +14,29 @@
1415
1516 $wgLanguageCode = 'en';
1617 $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
 18+
 19+ // Hardcode namespaces during test runs,
 20+ // so that html output based on existing namespaces
 21+ // can be properly evaluated.
 22+ self::$oldNamespaces = $wgContLang->namespaceNames;
 23+ $wgContLang->namespaceNames = array(
 24+ -2 => 'Media',
 25+ -1 => 'Special',
 26+ 0 => '',
 27+ 1 => 'Talk',
 28+ 2 => 'User',
 29+ 3 => 'User_talk',
 30+ 4 => 'MyWiki',
 31+ 5 => 'MyWiki_Talk',
 32+ 6 => 'File',
 33+ 7 => 'File_talk',
 34+ 8 => 'MediaWiki',
 35+ 9 => 'MediaWiki_talk',
 36+ 10 => 'Template',
 37+ 11 => 'Template_talk',
 38+ 100 => 'Custom',
 39+ 101 => 'Custom_talk',
 40+ );
1741 }
1842
1943 public function tearDown() {
@@ -20,6 +44,7 @@
2145 $wgLang = self::$oldLang;
2246 $wgContLang = self::$oldContLang;
2347 $wgLanguageCode = $wgContLang->getCode();
 48+ $wgContLang->namespaceNames = self::$oldNamespaces;
2449 }
2550
2651 public function testExpandAttributesSkipsNullAndFalse() {
@@ -182,4 +207,51 @@
183208 )))
184209 );
185210 }
 211+
 212+ function testNamespaceSelector() {
 213+ $this->assertEquals(
 214+ '<select id="namespace" name="namespace">
 215+<option value="0">(Main)</option>
 216+<option value="1">Talk</option>
 217+<option value="2">User</option>
 218+<option value="3">User talk</option>
 219+<option value="4">MyWiki</option>
 220+<option value="5">MyWiki Talk</option>
 221+<option value="6">File</option>
 222+<option value="7">File talk</option>
 223+<option value="8">MediaWiki</option>
 224+<option value="9">MediaWiki talk</option>
 225+<option value="10">Template</option>
 226+<option value="11">Template talk</option>
 227+<option value="100">Custom</option>
 228+<option value="101">Custom talk</option>
 229+</select>',
 230+ Html::namespaceSelector(),
 231+ 'Basic namespace selector without custom options'
 232+ );
 233+ $this->assertEquals(
 234+ '<label for="mw-test-namespace">Select a namespace:</label>&#160;<select id="mw-test-namespace" name="wpNamespace">
 235+<option value="all">all</option>
 236+<option value="0">(Main)</option>
 237+<option value="1">Talk</option>
 238+<option value="2" selected="">User</option>
 239+<option value="3">User talk</option>
 240+<option value="4">MyWiki</option>
 241+<option value="5">MyWiki Talk</option>
 242+<option value="6">File</option>
 243+<option value="7">File talk</option>
 244+<option value="8">MediaWiki</option>
 245+<option value="9">MediaWiki talk</option>
 246+<option value="10">Template</option>
 247+<option value="11">Template talk</option>
 248+<option value="100">Custom</option>
 249+<option value="101">Custom talk</option>
 250+</select>',
 251+ Html::namespaceSelector(
 252+ array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
 253+ array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
 254+ ),
 255+ 'Basic namespace selector with custom values'
 256+ );
 257+ }
186258 }
Index: trunk/phase3/includes/Html.php
@@ -713,17 +713,21 @@
714714 global $wgContLang;
715715
716716 $selectAttribs = $selectAttribs + array(
717 - 'id' => 'mw-namespaceselect',
 717+ 'id' => 'namespace',
718718 'name' => 'namespace',
719719 );
720720 ksort( $selectAttribs );
721721
722 - // If string only contains digits, convert to clean int. Selected could also
723 - // be "all" or "" etc. which needs to be left untouched.
724 - // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
725 - // and returns false for already clean ints. Use regex instead..
726 - if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
727 - $params['selected'] = intval( $params['selected'] );
 722+ if ( isset( $params['selected'] ) ) {
 723+ // If string only contains digits, convert to clean int. Selected could also
 724+ // be "all" or "" etc. which needs to be left untouched.
 725+ // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
 726+ // and returns false for already clean ints. Use regex instead..
 727+ if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
 728+ $params['selected'] = intval( $params['selected'] );
 729+ }
 730+ } else {
 731+ $params['selected'] = '';
728732 }
729733
730734 $options = array();
@@ -749,7 +753,7 @@
750754 . "\n"
751755 . Html::closeElement( 'select' );
752756 if ( isset( $params['label'] ) ) {
753 - $ret = Xml::label( $params['label'], $selectAttribs['name'] ) . '&#160;' . $ret;
 757+ $ret = Xml::label( $params['label'], $selectAttribs['id'] ) . '&#160;' . $ret;
754758 }
755759 return $ret;
756760 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r109994Fix broken unit test...krinkle03:45, 25 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r41425Cleanup for Xml::namespaceSelector():...demon14:05, 30 September 2008
r109698[Unit testing] Add unit tests for Xml::namespaceSelector...krinkle22:26, 21 January 2012
r109974[Unit testing] Re-order attribs to a-z to make testing more reliable...krinkle00:52, 25 January 2012
r109990[Xml/Html] new method Html::namespaceSelector...krinkle03:01, 25 January 2012

Comments

#Comment by Krinkle (talk | contribs)   03:51, 25 January 2012

Broke unit test: http://integration.mediawiki.org/ci/job/MediaWiki-sqlite-phpunit/6351/

[exec] There were 3 failures:
     [exec] 
     [exec] 1) ParserTests::testParserTest with data set #35 ('Definition list with wikilink containing colon', '; [[Help:FAQ]]: The least-read page on Wikipedia', '<dl><dt> <a href="https://www.mediawiki.org/index.php?title=Help:FAQ&action=edit&redlink=1" class="new" title="Help:FAQ (page does not exist)">Help:FAQ</a></dt><dd> The least-read page on Wikipedia
     [exec] </dd></dl>
     [exec] ', '', '')
     [exec] Definition list with wikilink containing colon
     [exec] Failed asserting that two strings are equal.
     [exec] --- Expected
     [exec] +++ Actual
     [exec] @@ @@
     [exec] -<dl><dt> <a href="https://www.mediawiki.org/index.php?title=Help:FAQ&action=edit&redlink=1" class="new" title="Help:FAQ (page does not exist)">Help:FAQ</a></dt><dd> The least-read page on Wikipedia
     [exec] +<dl><dt> <a href="https://www.mediawiki.org/index.php?title=:FAQ&action=edit&redlink=1" class="new" title=":FAQ (page does not exist)">Help:FAQ</a></dt><dd> The least-read page on Wikipedia
     [exec]  </dd></dl>
     [exec] 
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/includes/parser/NewParserTest.php:547
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiTestCase.php:66
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiPHPUnitCommand.php:45
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/phpunit.php:60
     [exec] 
     [exec] 2) ParserTests::testParserTest with data set #330 ('Link to category', '[[:Category:MediaWiki User\'s Guide]]', '<p><a href="https://www.mediawiki.org/wiki/Category:MediaWiki_User%27s_Guide" title="Category:MediaWiki User\'s Guide">Category:MediaWiki User\'s Guide</a>
     [exec] </p>', '', '')
     [exec] Link to category
     [exec] Failed asserting that two strings are equal.
     [exec] --- Expected
     [exec] +++ Actual
     [exec] @@ @@
     [exec] -<p><a href="https://www.mediawiki.org/wiki/Category:MediaWiki_User%27s_Guide" title="Category:MediaWiki User's Guide">Category:MediaWiki User's Guide</a>
     [exec] +<p><a href="https://www.mediawiki.org/wiki/:MediaWiki_User%27s_Guide" title=":MediaWiki User's Guide">Category:MediaWiki User's Guide</a>
     [exec]  </p>
     [exec] 
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/includes/parser/NewParserTest.php:547
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiTestCase.php:66
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiPHPUnitCommand.php:45
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/phpunit.php:60
     [exec] 
     [exec] 3) ParserTests::testParserTest with data set #331 ('Simple category', '[[Category:MediaWiki User\'s Guide]]', '<a href="https://www.mediawiki.org/wiki/Category:MediaWiki_User%27s_Guide" title="Category:MediaWiki User\'s Guide">MediaWiki User\'s Guide</a>', 'cat', '')
     [exec] Simple category
     [exec] Failed asserting that two strings are equal.
     [exec] --- Expected
     [exec] +++ Actual
     [exec] @@ @@
     [exec] -<a href="https://www.mediawiki.org/wiki/Category:MediaWiki_User%27s_Guide" title="Category:MediaWiki User's Guide">MediaWiki User's Guide</a>
     [exec] +<a href="https://www.mediawiki.org/index.php?title=MediaWiki_User%27s_Guide&action=edit&redlink=1" class="new" title="MediaWiki User's Guide (page does not exist)">MediaWiki User's Guide</a>
     [exec] 
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/includes/parser/NewParserTest.php:547
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiTestCase.php:66
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/MediaWikiPHPUnitCommand.php:45
     [exec] /var/lib/jenkins/jobs/MediaWiki-sqlite-phpunit/workspace/mw-core/tests/phpunit/phpunit.php:60
     [exec] 


Fixed in r109994.

#Comment by Aaron Schulz (talk | contribs)   04:50, 1 February 2012

On my wiki (using Win 7, PHP 5.3.0):

1) HtmlTest::testNamespaceSelector
Basic namespace selector without custom options
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 <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>

C:\wamp\www\MediaWiki\tests\phpunit\includes\HtmlTest.php:234
C:\wamp\www\MediaWiki\tests\phpunit\MediaWikiTestCase.php:70
C:\wamp\www\MediaWiki\tests\phpunit\MediaWikiPHPUnitCommand.php:45
C:\wamp\www\MediaWiki\tests\phpunit\phpunit.php:60

2) XmlTest::testNamespaceSelector
Basic namespace selector without custom options
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 <select class="namespaceselector" 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>

C:\wamp\www\MediaWiki\tests\phpunit\includes\XmlTest.php:216
C:\wamp\www\MediaWiki\tests\phpunit\MediaWikiTestCase.php:70
C:\wamp\www\MediaWiki\tests\phpunit\MediaWikiPHPUnitCommand.php:45
C:\wamp\www\MediaWiki\tests\phpunit\phpunit.php:60
#Comment by Aaron Schulz (talk | contribs)   18:42, 2 February 2012

Fixed in r110597.

Status & tagging log