Add test for completionSearch with wgCapitalLinkOverrides
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
index c74c893..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 ) {
@@ -121,7 +134,63 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                $this->assertEquals(
                        [ 'Smithee' ],
                        $this->fetchIds( $this->search->searchText( 'smithee' ) ),
-                       "Plain search failed" );
+                       "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 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();
+               $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
+               $this->assertStringStartsWith( $snippet,
+                       $match->getTextSnippet( $res->termMatches() ),
+                       "Highlight a phrase search" );
        }
 
        public function testTextPowerSearch() {
@@ -132,7 +201,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                                'Talk:Not Main Page',
                        ],
                        $this->fetchIds( $this->search->searchText( 'smithee' ) ),
-                       "Power search failed" );
+                       "Power search" );
        }
 
        public function testTitleSearch() {
@@ -142,7 +211,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                                'Smithee',
                        ],
                        $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
-                       "Title search failed" );
+                       "Title search" );
        }
 
        public function testTextTitlePowerSearch() {
@@ -154,7 +223,49 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                                'Talk:Smithee',
                        ],
                        $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
-                       "Title power search failed" );
+                       "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() );
        }
 
        /**
@@ -164,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();
@@ -202,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 );
@@ -231,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 ) {
@@ -245,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 ) {