Add ISearchResultSet
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
index 2b27571..d66e480 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\Rdbms\LoadBalancerSingle;
+
 /**
  * @group Search
  * @group Database
@@ -32,10 +34,15 @@ 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 );
+               $lb = LoadBalancerSingle::newFromConnection( $this->db );
+               $this->search = new $searchType( $lb );
        }
 
        protected function tearDown() {
@@ -52,7 +59,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 +87,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 ) {
@@ -174,7 +190,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                $match = $res->getIterator()->current();
                $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
                $this->assertStringStartsWith( $snippet,
-                       $match->getTextSnippet( $res->termMatches() ),
+                       $match->getTextSnippet(),
                        "Highlight a phrase search" );
        }
 
@@ -211,12 +227,54 @@ 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
         */
        public function testSearchIndexFields() {
                /**
-                * @var $mockEngine SearchEngine
+                * @var SearchEngine $mockEngine
                 */
                $mockEngine = $this->getMockBuilder( SearchEngine::class )
                        ->setMethods( [ 'makeSearchFieldMapping' ] )->getMock();
@@ -225,7 +283,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                        $mockField =
                                $this->getMockBuilder( SearchIndexFieldDefinition::class )->setConstructorArgs( [
                                        $name,
-                                       $type
+                                       $type,
                                ] )->getMock();
 
                        $mockField->expects( $this->any() )->method( 'getMapping' )->willReturn( [
@@ -288,8 +346,9 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                $setAugmentor = $this->createMock( ResultSetAugmentor::class );
                $setAugmentor->expects( $this->once() )
                        ->method( 'augmentAll' )
-                       ->willReturnCallback( function ( SearchResultSet $resultSet ) {
+                       ->willReturnCallback( function ( ISearchResultSet $resultSet ) {
                                $data = [];
+                               /** @var SearchResult $result */
                                foreach ( $resultSet as $result ) {
                                        $id = $result->getTitle()->getArticleID();
                                        $data[$id] = "Result:$id:" . $result->getTitle()->getText();
@@ -347,7 +406,7 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                                [
                                        'query' => 'foo',
                                ],
-                               false
+                               false,
                        ],
                        'empty' => [
                                [
@@ -387,34 +446,34 @@ class SearchEngineTest extends MediaWikiLangTestCase {
                                        'query' => 'all:test',
                                        'withAll' => false,
                                ],
-                               false
+                               false,
                        ],
                        'ns only' => [
                                [
                                        'query' => 'help:',
                                ],
-                               [ '', [ NS_HELP ] ]
+                               [ '', [ NS_HELP ] ],
                        ],
                        'all only' => [
                                [
                                        'query' => 'all:',
                                        'withAll' => true,
                                ],
-                               [ '', null ]
+                               [ '', null ],
                        ],
                        'all wins over namespace when first' => [
                                [
                                        'query' => 'all:help:test',
                                        'withAll' => true,
                                ],
-                               [ 'help:test', null ]
+                               [ 'help:test', null ],
                        ],
                        'ns wins over all when first' => [
                                [
                                        'query' => 'help:all:test',
                                        'withAll' => true,
                                ],
-                               [ 'all:test', [ NS_HELP ] ]
+                               [ 'all:test', [ NS_HELP ] ],
                        ],
                ];
        }