$searchType = SearchEngineFactory::getSearchEngineClass( $this->db );
$this->setMwGlobals( [
- 'wgSearchType' => $searchType
+ 'wgSearchType' => $searchType,
+ 'wgCapitalLinks' => true,
+ 'wgCapitalLinkOverrides' => [
+ NS_CATEGORY => false // for testCompletionSearchMustRespectCapitalLinkOverrides
+ ]
] );
$this->search = new $searchType( $this->db );
// Reset the search type back to default - some extensions may have
// overridden it.
- $this->setMwGlobals( [ 'wgSearchType' => null ] );
+ $this->setMwGlobals( [
+ 'wgSearchType' => null,
+ 'wgCapitalLinks' => true,
+ 'wgCapitalLinkOverrides' => [
+ NS_CATEGORY => false // for testCompletionSearchMustRespectCapitalLinkOverrides
+ ]
+ ] );
$this->insertPage( 'Not_Main_Page', 'This is not a main page' );
$this->insertPage(
$this->insertPage( 'HalfNumbers', '1234567890' );
$this->insertPage( 'FullNumbers', '1234567890' );
$this->insertPage( 'DomainName', 'example.com' );
+ $this->insertPage( 'DomainName', 'example.com' );
+ $this->insertPage( 'Category:search is not Search', '' );
+ $this->insertPage( 'Category:Search is not search', '' );
}
protected function fetchIds( $results ) {
"Plain search" );
}
+ public function testWildcardSearch() {
+ $res = $this->search->searchText( 'smith*' );
+ $this->assertEquals(
+ [ 'Smithee' ],
+ $this->fetchIds( $res ),
+ "Search with wildcards" );
+
+ $res = $this->search->searchText( 'smithson*' );
+ $this->assertEquals(
+ [],
+ $this->fetchIds( $res ),
+ "Search with wildcards must not find unrelated articles" );
+
+ $res = $this->search->searchText( 'smith* smithee' );
+ $this->assertEquals(
+ [ 'Smithee' ],
+ $this->fetchIds( $res ),
+ "Search with wildcards can be combined with simple terms" );
+
+ $res = $this->search->searchText( 'smith* "one who smiths"' );
+ $this->assertEquals(
+ [ 'Smithee' ],
+ $this->fetchIds( $res ),
+ "Search with wildcards can be combined with phrase search" );
+ }
+
public function testPhraseSearch() {
$res = $this->search->searchText( '"smithee is one who smiths"' );
$this->assertEquals(
[ 'Smithee' ],
$this->fetchIds( $res ),
"Search a phrase" );
- $res = $this->search->searchText( '"smithee is one who smiths"' );
+
+ $res = $this->search->searchText( '"smithee is who smiths"' );
+ $this->assertEquals(
+ [],
+ $this->fetchIds( $res ),
+ "Phrase search is not sloppy, search terms must be adjacent" );
+
+ $res = $this->search->searchText( '"is smithee one who smiths"' );
+ $this->assertEquals(
+ [],
+ $this->fetchIds( $res ),
+ "Phrase search is ordered" );
+ }
+
+ public function testPhraseSearchHighlight() {
+ $phrase = "smithee is one who smiths";
+ $res = $this->search->searchText( "\"$phrase\"" );
$match = $res->next();
- $terms = [ 'smithee', 'is', 'one', 'who', 'smiths' ];
- $snippet = "";
- foreach ( $terms as $term ) {
- $snippet .= " <span class='searchmatch'>" . $term . "</span>";
- }
- $this->assertRegexp( '/' . preg_quote( $snippet, '/' ) . '/',
+ $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
+ $this->assertStringStartsWith( $snippet,
$match->getTextSnippet( $res->termMatches() ),
"Highlight a phrase search" );
}
"Title power search" );
}
+ public function provideCompletionSearchMustRespectCapitalLinkOverrides() {
+ return [
+ 'Searching for "smithee" finds Smithee on NS_MAIN' => [
+ 'smithee',
+ 'Smithee',
+ [ NS_MAIN ],
+ ],
+ 'Searching for "search is" will finds "search is not Search" on NS_CATEGORY' => [
+ 'search is',
+ 'Category:search is not Search',
+ [ NS_CATEGORY ],
+ ],
+ 'Searching for "Search is" will finds "search is not Search" on NS_CATEGORY' => [
+ 'Search is',
+ 'Category:Search is not search',
+ [ NS_CATEGORY ],
+ ],
+ ];
+ }
+
+ /**
+ * Test that the search query is not munged using wrong CapitalLinks setup
+ * (in other test that the default search backend can benefit from wgCapitalLinksOverride)
+ * Guard against regressions like T208255
+ * @dataProvider provideCompletionSearchMustRespectCapitalLinkOverrides
+ * @covers SearchEngine::completionSearch
+ * @covers PrefixSearch::defaultSearchBackend
+ * @param string $search
+ * @param string $expectedSuggestion
+ * @param int[] $namespaces
+ */
+ public function testCompletionSearchMustRespectCapitalLinkOverrides(
+ $search,
+ $expectedSuggestion,
+ array $namespaces
+ ) {
+ $this->search->setNamespaces( $namespaces );
+ $results = $this->search->completionSearch( $search );
+ $this->assertEquals( 1, $results->getSize() );
+ $this->assertEquals( $expectedSuggestion, $results->getSuggestions()[0]->getText() );
+ }
+
/**
* @covers SearchEngine::getSearchIndexFields
*/
/**
* @var $mockEngine SearchEngine
*/
- $mockEngine = $this->getMockBuilder( 'SearchEngine' )
+ $mockEngine = $this->getMockBuilder( SearchEngine::class )
->setMethods( [ 'makeSearchFieldMapping' ] )->getMock();
$mockFieldBuilder = function ( $name, $type ) {
$mockField =
- $this->getMockBuilder( 'SearchIndexFieldDefinition' )->setConstructorArgs( [
+ $this->getMockBuilder( SearchIndexFieldDefinition::class )->setConstructorArgs( [
$name,
$type
] )->getMock();
$fields = $mockEngine->getSearchIndexFields();
$this->assertArrayHasKey( 'language', $fields );
$this->assertArrayHasKey( 'category', $fields );
- $this->assertInstanceOf( 'SearchIndexField', $fields['testField'] );
+ $this->assertInstanceOf( SearchIndexField::class, $fields['testField'] );
$mapping = $fields['testField']->getMapping( $mockEngine );
$this->assertArrayHasKey( 'testData', $mapping );
}
public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
- $setAugmentor = $this->createMock( 'ResultSetAugmentor' );
+ $setAugmentor = $this->createMock( ResultSetAugmentor::class );
$setAugmentor->expects( $this->once() )
->method( 'augmentAll' )
->willReturnCallback( function ( SearchResultSet $resultSet ) {
} );
$setAugmentors['testSet'] = $setAugmentor;
- $rowAugmentor = $this->createMock( 'ResultAugmentor' );
+ $rowAugmentor = $this->createMock( ResultAugmentor::class );
$rowAugmentor->expects( $this->exactly( 2 ) )
->method( 'augment' )
->willReturnCallback( function ( SearchResult $result ) {