2 QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
9 QUnit.test( 'saveOption', 2, function ( assert ) {
10 var api = new mw.Api(),
11 stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
13 api.saveOption( 'foo', 'bar' );
15 assert.ok( stub.calledOnce, '#saveOptions called once' );
16 assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' );
19 QUnit.test( 'saveOptions without Unit Separator', 13, function ( assert ) {
20 var api = new mw.Api( { useUS: false } );
22 // We need to respond to the request for token first, otherwise the other requests won't be sent
23 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
24 api.getToken( 'options' );
26 /meta=tokens&type=csrf/,
27 [ 200, { 'Content-Type': 'application/json' },
28 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
31 // Requests are POST, match requestBody instead of url
32 this.server.respond( function ( request ) {
33 switch ( request.requestBody ) {
35 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
37 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
39 case 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C':
40 case 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C':
41 case 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C':
43 case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
44 // reset an option, not bundleable
45 case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
46 assert.ok( true, 'Repond to ' + request.requestBody );
47 request.respond( 200, { 'Content-Type': 'application/json' },
48 '{ "options": "success" }' );
51 assert.ok( false, 'Unexpected request: ' + request.requestBody );
55 return QUnit.whenPromisesComplete(
56 api.saveOptions( {} ).then( function () {
57 assert.ok( true, 'Request completed: empty case' );
59 api.saveOptions( { foo: 'bar' } ).then( function () {
60 assert.ok( true, 'Request completed: simple' );
62 api.saveOptions( { foo: 'bar', baz: 'quux' } ).then( function () {
63 assert.ok( true, 'Request completed: two options' );
65 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).then( function () {
66 assert.ok( true, 'Request completed: not bundleable' );
68 api.saveOptions( { foo: null } ).then( function () {
69 assert.ok( true, 'Request completed: reset an option' );
71 api.saveOptions( { 'foo|bar=quux': null } ).then( function () {
72 assert.ok( true, 'Request completed: reset an option, not bundleable' );
77 QUnit.test( 'saveOptions with Unit Separator', 14, function ( assert ) {
78 var api = new mw.Api( { useUS: true } );
80 // We need to respond to the request for token first, otherwise the other requests won't be sent
81 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
82 api.getToken( 'options' );
84 /meta=tokens&type=csrf/,
85 [ 200, { 'Content-Type': 'application/json' },
86 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
89 // Requests are POST, match requestBody instead of url
90 this.server.respond( function ( request ) {
91 switch ( request.requestBody ) {
93 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
95 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
96 // bundleable with unit separator
97 case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C':
98 // not bundleable with unit separator
99 case 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C':
100 case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C':
102 case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
103 // reset an option, not bundleable
104 case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
105 assert.ok( true, 'Repond to ' + request.requestBody );
106 request.respond( 200, { 'Content-Type': 'application/json' },
107 '{ "options": "success" }' );
110 assert.ok( false, 'Unexpected request: ' + request.requestBody );
114 return QUnit.whenPromisesComplete(
115 api.saveOptions( {} ).done( function () {
116 assert.ok( true, 'Request completed: empty case' );
118 api.saveOptions( { foo: 'bar' } ).done( function () {
119 assert.ok( true, 'Request completed: simple' );
121 api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
122 assert.ok( true, 'Request completed: two options' );
124 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
125 assert.ok( true, 'Request completed: bundleable with unit separator' );
127 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', 'baz=baz': 'quux' } ).done( function () {
128 assert.ok( true, 'Request completed: not bundleable with unit separator' );
130 api.saveOptions( { foo: null } ).done( function () {
131 assert.ok( true, 'Request completed: reset an option' );
133 api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
134 assert.ok( true, 'Request completed: reset an option, not bundleable' );