2020-05-14 21:14:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-02-11 09:59:41 +00:00
|
|
|
const { action, assert, REST, utils, wiki } = require( 'api-testing' );
|
2020-01-15 01:20:50 +00:00
|
|
|
|
|
|
|
|
describe( 'Search', () => {
|
2020-05-20 18:52:47 +00:00
|
|
|
const client = new REST( 'rest.php/v1' );
|
2020-03-25 17:21:12 +00:00
|
|
|
const sharedTitleTerm = 'X' + utils.uniq(); // NOTE: must start with upper-case letter
|
|
|
|
|
const pageWithBothTerms = utils.title( `${sharedTitleTerm}XXX` );
|
|
|
|
|
const pageWithOneTerm = utils.title( `${sharedTitleTerm}YYY` );
|
2020-02-28 11:59:25 +00:00
|
|
|
const pageWithOwnTitle = utils.title( 'ZZZ' );
|
|
|
|
|
const searchTerm = utils.uniq();
|
|
|
|
|
const searchTerm2 = utils.uniq();
|
2020-01-15 01:20:50 +00:00
|
|
|
let alice;
|
|
|
|
|
let mindy;
|
|
|
|
|
|
|
|
|
|
before( async () => {
|
|
|
|
|
alice = await action.alice();
|
|
|
|
|
mindy = await action.mindy();
|
2020-02-28 11:59:25 +00:00
|
|
|
await alice.edit( pageWithBothTerms, { text: `${searchTerm} ${searchTerm2}` } );
|
|
|
|
|
await alice.edit( pageWithOneTerm, { text: searchTerm2 } );
|
|
|
|
|
await alice.edit( pageWithOwnTitle, { text: pageWithOwnTitle } );
|
2022-02-11 09:59:41 +00:00
|
|
|
await wiki.runAllJobs();
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
describe( 'GET /search/page?q={term}', () => {
|
|
|
|
|
it( 'should return empty array when search term has no title or text matches', async () => {
|
|
|
|
|
const nonExistentTerm = utils.uniq();
|
|
|
|
|
const { body } = await client.get( `/search/page?q=${nonExistentTerm}` );
|
|
|
|
|
const noResultsResponse = { pages: [] };
|
|
|
|
|
assert.deepEqual( noResultsResponse, body );
|
|
|
|
|
} );
|
|
|
|
|
it( 'should return array of pages when there is only a text match', async () => {
|
2020-03-25 21:33:27 +00:00
|
|
|
const { body, headers } = await client.get( `/search/page?q=${searchTerm}` );
|
2020-01-15 01:20:50 +00:00
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
const returnPage = body.pages[ 0 ];
|
|
|
|
|
assert.nestedProperty( returnPage, 'title' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'id' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'key' );
|
2020-02-28 11:59:25 +00:00
|
|
|
assert.nestedProperty( returnPage, 'excerpt' );
|
2020-04-15 12:33:15 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
|
|
|
|
|
assert.nestedPropertyVal( returnPage, 'description', null );
|
2022-01-13 19:35:27 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'matched_title', null );
|
2023-03-31 08:00:01 +00:00
|
|
|
assert.include( returnPage.excerpt, `<span class="searchmatch">${searchTerm}</span>` );
|
2020-03-25 21:33:27 +00:00
|
|
|
|
|
|
|
|
// full-text search should not have cache-control
|
|
|
|
|
if ( headers[ 'cache-control' ] ) {
|
|
|
|
|
assert.notMatch( headers[ 'cache-control' ], /\bpublic\b/ );
|
|
|
|
|
assert.notMatch( headers[ 'cache-control' ], /\bmax-age=0*[1-9]\b/ );
|
|
|
|
|
assert.notMatch( headers[ 'cache-control' ], /\bs-maxage=0*[1-9]\b/ );
|
|
|
|
|
}
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should return array of pages when there is only title match', async () => {
|
2020-02-28 11:59:25 +00:00
|
|
|
const { body } = await client.get( `/search/page?q=${pageWithBothTerms}` );
|
2020-01-15 01:20:50 +00:00
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
const returnPage = body.pages[ 0 ];
|
|
|
|
|
assert.nestedProperty( returnPage, 'title' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'id' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'key' );
|
|
|
|
|
assert.nestedPropertyVal( returnPage, 'excerpt', null );
|
2020-04-15 12:33:15 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
|
|
|
|
|
assert.nestedPropertyVal( returnPage, 'description', null );
|
2022-01-13 19:35:27 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'matched_title', null );
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should return a single page when there is a title and text match on the same page', async () => {
|
2020-02-28 11:59:25 +00:00
|
|
|
const { body } = await client.get( `/search/page?q=${pageWithOwnTitle}` );
|
2020-01-15 01:20:50 +00:00
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
const returnPage = body.pages[ 0 ];
|
|
|
|
|
assert.nestedProperty( returnPage, 'title' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'id' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'key' );
|
2020-02-28 11:59:25 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'title', pageWithOwnTitle );
|
2020-04-15 12:33:15 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
|
|
|
|
|
assert.nestedPropertyVal( returnPage, 'description', null );
|
2022-01-13 19:35:27 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'matched_title', null );
|
2020-02-28 11:59:25 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should return two pages when both pages match', async () => {
|
|
|
|
|
const { body } = await client.get( `/search/page?q=${searchTerm2}` );
|
|
|
|
|
assert.lengthOf( body.pages, 2 );
|
|
|
|
|
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ];
|
2020-03-25 17:21:12 +00:00
|
|
|
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ];
|
|
|
|
|
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) );
|
2020-02-28 11:59:25 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should return only one page when two pages match but limit is 1', async () => {
|
|
|
|
|
const { body } = await client.get( `/search/page?q=${searchTerm2}&limit=1` );
|
|
|
|
|
assert.lengthOf( body.pages, 1 );
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should not return results when page with term has been deleted', async () => {
|
|
|
|
|
const pageToDelete = 'Delete Page';
|
|
|
|
|
const deleteTerm = `Delete_${utils.uniq()}`;
|
|
|
|
|
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } );
|
|
|
|
|
await mindy.action( 'delete', {
|
|
|
|
|
title,
|
|
|
|
|
summary: 'testing',
|
|
|
|
|
token: await mindy.token( 'csrf' )
|
|
|
|
|
}, 'POST' );
|
2022-02-11 09:59:41 +00:00
|
|
|
await wiki.runAllJobs();
|
2020-01-15 01:20:50 +00:00
|
|
|
const { body } = await client.get( `/search/page?q=${deleteTerm}` );
|
|
|
|
|
assert.lengthOf( body.pages, 0 );
|
|
|
|
|
} );
|
2022-01-13 19:35:27 +00:00
|
|
|
it( 'should ignore duplicate redirect source and target if both pages are a match', async () => {
|
|
|
|
|
const redirectSource = utils.title( 'redirect_source_' );
|
|
|
|
|
const redirectTarget = utils.title( 'redirect_target_' );
|
|
|
|
|
const uniquePageText = utils.uniq();
|
|
|
|
|
|
|
|
|
|
await alice.edit( redirectSource,
|
|
|
|
|
{ text: `#REDIRECT [[ ${redirectTarget} ]]. ${uniquePageText}.` }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: `${uniquePageText}` } );
|
|
|
|
|
|
2022-02-11 09:59:41 +00:00
|
|
|
await wiki.runAllJobs();
|
2022-01-13 19:35:27 +00:00
|
|
|
const { body } = await client.get( `/search/page?q=${uniquePageText}` );
|
|
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle );
|
|
|
|
|
} );
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|
2020-03-25 17:21:12 +00:00
|
|
|
|
|
|
|
|
describe( 'GET /search/title?q={term}', () => {
|
|
|
|
|
it( 'should return empty array when search term has no title matches', async () => {
|
|
|
|
|
const nonExistentTerm = utils.uniq();
|
|
|
|
|
const { body } = await client.get( `/search/title?q=${nonExistentTerm}` );
|
|
|
|
|
assert.lengthOf( body.pages, 0 );
|
|
|
|
|
} );
|
|
|
|
|
it( 'should not return pages when there is only a text match', async () => {
|
|
|
|
|
const { body } = await client.get( `/search/title?q=${searchTerm}` );
|
|
|
|
|
assert.lengthOf( body.pages, 0 );
|
|
|
|
|
} );
|
|
|
|
|
it( 'should return array of pages when there is a title match', async () => {
|
2020-03-25 21:33:27 +00:00
|
|
|
const { body, headers } = await client.get( `/search/title?q=${pageWithBothTerms}` );
|
2020-03-25 17:21:12 +00:00
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
const returnPage = body.pages[ 0 ];
|
|
|
|
|
assert.nestedProperty( returnPage, 'title' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'id' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'key' );
|
|
|
|
|
assert.nestedProperty( returnPage, 'excerpt' );
|
2020-04-15 12:33:15 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
|
|
|
|
|
assert.nestedPropertyVal( returnPage, 'description', null );
|
2022-01-13 19:35:27 +00:00
|
|
|
assert.nestedPropertyVal( returnPage, 'matched_title', null );
|
2020-03-25 21:33:27 +00:00
|
|
|
|
|
|
|
|
// completion search should encourage caching
|
|
|
|
|
assert.nestedProperty( headers, 'cache-control' );
|
|
|
|
|
assert.match( headers[ 'cache-control' ], /\bpublic\b/ );
|
|
|
|
|
assert.match( headers[ 'cache-control' ], /\bmax-age=[1-9]\d*/ );
|
2020-03-25 17:21:12 +00:00
|
|
|
} );
|
|
|
|
|
it( 'should return two pages when both pages match', async () => {
|
|
|
|
|
const { body } = await client.get( `/search/title?q=${sharedTitleTerm}` );
|
|
|
|
|
assert.lengthOf( body.pages, 2 );
|
|
|
|
|
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ];
|
|
|
|
|
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ];
|
|
|
|
|
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) );
|
|
|
|
|
} );
|
|
|
|
|
it( 'should return only one page when two pages match but limit is 1', async () => {
|
|
|
|
|
const { body } = await client.get( `/search/title?q=${sharedTitleTerm}&limit=1` );
|
|
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
} );
|
|
|
|
|
it( 'should not return deleted page', async () => {
|
|
|
|
|
const deleteTerm = utils.uniq();
|
|
|
|
|
const pageToDelete = utils.title( `${deleteTerm}_test_` );
|
|
|
|
|
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } );
|
|
|
|
|
await mindy.action( 'delete', {
|
|
|
|
|
title,
|
|
|
|
|
summary: 'testing',
|
|
|
|
|
token: await mindy.token( 'csrf' )
|
|
|
|
|
}, 'POST' );
|
2022-02-22 08:26:37 +00:00
|
|
|
await wiki.runAllJobs();
|
2020-03-25 17:21:12 +00:00
|
|
|
const { body } = await client.get( `/search/title?q=${deleteTerm}` );
|
|
|
|
|
assert.lengthOf( body.pages, 0 );
|
|
|
|
|
} );
|
2022-01-13 19:35:27 +00:00
|
|
|
it( 'should include redirect for page if one exists', async () => {
|
|
|
|
|
const redirectSource = utils.title( 'redirect_source_' );
|
|
|
|
|
const redirectTarget = utils.title( 'redirect_target_' );
|
|
|
|
|
|
|
|
|
|
const { title: redirectSourceTitle } = await alice.edit( redirectSource,
|
|
|
|
|
{ text: `#REDIRECT [[ ${redirectTarget} ]]` }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: 'foo' } );
|
2022-02-22 08:26:37 +00:00
|
|
|
await wiki.runAllJobs();
|
2022-01-13 19:35:27 +00:00
|
|
|
|
|
|
|
|
const { body } = await client.get( `/search/title?q=${redirectSourceTitle}` );
|
|
|
|
|
assert.lengthOf( body.pages, 1 );
|
|
|
|
|
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle );
|
|
|
|
|
assert.nestedPropertyVal( body.pages[ 0 ], 'matched_title', redirectSourceTitle );
|
|
|
|
|
} );
|
2020-03-25 17:21:12 +00:00
|
|
|
} );
|
2020-01-15 01:20:50 +00:00
|
|
|
} );
|