Merge "Define 'MW_UPDATER' when running update.php"
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.user.test.js
blobf422bc1e27e3bc58b2bdb7f58fdeecd51d0ee0c7
1 ( function ( mw, $ ) {
2         QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment() );
4         QUnit.test( 'options', 1, function ( assert ) {
5                 assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
6         } );
8         QUnit.test( 'user status', 11, function ( assert ) {
9                 /**
10                  * Tests can be run under three different conditions:
11                  *   1) From tests/qunit/index.html, user will be anonymous.
12                  *   2) Logged in on [[Special:JavaScriptTest/qunit]]
13                  *   3) Anonymously at the same special page.
14                  */
16                 // Forge an anonymous user:
17                 mw.config.set( 'wgUserName', null );
18                 delete mw.config.values.wgUserId;
20                 assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' );
21                 assert.strictEqual( mw.user.name(), null, 'user.name() compatibility' );
22                 assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' );
23                 assert.assertTrue( mw.user.anonymous(), 'user.anonymous() compatibility' );
24                 assert.strictEqual( mw.user.getId(), 0, 'user.getId() returns 0 when anonymous' );
26                 // Not part of startUp module
27                 mw.config.set( 'wgUserName', 'John' );
28                 mw.config.set( 'wgUserId', 123 );
30                 assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' );
31                 assert.equal( mw.user.name(), 'John', 'user.name() compatibility' );
32                 assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' );
33                 assert.assertFalse( mw.user.anonymous(), 'user.anonymous() compatibility' );
34                 assert.strictEqual( mw.user.getId(), 123, 'user.getId() returns correct ID when logged-in' );
36                 assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' );
37         } );
39         QUnit.asyncTest( 'getGroups', 3, function ( assert ) {
40                 mw.user.getGroups( function ( groups ) {
41                         // First group should always be '*'
42                         assert.equal( $.type( groups ), 'array', 'Callback gets an array' );
43                         assert.notStrictEqual( $.inArray( '*', groups ), -1, '"*"" is in the list' );
44                         // Sort needed because of different methods if creating the arrays,
45                         // only the content matters.
46                         assert.deepEqual( groups.sort(), mw.config.get( 'wgUserGroups' ).sort(), 'Array contains all groups, just like wgUserGroups' );
47                         QUnit.start();
48                 } );
49         } );
51         QUnit.test( 'getRights', 2, function ( assert ) {
52                 QUnit.stop();
53                 QUnit.stop();
55                 mw.user.getRights( function ( rights ) {
56                         assert.equal( $.type( rights ), 'array', 'Callback gets an array' );
57                         QUnit.start();
58                 } );
60                 mw.user.getRights().done( function ( rights ) {
61                         assert.equal( $.type( rights ), 'array', 'Using promise interface instead of callback' );
62                         QUnit.start();
63                 } );
64         } );
65 }( mediaWiki, jQuery ) );