Special case opus mime detction
[mediawiki.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.edit.test.js
blobf83f66cc34feb26e2751a8fdcb8c9e189443abdd
1 ( function ( mw, $ ) {
2         QUnit.module( 'mediawiki.api.edit', QUnit.newMwEnvironment( {
3                 setup: function () {
4                         this.server = this.sandbox.useFakeServer();
5                         this.server.respondImmediately = true;
6                 }
7         } ) );
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                 } );
41                 return new mw.Api()
42                         .edit( 'Sandbox', function ( revision ) {
43                                 return revision.content.replace( 'Sand', 'Box' );
44                         } )
45                         .then( function ( edit ) {
46                                 assert.equal( edit.newrevid, 13 );
47                         } );
48         } );
50         QUnit.test( 'edit( title, transform Promise )', function ( assert ) {
51                 this.server.respond( function ( req ) {
52                         if ( /query.+titles=Async/.test( req.url ) ) {
53                                 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
54                                         curtimestamp: '2016-02-02T12:00:00Z',
55                                         query: {
56                                                 pages: [ {
57                                                         pageid: 4,
58                                                         ns: 0,
59                                                         title:  'Async',
60                                                         revisions: [ {
61                                                                 timestamp: '2016-02-01T12:00:00Z',
62                                                                 contentformat: 'text/x-wiki',
63                                                                 contentmodel: 'wikitext',
64                                                                 content: 'Async.'
65                                                         } ]
66                                                 } ]
67                                         }
68                                 } ) );
69                         }
70                         if ( /edit.+basetimestamp=2016-02-01.+starttimestamp=2016-02-02.+text=Promise%2E/.test( req.requestBody ) ) {
71                                 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
72                                         edit: {
73                                                 result: 'Success',
74                                                 oldrevid: 21,
75                                                 newrevid: 23,
76                                                 newtimestamp: '2016-02-03T12:00:00Z'
77                                         }
78                                 } ) );
79                         }
80                 } );
82                 return new mw.Api()
83                         .edit( 'Async', function ( revision ) {
84                                 return $.Deferred().resolve( revision.content.replace( 'Async', 'Promise' ) );
85                         } )
86                         .then( function ( edit ) {
87                                 assert.equal( edit.newrevid, 23 );
88                         } );
89         } );
91         QUnit.test( 'edit( title, transform Object )', function ( assert ) {
92                 this.server.respond( function ( req ) {
93                         if ( /query.+titles=Param/.test( req.url ) ) {
94                                 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
95                                         curtimestamp: '2016-03-02T12:00:00Z',
96                                         query: {
97                                                 pages: [ {
98                                                         pageid: 3,
99                                                         ns: 0,
100                                                         title:  'Param',
101                                                         revisions: [ {
102                                                                 timestamp: '2016-03-01T12:00:00Z',
103                                                                 contentformat: 'text/x-wiki',
104                                                                 contentmodel: 'wikitext',
105                                                                 content: '...'
106                                                         } ]
107                                                 } ]
108                                         }
109                                 } ) );
110                         }
111                         if ( /edit.+basetimestamp=2016-03-01.+starttimestamp=2016-03-02.+text=Content&summary=Sum/.test( req.requestBody ) ) {
112                                 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
113                                         edit: {
114                                                 result: 'Success',
115                                                 oldrevid: 31,
116                                                 newrevid: 33,
117                                                 newtimestamp: '2016-03-03T12:00:00Z'
118                                         }
119                                 } ) );
120                         }
121                 } );
123                 return new mw.Api()
124                         .edit( 'Param', function () {
125                                 return { text: 'Content', summary: 'Sum' };
126                         } )
127                         .then( function ( edit ) {
128                                 assert.equal( edit.newrevid, 33 );
129                         } );
130         } );
132         QUnit.test( 'create( title, content )', function ( assert ) {
133                 this.server.respond( function ( req ) {
134                         if ( /edit.+text=Sand/.test( req.requestBody ) ) {
135                                 req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
136                                         edit: {
137                                                 'new': true,
138                                                 result: 'Success',
139                                                 newrevid: 41,
140                                                 newtimestamp: '2016-04-01T12:00:00Z'
141                                         }
142                                 } ) );
143                         }
144                 } );
146                 return new mw.Api()
147                         .create( 'Sandbox', { summary: 'Load sand particles.' }, 'Sand.' )
148                         .then( function ( page ) {
149                                 assert.equal( page.newrevid, 41 );
150                         } );
151         } );
153 }( mediaWiki, jQuery ) );