| 1 |
'use strict'; |
| 2 |
|
| 3 |
const { action, assert, utils } = require( 'api-testing' ); |
| 4 |
|
| 5 |
describe( 'Visual Editor API', () => { |
| 6 |
const title = utils.title( 'VisualEditor' ); |
| 7 |
|
| 8 |
let alice; |
| 9 |
let pageInfo; |
| 10 |
|
| 11 |
before( async () => { |
| 12 |
const textX = 'Hello World! {{Template Requests}}'; |
| 13 |
|
| 14 |
alice = await action.alice(); |
| 15 |
pageInfo = await alice.edit( title, { text: textX } ); |
| 16 |
} ); |
| 17 |
|
| 18 |
|
| 19 |
it( 'can load metadata', async () => { |
| 20 |
const result = await alice.action( 'visualeditor', { page: title, paction: 'metadata' } ); |
| 21 |
assert.equal( result.visualeditor.oldid, pageInfo.newrevid ); |
| 22 |
assert.nestedProperty( result.visualeditor, 'copyrightWarning' ); |
| 23 |
assert.nestedProperty( result.visualeditor, 'checkboxesDef' ); |
| 24 |
assert.nestedProperty( result.visualeditor, 'checkboxesMessages' ); |
| 25 |
assert.equal( result.visualeditor.oldid, pageInfo.newrevid ); |
| 26 |
} ); |
| 27 |
|
| 28 |
it( 'able to parse', async () => { |
| 29 |
const result = await alice.action( 'visualeditor', { page: title, paction: 'parse' } ); |
| 30 |
|
| 31 |
assert.equal( result.visualeditor.result, 'success' ); |
| 32 |
assert.nestedProperty( result.visualeditor, 'copyrightWarning' ); |
| 33 |
assert.nestedProperty( result.visualeditor, 'checkboxesDef' ); |
| 34 |
assert.nestedProperty( result.visualeditor, 'checkboxesMessages' ); |
| 35 |
|
| 36 |
assert.nestedProperty( result.visualeditor, 'etag' ); |
| 37 |
assert.match( result.visualeditor.etag, /^(W\/)?".*\d+\// ); |
| 38 |
|
| 39 |
assert.nestedProperty( result.visualeditor, 'oldid' ); |
| 40 |
assert.equal( result.visualeditor.oldid, pageInfo.newrevid ); |
| 41 |
|
| 42 |
assert.nestedProperty( result.visualeditor, 'content' ); |
| 43 |
assert.include( result.visualeditor.content, 'Hello World!' ); |
| 44 |
assert.include( result.visualeditor.content, '<html' ); |
| 45 |
} ); |
| 46 |
|
| 47 |
it( 'able to parsefragment', async () => { |
| 48 |
const result = await alice.action( 'visualeditor', { page: title, paction: 'parsefragment', wikitext: 'wonderer' } ); |
| 49 |
assert.equal( result.visualeditor.result, 'success' ); |
| 50 |
|
| 51 |
assert.nestedProperty( result.visualeditor, 'content' ); |
| 52 |
assert.include( result.visualeditor.content, 'wonderer' ); |
| 53 |
assert.notInclude( result.visualeditor.content, '<html' ); |
| 54 |
} ); |
| 55 |
|
| 56 |
it( 'templatesUsed', async () => { |
| 57 |
const result = await alice.action( 'visualeditor', { page: title, paction: 'templatesused', wikitext: 'test' } ); |
| 58 |
assert.include( result.visualeditor, 'Template Requests' ); |
| 59 |
} ); |
| 60 |
|
| 61 |
it( 'can load wikitext', async () => { |
| 62 |
const result = await alice.action( 'visualeditor', { page: title, paction: 'wikitext' } ); |
| 63 |
assert.equal( result.visualeditor.result, 'success' ); |
| 64 |
} ); |
| 65 |
|
| 66 |
|
| 67 |
const page = utils.title( 'VisualEditorNew' ); |
| 68 |
|
| 69 |
describe( 'Editing', () => { |
| 70 |
it( 'Should create page, edit and save page with HTML', async () => { |
| 71 |
const token = await alice.token(); |
| 72 |
const html = '<p>save paction</p>'; |
| 73 |
const summary = 'save test workflow'; |
| 74 |
const result = await alice.action( |
| 75 |
'visualeditoredit', |
| 76 |
{ |
| 77 |
page: page, |
| 78 |
paction: 'save', |
| 79 |
token: token, |
| 80 |
html: html, |
| 81 |
summary: summary |
| 82 |
}, |
| 83 |
'post' |
| 84 |
); |
| 85 |
|
| 86 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 87 |
} ); |
| 88 |
|
| 89 |
it( 'Should refuse to edit with a bad token', async () => { |
| 90 |
const token = 'dshfkjdsakf'; |
| 91 |
const html = '<p>save paction</p>'; |
| 92 |
const summary = 'save test workflow'; |
| 93 |
const error = await alice.actionError( |
| 94 |
'visualeditoredit', |
| 95 |
{ |
| 96 |
page: page, |
| 97 |
paction: 'save', |
| 98 |
token: token, |
| 99 |
html: html, |
| 100 |
summary: summary |
| 101 |
}, |
| 102 |
'post' |
| 103 |
); |
| 104 |
|
| 105 |
assert.equal( error.code, 'badtoken' ); |
| 106 |
} ); |
| 107 |
|
| 108 |
it( 'Should use selser when editing', async () => { |
| 109 |
const token = await alice.token(); |
| 110 |
let result; |
| 111 |
|
| 112 |
|
| 113 |
const originalWikitext = '*a\n* b\n* <i>c</I>'; |
| 114 |
|
| 115 |
result = await alice.action( |
| 116 |
'visualeditoredit', |
| 117 |
{ |
| 118 |
page, |
| 119 |
paction: 'save', |
| 120 |
token, |
| 121 |
wikitext: originalWikitext, |
| 122 |
summary: 'editing wikitext' |
| 123 |
}, |
| 124 |
'post' |
| 125 |
); |
| 126 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 127 |
|
| 128 |
|
| 129 |
result = await alice.action( 'visualeditor', { page, paction: 'parse' } ); |
| 130 |
assert.equal( result.visualeditor.result, 'success' ); |
| 131 |
|
| 132 |
let html = result.visualeditor.content; |
| 133 |
const etag = result.visualeditor.etag; |
| 134 |
const oldid = result.visualeditor.oldid; |
| 135 |
|
| 136 |
|
| 137 |
html = html.replace( '</body>', '<p>More Text</p></body>' ); |
| 138 |
result = await alice.action( |
| 139 |
'visualeditoredit', |
| 140 |
{ |
| 141 |
page, |
| 142 |
paction: 'save', |
| 143 |
token, |
| 144 |
html, |
| 145 |
etag, |
| 146 |
oldid, |
| 147 |
summary: 'appending html' |
| 148 |
}, |
| 149 |
'post' |
| 150 |
); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 157 |
|
| 158 |
|
| 159 |
result = await alice.action( 'visualeditor', { page, paction: 'wikitext' } ); |
| 160 |
assert.equal( result.visualeditor.result, 'success' ); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
const newWikitext = result.visualeditor.content; |
| 165 |
assert.include( newWikitext, originalWikitext ); |
| 166 |
assert.include( newWikitext, 'More Text' ); |
| 167 |
} ); |
| 168 |
|
| 169 |
it( 'Should save edit after switching from source mode (T321862)', async () => { |
| 170 |
const token = await alice.token(); |
| 171 |
let result; |
| 172 |
|
| 173 |
|
| 174 |
const originalWikitext = '*a\n* b\n* <i>c</I>'; |
| 175 |
|
| 176 |
result = await alice.action( |
| 177 |
'visualeditoredit', |
| 178 |
{ |
| 179 |
page, |
| 180 |
paction: 'save', |
| 181 |
token, |
| 182 |
wikitext: originalWikitext, |
| 183 |
summary: 'editing wikitext' |
| 184 |
}, |
| 185 |
'post' |
| 186 |
); |
| 187 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 188 |
|
| 189 |
|
| 190 |
const modifiedWikitext = originalWikitext + '\nfirst addition'; |
| 191 |
|
| 192 |
|
| 193 |
result = await alice.action( |
| 194 |
'visualeditor', |
| 195 |
{ |
| 196 |
page, |
| 197 |
paction: 'parse', |
| 198 |
wikitext: modifiedWikitext, |
| 199 |
stash: 'yes' |
| 200 |
}, |
| 201 |
'post' |
| 202 |
); |
| 203 |
assert.equal( result.visualeditor.result, 'success' ); |
| 204 |
|
| 205 |
|
| 206 |
const html = result.visualeditor.content; |
| 207 |
const etag = result.visualeditor.etag; |
| 208 |
const modifiedHtml = html.replace( '</body>', '<p>second addition</p></body>' ); |
| 209 |
|
| 210 |
result = await alice.action( |
| 211 |
'visualeditoredit', |
| 212 |
{ |
| 213 |
page, |
| 214 |
paction: 'save', |
| 215 |
token, |
| 216 |
html: modifiedHtml, |
| 217 |
etag, |
| 218 |
summary: 'appending html' |
| 219 |
}, |
| 220 |
'post' |
| 221 |
); |
| 222 |
|
| 223 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 224 |
|
| 225 |
|
| 226 |
result = await alice.action( 'visualeditor', { page, paction: 'wikitext' } ); |
| 227 |
assert.equal( result.visualeditor.result, 'success' ); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
const newWikitext = result.visualeditor.content; |
| 232 |
assert.include( newWikitext, originalWikitext ); |
| 233 |
assert.include( newWikitext, 'first addition' ); |
| 234 |
assert.include( newWikitext, 'second addition' ); |
| 235 |
} ); |
| 236 |
} ); |
| 237 |
|
| 238 |
it( 'Should show page diff', async () => { |
| 239 |
const token = await alice.token(); |
| 240 |
const html = '<p>diff paction</p>'; |
| 241 |
const summary = 'diff page test workflow'; |
| 242 |
const result = await alice.action( |
| 243 |
'visualeditoredit', |
| 244 |
{ |
| 245 |
page: title, |
| 246 |
paction: 'diff', |
| 247 |
token: token, |
| 248 |
html: html, |
| 249 |
summary: summary |
| 250 |
}, |
| 251 |
'post' |
| 252 |
); |
| 253 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 254 |
} ); |
| 255 |
|
| 256 |
it( 'Should serialize page', async () => { |
| 257 |
const token = await alice.token(); |
| 258 |
const html = '<h2>serialize paction test</h2>'; |
| 259 |
const summary = 'serialize page test workflow'; |
| 260 |
const result = await alice.action( |
| 261 |
'visualeditoredit', |
| 262 |
{ |
| 263 |
page: title, |
| 264 |
paction: 'serialize', |
| 265 |
token: token, |
| 266 |
html: html, |
| 267 |
summary: summary |
| 268 |
}, |
| 269 |
'post' |
| 270 |
); |
| 271 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 272 |
|
| 273 |
|
| 274 |
assert.equal( result.visualeditoredit.content.trim(), '== serialize paction test ==' ); |
| 275 |
} ); |
| 276 |
|
| 277 |
it( 'Should serialize page for cache', async () => { |
| 278 |
const token = await alice.token(); |
| 279 |
const html = '<p>serialize for cache paction</p>'; |
| 280 |
const summary = 'serializeforcache create page test workflow'; |
| 281 |
const result = await alice.action( |
| 282 |
'visualeditoredit', |
| 283 |
{ |
| 284 |
page: title, |
| 285 |
paction: 'serializeforcache', |
| 286 |
token: token, |
| 287 |
html: html, |
| 288 |
summary: summary |
| 289 |
}, |
| 290 |
'post' |
| 291 |
); |
| 292 |
assert.equal( result.visualeditoredit.result, 'success' ); |
| 293 |
} ); |
| 294 |
} ); |