resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.edit.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.api.edit', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.server.respondImmediately = true;
6 }
7 } ) );
8
9 QUnit.test( 'edit( title, transform String )', function ( assert ) {
10 this.server.respond( function ( req ) {
11 if ( /query.+titles=Sandbox/.test( req.url ) ) {
12 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
13 curtimestamp: '2016-01-02T12:00:00Z',
14 query: {
15 pages: [ {
16 pageid: 1,
17 ns: 0,
18 title: 'Sandbox',
19 revisions: [ {
20 timestamp: '2016-01-01T12:00:00Z',
21 contentformat: 'text/x-wiki',
22 contentmodel: 'wikitext',
23 content: 'Sand.'
24 } ]
25 } ]
26 }
27 } ) );
28 }
29 if ( /edit.+basetimestamp=2016-01-01.+starttimestamp=2016-01-02.+text=Box%2E/.test( req.requestBody ) ) {
30 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
31 edit: {
32 result: 'Success',
33 oldrevid: 11,
34 newrevid: 13,
35 newtimestamp: '2016-01-03T12:00:00Z'
36 }
37 } ) );
38 }
39 } );
40
41 return new mw.Api()
42 .edit( 'Sandbox', function ( revision ) {
43 return revision.content.replace( 'Sand', 'Box' );
44 } )
45 .then( function ( edit ) {
46 assert.strictEqual( edit.newrevid, 13 );
47 } );
48 } );
49
50 QUnit.test( 'edit( mw.Title, transform String )', function ( assert ) {
51 this.server.respond( function ( req ) {
52 if ( /query.+titles=Sandbox/.test( req.url ) ) {
53 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
54 curtimestamp: '2016-01-02T12:00:00Z',
55 query: {
56 pages: [ {
57 pageid: 1,
58 ns: 0,
59 title: 'Sandbox',
60 revisions: [ {
61 timestamp: '2016-01-01T12:00:00Z',
62 contentformat: 'text/x-wiki',
63 contentmodel: 'wikitext',
64 content: 'Sand.'
65 } ]
66 } ]
67 }
68 } ) );
69 }
70 if ( /edit.+basetimestamp=2016-01-01.+starttimestamp=2016-01-02.+text=Box%2E/.test( req.requestBody ) ) {
71 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
72 edit: {
73 result: 'Success',
74 oldrevid: 11,
75 newrevid: 13,
76 newtimestamp: '2016-01-03T12:00:00Z'
77 }
78 } ) );
79 }
80 } );
81
82 return new mw.Api()
83 .edit( new mw.Title( 'Sandbox' ), function ( revision ) {
84 return revision.content.replace( 'Sand', 'Box' );
85 } )
86 .then( function ( edit ) {
87 assert.strictEqual( edit.newrevid, 13 );
88 } );
89 } );
90
91 QUnit.test( 'edit( title, transform Promise )', function ( assert ) {
92 this.server.respond( function ( req ) {
93 if ( /query.+titles=Async/.test( req.url ) ) {
94 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
95 curtimestamp: '2016-02-02T12:00:00Z',
96 query: {
97 pages: [ {
98 pageid: 4,
99 ns: 0,
100 title: 'Async',
101 revisions: [ {
102 timestamp: '2016-02-01T12:00:00Z',
103 contentformat: 'text/x-wiki',
104 contentmodel: 'wikitext',
105 content: 'Async.'
106 } ]
107 } ]
108 }
109 } ) );
110 }
111 if ( /edit.+basetimestamp=2016-02-01.+starttimestamp=2016-02-02.+text=Promise%2E/.test( req.requestBody ) ) {
112 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
113 edit: {
114 result: 'Success',
115 oldrevid: 21,
116 newrevid: 23,
117 newtimestamp: '2016-02-03T12:00:00Z'
118 }
119 } ) );
120 }
121 } );
122
123 return new mw.Api()
124 .edit( 'Async', function ( revision ) {
125 return $.Deferred().resolve( revision.content.replace( 'Async', 'Promise' ) );
126 } )
127 .then( function ( edit ) {
128 assert.strictEqual( edit.newrevid, 23 );
129 } );
130 } );
131
132 QUnit.test( 'edit( title, transform Object )', function ( assert ) {
133 this.server.respond( function ( req ) {
134 if ( /query.+titles=Param/.test( req.url ) ) {
135 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
136 curtimestamp: '2016-03-02T12:00:00Z',
137 query: {
138 pages: [ {
139 pageid: 3,
140 ns: 0,
141 title: 'Param',
142 revisions: [ {
143 timestamp: '2016-03-01T12:00:00Z',
144 contentformat: 'text/x-wiki',
145 contentmodel: 'wikitext',
146 content: '...'
147 } ]
148 } ]
149 }
150 } ) );
151 }
152 if ( /edit.+basetimestamp=2016-03-01.+starttimestamp=2016-03-02.+text=Content&summary=Sum/.test( req.requestBody ) ) {
153 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
154 edit: {
155 result: 'Success',
156 oldrevid: 31,
157 newrevid: 33,
158 newtimestamp: '2016-03-03T12:00:00Z'
159 }
160 } ) );
161 }
162 } );
163
164 return new mw.Api()
165 .edit( 'Param', function () {
166 return { text: 'Content', summary: 'Sum' };
167 } )
168 .then( function ( edit ) {
169 assert.strictEqual( edit.newrevid, 33 );
170 } );
171 } );
172
173 QUnit.test( 'edit( invalid-title, transform String )', function ( assert ) {
174 this.server.respond( function ( req ) {
175 if ( /query.+titles=%1F%7C/.test( req.url ) ) {
176 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
177 query: {
178 pages: [ {
179 title: '|',
180 invalidreason: 'The requested page title contains invalid characters: "|".',
181 invalid: true
182 } ]
183 }
184 } ) );
185 }
186 } );
187
188 return new mw.Api()
189 .edit( '|', function ( revision ) {
190 return revision.content.replace( 'Sand', 'Box' );
191 } )
192 .then( function () {
193 return $.Deferred().reject( 'Unexpected success' );
194 }, function ( reason ) {
195 assert.strictEqual( reason, 'invalidtitle' );
196 } );
197 } );
198
199 QUnit.test( 'create( title, content )', function ( assert ) {
200 this.server.respond( function ( req ) {
201 if ( /edit.+text=Sand/.test( req.requestBody ) ) {
202 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
203 edit: {
204 new: true,
205 result: 'Success',
206 newrevid: 41,
207 newtimestamp: '2016-04-01T12:00:00Z'
208 }
209 } ) );
210 }
211 } );
212
213 return new mw.Api()
214 .create( 'Sandbox', { summary: 'Load sand particles.' }, 'Sand.' )
215 .then( function ( page ) {
216 assert.strictEqual( page.newrevid, 41 );
217 } );
218 } );
219
220 }() );