Merge "resourceloader: Simplify StringSet fallback"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.user.test.js
index 7044069..1e35211 100644 (file)
@@ -1,16 +1,20 @@
-( function ( mw ) {
+( function () {
        QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
                setup: function () {
                        this.server = this.sandbox.useFakeServer();
-                       this.crypto = window.crypto;
-                       this.msCrypto = window.msCrypto;
+                       // Cannot stub by simple assignment because read-only.
+                       // Instead, stub in tests by using 'delete', and re-create
+                       // in teardown using the original descriptor (including its
+                       // accessors and readonly settings etc.)
+                       this.crypto = Object.getOwnPropertyDescriptor( window, 'crypto' );
+                       this.msCrypto = Object.getOwnPropertyDescriptor( window, 'msCrypto' );
                },
                teardown: function () {
                        if ( this.crypto ) {
-                               window.crypto = this.crypto;
+                               Object.defineProperty( window, 'crypto', this.crypto );
                        }
                        if ( this.msCrypto ) {
-                               window.msCrypto = this.msCrypto;
+                               Object.defineProperty( window, 'msCrypto', this.msCrypto );
                        }
                }
        } ) );
@@ -70,7 +74,7 @@
                result = mw.user.generateRandomSessionId();
                assert.strictEqual( typeof result, 'string', 'type' );
                assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' );
-               assert.strictEqual( result.length, 16, 'size' );
+               assert.strictEqual( result.length, 20, 'size' );
 
                result2 = mw.user.generateRandomSessionId();
                assert.notEqual( result, result2, 'different when called multiple times' );
                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;
-               }
+               delete window.crypto;
+               delete window.msCrypto;
+               // Assert that the above actually worked. If we use the wrong method
+               // of stubbing, JavaScript silently continues and we need to know that
+               // it was the wrong method. As of writing, assigning undefined is
+               // ineffective as the window property for Crypto is read-only.
+               // However, deleting does work. (T203275)
+               assert.strictEqual( window.crypto || window.msCrypto, undefined, 'fallback is active' );
 
                result = mw.user.generateRandomSessionId();
                assert.strictEqual( typeof result, 'string', 'type' );
                assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' );
-               assert.strictEqual( result.length, 16, 'size' );
+               assert.strictEqual( result.length, 20, 'size' );
 
                result2 = mw.user.generateRandomSessionId();
                assert.notEqual( result, result2, 'different when called multiple times' );
                var result = mw.user.getPageviewToken(),
                        result2 = mw.user.getPageviewToken();
                assert.strictEqual( typeof result, 'string', 'type' );
-               assert.strictEqual( /^[a-f0-9]{16}$/.test( result ), true, '16 HEX symbols string' );
+               assert.strictEqual( /^[a-f0-9]{20}$/.test( result ), true, '20 HEX symbols string' );
                assert.strictEqual( result2, result, 'sticky' );
        } );
 
                assert.strictEqual( result.trim(), result, 'no leading or trailing whitespace' );
                assert.strictEqual( result2, result, 'retained' );
        } );
-}( mediaWiki ) );
+}() );