3 use Wikimedia\Rdbms\LoadBalancerSingle
;
9 * @covers SearchEngine<extended>
10 * @note Coverage will only ever show one of on of the Search* classes
12 class SearchEngineTest
extends MediaWikiLangTestCase
{
20 * Checks for database type & version.
21 * Will skip current test if DB does not support search.
23 protected function setUp() {
26 // Search tests require MySQL or SQLite with FTS
27 $dbType = $this->db
->getType();
28 $dbSupported = ( $dbType === 'mysql' )
29 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
31 if ( !$dbSupported ) {
32 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
35 $searchType = SearchEngineFactory
::getSearchEngineClass( $this->db
);
36 $this->setMwGlobals( [
37 'wgSearchType' => $searchType,
38 'wgCapitalLinks' => true,
39 'wgCapitalLinkOverrides' => [
40 NS_CATEGORY
=> false // for testCompletionSearchMustRespectCapitalLinkOverrides
44 $lb = LoadBalancerSingle
::newFromConnection( $this->db
);
45 $this->search
= new $searchType( $lb );
48 protected function tearDown() {
49 unset( $this->search
);
54 public function addDBDataOnce() {
55 if ( !$this->isWikitextNS( NS_MAIN
) ) {
56 // @todo cover the case of non-wikitext content in the main namespace
60 // Reset the search type back to default - some extensions may have
62 $this->setMwGlobals( [
63 'wgSearchType' => null,
64 'wgCapitalLinks' => true,
65 'wgCapitalLinkOverrides' => [
66 NS_CATEGORY
=> false // for testCompletionSearchMustRespectCapitalLinkOverrides
70 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
73 'This is not a talk page to the main page, see [[smithee]]'
75 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
76 $this->insertPage( 'Talk:Smithee', 'This article sucks.' );
77 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
78 $this->insertPage( 'Another_page', 'This page also is unrelated.' );
79 $this->insertPage( 'Help:Help', 'Help me!' );
80 $this->insertPage( 'Thppt', 'Blah blah' );
81 $this->insertPage( 'Alan_Smithee', 'yum' );
82 $this->insertPage( 'Pages', 'are\'food' );
83 $this->insertPage( 'HalfOneUp', 'AZ' );
84 $this->insertPage( 'FullOneUp', 'AZ' );
85 $this->insertPage( 'HalfTwoLow', 'az' );
86 $this->insertPage( 'FullTwoLow', 'az' );
87 $this->insertPage( 'HalfNumbers', '1234567890' );
88 $this->insertPage( 'FullNumbers', '1234567890' );
89 $this->insertPage( 'DomainName', 'example.com' );
90 $this->insertPage( 'DomainName', 'example.com' );
91 $this->insertPage( 'Category:search is not Search', '' );
92 $this->insertPage( 'Category:Search is not search', '' );
95 protected function fetchIds( $results ) {
96 if ( !$this->isWikitextNS( NS_MAIN
) ) {
97 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
98 . "in the main namespace" );
100 $this->assertTrue( is_object( $results ) );
103 foreach ( $results as $row ) {
104 $matches[] = $row->getTitle()->getPrefixedText();
107 # Search is not guaranteed to return results in a certain order;
108 # sort them numerically so we will compare simply that we received
109 # the expected matches.
115 public function testFullWidth() {
117 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
118 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
119 "Search for normalized from Half-width Upper" );
121 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
122 $this->fetchIds( $this->search
->searchText( 'az' ) ),
123 "Search for normalized from Half-width Lower" );
125 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
126 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
127 "Search for normalized from Full-width Upper" );
129 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
130 $this->fetchIds( $this->search
->searchText( 'az' ) ),
131 "Search for normalized from Full-width Lower" );
134 public function testTextSearch() {
137 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
141 public function testWildcardSearch() {
142 $res = $this->search
->searchText( 'smith*' );
145 $this->fetchIds( $res ),
146 "Search with wildcards" );
148 $res = $this->search
->searchText( 'smithson*' );
151 $this->fetchIds( $res ),
152 "Search with wildcards must not find unrelated articles" );
154 $res = $this->search
->searchText( 'smith* smithee' );
157 $this->fetchIds( $res ),
158 "Search with wildcards can be combined with simple terms" );
160 $res = $this->search
->searchText( 'smith* "one who smiths"' );
163 $this->fetchIds( $res ),
164 "Search with wildcards can be combined with phrase search" );
167 public function testPhraseSearch() {
168 $res = $this->search
->searchText( '"smithee is one who smiths"' );
171 $this->fetchIds( $res ),
174 $res = $this->search
->searchText( '"smithee is who smiths"' );
177 $this->fetchIds( $res ),
178 "Phrase search is not sloppy, search terms must be adjacent" );
180 $res = $this->search
->searchText( '"is smithee one who smiths"' );
183 $this->fetchIds( $res ),
184 "Phrase search is ordered" );
187 public function testPhraseSearchHighlight() {
188 $phrase = "smithee is one who smiths";
189 $res = $this->search
->searchText( "\"$phrase\"" );
190 $match = $res->getIterator()->current();
191 $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
192 $this->assertStringStartsWith( $snippet,
193 $match->getTextSnippet(),
194 "Highlight a phrase search" );
197 public function testTextPowerSearch() {
198 $this->search
->setNamespaces( [ 0, 1, 4 ] );
202 'Talk:Not Main Page',
204 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
208 public function testTitleSearch() {
214 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
218 public function testTextTitlePowerSearch() {
219 $this->search
->setNamespaces( [ 0, 1, 4 ] );
226 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
227 "Title power search" );
230 public function provideCompletionSearchMustRespectCapitalLinkOverrides() {
232 'Searching for "smithee" finds Smithee on NS_MAIN' => [
237 'Searching for "search is" will finds "search is not Search" on NS_CATEGORY' => [
239 'Category:search is not Search',
242 'Searching for "Search is" will finds "search is not Search" on NS_CATEGORY' => [
244 'Category:Search is not search',
251 * Test that the search query is not munged using wrong CapitalLinks setup
252 * (in other test that the default search backend can benefit from wgCapitalLinksOverride)
253 * Guard against regressions like T208255
254 * @dataProvider provideCompletionSearchMustRespectCapitalLinkOverrides
255 * @covers SearchEngine::completionSearch
256 * @covers PrefixSearch::defaultSearchBackend
257 * @param string $search
258 * @param string $expectedSuggestion
259 * @param int[] $namespaces
261 public function testCompletionSearchMustRespectCapitalLinkOverrides(
266 $this->search
->setNamespaces( $namespaces );
267 $results = $this->search
->completionSearch( $search );
268 $this->assertEquals( 1, $results->getSize() );
269 $this->assertEquals( $expectedSuggestion, $results->getSuggestions()[0]->getText() );
273 * @covers SearchEngine::getSearchIndexFields
275 public function testSearchIndexFields() {
277 * @var SearchEngine $mockEngine
279 $mockEngine = $this->getMockBuilder( SearchEngine
::class )
280 ->setMethods( [ 'makeSearchFieldMapping' ] )->getMock();
282 $mockFieldBuilder = function ( $name, $type ) {
284 $this->getMockBuilder( SearchIndexFieldDefinition
::class )->setConstructorArgs( [
289 $mockField->expects( $this->any() )->method( 'getMapping' )->willReturn( [
290 'testData' => 'test',
295 $mockField->expects( $this->any() )
297 ->willReturn( $mockField );
302 $mockEngine->expects( $this->atLeastOnce() )
303 ->method( 'makeSearchFieldMapping' )
304 ->willReturnCallback( $mockFieldBuilder );
306 // Not using mock since PHPUnit mocks do not work properly with references in params
307 $this->setTemporaryHook( 'SearchIndexFields',
308 function ( &$fields, SearchEngine
$engine ) use ( $mockFieldBuilder ) {
309 $fields['testField'] =
310 $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
314 $fields = $mockEngine->getSearchIndexFields();
315 $this->assertArrayHasKey( 'language', $fields );
316 $this->assertArrayHasKey( 'category', $fields );
317 $this->assertInstanceOf( SearchIndexField
::class, $fields['testField'] );
319 $mapping = $fields['testField']->getMapping( $mockEngine );
320 $this->assertArrayHasKey( 'testData', $mapping );
321 $this->assertEquals( 'test', $mapping['testData'] );
324 public function hookSearchIndexFields( $mockFieldBuilder, &$fields, SearchEngine
$engine ) {
325 $fields['testField'] = $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
329 public function testAugmentorSearch() {
330 $this->search
->setNamespaces( [ 0, 1, 4 ] );
331 $resultSet = $this->search
->searchText( 'smithee' );
332 // Not using mock since PHPUnit mocks do not work properly with references in params
333 $this->mergeMwGlobalArrayValue( 'wgHooks',
334 [ 'SearchResultsAugment' => [ [ $this, 'addAugmentors' ] ] ] );
335 $this->search
->augmentSearchResults( $resultSet );
336 foreach ( $resultSet as $result ) {
337 $id = $result->getTitle()->getArticleID();
338 $augmentData = "Result:$id:" . $result->getTitle()->getText();
339 $augmentData2 = "Result2:$id:" . $result->getTitle()->getText();
340 $this->assertEquals( [ 'testSet' => $augmentData, 'testRow' => $augmentData2 ],
341 $result->getExtensionData() );
345 public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
346 $setAugmentor = $this->createMock( ResultSetAugmentor
::class );
347 $setAugmentor->expects( $this->once() )
348 ->method( 'augmentAll' )
349 ->willReturnCallback( function ( ISearchResultSet
$resultSet ) {
351 /** @var SearchResult $result */
352 foreach ( $resultSet as $result ) {
353 $id = $result->getTitle()->getArticleID();
354 $data[$id] = "Result:$id:" . $result->getTitle()->getText();
358 $setAugmentors['testSet'] = $setAugmentor;
360 $rowAugmentor = $this->createMock( ResultAugmentor
::class );
361 $rowAugmentor->expects( $this->exactly( 2 ) )
362 ->method( 'augment' )
363 ->willReturnCallback( function ( SearchResult
$result ) {
364 $id = $result->getTitle()->getArticleID();
365 return "Result2:$id:" . $result->getTitle()->getText();
367 $rowAugmentors['testRow'] = $rowAugmentor;
370 public function testFiltersMissing() {
371 $availableResults = [];
372 foreach ( range( 0, 11 ) as $i ) {
373 $title = "Search_Result_$i";
374 $availableResults[] = $title;
375 // pages not created must be filtered
377 $this->editSearchResultPage( $title );
380 MockCompletionSearchEngine
::addMockResults( 'foo', $availableResults );
382 $engine = new MockCompletionSearchEngine();
383 $engine->setLimitOffset( 10, 0 );
384 $results = $engine->completionSearch( 'foo' );
385 $this->assertEquals( 5, $results->getSize() );
386 $this->assertTrue( $results->hasMoreResults() );
388 $engine->setLimitOffset( 10, 10 );
389 $results = $engine->completionSearch( 'foo' );
390 $this->assertEquals( 1, $results->getSize() );
391 $this->assertFalse( $results->hasMoreResults() );
394 private function editSearchResultPage( $title ) {
395 $page = WikiPage
::factory( Title
::newFromText( $title ) );
396 $page->doEditContent(
397 new WikitextContent( 'UTContent' ),
399 EDIT_NEW | EDIT_SUPPRESS_RC
403 public function provideDataForParseNamespacePrefix() {
417 'namespace prefix' => [
419 'query' => 'help:test',
421 [ 'test', [ NS_HELP
] ],
423 'accented namespace prefix with hook' => [
425 'query' => 'hélp:test',
428 [ 'test', [ NS_HELP
] ],
430 'accented namespace prefix without hook' => [
432 'query' => 'hélp:test',
437 'all with all keyword allowed' => [
439 'query' => 'all:test',
444 'all with all keyword disallowed' => [
446 'query' => 'all:test',
464 'all wins over namespace when first' => [
466 'query' => 'all:help:test',
469 [ 'help:test', null ],
471 'ns wins over all when first' => [
473 'query' => 'help:all:test',
476 [ 'all:test', [ NS_HELP
] ],
482 * @dataProvider provideDataForParseNamespacePrefix
483 * @param array $params
484 * @param array|false $expected
486 * @throws MWException
488 public function testParseNamespacePrefix( array $params, $expected ) {
489 $this->setTemporaryHook( 'PrefixSearchExtractNamespace', function ( &$namespaces, &$query ) {
490 if ( strpos( $query, 'hélp:' ) === 0 ) {
491 $namespaces = [ NS_HELP
];
492 $query = substr( $query, strlen( 'hélp:' ) );
497 if ( isset( $params['withAll'] ) && isset( $params['withHook'] ) ) {
498 $testSet[] = $params;
499 } elseif ( isset( $params['withAll'] ) ) {
500 $testSet[] = $params +
[ 'withHook' => true ];
501 $testSet[] = $params +
[ 'withHook' => false ];
502 } elseif ( isset( $params['withHook'] ) ) {
503 $testSet[] = $params +
[ 'withAll' => true ];
504 $testSet[] = $params +
[ 'withAll' => false ];
506 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => true ];
507 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => false ];
508 $testSet[] = $params +
[ 'withAll' => false, 'withHook' => false ];
509 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => false ];
512 foreach ( $testSet as $test ) {
513 $actual = SearchEngine
::parseNamespacePrefixes( $test['query'],
514 $test['withAll'], $test['withHook'] );
515 $this->assertEquals( $expected, $actual, 'with params: ' . print_r( $test, true ) );