Merge "Set context on RedirectSpecialPage in MediaWiki.php"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.jqueryMsg.test.js
index 24528bb..07eddbf 100644 (file)
        QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
                setup: function () {
                        this.originalMwLanguage = mw.language;
+                       this.parserDefaults = mw.jqueryMsg.getParserDefaults();
+                       mw.jqueryMsg.setParserDefaults( {
+                               magic: {
+                                       SITENAME: 'Wiki'
+                               }
+                       } );
 
                        specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
 
                        expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
                        expectedListUsersSitename = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
-                               mw.config.get( 'wgSiteName' ) + '</a>';
+                               'Wiki</a>';
 
                        expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
 
@@ -32,6 +38,7 @@
                },
                teardown: function () {
                        mw.language = this.originalMwLanguage;
+                       mw.jqueryMsg.setParserDefaults( this.parserDefaults );
                },
                config: {
                        wgArticlePath: '/wiki/$1',
        /**
         * @param {Function[]} tasks List of functions that perform tasks
         *  that may be asynchronous. Invoke the callback parameter when done.
-        * @param {Function} complete Called when all tasks are done, or when the sequence is aborted.
         */
-       function process( tasks, complete ) {
+       function process( tasks ) {
                /*jshint latedef:false */
                function abort() {
                        tasks.splice( 0, tasks.length );
                        } else {
                                // Remove tasks list to indicate the process is final.
                                tasks = null;
-                               complete();
                        }
                }
                next();
                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' );
                assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
-               assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in ' + mw.config.get( 'wgSiteName' ), 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
+               assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in Wiki', 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
                assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
        } );
 
                mw.user.options.set( 'gender', originalGender );
        } );
 
+       QUnit.test( 'Case changing', 8, function ( assert ) {
+               mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
+
+               mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
+
+               mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
+               mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
+               assert.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
+
+               mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
+               mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
+               assert.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
+
+               mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
+               assert.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
+               mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
+               assert.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
+       } );
+
        QUnit.test( 'Grammar', 2, function ( assert ) {
-               assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' );
+               assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );
 
                mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
                assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
                mw.messages.set( mw.libs.phpParserData.messages );
                var tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
                        return function ( next, abort ) {
+                               var done = assert.async();
                                getMwLanguage( test.lang )
                                        .then( function ( langClass ) {
                                                mw.config.set( 'wgUserLanguage', test.lang );
                                        }, function () {
                                                assert.ok( false, 'Language "' + test.lang + '" failed to load.' );
                                        } )
+                                       .then( done, done )
                                        .then( next, abort );
                        };
                } );
 
-               QUnit.stop();
-               process( tasks, QUnit.start );
+               process( tasks );
        } );
 
        QUnit.test( 'Links', 14, function ( assert ) {
 
                assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
 
-               assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
+               assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
 
                mw.config.set( 'wgUserLanguage', 'en' );
                assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
                        message[ format ]();
                        assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
                        assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
+                       delete mw.messages[ format ];
                }
 
                verifyGetMessageFunction( 'curly-brace', 'parse', true );
                mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
                var queue = $.map( formatnumTests, function ( test ) {
                        return function ( next, abort ) {
+                               var done = assert.async();
                                getMwLanguage( test.lang )
                                        .then( function ( langClass ) {
                                                mw.config.set( 'wgUserLanguage', test.lang );
                                        }, function () {
                                                assert.ok( false, 'Language "' + test.lang + '" failed to load' );
                                        } )
+                                       .then( done, done )
                                        .then( next, abort );
                        };
                } );
-               QUnit.stop();
-               process( queue, QUnit.start );
+               process( queue );
        } );
 
        // HTML in wikitext
                assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
        } );
 
-       QUnit.test( 'Integration', 4, function ( assert ) {
-               var expected, logSpy;
+       QUnit.test( 'Integration', 5, function ( assert ) {
+               var expected, logSpy, msg;
 
                expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
                mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
                        expected,
                        'jQuery plugin $.fn.msg() works correctly'
                );
+
+               mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
+               msg = mw.message(
+                       'integration-test-extlink',
+                       $( '<a>' ).attr( 'href', 'http://example.com/' )
+               );
+               msg.parse(); // Not a no-op
+               assert.equal(
+                       msg.parse(),
+                       '<a href="http://example.com/">Link</a>',
+                       'Calling .parse() multiple times does not duplicate link contents'
+               );
        } );
 
 }( mediaWiki, jQuery ) );