| 1 |
QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment(), ( hooks ) => { |
| 2 |
hooks.beforeEach( function () { |
| 3 |
this.server = this.sandbox.useFakeServer(); |
| 4 |
this.server.respondImmediately = true; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
this.crypto = Object.getOwnPropertyDescriptor( window, 'crypto' ); |
| 10 |
this.msCrypto = Object.getOwnPropertyDescriptor( window, 'msCrypto' ); |
| 11 |
} ); |
| 12 |
hooks.afterEach( function () { |
| 13 |
if ( this.crypto ) { |
| 14 |
Object.defineProperty( window, 'crypto', this.crypto ); |
| 15 |
} |
| 16 |
if ( this.msCrypto ) { |
| 17 |
Object.defineProperty( window, 'msCrypto', this.msCrypto ); |
| 18 |
} |
| 19 |
} ); |
| 20 |
|
| 21 |
QUnit.test( 'options', ( assert ) => { |
| 22 |
assert.true( mw.user.options instanceof mw.Map, 'options instance of mw.Map' ); |
| 23 |
} ); |
| 24 |
|
| 25 |
QUnit.test( 'getters (anonymous)', ( assert ) => { |
| 26 |
|
| 27 |
mw.config.set( 'wgUserName', null ); |
| 28 |
mw.config.set( 'wgUserId', null ); |
| 29 |
|
| 30 |
assert.strictEqual( mw.user.getName(), null, 'getName()' ); |
| 31 |
assert.strictEqual( mw.user.isAnon(), true, 'isAnon()' ); |
| 32 |
assert.strictEqual( mw.user.getId(), 0, 'getId()' ); |
| 33 |
} ); |
| 34 |
|
| 35 |
QUnit.test( 'getters (logged-in)', ( assert ) => { |
| 36 |
mw.config.set( 'wgUserName', 'John' ); |
| 37 |
mw.config.set( 'wgUserIsTemp', false ); |
| 38 |
mw.config.set( 'wgUserId', 123 ); |
| 39 |
|
| 40 |
assert.strictEqual( mw.user.getName(), 'John', 'getName()' ); |
| 41 |
assert.strictEqual( mw.user.isAnon(), false, 'isAnon()' ); |
| 42 |
assert.strictEqual( mw.user.getId(), 123, 'getId()' ); |
| 43 |
assert.strictEqual( mw.user.isNamed(), true, 'isNamed()' ); |
| 44 |
assert.strictEqual( mw.user.isTemp(), false, 'isTemp()' ); |
| 45 |
|
| 46 |
assert.strictEqual( mw.user.id(), 'John', 'user.id()' ); |
| 47 |
} ); |
| 48 |
|
| 49 |
QUnit.test( 'getGroups (callback)', ( assert ) => { |
| 50 |
const done = assert.async(); |
| 51 |
mw.config.set( 'wgUserGroups', [ '*', 'user' ] ); |
| 52 |
|
| 53 |
mw.user.getGroups( ( groups ) => { |
| 54 |
assert.deepEqual( groups, [ '*', 'user' ], 'Result' ); |
| 55 |
done(); |
| 56 |
} ); |
| 57 |
} ); |
| 58 |
|
| 59 |
QUnit.test( 'getGroups (Promise)', ( assert ) => { |
| 60 |
mw.config.set( 'wgUserGroups', [ '*', 'user' ] ); |
| 61 |
|
| 62 |
return mw.user.getGroups().then( ( groups ) => { |
| 63 |
assert.deepEqual( groups, [ '*', 'user' ], 'Result' ); |
| 64 |
} ); |
| 65 |
} ); |
| 66 |
|
| 67 |
QUnit.test( 'getRights (callback)', function ( assert ) { |
| 68 |
const done = assert.async(); |
| 69 |
|
| 70 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, |
| 71 |
'{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }' |
| 72 |
] ); |
| 73 |
|
| 74 |
mw.user.getRights( ( rights ) => { |
| 75 |
assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' ); |
| 76 |
done(); |
| 77 |
} ); |
| 78 |
} ); |
| 79 |
|
| 80 |
QUnit.test( 'getRights (Promise)', function ( assert ) { |
| 81 |
this.server.respond( [ 200, { 'Content-Type': 'application/json' }, |
| 82 |
'{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }' |
| 83 |
] ); |
| 84 |
|
| 85 |
return mw.user.getRights().then( ( rights ) => { |
| 86 |
assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' ); |
| 87 |
} ); |
| 88 |
} ); |
| 89 |
|
| 90 |
QUnit.test( 'generateRandomSessionId', ( assert ) => { |
| 91 |
const result = mw.user.generateRandomSessionId(); |
| 92 |
assert.strictEqual( typeof result, 'string', 'type' ); |
| 93 |
assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' ); |
| 94 |
assert.strictEqual( result.length, 20, 'size' ); |
| 95 |
|
| 96 |
const result2 = mw.user.generateRandomSessionId(); |
| 97 |
assert.notStrictEqual( result, result2, 'different when called multiple times' ); |
| 98 |
|
| 99 |
} ); |
| 100 |
|
| 101 |
QUnit.test( 'generateRandomSessionId (fallback)', ( assert ) => { |
| 102 |
|
| 103 |
delete window.crypto; |
| 104 |
delete window.msCrypto; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
assert.strictEqual( window.crypto || window.msCrypto, undefined, 'fallback is active' ); |
| 111 |
|
| 112 |
const result = mw.user.generateRandomSessionId(); |
| 113 |
assert.strictEqual( typeof result, 'string', 'type' ); |
| 114 |
assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' ); |
| 115 |
assert.strictEqual( result.length, 20, 'size' ); |
| 116 |
|
| 117 |
const result2 = mw.user.generateRandomSessionId(); |
| 118 |
assert.notStrictEqual( result, result2, 'different when called multiple times' ); |
| 119 |
} ); |
| 120 |
|
| 121 |
QUnit.test( 'getPageviewToken', ( assert ) => { |
| 122 |
const result = mw.user.getPageviewToken(), |
| 123 |
result2 = mw.user.getPageviewToken(); |
| 124 |
assert.strictEqual( typeof result, 'string', 'type' ); |
| 125 |
assert.strictEqual( /^[a-f0-9]{20}$/.test( result ), true, '20 HEX symbols string' ); |
| 126 |
assert.strictEqual( result2, result, 'sticky' ); |
| 127 |
} ); |
| 128 |
|
| 129 |
QUnit.test( 'sessionId', ( assert ) => { |
| 130 |
const result = mw.user.sessionId(), |
| 131 |
result2 = mw.user.sessionId(); |
| 132 |
assert.strictEqual( typeof result, 'string', 'type' ); |
| 133 |
assert.strictEqual( result.trim(), result, 'no leading or trailing whitespace' ); |
| 134 |
assert.strictEqual( result2, result, 'retained' ); |
| 135 |
} ); |
| 136 |
|
| 137 |
QUnit.module( 'clientPrefs', ( hooks ) => { |
| 138 |
const CLIENT_PREF_COOKIE_NAME = 'mwclientpreferences'; |
| 139 |
let docClass; |
| 140 |
|
| 141 |
hooks.beforeEach( () => { |
| 142 |
docClass = document.documentElement.getAttribute( 'class' ); |
| 143 |
document.documentElement.setAttribute( 'class', '' ); |
| 144 |
|
| 145 |
mw.cookie.set( CLIENT_PREF_COOKIE_NAME, null ); |
| 146 |
|
| 147 |
mw.config.set( { wgUserName: null } ); |
| 148 |
} ); |
| 149 |
hooks.afterEach( () => { |
| 150 |
mw.cookie.set( CLIENT_PREF_COOKIE_NAME, null ); |
| 151 |
document.documentElement.setAttribute( 'class', docClass ); |
| 152 |
} ); |
| 153 |
|
| 154 |
QUnit.test( 'get() [from HTML element]', ( assert ) => { |
| 155 |
document.documentElement.setAttribute( 'class', 'client-js font-size-clientpref-1 font-size-clientpref-unrelated-class invalid-clientpref-bad-value ambiguous-clientpref-off ambiguous-clientpref-on' ); |
| 156 |
const result = mw.user.clientPrefs.get( 'font-size' ); |
| 157 |
const badValue = mw.user.clientPrefs.get( 'invalid' ); |
| 158 |
const ambiguousValue = mw.user.clientPrefs.get( 'ambiguous' ); |
| 159 |
assert.strictEqual( result, '1', 'client preferences are read from HTML element' ); |
| 160 |
assert.strictEqual( badValue, false, 'classes in the wrong format are ignored.' ); |
| 161 |
assert.strictEqual( ambiguousValue, false, 'ambiguous values are resolved to false' ); |
| 162 |
} ); |
| 163 |
|
| 164 |
QUnit.test( 'get() [never read from cookie]', ( assert ) => { |
| 165 |
mw.cookie.set( CLIENT_PREF_COOKIE_NAME, 'unknown~500' ); |
| 166 |
const resultUnknown = mw.user.clientPrefs.get( 'unknown' ); |
| 167 |
assert.false( resultUnknown, |
| 168 |
'if an appropriate class is not on the HTML element it returns false even if there is a value in the cookie' ); |
| 169 |
} ); |
| 170 |
|
| 171 |
QUnit.test( 'set() [valid preferences]', ( assert ) => { |
| 172 |
document.documentElement.classList.add( 'limited-width-clientpref-1', 'font-size-clientpref-100' ); |
| 173 |
const resultLimitedWidth = mw.user.clientPrefs.set( 'limited-width', '0' ); |
| 174 |
const resultFontSize = mw.user.clientPrefs.set( 'font-size', '10' ); |
| 175 |
assert.true( resultLimitedWidth, 'the client preference limited width was set correctly' ); |
| 176 |
assert.true( resultFontSize, 'the client preference font size was set correctly' ); |
| 177 |
assert.true( document.documentElement.classList.contains( 'limited-width-clientpref-0' ), |
| 178 |
'the limited width class on the document was updated' ); |
| 179 |
assert.true( document.documentElement.classList.contains( 'font-size-clientpref-10' ), |
| 180 |
'the font size classes on the document was updated correctly' ); |
| 181 |
const mwclientpreferences = mw.cookie.get( CLIENT_PREF_COOKIE_NAME ); |
| 182 |
assert.true( |
| 183 |
mwclientpreferences && |
| 184 |
mwclientpreferences.includes( 'limited-width-clientpref-0' ) && |
| 185 |
mwclientpreferences.includes( 'font-size-clientpref-10' ), |
| 186 |
'cookie was set correctly' |
| 187 |
); |
| 188 |
} ); |
| 189 |
|
| 190 |
QUnit.test( 'set() [invalid preferences]', ( assert ) => { |
| 191 |
document.documentElement.classList.add( 'client-js' ); |
| 192 |
const result = mw.user.clientPrefs.set( 'client', 'nojs' ); |
| 193 |
assert.false( result, 'the client preference was rejected (lacking -clientpref- suffix)' ); |
| 194 |
assert.true( document.documentElement.classList.contains( 'client-js' ), 'the classes on the document were not changed' ); |
| 195 |
} ); |
| 196 |
|
| 197 |
QUnit.test( 'set() [invalid characters]', ( assert ) => { |
| 198 |
document.documentElement.setAttribute( 'class', 'client-js bar-clientpref-1' ); |
| 199 |
[ |
| 200 |
[ 'client-js bar', 'nojs' ], |
| 201 |
[ 'bar-clientpref', '50' ], |
| 202 |
[ 'bar', ' client-nojs' ], |
| 203 |
[ 'bar', 'client-nojs' ], |
| 204 |
[ '', 'nothing' ], |
| 205 |
[ 'feature', '' ], |
| 206 |
[ 'foo!client~no-js!bar', 'hi' ] |
| 207 |
].forEach( ( test, i ) => { |
| 208 |
const result = mw.user.clientPrefs.set( test[ 0 ], test[ 1 ] ); |
| 209 |
assert.false( result, 'the client preference was rejected (invalid characters in name) (test case ' + i + ')' ); |
| 210 |
} ); |
| 211 |
|
| 212 |
assert.strictEqual( document.documentElement.getAttribute( 'class' ), 'client-js bar-clientpref-1' ); |
| 213 |
} ); |
| 214 |
|
| 215 |
QUnit.test( 'set() [create cookie on change]', ( assert ) => { |
| 216 |
document.documentElement.setAttribute( 'class', 'dark-mode-clientpref-enabled' ); |
| 217 |
let result = mw.user.clientPrefs.set( 'dark-mode', 'disabled' ); |
| 218 |
assert.true( result, 'the client preference was stored successfully' ); |
| 219 |
assert.strictEqual( |
| 220 |
document.documentElement.getAttribute( 'class' ), |
| 221 |
'dark-mode-clientpref-disabled', |
| 222 |
'the class was modified' |
| 223 |
); |
| 224 |
let mwclientpreferences = mw.cookie.get( CLIENT_PREF_COOKIE_NAME ); |
| 225 |
assert.strictEqual( |
| 226 |
mwclientpreferences, |
| 227 |
'dark-mode-clientpref-disabled', |
| 228 |
'it was stored to a cookie' |
| 229 |
); |
| 230 |
result = mw.user.clientPrefs.set( 'dark-mode', 'enabled' ); |
| 231 |
assert.true( result, 'the 2nd client preference was also stored successfully' ); |
| 232 |
assert.strictEqual( |
| 233 |
document.documentElement.getAttribute( 'class' ), |
| 234 |
'dark-mode-clientpref-enabled', |
| 235 |
'the class was also modified again' |
| 236 |
); |
| 237 |
mwclientpreferences = mw.cookie.get( CLIENT_PREF_COOKIE_NAME ); |
| 238 |
assert.strictEqual( |
| 239 |
mwclientpreferences, |
| 240 |
'dark-mode-clientpref-enabled', |
| 241 |
'always store even if it matches default as we have no knowledge of what the default could be' |
| 242 |
); |
| 243 |
} ); |
| 244 |
|
| 245 |
QUnit.test( 'set() [always store values]', ( assert ) => { |
| 246 |
|
| 247 |
document.documentElement.setAttribute( 'class', 'client-js not-a-feature-clientpref-bad-value font-clientpref-32 dark-mode-clientpref-32 limited-width-clientpref-enabled' ); |
| 248 |
mw.user.clientPrefs.set( 'dark-mode', 'enabled' ); |
| 249 |
const mwclientpreferences = mw.cookie.get( CLIENT_PREF_COOKIE_NAME ); |
| 250 |
assert.strictEqual( |
| 251 |
mwclientpreferences, |
| 252 |
'dark-mode-clientpref-enabled', |
| 253 |
'always store even if it matches default as we have no knowledge of what the default could be' |
| 254 |
); |
| 255 |
} ); |
| 256 |
|
| 257 |
QUnit.test( 'set() [throw if registered user]', ( assert ) => { |
| 258 |
mw.config.set( { wgUserName: 'Example' } ); |
| 259 |
|
| 260 |
assert.throws( () => { |
| 261 |
mw.user.clientPrefs.set( 'foo', 'bar' ); |
| 262 |
}, /unregistered users only/, 'error' ); |
| 263 |
} ); |
| 264 |
} ); |
| 265 |
} ); |