Merge "resourceloader: Simplify StringSet fallback"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.options.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 QUnit.test( 'saveOption', function ( assert ) {
10 var api = new mw.Api(),
11 stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
12
13 api.saveOption( 'foo', 'bar' );
14
15 assert.ok( stub.calledOnce, '#saveOptions called once' );
16 assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' );
17 } );
18
19 QUnit.test( 'saveOptions without Unit Separator', function ( assert ) {
20 var api = new mw.Api( { useUS: false } );
21
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' );
25 this.server.respond(
26 /meta=tokens&type=csrf/,
27 [ 200, { 'Content-Type': 'application/json' },
28 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
29 );
30
31 // Requests are POST, match requestBody instead of url
32 this.server.respond( function ( request ) {
33 if ( [
34 // simple
35 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
36 // two options
37 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
38 // not bundleable
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',
42 // reset an option
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" }' );
50 } else {
51 assert.ok( false, 'Unexpected request: ' + request.requestBody );
52 }
53 } );
54
55 return QUnit.whenPromisesComplete(
56 api.saveOptions( {} ).then( function () {
57 assert.ok( true, 'Request completed: empty case' );
58 } ),
59 api.saveOptions( { foo: 'bar' } ).then( function () {
60 assert.ok( true, 'Request completed: simple' );
61 } ),
62 api.saveOptions( { foo: 'bar', baz: 'quux' } ).then( function () {
63 assert.ok( true, 'Request completed: two options' );
64 } ),
65 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).then( function () {
66 assert.ok( true, 'Request completed: not bundleable' );
67 } ),
68 api.saveOptions( { foo: null } ).then( function () {
69 assert.ok( true, 'Request completed: reset an option' );
70 } ),
71 api.saveOptions( { 'foo|bar=quux': null } ).then( function () {
72 assert.ok( true, 'Request completed: reset an option, not bundleable' );
73 } )
74 );
75 } );
76
77 QUnit.test( 'saveOptions with Unit Separator', function ( assert ) {
78 var api = new mw.Api( { useUS: true } );
79
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' );
83 this.server.respond(
84 /meta=tokens&type=csrf/,
85 [ 200, { 'Content-Type': 'application/json' },
86 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
87 );
88
89 // Requests are POST, match requestBody instead of url
90 this.server.respond( function ( request ) {
91 if ( [
92 // simple
93 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
94 // two options
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',
101 // reset an option
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 );
107 request.respond(
108 200,
109 { 'Content-Type': 'application/json' },
110 '{ "options": "success" }'
111 );
112 } else {
113 assert.ok( false, 'Unexpected request: ' + request.requestBody );
114 }
115 } );
116
117 return QUnit.whenPromisesComplete(
118 api.saveOptions( {} ).done( function () {
119 assert.ok( true, 'Request completed: empty case' );
120 } ),
121 api.saveOptions( { foo: 'bar' } ).done( function () {
122 assert.ok( true, 'Request completed: simple' );
123 } ),
124 api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
125 assert.ok( true, 'Request completed: two options' );
126 } ),
127 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
128 assert.ok( true, 'Request completed: bundleable with unit separator' );
129 } ),
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' );
132 } ),
133 api.saveOptions( { foo: null } ).done( function () {
134 assert.ok( true, 'Request completed: reset an option' );
135 } ),
136 api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
137 assert.ok( true, 'Request completed: reset an option, not bundleable' );
138 } )
139 );
140 } );
141 }() );