Merge "resourceloader: Introduce new module state "executing""
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.user.test.js
index fe22b7a..04e002d 100644 (file)
@@ -1,7 +1,17 @@
-( function ( mw ) {
+( function ( mw, $ ) {
        QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
                setup: function () {
                        this.server = this.sandbox.useFakeServer();
+                       this.crypto = window.crypto;
+                       this.msCrypto = window.msCrypto;
+               },
+               teardown: function () {
+                       if ( this.crypto ) {
+                               window.crypto = this.crypto;
+                       }
+                       if ( this.msCrypto ) {
+                               window.msCrypto = this.msCrypto;
+                       }
                }
        } ) );
 
                this.server.respond();
        } );
 
-       QUnit.test( 'session numbers', 4, function ( assert ) {
-               /*global $:false */
-               var sessionId = mw.user.generateRandomSessionId(),
-               cryptoObj = window.crypto;
-
-               assert.equal( typeof sessionId, 'string', 'generateRandomSessionId should return a string' );
-               assert.equal( $.trim(sessionId), sessionId, 'generateRandomSessionId should not contain whitespace' );
-               // pretend crypto API is not there and do same test, make sure code runs
-               // through  Math.random loop
-               window.crypto = undefined;
-               sessionId =  mw.user.generateRandomSessionId();
-               assert.equal( typeof sessionId, 'string', 'generateRandomSessionId should return a string' );
-               assert.equal( sessionId.trim(), sessionId, 'generateRandomSessionId should not be empty' );
-               //restoring crypto object
-               window.crypto = cryptoObj;
+       QUnit.test( 'generateRandomSessionId', 4, function ( assert ) {
+               var result, result2;
+
+               result = mw.user.generateRandomSessionId();
+               assert.equal( typeof result, 'string', 'type' );
+               assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
+               assert.equal( result.length, 16, 'size' );
+
+               result2 = mw.user.generateRandomSessionId();
+               assert.notEqual( result, result2, 'different when called multiple times' );
+
+       } );
+
+       QUnit.test( 'generateRandomSessionId (fallback)', 4, function ( assert ) {
+               var result, result2;
+
+               // Pretend crypto API is not there to test the Math.random fallback
+               if ( window.crypto ) {
+                       window.crypto = undefined;
+               }
+               if ( window.msCrypto ) {
+                       window.msCrypto = undefined;
+               }
+
+               result = mw.user.generateRandomSessionId();
+               assert.equal( typeof result, 'string', 'type' );
+               assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
+               assert.equal( result.length, 16, 'size' );
+
+               result2 = mw.user.generateRandomSessionId();
+               assert.notEqual( result, result2, 'different when called multiple times' );
 
        } );
-}( mediaWiki ) );
+}( mediaWiki, jQuery ) );