Add to/tweak mediawiki and jqueryMsg JS tests, fix comment
authorMatthew Flaschen <mflaschen@wikimedia.org>
Tue, 12 Feb 2013 21:51:35 +0000 (16:51 -0500)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Wed, 3 Apr 2013 23:37:19 +0000 (19:37 -0400)
* Remove getOuterHtml.
* Use htmlEqual where getOuterHtml was used, among other places.
* The comment reflected a change I considered making, but did not in the end.
* Add a few link tests
* Minor tweaks to other test code for consistency
* Replace wikiGetlink with explicit output

Change-Id: Id6a857c9686901f8db4944fcc081d4b9c4457fa8

resources/mediawiki/mediawiki.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.test.js

index 2ceb3ea..d8a1aed 100644 (file)
@@ -134,8 +134,7 @@ var mw = ( function ( $, undefined ) {
 
        Message.prototype = {
                /**
-                * Simple message parser, does $N replacement, HTML-escaping (only for
-                * 'escaped' format), and nothing else.
+                * Simple message parser, does $N replacement and nothing else.
                 *
                 * This may be overridden to provide a more complex message parser.
                 *
index 0a9df96..697159c 100644 (file)
@@ -1,20 +1,11 @@
 ( function ( mw, $ ) {
-       var mwLanguageCache = {}, oldGetOuterHtml, formatnumTests, specialCharactersPageName,
+       var mwLanguageCache = {}, formatnumTests, specialCharactersPageName,
                expectedListUsers, expectedEntrypoints;
 
        QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
                setup: function () {
                        this.orgMwLangauge = mw.language;
                        mw.language = $.extend( true, {}, this.orgMwLangauge );
-                       oldGetOuterHtml = $.fn.getOuterHtml;
-                       $.fn.getOuterHtml = function () {
-                               var $div = $( '<div>' ), html;
-                               $div.append( $( this ).eq( 0 ).clone() );
-                               html = $div.html();
-                               $div.empty();
-                               $div = undefined;
-                               return html;
-                       };
 
                        // Messages that are reused in multiple tests
                        mw.messages.set( {
                                'external-link-replace': 'Foo [$1 bar]'
                        } );
 
+                       mw.config.set( {
+                               wgArticlePath: '/wiki/$1'
+                       } );
+
                        specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
 
-                       expectedListUsers = '注册' + $( '<a>' ).attr( {
-                               title: 'Special:ListUsers',
-                               href: mw.util.wikiGetlink( 'Special:ListUsers' )
-                       } ).text( '用户' ).getOuterHtml();
+                       expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
 
                        expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
                },
                teardown: function () {
                        mw.language = this.orgMwLangauge;
-                       $.fn.getOuterHtml = oldGetOuterHtml;
                }
        } ) );
 
                 the bold was removed because it is not yet implemented.
                 */
 
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'jquerymsg-test-statistics-users' ),
                        expectedListUsers,
                        'Piped wikilink'
                );
 
                expectedDisambiguationsText = 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from ' +
-                       $( '<a>' ).attr( {
-                               title: 'MediaWiki:Disambiguationspage',
-                               href: mw.util.wikiGetlink( 'MediaWiki:Disambiguationspage' )
-                       } ).text( 'MediaWiki:Disambiguationspage' ).getOuterHtml() + '.';
+                       '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
+
                mw.messages.set( 'disambiguations-text', 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from [[MediaWiki:Disambiguationspage]].' );
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'disambiguations-text' ),
                        expectedDisambiguationsText,
                        'Wikilink without pipe'
                );
 
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'jquerymsg-test-version-entrypoints-index-php' ),
                        expectedEntrypoints,
                        'External link'
                        'Pipe trick should return error string.'
                );
 
-               expectedMultipleBars = $( '<a>' ).attr( {
-                       title: 'Main Page',
-                       href: mw.util.wikiGetlink( 'Main Page' )
-               } ).text( 'Main|Page' ).getOuterHtml();
+               expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
                mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'multiple-bars' ),
                        expectedMultipleBars,
                        'Bar in anchor'
                );
 
-               expectedSpecialCharacters = $( '<a>' ).attr( {
-                       title: specialCharactersPageName,
-                       href: mw.util.wikiGetlink( specialCharactersPageName )
-               } ).text( specialCharactersPageName ).getOuterHtml();
+               expectedSpecialCharacters = '<a title="&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?</a>';
 
                mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'special-characters' ),
                        expectedSpecialCharacters,
                        'Special characters'
                        mw.messages.get( 'jquerymsg-test-statistics-users' ),
                        'Internal link message unchanged when format is \'text\''
                );
-               assert.equal(
+               assert.htmlEqual(
                        formatParse( 'jquerymsg-test-statistics-users' ),
                        expectedListUsers,
                        'Internal link message parsed when format is \'parse\''
                        mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
                        'External link message unchanged when format is \'text\''
                );
-               assert.equal(
+               assert.htmlEqual(
                        formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
                        expectedEntrypoints,
                        'External link message processed when format is \'parse\''
                        'Foo [http://example.com bar]',
                        'External link message only substitutes parameter when format is \'text\''
                );
-               assert.equal(
+               assert.htmlEqual(
                        formatParse( 'external-link-replace', 'http://example.com' ),
                        'Foo <a href="http://example.com">bar</a>',
                        'External link message processed when format is \'parse\''
        QUnit.test( 'Int', 4, function ( assert ) {
                var parser = mw.jqueryMsg.getMessageFunction(),
                        newarticletextSource = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the [[{{Int:Helppage}}|help page]] for more info). If you are here by mistake, click your browser\'s back button.',
-                       expectedNewarticletext;
+                       expectedNewarticletext,
+                       helpPageTitle = 'Help:Contents';
 
-               mw.messages.set( 'helppage', 'Help:Contents' );
+               mw.messages.set( 'helppage', helpPageTitle );
 
                expectedNewarticletext = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the ' +
-                       $( '<a>' ).attr( {
-                               title: mw.msg( 'helppage' ),
-                               href: mw.util.wikiGetlink( mw.msg( 'helppage' ) )
-                       } ).text( 'help page' ).getOuterHtml() + ' for more info). If you are here by mistake, click your browser\'s back button.';
+                       '<a title="Help:Contents" href="/wiki/Help:Contents">help page</a> for more info). If you are here by mistake, click your browser\'s back button.';
 
                mw.messages.set( 'newarticletext', newarticletextSource );
 
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'newarticletext' ),
                        expectedNewarticletext,
                        'Link with nested message'
                mw.messages.set( 'newarticletext-lowercase',
                        newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
 
-               assert.equal(
+               assert.htmlEqual(
                        parser( 'newarticletext-lowercase' ),
                        expectedNewarticletext,
                        'Link with nested message, lowercase include'
index e8663f8..7ae9826 100644 (file)
                                'gender-plural-msg': '{{GENDER:$1|he|she|they}} {{PLURAL:$2|is|are}} awesome',
                                'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
                                'formatnum-msg': '{{formatnum:$1}}',
-                               'int-msg': 'Some {{int:other-message}}'
+                               'int-msg': 'Some {{int:other-message}}',
+                               'mediawiki-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
+                               'external-link-replace': 'Foo [$1 bar]'
                        } );
 
-                       // For formatnum tests
-                       mw.config.set( 'wgUserLanguage', 'en' );
+                       mw.config.set( {
+                               wgArticlePath: '/wiki/$1',
+
+                               // For formatnum tests
+                               wgUserLanguage: 'en'
+                       } );
 
                        specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
                }
                assert.ok( mw.config instanceof mw.Map, 'mw.config instance of mw.Map' );
        } );
 
-       QUnit.test( 'mw.message & mw.messages', 54, function ( assert ) {
+       QUnit.test( 'mw.message & mw.messages', 68, function ( assert ) {
                var goodbye, hello;
 
                // Convenience method for asserting the same result for multiple formats
                assert.equal( hello.escaped(), 'Hello &lt;b&gt;awesome&lt;/b&gt; world', 'Message.escaped returns the escaped message' );
                assert.equal( hello.format, 'escaped', 'Message.escaped correctly updated the "format" property' );
 
-               assert.ok( mw.messages.set( 'escaped-with-curly-brace', '"{{SITENAME}}" is the home of {{int:other-message}}' ) );
-               assert.equal( mw.message( 'escaped-with-curly-brace' ).escaped(), mw.html.escape( '"' + mw.config.get( 'wgSiteName' ) + '" is the home of Other Message' ), 'Escaped format works correctly for curly brace message' );
+               assert.ok( mw.messages.set( 'multiple-curly-brace', '"{{SITENAME}}" is the home of {{int:other-message}}' ), 'mw.messages.set: Register' );
+               assertMultipleFormats( ['multiple-curly-brace'], ['text', 'parse'], '"' + mw.config.get( 'wgSiteName') + '" is the home of Other Message', 'Curly brace format works correctly' );
+               assert.equal( mw.message( 'multiple-curly-brace' ).plain(), mw.messages.get( 'multiple-curly-brace' ), 'Plain format works correctly for curly brace message' );
+               assert.equal( mw.message( 'multiple-curly-brace' ).escaped(), mw.html.escape( '"' + mw.config.get( 'wgSiteName') + '" is the home of Other Message' ), 'Escaped format works correctly for curly brace message' );
+
+               assert.ok( mw.messages.set( 'multiple-square-brackets-and-ampersand', 'Visit the [[Project:Community portal|community portal]] & [[Project:Help desk|help desk]]' ), 'mw.messages.set: Register' );
+               assertMultipleFormats( ['multiple-square-brackets-and-ampersand'], ['plain', 'text'], mw.messages.get( 'multiple-square-brackets-and-ampersand' ), 'Square bracket message is not processed' );
+               assert.equal( mw.message( 'multiple-square-brackets-and-ampersand' ).escaped(), 'Visit the [[Project:Community portal|community portal]] &amp; [[Project:Help desk|help desk]]', 'Escaped format works correctly for square bracket message' );
+               assert.htmlEqual( mw.message( 'multiple-square-brackets-and-ampersand' ).parse(), 'Visit the ' +
+                       '<a title="Project:Community portal" href="/wiki/Project:Community_portal">community portal</a>' +
+                       ' &amp; <a title="Project:Help desk" href="/wiki/Project:Help_desk">help desk</a>', 'Internal links work with parse' );
+
+               assertMultipleFormats( ['mediawiki-test-version-entrypoints-index-php'], ['plain', 'text', 'escaped'], mw.messages.get( 'mediawiki-test-version-entrypoints-index-php' ), 'External link markup is unprocessed' );
+               assert.htmlEqual( mw.message( 'mediawiki-test-version-entrypoints-index-php' ).parse(), '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>', 'External link works correctly in parse mode' );
 
-               assert.ok( mw.messages.set( 'escaped-with-square-brackets', 'Visit the [[Project:Community portal|community portal]] & [[Project:Help desk|help desk]]' ) );
-               assert.equal( mw.message( 'escaped-with-square-brackets' ).escaped(), 'Visit the [[Project:Community portal|community portal]] &amp; [[Project:Help desk|help desk]]', 'Escaped format works correctly for square bracket message' );
+               assertMultipleFormats( ['external-link-replace', 'http://example.org/?x=y&z'], ['plain', 'text'] , 'Foo [http://example.org/?x=y&z bar]', 'Parameters are substituted but external link is not processed' );
+               assert.equal( mw.message( 'external-link-replace', 'http://example.org/?x=y&z' ).escaped(), 'Foo [http://example.org/?x=y&amp;z bar]', 'In escaped mode, parameters are substituted and ampersand is escaped, but external link is not processed' );
+               assert.htmlEqual( mw.message( 'external-link-replace', 'http://example.org/?x=y&z' ).parse(), 'Foo <a href="http://example.org/?x=y&amp;z">bar</a>', 'External link with replacement works in parse mode without double-escaping' );
 
                hello.parse();
                assert.equal( hello.format, 'parse', 'Message.parse correctly updated the "format" property' );
                assert.ok( mw.messages.set( 'mediawiki-test-categorytree-collapse-bullet', '[<b>−</b>]' ), 'mw.messages.set: Register' );
                assert.equal( mw.message( 'mediawiki-test-categorytree-collapse-bullet' ).plain(), mw.messages.get( 'mediawiki-test-categorytree-collapse-bullet' ), 'Single square brackets unchanged in plain mode' );
 
-               assert.ok( mw.messages.set( 'mediawiki-test-wikieditor-toolbar-help-content-signature-result', '<a href=\'#\' title=\'{{#special:mypage}}\'>Username</a> (<a href=\'#\' title=\'{{#special:mytalk}}\'>talk</a>)' ) );
+               assert.ok( mw.messages.set( 'mediawiki-test-wikieditor-toolbar-help-content-signature-result', '<a href=\'#\' title=\'{{#special:mypage}}\'>Username</a> (<a href=\'#\' title=\'{{#special:mytalk}}\'>talk</a>)' ), 'mw.messages.set: Register' );
                assert.equal( mw.message( 'mediawiki-test-wikieditor-toolbar-help-content-signature-result' ).plain(), mw.messages.get( 'mediawiki-test-wikieditor-toolbar-help-content-signature-result' ), 'HTML message with curly braces is not changed in plain mode' );
 
                assertMultipleFormats( ['gender-plural-msg', 'male', 1], ['text', 'parse', 'escaped'], 'he is awesome', 'Gender and plural are resolved' );
                assert.equal( mw.msg( 'hello' ), 'Hello <b>awesome</b> world', 'Gets message with default options (existing message)' );
                assert.equal( mw.msg( 'goodbye' ), '<goodbye>', 'Gets message with default options (nonexistent message)' );
 
-               assert.ok( mw.messages.set( 'plural-item', 'Found $1 {{PLURAL:$1|item|items}}' ) );
+               assert.ok( mw.messages.set( 'plural-item' , 'Found $1 {{PLURAL:$1|item|items}}' ), 'mw.messages.set: Register' );
                assert.equal( mw.msg( 'plural-item', 5 ), 'Found 5 items', 'Apply plural for count 5' );
                assert.equal( mw.msg( 'plural-item', 0 ), 'Found 0 items', 'Apply plural for count 0' );
                assert.equal( mw.msg( 'plural-item', 1 ), 'Found 1 item', 'Apply plural for count 1' );