2 QUnit
.module( 'mediawiki.user', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
5 this.crypto
= window
.crypto
;
6 this.msCrypto
= window
.msCrypto
;
8 teardown: function () {
10 window
.crypto
= this.crypto
;
12 if ( this.msCrypto
) {
13 window
.msCrypto
= this.msCrypto
;
18 QUnit
.test( 'options', function ( assert
) {
19 assert
.ok( mw
.user
.options
instanceof mw
.Map
, 'options instance of mw.Map' );
22 QUnit
.test( 'getters (anonymous)', function ( assert
) {
23 // Forge an anonymous user
24 mw
.config
.set( 'wgUserName', null );
25 mw
.config
.set( 'wgUserId', null );
27 assert
.strictEqual( mw
.user
.getName(), null, 'getName()' );
28 assert
.strictEqual( mw
.user
.isAnon(), true, 'isAnon()' );
29 assert
.strictEqual( mw
.user
.getId(), 0, 'getId()' );
32 QUnit
.test( 'getters (logged-in)', function ( assert
) {
33 mw
.config
.set( 'wgUserName', 'John' );
34 mw
.config
.set( 'wgUserId', 123 );
36 assert
.equal( mw
.user
.getName(), 'John', 'getName()' );
37 assert
.strictEqual( mw
.user
.isAnon(), false, 'isAnon()' );
38 assert
.strictEqual( mw
.user
.getId(), 123, 'getId()' );
40 assert
.equal( mw
.user
.id(), 'John', 'user.id()' );
43 QUnit
.test( 'getUserInfo', function ( assert
) {
44 mw
.config
.set( 'wgUserGroups', [ '*', 'user' ] );
46 mw
.user
.getGroups( function ( groups
) {
47 assert
.deepEqual( groups
, [ '*', 'user' ], 'Result' );
50 mw
.user
.getRights( function ( rights
) {
51 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
54 mw
.user
.getRights().done( function ( rights
) {
55 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
58 this.server
.respondWith( /meta=userinfo/, function ( request
) {
59 request
.respond( 200, { 'Content-Type': 'application/json' },
60 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
64 this.server
.respond();
67 QUnit
.test( 'generateRandomSessionId', function ( assert
) {
70 result
= mw
.user
.generateRandomSessionId();
71 assert
.equal( typeof result
, 'string', 'type' );
72 assert
.equal( $.trim( result
), result
, 'no whitespace at beginning or end' );
73 assert
.equal( result
.length
, 16, 'size' );
75 result2
= mw
.user
.generateRandomSessionId();
76 assert
.notEqual( result
, result2
, 'different when called multiple times' );
80 QUnit
.test( 'generateRandomSessionId (fallback)', function ( assert
) {
83 // Pretend crypto API is not there to test the Math.random fallback
84 if ( window
.crypto
) {
85 window
.crypto
= undefined;
87 if ( window
.msCrypto
) {
88 window
.msCrypto
= undefined;
91 result
= mw
.user
.generateRandomSessionId();
92 assert
.equal( typeof result
, 'string', 'type' );
93 assert
.equal( $.trim( result
), result
, 'no whitespace at beginning or end' );
94 assert
.equal( result
.length
, 16, 'size' );
96 result2
= mw
.user
.generateRandomSessionId();
97 assert
.notEqual( result
, result2
, 'different when called multiple times' );
100 }( mediaWiki
, jQuery
) );