Add test for completionSearch with wgCapitalLinkOverrides
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
index c945d1e..b7bc153 100644 (file)
@@ -32,7 +32,11 @@ class SearchEngineTest extends MediaWikiLangTestCase {
 
                $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 );
@@ -52,7 +56,13 @@ class SearchEngineTest extends MediaWikiLangTestCase {
 
                // 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(
@@ -74,6 +84,9 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                $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 ) {
@@ -124,20 +137,58 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                        "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" );
        }
@@ -175,6 +226,48 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                        "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
         */
@@ -182,12 +275,12 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                /**
                 * @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();
@@ -220,7 +313,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                $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 );
@@ -249,7 +342,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
        }
 
        public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
-               $setAugmentor = $this->createMock( 'ResultSetAugmentor' );
+               $setAugmentor = $this->createMock( ResultSetAugmentor::class );
                $setAugmentor->expects( $this->once() )
                        ->method( 'augmentAll' )
                        ->willReturnCallback( function ( SearchResultSet $resultSet ) {
@@ -263,7 +356,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                        } );
                $setAugmentors['testSet'] = $setAugmentor;
 
-               $rowAugmentor = $this->createMock( 'ResultAugmentor' );
+               $rowAugmentor = $this->createMock( ResultAugmentor::class );
                $rowAugmentor->expects( $this->exactly( 2 ) )
                        ->method( 'augment' )
                        ->willReturnCallback( function ( SearchResult $result ) {