2 QUnit
.module( 'mediawiki.api.options', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
5 this.server
.respondImmediately
= true;
9 QUnit
.test( 'saveOption', 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', 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
) {
35 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
37 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
39 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C',
40 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C',
41 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C',
43 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
44 // reset an option, not bundleable
45 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
46 ].indexOf( request
.requestBody
) !== -1 ) {
47 assert
.ok( true, 'Repond to ' + request
.requestBody
);
48 request
.respond( 200, { 'Content-Type': 'application/json' },
49 '{ "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', 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
) {
93 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
95 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
96 // bundleable with unit separator
97 '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 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C',
100 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C',
102 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
103 // reset an option, not bundleable
104 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
105 ].indexOf( request
.requestBody
) !== -1 ) {
106 assert
.ok( true, 'Repond to ' + request
.requestBody
);
109 { 'Content-Type': 'application/json' },
110 '{ "options": "success" }'
113 assert
.ok( false, 'Unexpected request: ' + request
.requestBody
);
117 return QUnit
.whenPromisesComplete(
118 api
.saveOptions( {} ).done( function () {
119 assert
.ok( true, 'Request completed: empty case' );
121 api
.saveOptions( { foo
: 'bar' } ).done( function () {
122 assert
.ok( true, 'Request completed: simple' );
124 api
.saveOptions( { foo
: 'bar', baz
: 'quux' } ).done( function () {
125 assert
.ok( true, 'Request completed: two options' );
127 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', baz
: 'quux' } ).done( function () {
128 assert
.ok( true, 'Request completed: bundleable with unit separator' );
130 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', 'baz=baz': 'quux' } ).done( function () {
131 assert
.ok( true, 'Request completed: not bundleable with unit separator' );
133 api
.saveOptions( { foo
: null } ).done( function () {
134 assert
.ok( true, 'Request completed: reset an option' );
136 api
.saveOptions( { 'foo|bar=quux': null } ).done( function () {
137 assert
.ok( true, 'Request completed: reset an option, not bundleable' );