| 1 | 'use strict'; |
| 2 | |
| 3 | const { action, assert, utils } = require( 'api-testing' ); |
| 4 | |
| 5 | describe( 'The parse action', () => { |
| 6 | let alice; |
| 7 | const pageTitle = utils.title( 'Parsing_' ); |
| 8 | const edits = {}; |
| 9 | |
| 10 | before( async () => { |
| 11 | [ alice ] = await Promise.all( [ |
| 12 | action.alice() |
| 13 | ] ); |
| 14 | |
| 15 | edits.pageCreation = await alice.edit( pageTitle, { |
| 16 | text: 'This is a \'\'test\'\'' |
| 17 | } ); |
| 18 | } ); |
| 19 | |
| 20 | it( 'supports parsing the current content of a page', async () => { |
| 21 | const result = await alice.action( 'parse', { |
| 22 | page: pageTitle |
| 23 | } ); |
| 24 | |
| 25 | assert.include( result.parse.text[ '*' ], 'This is a <i>test</i>' ); |
| 26 | } ); |
| 27 | |
| 28 | it( 'supports parsing text supplied as a parameter', async () => { |
| 29 | const result = await alice.action( 'parse', { |
| 30 | title: pageTitle, |
| 31 | text: 'This is another \'\'test\'\'' |
| 32 | } ); |
| 33 | |
| 34 | assert.include( result.parse.text[ '*' ], 'another <i>test</i>' ); |
| 35 | } ); |
| 36 | |
| 37 | describe( 'with magic words', () => { |
| 38 | it( 'supports __FORCETOC__', async () => { |
| 39 | const result = await alice.action( 'parse', { |
| 40 | title: pageTitle, |
| 41 | text: '__FORCETOC__\n' + |
| 42 | '== One ==' + |
| 43 | '== Two ==' |
| 44 | } ); |
| 45 | |
| 46 | assert.match( result.parse.text[ '*' ], /id="toc"|property="mw:PageProp\/toc"/ ); |
| 47 | } ); |
| 48 | |
| 49 | it( 'supports __NOTOC__', async () => { |
| 50 | const result = await alice.action( 'parse', { |
| 51 | title: pageTitle, |
| 52 | text: '__NOTOC__\n' + |
| 53 | '== One ==' + |
| 54 | '== Two ==' + |
| 55 | '== Three ==' + |
| 56 | '== Four ==' |
| 57 | } ); |
| 58 | |
| 59 | assert.notMatch( result.parse.text[ '*' ], /id="toc"|property="mw:PageProp\/toc"/ ); |
| 60 | } ); |
| 61 | } ); |
| 62 | |
| 63 | describe( 'with variables', () => { |
| 64 | it( 'supports {{PAGENAMEE}}', async () => { |
| 65 | const result = await alice.action( 'parse', { |
| 66 | title: pageTitle, |
| 67 | text: 'This is {{PAGENAMEE}}' |
| 68 | } ); |
| 69 | |
| 70 | assert.include( result.parse.text[ '*' ], `This is ${ pageTitle }` ); |
| 71 | } ); |
| 72 | it( 'supports {{REVISIONID}} and {{REVISIONUSER}} via parameters', async () => { |
| 73 | const result = await alice.action( 'parse', { |
| 74 | title: pageTitle, |
| 75 | revid: edits.pageCreation.newrevid, |
| 76 | text: 'This is {{REVISIONID}} by {{REVISIONUSER}}' |
| 77 | } ); |
| 78 | |
| 79 | assert.include( |
| 80 | result.parse.text[ '*' ], |
| 81 | `This is ${ edits.pageCreation.newrevid } by ${ edits.pageCreation.param_user }` |
| 82 | ); |
| 83 | } ); |
| 84 | it( 'supports {{REVISIONID}} and {{REVISIONUSER}} of a saved revision', async () => { |
| 85 | const anotherTitle = utils.title( 'Parser_saved_magic_' ); |
| 86 | |
| 87 | const anotherEdit = await alice.edit( anotherTitle, { |
| 88 | text: 'This is {{REVISIONID}} by {{REVISIONUSER}}' |
| 89 | } ); |
| 90 | |
| 91 | const result = await alice.action( 'parse', { |
| 92 | page: anotherTitle |
| 93 | } ); |
| 94 | |
| 95 | assert.include( |
| 96 | result.parse.text[ '*' ], |
| 97 | `This is ${ anotherEdit.newrevid } by ${ anotherEdit.param_user }` |
| 98 | ); |
| 99 | } ); |
| 100 | } ); |
| 101 | |
| 102 | describe( 'with templates', () => { |
| 103 | const templateTitle = utils.title( 'Template:Parsing_' ); |
| 104 | const templateText = '{{{greeting|Hello}}} {{{1|world}}}!'; |
| 105 | |
| 106 | before( async () => { |
| 107 | await alice.edit( templateTitle, { text: templateText } ); |
| 108 | } ); |
| 109 | |
| 110 | it( 'supports optional parameters', async () => { |
| 111 | const result = await alice.action( 'parse', { |
| 112 | title: pageTitle, |
| 113 | text: `Say: {{${ templateTitle }}}` |
| 114 | } ); |
| 115 | |
| 116 | assert.include( result.parse.text[ '*' ], 'Say: Hello world!' ); |
| 117 | } ); |
| 118 | |
| 119 | it( 'supports positional parameters', async () => { |
| 120 | const result = await alice.action( 'parse', { |
| 121 | title: pageTitle, |
| 122 | text: `Say: {{${ templateTitle }|you}}` |
| 123 | } ); |
| 124 | |
| 125 | assert.include( result.parse.text[ '*' ], 'Say: Hello you!' ); |
| 126 | } ); |
| 127 | |
| 128 | it( 'supports named parameters', async () => { |
| 129 | const result = await alice.action( 'parse', { |
| 130 | title: pageTitle, |
| 131 | text: `Say: {{${ templateTitle }|greeting=Ciao}}` |
| 132 | } ); |
| 133 | |
| 134 | assert.include( result.parse.text[ '*' ], 'Say: Ciao world!' ); |
| 135 | } ); |
| 136 | } ); |
| 137 | |
| 138 | describe( 'with parser functions', () => { |
| 139 | it( 'supports {{plural}}', async () => { |
| 140 | const result = await alice.action( 'parse', { |
| 141 | title: pageTitle, |
| 142 | text: '{{plural:1|one|many}} or {{plural:2|one|many}}' |
| 143 | } ); |
| 144 | |
| 145 | assert.include( result.parse.text[ '*' ], 'one or many' ); |
| 146 | } ); |
| 147 | it( 'supports {{ns}}', async () => { |
| 148 | const result = await alice.action( 'parse', { |
| 149 | title: pageTitle, |
| 150 | text: '{{ns:1}}, {{ns:2}}, {{ns:3}}' |
| 151 | } ); |
| 152 | |
| 153 | assert.include( result.parse.text[ '*' ], 'Talk, User, User talk' ); |
| 154 | } ); |
| 155 | it( 'supports {{uc}} and {{lc}}', async () => { |
| 156 | const result = await alice.action( 'parse', { |
| 157 | title: pageTitle, |
| 158 | text: '{{uc:Foo}} or {{lc:Foo}}' |
| 159 | } ); |
| 160 | |
| 161 | assert.include( result.parse.text[ '*' ], 'FOO or foo' ); |
| 162 | } ); |
| 163 | } ); |
| 164 | |
| 165 | } ); |