Add getId to mw.user, returning wgUserId (or 0 for anons).
authorMatthew Flaschen <mflaschen@wikimedia.org>
Thu, 25 Jul 2013 08:49:40 +0000 (01:49 -0700)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Sat, 3 Aug 2013 01:58:14 +0000 (21:58 -0400)
This is equivalent to how User->getId works on the server.

Small formatting update to another documentation block.

Change-Id: I31906398f572dd4f62d89b53dfd05a362bf6d5a5

resources/mediawiki/mediawiki.user.js
tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js

index e8366a5..7cf9d8f 100644 (file)
                        return id;
                },
 
+               /**
+                * Gets the current user's database id.
+                *
+                * Not to be confused with #id
+                *
+                * @return {number} Current user's id, or 0 if user is anonymous
+                */
+               getId: function () {
+                       return mw.config.get( 'wgUserId', 0 );
+               },
+
                /**
                 * Gets the current user's name.
                 *
-                * @return {string|null} User name string or null if users is anonymous
+                * @return {string|null} User name string or null if user is anonymous
                 */
                getName: function () {
                        return mw.config.get( 'wgUserName' );
                /**
                 * Gets the current user's name or the session ID
                 *
+                * Not to be confused with #getId
+                *
                 * @return {string} User name or random session ID
                 */
                id: function () {
index 875ab91..96be3d1 100644 (file)
@@ -5,7 +5,7 @@
                assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
        } );
 
-       QUnit.test( 'user status', 9, function ( assert ) {
+       QUnit.test( 'user status', 11, function ( assert ) {
                /**
                 * Tests can be run under three different conditions:
                 *   1) From tests/qunit/index.html, user will be anonymous.
 
                // Forge an anonymous user:
                mw.config.set( 'wgUserName', null );
+               delete mw.config.values.wgUserId;
 
                assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' );
                assert.strictEqual( mw.user.name(), null, 'user.name() compatibility' );
                assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' );
                assert.assertTrue( mw.user.anonymous(), 'user.anonymous() compatibility' );
+               assert.strictEqual( mw.user.getId(), 0, 'user.getId() returns 0 when anonymous' );
 
                // Not part of startUp module
                mw.config.set( 'wgUserName', 'John' );
+               mw.config.set( 'wgUserId', 123 );
 
                assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' );
                assert.equal( mw.user.name(), 'John', 'user.name() compatibility' );
                assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' );
                assert.assertFalse( mw.user.anonymous(), 'user.anonymous() compatibility' );
+               assert.strictEqual( mw.user.getId(), 123, 'user.getId() returns correct ID when logged-in' );
 
                assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' );
        } );