X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fphpunit%2Fincludes%2Fsearch%2FSearchEngineTest.php;h=e8077769e166e1bec25be0536f3893c42c8d9a52;hb=faf7cc4a09848c538320bd2b9067b1a77c0a0183;hp=c945d1e217381a297d545eec36c44e73413ea1d4;hpb=04d149b816c23b9a35db9e2258ee43eb04edd2d6;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/search/SearchEngineTest.php b/tests/phpunit/includes/search/SearchEngineTest.php index c945d1e217..e8077769e1 100644 --- a/tests/phpunit/includes/search/SearchEngineTest.php +++ b/tests/phpunit/includes/search/SearchEngineTest.php @@ -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 .= " " . $term . ""; - } - $this->assertRegexp( '/' . preg_quote( $snippet, '/' ) . '/', + $snippet = "A " . $phrase . ""; + $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 ) {