Merge "Ignore noop DB transactions errors on connection loss"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.options.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 }
6 } ) );
7
8 QUnit.test( 'saveOption', function ( assert ) {
9 QUnit.expect( 2 );
10
11 var
12 api = new mw.Api(),
13 stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
14
15 api.saveOption( 'foo', 'bar' );
16
17 assert.ok( stub.calledOnce, '#saveOptions called once' );
18 assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' );
19 } );
20
21 QUnit.test( 'saveOptions', function ( assert ) {
22 QUnit.expect( 13 );
23
24 var api = new mw.Api();
25
26 // We need to respond to the request for token first, otherwise the other requests won't be sent
27 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
28 api.getToken( 'options' );
29 this.server.respond(
30 /meta=tokens&type=csrf/,
31 [ 200, { 'Content-Type': 'application/json' },
32 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
33 );
34
35 api.saveOptions( {} ).done( function () {
36 assert.ok( true, 'Request completed: empty case' );
37 } );
38 api.saveOptions( { foo: 'bar' } ).done( function () {
39 assert.ok( true, 'Request completed: simple' );
40 } );
41 api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
42 assert.ok( true, 'Request completed: two options' );
43 } );
44 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
45 assert.ok( true, 'Request completed: not bundleable' );
46 } );
47 api.saveOptions( { foo: null } ).done( function () {
48 assert.ok( true, 'Request completed: reset an option' );
49 } );
50 api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
51 assert.ok( true, 'Request completed: reset an option, not bundleable' );
52 } );
53
54 // Requests are POST, match requestBody instead of url
55 this.server.respond( function ( request ) {
56 switch ( request.requestBody ) {
57 // simple
58 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
59 // two options
60 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
61 // not bundleable
62 case 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C':
63 case 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C':
64 case 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C':
65 // reset an option
66 case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
67 // reset an option, not bundleable
68 case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
69 assert.ok( true, 'Repond to ' + request.requestBody );
70 request.respond( 200, { 'Content-Type': 'application/json' },
71 '{ "options": "success" }' );
72 break;
73 default:
74 assert.ok( false, 'Unexpected request: ' + request.requestBody );
75 }
76 } );
77 } );
78 }( mediaWiki ) );