Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
index c945d1e..e807776 100644 (file)
@@ -124,20 +124,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" );
        }
@@ -182,12 +220,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 +258,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 +287,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 +301,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 ) {