From 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sun, 8 Dec 2013 09:55:49 +0100 Subject: Update to MediaWiki 1.22.0 --- .../resources/mediawiki/mediawiki.Title.test.js | 268 ++++++++++++++-- .../mediawiki/mediawiki.jqueryMsg.test.js | 337 ++++++++++++++------- .../suites/resources/mediawiki/mediawiki.test.js | 171 ++++++++++- .../resources/mediawiki/mediawiki.user.test.js | 6 +- .../resources/mediawiki/mediawiki.util.test.js | 91 ++++-- 5 files changed, 706 insertions(+), 167 deletions(-) (limited to 'tests/qunit/suites/resources/mediawiki') diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js index 30a31ef7..ab96f753 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js @@ -1,4 +1,4 @@ -( function ( mw ) { +( function ( mw, $ ) { // mw.Title relies on these three config vars // Restore them after each test run var config = { @@ -53,49 +53,184 @@ antarctic_waterfowl: 100 }, wgCaseSensitiveNamespaces: [] + }, + repeat = function ( input, multiplier ) { + return new Array( multiplier + 1 ).join( input ); + }, + cases = { + // See also TitleTest.php#testSecureAndSplit + valid: [ + 'Sandbox', + 'A "B"', + 'A \'B\'', + '.com', + '~', + '"', + '\'', + 'Talk:Sandbox', + 'Talk:Foo:Sandbox', + 'File:Example.svg', + 'File_talk:Example.svg', + 'Foo/.../Sandbox', + 'Sandbox/...', + 'A~~', + // Length is 256 total, but only title part matters + 'Category:' + repeat( 'x', 248 ), + repeat( 'x', 252 ) + ], + invalid: [ + '', + '__ __', + ' __ ', + // Bad characters forbidden regardless of wgLegalTitleChars + 'A [ B', + 'A ] B', + 'A { B', + 'A } B', + 'A < B', + 'A > B', + 'A | B', + // URL encoding + 'A%20B', + 'A%23B', + 'A%2523B', + // XML/HTML character entity references + // Note: The ones with # are commented out as those are interpreted as fragment and + // as such end up being valid. + 'A é B', + //'A é B', + //'A é B', + // Subject of NS_TALK does not roundtrip to NS_MAIN + 'Talk:File:Example.svg', + // Directory navigation + '.', + '..', + './Sandbox', + '../Sandbox', + 'Foo/./Sandbox', + 'Foo/../Sandbox', + 'Sandbox/.', + 'Sandbox/..', + // Tilde + 'A ~~~ Name', + 'A ~~~~ Signature', + 'A ~~~~~ Timestamp', + repeat( 'x', 256 ), + // Extension separation is a js invention, for length + // purposes it is part of the title + repeat( 'x', 252 ) + '.json', + // Namespace prefix without actual title + // ':', // bug 54044 + 'Talk:', + 'Category: ', + 'Category: #bar' + ] }; QUnit.module( 'mediawiki.Title', QUnit.newMwEnvironment( { config: config } ) ); - QUnit.test( 'Transformation', 8, function ( assert ) { + QUnit.test( 'constructor', cases.invalid.length, function ( assert ) { + var i, title; + for ( i = 0; i < cases.valid.length; i++ ) { + title = new mw.Title( cases.valid[i] ); + } + for ( i = 0; i < cases.invalid.length; i++ ) { + /*jshint loopfunc:true */ + title = cases.invalid[i]; + assert.throws( function () { + return new mw.Title( title ); + }, cases.invalid[i] ); + } + } ); + + QUnit.test( 'newFromText', cases.valid.length + cases.invalid.length, function ( assert ) { + var i; + for ( i = 0; i < cases.valid.length; i++ ) { + assert.equal( + $.type( mw.Title.newFromText( cases.valid[i] ) ), + 'object', + cases.valid[i] + ); + } + for ( i = 0; i < cases.invalid.length; i++ ) { + assert.equal( + $.type( mw.Title.newFromText( cases.invalid[i] ) ), + 'null', + cases.invalid[i] + ); + } + } ); + + QUnit.test( 'Basic parsing', 12, function ( assert ) { + var title; + title = new mw.Title( 'File:Foo_bar.JPG' ); + + assert.equal( title.getNamespaceId(), 6 ); + assert.equal( title.getNamespacePrefix(), 'File:' ); + assert.equal( title.getName(), 'Foo_bar' ); + assert.equal( title.getNameText(), 'Foo bar' ); + assert.equal( title.getExtension(), 'JPG' ); + assert.equal( title.getDotExtension(), '.JPG' ); + assert.equal( title.getMain(), 'Foo_bar.JPG' ); + assert.equal( title.getMainText(), 'Foo bar.JPG' ); + assert.equal( title.getPrefixedDb(), 'File:Foo_bar.JPG' ); + assert.equal( title.getPrefixedText(), 'File:Foo bar.JPG' ); + + title = new mw.Title( 'Foo#bar' ); + assert.equal( title.getPrefixedText(), 'Foo' ); + assert.equal( title.getFragment(), 'bar' ); + } ); + + QUnit.test( 'Transformation', 11, function ( assert ) { var title; title = new mw.Title( 'File:quux pif.jpg' ); - assert.equal( title.getName(), 'Quux_pif' ); + assert.equal( title.getNameText(), 'Quux pif', 'First character of title' ); title = new mw.Title( 'File:Glarg_foo_glang.jpg' ); - assert.equal( title.getNameText(), 'Glarg foo glang' ); + assert.equal( title.getNameText(), 'Glarg foo glang', 'Underscores' ); title = new mw.Title( 'User:ABC.DEF' ); - assert.equal( title.toText(), 'User:ABC.DEF' ); - assert.equal( title.getNamespaceId(), 2 ); - assert.equal( title.getNamespacePrefix(), 'User:' ); + assert.equal( title.toText(), 'User:ABC.DEF', 'Round trip text' ); + assert.equal( title.getNamespaceId(), 2, 'Parse canonical namespace prefix' ); + + title = new mw.Title( 'Image:quux pix.jpg' ); + assert.equal( title.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' ); title = new mw.Title( 'uSEr:hAshAr' ); assert.equal( title.toText(), 'User:HAshAr' ); - assert.equal( title.getNamespaceId(), 2 ); + assert.equal( title.getNamespaceId(), 2, 'Case-insensitive namespace prefix' ); - title = new mw.Title( ' MediaWiki: Foo bar .js ' ); - // Don't ask why, it's the way the backend works. One space is kept of each set - assert.equal( title.getName(), 'Foo_bar_.js', 'Merge multiple spaces to a single space.' ); - } ); + // Don't ask why, it's the way the backend works. One space is kept of each set. + title = new mw.Title( 'Foo __ \t __ bar' ); + assert.equal( title.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' ); - QUnit.test( 'Main text for filename', 8, function ( assert ) { - var title = new mw.Title( 'File:foo_bar.JPG' ); + // Regression test: Previously it would only detect an extension if there is no space after it + title = new mw.Title( 'Example.js ' ); + assert.equal( title.getExtension(), 'js', 'Space after an extension is stripped' ); - assert.equal( title.getNamespaceId(), 6 ); - assert.equal( title.getNamespacePrefix(), 'File:' ); - assert.equal( title.getName(), 'Foo_bar' ); - assert.equal( title.getNameText(), 'Foo bar' ); - assert.equal( title.getMain(), 'Foo_bar.JPG' ); - assert.equal( title.getMainText(), 'Foo bar.JPG' ); - assert.equal( title.getExtension(), 'JPG' ); - assert.equal( title.getDotExtension(), '.JPG' ); + title = new mw.Title( 'Example#foo' ); + assert.equal( title.getFragment(), 'foo', 'Fragment' ); + + title = new mw.Title( 'Example#_foo_bar baz_' ); + assert.equal( title.getFragment(), ' foo bar baz', 'Fragment' ); } ); - QUnit.test( 'Namespace detection and conversion', 6, function ( assert ) { + QUnit.test( 'Namespace detection and conversion', 10, function ( assert ) { var title; + title = new mw.Title( 'File:User:Example' ); + assert.equal( title.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' ); + + title = new mw.Title( 'Example', 6 ); + assert.equal( title.getNamespaceId(), 6, 'Default namespace passed is used' ); + + title = new mw.Title( 'User:Example', 6 ); + assert.equal( title.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' ); + + title = new mw.Title( ':Example', 6 ); + assert.equal( title.getNamespaceId(), 0, 'Colon forces main namespace' ); + title = new mw.Title( 'something.PDF', 6 ); assert.equal( title.toString(), 'File:Something.PDF' ); @@ -189,10 +324,93 @@ mw.config.set( 'wgArticlePath', '/wiki/$1' ); title = new mw.Title( 'Foobar' ); - assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionally, toString passing to wikiGetlink' ); + assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionally, getUrl uses mw.util.getUrl' ); title = new mw.Title( 'John Doe', 3 ); assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' ); } ); -}( mediaWiki ) ); + QUnit.test( 'newFromImg', 28, function ( assert ) { + var title, i, thisCase, prefix, + cases = [ + { + url: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg', + typeOfUrl: 'Normal hashed directory thumbnail', + nameText: 'Anticlockwise heliotrope\'s', + prefixedText: 'File:Anticlockwise heliotrope\'s.jpg' + }, + + { + url: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png', + typeOfUrl: 'Commons thumbnail', + nameText: 'Wikipedia-logo-v2', + prefixedText: 'File:Wikipedia-logo-v2.svg' + }, + + { + url: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg', + typeOfUrl: 'Full image', + nameText: 'Anticlockwise heliotrope\'s', + prefixedText: 'File:Anticlockwise heliotrope\'s.jpg' + }, + + { + url: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180', + typeOfUrl: 'thumb.php-based thumbnail', + nameText: 'Stuffless Figaro\'s', + prefixedText: 'File:Stuffless Figaro\'s.jpg' + }, + + { + url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png', + typeOfUrl: 'Commons unhashed thumbnail', + nameText: 'Wikipedia-logo-v2', + prefixedText: 'File:Wikipedia-logo-v2.svg' + }, + + { + url: '/wiki/images/Anticlockwise_heliotrope%27s.jpg', + typeOfUrl: 'Unhashed local file', + nameText: 'Anticlockwise heliotrope\'s', + prefixedText: 'File:Anticlockwise heliotrope\'s.jpg' + }, + + { + url: '', + typeOfUrl: 'Empty string' + }, + + { + url: 'foo', + typeOfUrl: 'String with only alphabet characters' + }, + + { + url: 'foobar.foobar', + typeOfUrl: 'Not a file path' + }, + + { + url: '/a/a0/blah blah blah', + typeOfUrl: 'Space characters' + } + ]; + + for ( i = 0; i < cases.length; i++ ) { + thisCase = cases[i]; + title = mw.Title.newFromImg( { src: thisCase.url } ); + + if ( thisCase.nameText !== undefined ) { + prefix = '[' + thisCase.typeOfUrl + ' URL' + '] '; + + assert.notStrictEqual( title, null, prefix + 'Parses successfully' ); + assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' ); + assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' ); + assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' ); + } else { + assert.strictEqual( title, null, thisCase.typeOfUrl + ', should not produce an mw.Title object' ); + } + } + } ); + +}( mediaWiki, jQuery ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index 0a9df966..be362e22 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@ -1,20 +1,17 @@ ( function ( mw, $ ) { - var mwLanguageCache = {}, oldGetOuterHtml, formatnumTests, specialCharactersPageName, + var mwLanguageCache = {}, formatText, formatParse, formatnumTests, specialCharactersPageName, expectedListUsers, expectedEntrypoints; + // When the expected result is the same in both modes + function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) { + assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' ); + assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' ); + } + 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 = $( '
' ), 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( { @@ -39,18 +36,26 @@ 'external-link-replace': 'Foo [$1 bar]' } ); + mw.config.set( { + wgArticlePath: '/wiki/$1' + } ); + specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?'; - expectedListUsers = '注册' + $( '' ).attr( { - title: 'Special:ListUsers', - href: mw.util.wikiGetlink( 'Special:ListUsers' ) - } ).text( '用户' ).getOuterHtml(); + expectedListUsers = '注册用户'; expectedEntrypoints = 'index.php'; + + formatText = mw.jqueryMsg.getMessageFunction( { + format: 'text' + } ); + + formatParse = mw.jqueryMsg.getMessageFunction( { + format: 'parse' + } ); }, teardown: function () { mw.language = this.orgMwLangauge; - $.fn.getOuterHtml = oldGetOuterHtml; } } ) ); @@ -82,18 +87,16 @@ } QUnit.test( 'Replace', 9, function ( assert ) { - var parser = mw.jqueryMsg.getMessageFunction(); - mw.messages.set( 'simple', 'Foo $1 baz $2' ); - assert.equal( parser( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' ); - assert.equal( parser( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' ); - assert.equal( parser( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' ); + assert.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' ); + assert.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' ); + assert.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' ); mw.messages.set( 'plain-input', 'x$1y<z' ); assert.equal( - parser( 'plain-input', 'bar' ), + formatParse( 'plain-input', 'bar' ), '<foo foo="foo">xbary&lt;</foo>z', 'Input is not considered html' ); @@ -101,7 +104,7 @@ mw.messages.set( 'plain-replace', 'Foo $1' ); assert.equal( - parser( 'plain-replace', '>' ), + formatParse( 'plain-replace', '>' ), 'Foo <bar bar="bar">&gt;</bar>', 'Replacement is not considered html' ); @@ -109,71 +112,68 @@ mw.messages.set( 'object-replace', 'Foo $1' ); assert.equal( - parser( 'object-replace', $( '
>
' ) ), + formatParse( 'object-replace', $( '
>
' ) ), 'Foo
>
', 'jQuery objects are preserved as raw html' ); assert.equal( - parser( 'object-replace', $( '
>
' ).get( 0 ) ), + formatParse( 'object-replace', $( '
>
' ).get( 0 ) ), 'Foo
>
', 'HTMLElement objects are preserved as raw html' ); assert.equal( - parser( 'object-replace', $( '
>
' ).toArray() ), + formatParse( 'object-replace', $( '
>
' ).toArray() ), 'Foo
>
', 'HTMLElement[] arrays are preserved as raw html' ); assert.equal( - parser( 'external-link-replace', 'http://example.org/?x=y&z' ), + formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ), 'Foo bar', 'Href is not double-escaped in wikilink function' ); } ); QUnit.test( 'Plural', 3, function ( assert ) { - var parser = mw.jqueryMsg.getMessageFunction(); - - assert.equal( parser( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' ); - assert.equal( parser( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' ); - assert.equal( parser( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' ); + assert.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' ); + assert.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' ); + assert.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' ); } ); QUnit.test( 'Gender', 11, function ( assert ) { // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian - var user = mw.user, - parser = mw.jqueryMsg.getMessageFunction(); + var user = mw.user; user.options.set( 'gender', 'male' ); assert.equal( - parser( 'gender-msg', 'Bob', 'male' ), + formatParse( 'gender-msg', 'Bob', 'male' ), 'Bob: blue', 'Masculine from string "male"' ); assert.equal( - parser( 'gender-msg', 'Bob', user ), + formatParse( 'gender-msg', 'Bob', user ), 'Bob: blue', 'Masculine from mw.user object' ); user.options.set( 'gender', 'unknown' ); assert.equal( - parser( 'gender-msg', 'Foo', user ), + formatParse( 'gender-msg', 'Foo', user ), 'Foo: green', 'Neutral from mw.user object' ); assert.equal( - parser( 'gender-msg', 'Alice', 'female' ), + formatParse( 'gender-msg', 'Alice', 'female' ), 'Alice: pink', 'Feminine from string "female"' ); assert.equal( - parser( 'gender-msg', 'User' ), + formatParse( 'gender-msg', 'User' ), 'User: green', 'Neutral when no parameter given' ); assert.equal( - parser( 'gender-msg', 'User', 'unknown' ), + formatParse( 'gender-msg', 'User', 'unknown' ), 'User: green', 'Neutral from string "unknown"' ); @@ -181,43 +181,41 @@ mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' ); assert.equal( - parser( 'gender-msg-one-form', 'male', 10 ), + formatParse( 'gender-msg-one-form', 'male', 10 ), 'User: 10 edits', 'Gender neutral and plural form' ); assert.equal( - parser( 'gender-msg-one-form', 'female', 1 ), + formatParse( 'gender-msg-one-form', 'female', 1 ), 'User: 1 edit', 'Gender neutral and singular form' ); mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' ); assert.equal( - parser( 'gender-msg-lowercase', 'male' ), + formatParse( 'gender-msg-lowercase', 'male' ), 'he is awesome', 'Gender masculine' ); assert.equal( - parser( 'gender-msg-lowercase', 'female' ), + formatParse( 'gender-msg-lowercase', 'female' ), 'she is awesome', 'Gender feminine' ); mw.messages.set( 'gender-msg-wrong', '{{gender}} test' ); assert.equal( - parser( 'gender-msg-wrong', 'female' ), + formatParse( 'gender-msg-wrong', 'female' ), ' test', 'Invalid syntax should result in {{gender}} simply being stripped away' ); } ); QUnit.test( 'Grammar', 2, function ( assert ) { - var parser = mw.jqueryMsg.getMessageFunction(); - - assert.equal( parser( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' ); + assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' ); mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' ); - assert.equal( parser( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' ); + assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' ); } ); QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) { @@ -242,8 +240,7 @@ } ); QUnit.test( 'Links', 6, function ( assert ) { - var parser = mw.jqueryMsg.getMessageFunction(), - expectedDisambiguationsText, + var expectedDisambiguationsText, expectedMultipleBars, expectedSpecialCharacters; @@ -252,26 +249,24 @@ the bold was removed because it is not yet implemented. */ - assert.equal( - parser( 'jquerymsg-test-statistics-users' ), + assert.htmlEqual( + formatParse( '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 ' + - $( '' ).attr( { - title: 'MediaWiki:Disambiguationspage', - href: mw.util.wikiGetlink( 'MediaWiki:Disambiguationspage' ) - } ).text( 'MediaWiki:Disambiguationspage' ).getOuterHtml() + '.'; + 'MediaWiki:Disambiguationspage.'; + 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( - parser( 'disambiguations-text' ), + assert.htmlEqual( + formatParse( 'disambiguations-text' ), expectedDisambiguationsText, 'Wikilink without pipe' ); - assert.equal( - parser( 'jquerymsg-test-version-entrypoints-index-php' ), + assert.htmlEqual( + formatParse( 'jquerymsg-test-version-entrypoints-index-php' ), expectedEntrypoints, 'External link' ); @@ -279,30 +274,24 @@ // Pipe trick is not supported currently, but should not parse as text either. mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' ); assert.equal( - parser( 'pipe-trick' ), + formatParse( 'pipe-trick' ), 'pipe-trick: Parse error at position 0 in input: [[Tampa, Florida|]]', 'Pipe trick should return error string.' ); - expectedMultipleBars = $( '' ).attr( { - title: 'Main Page', - href: mw.util.wikiGetlink( 'Main Page' ) - } ).text( 'Main|Page' ).getOuterHtml(); + expectedMultipleBars = 'Main|Page'; mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' ); - assert.equal( - parser( 'multiple-bars' ), + assert.htmlEqual( + formatParse( 'multiple-bars' ), expectedMultipleBars, 'Bar in anchor' ); - expectedSpecialCharacters = $( '' ).attr( { - title: specialCharactersPageName, - href: mw.util.wikiGetlink( specialCharactersPageName ) - } ).text( specialCharactersPageName ).getOuterHtml(); + expectedSpecialCharacters = '"Who" wants to be a millionaire & live on 'Exotic Island'?'; mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' ); - assert.equal( - parser( 'special-characters' ), + assert.htmlEqual( + formatParse( 'special-characters' ), expectedSpecialCharacters, 'Special characters' ); @@ -310,32 +299,16 @@ // Tests that {{-transformation vs. general parsing are done as requested QUnit.test( 'Curly brace transformation', 14, function ( assert ) { - var formatText, formatParse, oldUserLang; - - oldUserLang = mw.config.get( 'wgUserLanguage' ); - - formatText = mw.jqueryMsg.getMessageFunction( { - format: 'text' - } ); - - formatParse = mw.jqueryMsg.getMessageFunction( { - format: 'parse' - } ); - - // When the expected result is the same in both modes - function assertBothModes( parserArguments, expectedResult, assertMessage ) { - assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' ); - assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' ); - } + var oldUserLang = mw.config.get( 'wgUserLanguage' ); - assertBothModes( ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' ); + assertBothModes( assert, ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' ); - assertBothModes( ['plural-msg', 5], 'Found 5 items', 'plural is resolved' ); + assertBothModes( assert, ['plural-msg', 5], 'Found 5 items', 'plural is resolved' ); - assertBothModes( ['grammar-msg'], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' ); + assertBothModes( assert, ['grammar-msg'], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' ); mw.config.set( 'wgUserLanguage', 'en' ); - assertBothModes( ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' ); + assertBothModes( assert, ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' ); // Test non-{{ wikitext, where behavior differs @@ -345,7 +318,7 @@ 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\'' @@ -357,7 +330,7 @@ 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\'' @@ -369,7 +342,7 @@ '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 bar', 'External link message processed when format is \'parse\'' @@ -379,28 +352,25 @@ } ); 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; + var 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, + 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 ' + - $( '' ).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.'; + 'help page for more info). If you are here by mistake, click your browser\'s back button.'; mw.messages.set( 'newarticletext', newarticletextSource ); - assert.equal( - parser( 'newarticletext' ), + assert.htmlEqual( + formatParse( 'newarticletext' ), expectedNewarticletext, 'Link with nested message' ); assert.equal( - parser( 'see-portal-url' ), + formatParse( 'see-portal-url' ), 'Project:Community portal is an important community page.', 'Nested message' ); @@ -408,8 +378,8 @@ mw.messages.set( 'newarticletext-lowercase', newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) ); - assert.equal( - parser( 'newarticletext-lowercase' ), + assert.htmlEqual( + formatParse( 'newarticletext-lowercase' ), expectedNewarticletext, 'Link with nested message, lowercase include' ); @@ -417,7 +387,7 @@ mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' ); assert.equal( - parser( 'uses-missing-int' ), + formatParse( 'uses-missing-int' ), '[doesnt-exist]', 'int: where nested message does not exist' ); @@ -596,4 +566,149 @@ QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) { } ); } ); +// HTML in wikitext +QUnit.test( 'HTML', 26, function ( assert ) { + mw.messages.set( 'jquerymsg-italics-msg', 'Very important' ); + + assertBothModes( assert, ['jquerymsg-italics-msg'], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' ); + + mw.messages.set( 'jquerymsg-bold-msg', 'Strong speaker' ); + assertBothModes( assert, ['jquerymsg-bold-msg'], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' ); + + mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is key' ); + assertBothModes( assert, ['jquerymsg-bold-italics-msg'], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' ); + + mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is vital' ); + assertBothModes( assert, ['jquerymsg-italics-bold-msg'], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' ); + + mw.messages.set( 'jquerymsg-italics-with-link', 'An italicized [[link|wiki-link]]' ); + + assert.htmlEqual( + formatParse( 'jquerymsg-italics-with-link' ), + 'An italicized wiki-link', + 'Italics with link inside in parse mode' + ); + + assert.equal( + formatText( 'jquerymsg-italics-with-link' ), + mw.messages.get( 'jquerymsg-italics-with-link' ), + 'Italics with link unchanged in text mode' + ); + + mw.messages.set( 'jquerymsg-italics-id-class', 'Foo' ); + assert.htmlEqual( + formatParse( 'jquerymsg-italics-id-class' ), + mw.messages.get( 'jquerymsg-italics-id-class' ), + 'ID and class are allowed' + ); + + mw.messages.set( 'jquerymsg-italics-onclick', 'Foo' ); + assert.htmlEqual( + formatParse( 'jquerymsg-italics-onclick' ), + '<i onclick="alert(\'foo\')">Foo</i>', + 'element with onclick is escaped because it is not allowed' + ); + + mw.messages.set( 'jquerymsg-script-msg', '' ); + assert.htmlEqual( + formatParse( 'jquerymsg-script-msg' ), + '<script >alert( "Who put this tag here?" );</script>', + 'Tag outside whitelist escaped in parse mode' + ); + + assert.equal( + formatText( 'jquerymsg-script-msg' ), + mw.messages.get( 'jquerymsg-script-msg' ), + 'Tag outside whitelist unchanged in text mode' + ); + + mw.messages.set( 'jquerymsg-script-link-msg', '' ); + assert.htmlEqual( + formatParse( 'jquerymsg-script-link-msg' ), + '<script>bar</script>', + 'Script tag text is escaped because that element is not allowed, but link inside is still HTML' + ); + + mw.messages.set( 'jquerymsg-mismatched-html', 'test' ); + assert.htmlEqual( + formatParse( 'jquerymsg-mismatched-html' ), + '<i class="important">test</b>', + 'Mismatched HTML start and end tag treated as text' + ); + + // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real + // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works. + mw.messages.set( 'jquerymsg-script-and-external-link', ' [http://example.com Foo bar]' ); + assert.htmlEqual( + formatParse( 'jquerymsg-script-and-external-link' ), + '<script>alert( "jquerymsg-script-and-external-link test" );</script> Foo bar', + 'HTML tags in external links not interfering with escaping of other tags' + ); + + mw.messages.set( 'jquerymsg-link-script', '[http://example.com ]' ); + assert.htmlEqual( + formatParse( 'jquerymsg-link-script' ), + '<script>alert( "jquerymsg-link-script test" );</script>', + 'Non-whitelisted HTML tag in external link anchor treated as text' + ); + + // Intentionally not using htmlEqual for the quote tests + mw.messages.set( 'jquerymsg-double-quotes-preserved', 'Double' ); + assert.equal( + formatParse( 'jquerymsg-double-quotes-preserved' ), + mw.messages.get( 'jquerymsg-double-quotes-preserved' ), + 'Attributes with double quotes are preserved as such' + ); + + mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', 'Single' ); + assert.equal( + formatParse( 'jquerymsg-single-quotes-normalized-to-double' ), + 'Single', + 'Attributes with single quotes are normalized to double' + ); + + mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', 'Styled' ); + assert.htmlEqual( + formatParse( 'jquerymsg-escaped-double-quotes-attribute' ), + mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ), + 'Escaped attributes are parsed correctly' + ); + + mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', 'Styled' ); + assert.htmlEqual( + formatParse( 'jquerymsg-escaped-single-quotes-attribute' ), + mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ), + 'Escaped attributes are parsed correctly' + ); + + + mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '[http://example.com Example]' ); + assert.htmlEqual( + formatParse( 'jquerymsg-wikitext-contents-parsed' ), + 'Example', + 'Contents of valid tag are treated as wikitext, so external link is parsed' + ); + + mw.messages.set( 'jquerymsg-wikitext-contents-script', '' ); + assert.htmlEqual( + formatParse( 'jquerymsg-wikitext-contents-script' ), + '<script>Script inside</script>', + 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text' + ); + + mw.messages.set( 'jquerymsg-unclosed-tag', 'Foobar' ); + assert.htmlEqual( + formatParse( 'jquerymsg-unclosed-tag' ), + 'Foo<tag>bar', + 'Nonsupported unclosed tags are escaped' + ); + + mw.messages.set( 'jquerymsg-self-closing-tag', 'Foobar' ); + assert.htmlEqual( + formatParse( 'jquerymsg-self-closing-tag' ), + 'Foo<tag/>bar', + 'Self-closing tags don\'t cause a parse error' + ); +} ); + }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index 01e78f61..bd4d1d21 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -15,11 +15,17 @@ '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\'?'; } @@ -124,7 +130,7 @@ 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', 100, function ( assert ) { var goodbye, hello; // Convenience method for asserting the same result for multiple formats @@ -158,11 +164,24 @@ assert.equal( hello.escaped(), 'Hello <b>awesome</b> 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]] & [[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 ' + + 'community portal' + + ' & help desk', 'Internal links work with parse' ); - 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]] & [[Project:Help desk|help desk]]', 'Escaped format works correctly for square bracket message' ); + 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(), 'index.php', 'External link works correctly in parse mode' ); + + 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&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 bar', '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' ); @@ -186,6 +205,16 @@ assertMultipleFormats( ['plural-test-msg', 6], ['text', 'parse', 'escaped'], 'There are 6 results', 'plural get resolved' ); assert.equal( mw.message( 'plural-test-msg', 6 ).plain(), 'There {{PLURAL:6|is|are}} 6 {{PLURAL:6|result|results}}', 'Parameter is substituted but plural is not resolved in plain' ); + assert.ok( mw.messages.set( 'plural-test-msg-explicit', 'There {{plural:$1|is one car|are $1 cars|0=are no cars|12=are a dozen cars}}' ), 'mw.messages.set: Register message with explicit plural forms' ); + assertMultipleFormats( ['plural-test-msg-explicit', 12], ['text', 'parse', 'escaped'], 'There are a dozen cars', 'explicit plural get resolved' ); + + assert.ok( mw.messages.set( 'plural-test-msg-explicit-beginning', 'Basket has {{plural:$1|0=no eggs|12=a dozen eggs|6=half a dozen eggs|one egg|$1 eggs}}' ), 'mw.messages.set: Register message with explicit plural forms' ); + assertMultipleFormats( ['plural-test-msg-explicit-beginning', 1], ['text', 'parse', 'escaped'], 'Basket has one egg', 'explicit plural given at beginning get resolved for singular' ); + assertMultipleFormats( ['plural-test-msg-explicit-beginning', 4], ['text', 'parse', 'escaped'], 'Basket has 4 eggs', 'explicit plural given at beginning get resolved for plural' ); + assertMultipleFormats( ['plural-test-msg-explicit-beginning', 6], ['text', 'parse', 'escaped'], 'Basket has half a dozen eggs', 'explicit plural given at beginning get resolved for 6' ); + assertMultipleFormats( ['plural-test-msg-explicit-beginning', 0], ['text', 'parse', 'escaped'], 'Basket has no eggs', 'explicit plural given at beginning get resolved for 0' ); + + assertMultipleFormats( ['mediawiki-test-pagetriage-del-talk-page-notify-summary'], ['plain', 'text'], mw.messages.get( 'mediawiki-test-pagetriage-del-talk-page-notify-summary' ), 'Double square brackets with no parameters unchanged' ); assertMultipleFormats( ['mediawiki-test-pagetriage-del-talk-page-notify-summary', specialCharactersPageName], ['plain', 'text'], 'Notifying author of deletion nomination for [[' + specialCharactersPageName + ']]', 'Double square brackets with one parameter' ); @@ -196,7 +225,7 @@ assert.ok( mw.messages.set( 'mediawiki-test-categorytree-collapse-bullet', '[]' ), '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', 'Username (talk)' ) ); + assert.ok( mw.messages.set( 'mediawiki-test-wikieditor-toolbar-help-content-signature-result', 'Username (talk)' ), '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' ); @@ -211,6 +240,42 @@ assertMultipleFormats( ['int-msg'], ['text', 'parse', 'escaped'], 'Some Other Message', 'int is resolved' ); assert.equal( mw.message( 'int-msg' ).plain(), mw.messages.get( 'int-msg' ), 'int is not resolved in plain mode' ); + + assert.ok( mw.messages.set( 'mediawiki-italics-msg', 'Very important' ), 'mw.messages.set: Register' ); + assertMultipleFormats( ['mediawiki-italics-msg'], ['plain', 'text', 'parse'], mw.messages.get( 'mediawiki-italics-msg' ), 'Simple italics unchanged' ); + assert.htmlEqual( + mw.message( 'mediawiki-italics-msg' ).escaped(), + '<i>Very</i> important', + 'Italics are escaped in escaped mode' + ); + + assert.ok( mw.messages.set( 'mediawiki-italics-with-link', 'An italicized [[link|wiki-link]]' ), 'mw.messages.set: Register' ); + assertMultipleFormats( ['mediawiki-italics-with-link'], ['plain', 'text'], mw.messages.get( 'mediawiki-italics-with-link' ), 'Italics with link unchanged' ); + assert.htmlEqual( + mw.message( 'mediawiki-italics-with-link' ).escaped(), + 'An <i>italicized [[link|wiki-link]]</i>', + 'Italics and link unchanged except for escaping in escaped mode' + ); + assert.htmlEqual( + mw.message( 'mediawiki-italics-with-link' ).parse(), + 'An italicized wiki-link', + 'Italics with link inside in parse mode' + ); + + assert.ok( mw.messages.set( 'mediawiki-script-msg', '' ), 'mw.messages.set: Register' ); + assertMultipleFormats( ['mediawiki-script-msg'], ['plain', 'text'], mw.messages.get( 'mediawiki-script-msg' ), 'Script unchanged' ); + assert.htmlEqual( + mw.message( 'mediawiki-script-msg' ).escaped(), + '<script >alert( "Who put this script here?" );</script>', + 'Script escaped when using escaped format' + ); + assert.htmlEqual( + mw.message( 'mediawiki-script-msg' ).parse(), + '<script >alert( "Who put this script here?" );</script>', + 'Script escaped when using parse format' + ); + + } ); QUnit.test( 'mw.msg', 14, function ( assert ) { @@ -218,7 +283,7 @@ assert.equal( mw.msg( 'hello' ), 'Hello awesome world', 'Gets message with default options (existing message)' ); assert.equal( mw.msg( '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' ); @@ -762,4 +827,90 @@ } ); + QUnit.test( 'mw.hook', 10, function ( assert ) { + var hook, add, fire, chars, callback; + + mw.hook( 'test.hook.unfired' ).add( function () { + assert.ok( false, 'Unfired hook' ); + } ); + + mw.hook( 'test.hook.basic' ).add( function () { + assert.ok( true, 'Basic callback' ); + } ); + mw.hook( 'test.hook.basic' ).fire(); + + mw.hook( 'test.hook.data' ).add( function ( data1, data2 ) { + assert.equal( data1, 'example', 'Fire with data (string param)' ); + assert.deepEqual( data2, ['two'], 'Fire with data (array param)' ); + } ); + mw.hook( 'test.hook.data' ).fire( 'example', ['two'] ); + + mw.hook( 'test.hook.chainable' ).add( function () { + assert.ok( true, 'Chainable' ); + } ).fire(); + + hook = mw.hook( 'test.hook.detach' ); + add = hook.add; + fire = hook.fire; + add( function ( x, y ) { + assert.deepEqual( [x, y], ['x', 'y'], 'Detached (contextless) with data' ); + } ); + fire( 'x', 'y' ); + + mw.hook( 'test.hook.fireBefore' ).fire().add( function () { + assert.ok( true, 'Invoke handler right away if it was fired before' ); + } ); + + mw.hook( 'test.hook.fireTwiceBefore' ).fire().fire().add( function () { + assert.ok( true, 'Invoke handler right away if it was fired before (only last one)' ); + } ); + + chars = []; + + mw.hook( 'test.hook.many' ) + .add( function ( chr ) { + chars.push( chr ); + } ) + .fire( 'x' ).fire( 'y' ).fire( 'z' ) + .add( function ( chr ) { + assert.equal( chr, 'z', 'Adding callback later invokes right away with last data' ); + } ); + + assert.deepEqual( chars, ['x', 'y', 'z'], 'Multiple callbacks with multiple fires' ); + + chars = []; + callback = function ( chr ) { + chars.push( chr ); + }; + + mw.hook( 'test.hook.variadic' ) + .add( + callback, + callback, + function ( chr ) { + chars.push( chr ); + }, + callback + ) + .fire( 'x' ) + .remove( + function () { + 'not-added'; + }, + callback + ) + .fire( 'y' ) + .remove( callback ) + .fire( 'z' ); + + assert.deepEqual( + chars, + ['x', 'x', 'x', 'x', 'y', 'z'], + '"add" and "remove" support variadic arguments. ' + + '"add" does not filter unique. ' + + '"remove" removes all equal by reference. ' + + '"remove" is silent if the function is not found' + ); + } ); + }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js index 875ab91a..96be3d1f 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js @@ -5,7 +5,7 @@ assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' ); } ); - QUnit.test( 'user status', 9, function ( assert ) { + QUnit.test( 'user status', 11, function ( assert ) { /** * Tests can be run under three different conditions: * 1) From tests/qunit/index.html, user will be anonymous. @@ -15,19 +15,23 @@ // Forge an anonymous user: mw.config.set( 'wgUserName', null ); + delete mw.config.values.wgUserId; assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' ); assert.strictEqual( mw.user.name(), null, 'user.name() compatibility' ); assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' ); assert.assertTrue( mw.user.anonymous(), 'user.anonymous() compatibility' ); + assert.strictEqual( mw.user.getId(), 0, 'user.getId() returns 0 when anonymous' ); // Not part of startUp module mw.config.set( 'wgUserName', 'John' ); + mw.config.set( 'wgUserId', 123 ); assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' ); assert.equal( mw.user.name(), 'John', 'user.name() compatibility' ); assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' ); assert.assertFalse( mw.user.anonymous(), 'user.anonymous() compatibility' ); + assert.strictEqual( mw.user.getId(), 123, 'user.getId() returns correct ID when logged-in' ); assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' ); } ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 6fc0731c..9216f0af 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -1,5 +1,13 @@ ( function ( mw, $ ) { - QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment() ); + QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment( { + setup: function () { + this.taPrefix = mw.util.tooltipAccessKeyPrefix; + mw.util.tooltipAccessKeyPrefix = 'ctrl-alt-'; + }, + teardown: function () { + mw.util.tooltipAccessKeyPrefix = this.taPrefix; + } + } ) ); QUnit.test( 'rawurlencode', 1, function ( assert ) { assert.equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' ); @@ -9,20 +17,24 @@ assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' ); } ); - QUnit.test( 'wikiGetlink', 3, function ( assert ) { + QUnit.test( 'getUrl', 4, function ( assert ) { // Not part of startUp module mw.config.set( 'wgArticlePath', '/wiki/$1' ); mw.config.set( 'wgPageName', 'Foobar' ); - var href = mw.util.wikiGetlink( 'Sandbox' ); + var href = mw.util.getUrl( 'Sandbox' ); assert.equal( href, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' ); - href = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' ); + href = mw.util.getUrl( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' ); assert.equal( href, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage', 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' ); - href = mw.util.wikiGetlink(); + href = mw.util.getUrl(); assert.equal( href, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' ); + + href = mw.util.getUrl( 'Sandbox', { action: 'edit' } ); + assert.equal( href, '/wiki/Sandbox?action=edit', + 'Simple title with query string; Get link for "Sandbox" with action=edit' ); } ); QUnit.test( 'wikiScript', 4, function ( assert ) { @@ -76,13 +88,13 @@ assert.strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' ); - tocHtml = '
' + + tocHtml = '
' + '
' + '

Contents

' + ' [Hide ]' + '
' + '
' + - '
'; + '
'; $( tocHtml ).appendTo( '#qunit-fixture' ); $toggleLink = $( '#togglelink' ); @@ -108,10 +120,14 @@ assert.strictEqual( mw.util.getParamValue( 'TEST', url ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' ); } ); - QUnit.test( 'tooltipAccessKey', 3, function ( assert ) { - assert.equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' ); - assert.ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' ); - assert.ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' ); + QUnit.test( 'tooltipAccessKey', 4, function ( assert ) { + assert.equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'tooltipAccessKeyPrefix must be a string' ); + assert.equal( $.type( mw.util.tooltipAccessKeyRegexp ), 'regexp', 'tooltipAccessKeyRegexp is a regexp' ); + assert.ok( mw.util.updateTooltipAccessKeys, 'updateTooltipAccessKeys is non-empty' ); + + 'Example [a]'.replace( mw.util.tooltipAccessKeyRegexp, function ( sub, m1, m2, m3, m4, m5, m6 ) { + assert.equal( m6, 'a', 'tooltipAccessKeyRegexp finds the accesskey hint' ); + } ); } ); QUnit.test( '$content', 2, function ( assert ) { @@ -125,17 +141,18 @@ * Previously, test elements where invisible to the selector since only * one element can have a given id. */ - QUnit.test( 'addPortletLink', 8, function ( assert ) { - var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo; + QUnit.test( 'addPortletLink', 13, function ( assert ) { + var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo, + addedAfter, tbRLDMnonexistentid, tbRLDMemptyjquery; pTestTb = '\
\ -
Toolbox
\ +

Toolbox

\
    \
    '; pCustom = '\
    \ -
    Views
    \ +

    Views

    \
      \
    • Foo
    • \
    • \ @@ -147,14 +164,15 @@
    '; vectorTabs = '\
    \ -
    Views
    \ +

    Views

    \
      \
      '; $( '#qunit-fixture' ).append( pTestTb, pCustom, vectorTabs ); tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader', - 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' ); + 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' + ); assert.ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' ); @@ -162,14 +180,32 @@ 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL ); $tbMW = $( tbMW ); + assert.propEqual( + $tbMW.getAttrs(), + { + id: 't-mworg' + }, + 'Validate attributes of created element' + ); + + assert.propEqual( + $tbMW.find( 'a' ).getAttrs(), + { + href: '//mediawiki.org/', + title: 'Go to MediaWiki.org [ctrl-alt-m]', + accesskey: 'm' + }, + 'Validate attributes of anchor tag in created element' + ); - assert.equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' ); assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' ); - assert.equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' ); + assert.strictEqual( $tbMW.next()[0], tbRL, 'Link is in the correct position (by passing nextnode)' ); - cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux' ); + cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' ); $cuQuux = $( cuQuux ); + assert.equal( $cuQuux.find( 'a' ).attr( 'title' ), 'Example [ctrl-alt-q]', 'Existing accesskey is stripped and updated' ); + assert.equal( $( '#p-test-custom #c-barmenu ul li' ).length, 1, @@ -185,6 +221,21 @@ assert.strictEqual( $tbMW.find( 'span' ).length, 0, 'No element should be added for porlets without vectorTabs class.' ); assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'A element should be added for porlets with vectorTabs class.' ); + + addedAfter = mw.util.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL ) ); + assert.strictEqual( $( addedAfter ).next()[0], tbRL, 'Link is in the correct position (by passing a jQuery object as nextnode)' ); + + // test case - nonexistent id as next node + tbRLDMnonexistentid = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', + 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' ); + + assert.equal( tbRLDMnonexistentid, $( '#p-test-tb li:last' )[0], 'Nonexistent id as nextnode adds the portlet at end' ); + + // test case - empty jquery object as next node + tbRLDMemptyjquery = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', + 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) ); + + assert.equal( tbRLDMemptyjquery, $( '#p-test-tb li:last' )[0], 'Empty jquery as nextnode adds the portlet at end' ); } ); QUnit.test( 'jsMessage', 1, function ( assert ) { -- cgit v1.2.3-54-g00ecf