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