2021-10-13 13:21:04 +00:00
|
|
|
{
|
2024-06-04 03:39:48 +00:00
|
|
|
const toggleCirrusSearchLookup = ( enabled ) => mw.config.set( 'wgVisualEditorConfig', ve.extendObject( {}, mw.config.get( 'wgVisualEditorConfig' ), {
|
2022-05-03 19:01:18 +00:00
|
|
|
cirrusSearchLookup: enabled !== false
|
2022-03-04 13:48:14 +00:00
|
|
|
} ) );
|
2022-05-03 19:01:18 +00:00
|
|
|
|
|
|
|
QUnit.module( 've.ui.MWTemplateTitleInputWidget', ve.test.utils.newMwEnvironment( {
|
2024-08-21 23:29:16 +00:00
|
|
|
messages: {
|
|
|
|
// Force `templateDataInstalled` condition
|
|
|
|
'templatedata-doc-subpage': '(templatedata-doc-subpage)'
|
|
|
|
},
|
2022-05-03 19:01:18 +00:00
|
|
|
// Config will be reset by newMwEnvironment's teardown
|
2024-09-18 03:49:12 +00:00
|
|
|
beforeEach: function () {
|
|
|
|
this.server = this.sandbox.useFakeServer();
|
|
|
|
toggleCirrusSearchLookup();
|
|
|
|
}
|
2022-05-03 19:01:18 +00:00
|
|
|
} ) );
|
2021-03-01 17:02:38 +00:00
|
|
|
|
|
|
|
QUnit.test( 'default prefixsearch', ( assert ) => {
|
2022-05-03 19:01:18 +00:00
|
|
|
toggleCirrusSearchLookup( false );
|
|
|
|
|
2024-05-01 17:25:33 +00:00
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget();
|
|
|
|
const query = 'a';
|
|
|
|
const apiParams = widget.getApiParams( query );
|
2021-03-01 17:02:38 +00:00
|
|
|
|
|
|
|
assert.deepEqual( apiParams, {
|
|
|
|
action: 'query',
|
|
|
|
generator: 'prefixsearch',
|
|
|
|
gpslimit: 10,
|
|
|
|
gpsnamespace: 10,
|
|
|
|
gpssearch: 'a',
|
|
|
|
ppprop: 'disambiguation',
|
|
|
|
prop: [ 'info', 'pageprops' ],
|
|
|
|
redirects: true
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'CirrusSearch: all API parameters', ( assert ) => {
|
2024-05-01 17:25:33 +00:00
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget();
|
|
|
|
const query = 'a';
|
|
|
|
const apiParams = widget.getApiParams( query );
|
2021-03-01 17:02:38 +00:00
|
|
|
|
|
|
|
assert.deepEqual( apiParams, {
|
|
|
|
action: 'query',
|
|
|
|
generator: 'search',
|
|
|
|
gsrlimit: 10,
|
|
|
|
gsrnamespace: 10,
|
2021-05-19 16:51:48 +00:00
|
|
|
gsrprop: 'redirecttitle',
|
2021-03-01 17:02:38 +00:00
|
|
|
gsrsearch: 'a*',
|
|
|
|
ppprop: 'disambiguation',
|
|
|
|
prop: [ 'info', 'pageprops' ],
|
|
|
|
redirects: true
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-05-19 16:51:48 +00:00
|
|
|
QUnit.test( 'CirrusSearch: showRedirectTargets disabled', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget( { showRedirectTargets: false } ),
|
|
|
|
apiParams = widget.getApiParams();
|
|
|
|
|
2021-11-02 01:27:09 +00:00
|
|
|
assert.false( 'gsrprop' in apiParams );
|
2021-05-19 16:51:48 +00:00
|
|
|
} );
|
|
|
|
|
2024-05-01 17:25:33 +00:00
|
|
|
QUnit.test.each( 'CirrusSearch: prefixsearch behavior', [
|
|
|
|
{
|
|
|
|
query: 'a',
|
|
|
|
expected: 'a*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'a ',
|
|
|
|
expected: 'a '
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'ü',
|
|
|
|
expected: 'ü*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '3',
|
|
|
|
expected: '3*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '!!',
|
|
|
|
expected: '!!'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'Foo:',
|
|
|
|
expected: 'Foo:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'Foo:Bar',
|
|
|
|
expected: 'Foo:Bar*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo_',
|
|
|
|
expected: 'foo_'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo-',
|
|
|
|
expected: 'foo-'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo+',
|
|
|
|
expected: 'foo+'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo/',
|
|
|
|
expected: 'foo/'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo~',
|
|
|
|
expected: 'foo~'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foo*',
|
|
|
|
expected: 'foo*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '(foo)',
|
|
|
|
expected: '(foo)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '[foo]',
|
|
|
|
expected: '[foo]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '{foo}',
|
|
|
|
expected: '{foo}'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '"foo"',
|
|
|
|
expected: '"foo"'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'foß',
|
|
|
|
expected: 'foß*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: '中文字',
|
|
|
|
expected: '中文字*'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: 'zhōngwénzì',
|
|
|
|
expected: 'zhōngwénzì*'
|
|
|
|
}
|
|
|
|
], ( assert, data ) => {
|
2021-03-01 17:02:38 +00:00
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget();
|
2024-05-01 17:25:33 +00:00
|
|
|
const apiParams = widget.getApiParams( data.query );
|
2021-03-01 17:02:38 +00:00
|
|
|
|
2024-05-01 17:25:33 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
apiParams.gsrsearch,
|
|
|
|
data.expected,
|
|
|
|
'Searching for ' + data.query
|
|
|
|
);
|
2021-03-01 17:02:38 +00:00
|
|
|
} );
|
2021-05-19 16:51:48 +00:00
|
|
|
|
2024-05-01 17:25:33 +00:00
|
|
|
QUnit.test( 'CirrusSearch with prefixsearch fallback', async function ( assert ) {
|
2024-09-18 03:49:12 +00:00
|
|
|
const api = new mw.Api();
|
2022-05-03 19:01:18 +00:00
|
|
|
this.sandbox.stub( api, 'get' )
|
|
|
|
.onFirstCall().returns( ve.createDeferred()
|
|
|
|
.resolve( { query: {
|
|
|
|
pages: [
|
|
|
|
{ pageid: 101, title: 'B' },
|
|
|
|
{ pageid: 102, title: 'A' },
|
|
|
|
// Documentation subpage, expected to be stripped
|
2024-05-30 15:56:49 +00:00
|
|
|
{ pageid: 103, title: 'A/(templatedata-doc-subpage)', index: 2 }
|
2022-05-03 19:01:18 +00:00
|
|
|
],
|
|
|
|
redirects: [
|
|
|
|
// Alternative source for indexes, expected to be copied to the pages array
|
|
|
|
{ from: '', to: 'B', index: 1 },
|
|
|
|
{ from: '', to: 'A', index: 0 }
|
|
|
|
]
|
|
|
|
} } )
|
2024-04-30 16:44:25 +00:00
|
|
|
.promise( { abort: () => {} } )
|
2022-05-03 19:01:18 +00:00
|
|
|
)
|
|
|
|
.onSecondCall().returns( ve.createDeferred()
|
|
|
|
.resolve( { query: { pages: [
|
|
|
|
// Duplicate found by CirrusSearch (above) and prefixsearch
|
|
|
|
{ pageid: 102, title: 'A', index: 2 },
|
|
|
|
// New prefixsearch matches, expected to be prepended, in order of relevance
|
|
|
|
{ pageid: 201, title: 'D', index: 1 },
|
|
|
|
{ pageid: 202, title: 'C', index: 0 }
|
|
|
|
] } } )
|
|
|
|
)
|
|
|
|
.onThirdCall().returns( ve.createDeferred()
|
|
|
|
.resolve( /* we can skip the templatedata request for this test */ )
|
|
|
|
);
|
|
|
|
|
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget( { api, showDescriptions: true } );
|
|
|
|
widget.setValue( 'something' );
|
2024-05-01 17:25:33 +00:00
|
|
|
|
|
|
|
const response = await widget.getLookupRequest();
|
|
|
|
assert.deepEqual( response.query.pages, [
|
|
|
|
{ pageid: 202, title: 'C', index: -10 },
|
|
|
|
{ pageid: 201, title: 'D', index: -9 },
|
|
|
|
{ pageid: 102, title: 'A', index: 0 },
|
|
|
|
{ pageid: 101, title: 'B', index: 1 }
|
|
|
|
] );
|
2022-05-03 19:01:18 +00:00
|
|
|
} );
|
|
|
|
|
2021-05-19 16:51:48 +00:00
|
|
|
QUnit.test( 'CirrusSearch: redirect is forwarded to the TitleOptionWidget', ( assert ) => {
|
2024-05-01 17:25:33 +00:00
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget();
|
|
|
|
const originalData = { redirecttitle: 'Template:From' };
|
|
|
|
const data = widget.getOptionWidgetData( 'Template:To', { originalData } );
|
2021-05-19 16:51:48 +00:00
|
|
|
|
|
|
|
assert.strictEqual( data.redirecttitle, 'Template:From' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'CirrusSearch: redirect appears in the description', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWTemplateTitleInputWidget();
|
|
|
|
|
|
|
|
let option = widget.createOptionWidget( { redirecttitle: 'Template:From' } );
|
|
|
|
assert.strictEqual(
|
|
|
|
option.$element.find( '.ve-ui-mwTemplateTitleInputWidget-redirectedfrom' ).text(),
|
|
|
|
'(redirectedfrom: From)'
|
|
|
|
);
|
|
|
|
|
|
|
|
widget.relative = false;
|
|
|
|
option = widget.createOptionWidget( { redirecttitle: 'Template:From' } );
|
|
|
|
assert.strictEqual(
|
|
|
|
option.$element.find( '.ve-ui-mwTemplateTitleInputWidget-redirectedfrom' ).text(),
|
|
|
|
'(redirectedfrom: Template:From)'
|
|
|
|
);
|
|
|
|
} );
|
2021-10-13 13:21:04 +00:00
|
|
|
}
|