Merge "Rank aliases in search in order they appear in the messages file."
[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 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 QUnit.test( 'saveOption', 2, 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', 13, 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 switch ( request.requestBody ) {
34 // simple
35 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
36 // two options
37 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
38 // not bundleable
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':
42 // reset an option
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" }' );
49 break;
50 default:
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', 14, 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 switch ( request.requestBody ) {
92 // simple
93 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
94 // two options
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':
101 // reset an option
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" }' );
108 break;
109 default:
110 assert.ok( false, 'Unexpected request: ' + request.requestBody );
111 }
112 } );
113
114 return QUnit.whenPromisesComplete(
115 api.saveOptions( {} ).done( function () {
116 assert.ok( true, 'Request completed: empty case' );
117 } ),
118 api.saveOptions( { foo: 'bar' } ).done( function () {
119 assert.ok( true, 'Request completed: simple' );
120 } ),
121 api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () {
122 assert.ok( true, 'Request completed: two options' );
123 } ),
124 api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () {
125 assert.ok( true, 'Request completed: bundleable with unit separator' );
126 } ),
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' );
129 } ),
130 api.saveOptions( { foo: null } ).done( function () {
131 assert.ok( true, 'Request completed: reset an option' );
132 } ),
133 api.saveOptions( { 'foo|bar=quux': null } ).done( function () {
134 assert.ok( true, 'Request completed: reset an option, not bundleable' );
135 } )
136 );
137 } );
138 }( mediaWiki ) );