7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
10 class SearchEngineTest
extends MediaWikiLangTestCase
{
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
21 protected function setUp() {
24 // Search tests require MySQL or SQLite with FTS
25 $dbType = $this->db
->getType();
26 $dbSupported = ( $dbType === 'mysql' )
27 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
29 if ( !$dbSupported ) {
30 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
33 $searchType = SearchEngineFactory
::getSearchEngineClass( $this->db
);
34 $this->setMwGlobals( [
35 'wgSearchType' => $searchType
38 $this->search
= new $searchType( $this->db
);
41 protected function tearDown() {
42 unset( $this->search
);
47 public function addDBDataOnce() {
48 if ( !$this->isWikitextNS( NS_MAIN
) ) {
49 // @todo cover the case of non-wikitext content in the main namespace
53 // Reset the search type back to default - some extensions may have
55 $this->setMwGlobals( [ 'wgSearchType' => null ] );
57 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
60 'This is not a talk page to the main page, see [[smithee]]'
62 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
63 $this->insertPage( 'Talk:Smithee', 'This article sucks.' );
64 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
65 $this->insertPage( 'Another_page', 'This page also is unrelated.' );
66 $this->insertPage( 'Help:Help', 'Help me!' );
67 $this->insertPage( 'Thppt', 'Blah blah' );
68 $this->insertPage( 'Alan_Smithee', 'yum' );
69 $this->insertPage( 'Pages', 'are\'food' );
70 $this->insertPage( 'HalfOneUp', 'AZ' );
71 $this->insertPage( 'FullOneUp', 'AZ' );
72 $this->insertPage( 'HalfTwoLow', 'az' );
73 $this->insertPage( 'FullTwoLow', 'az' );
74 $this->insertPage( 'HalfNumbers', '1234567890' );
75 $this->insertPage( 'FullNumbers', '1234567890' );
76 $this->insertPage( 'DomainName', 'example.com' );
79 protected function fetchIds( $results ) {
80 if ( !$this->isWikitextNS( NS_MAIN
) ) {
81 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
82 . "in the main namespace" );
84 $this->assertTrue( is_object( $results ) );
87 foreach ( $results as $row ) {
88 $matches[] = $row->getTitle()->getPrefixedText();
91 # Search is not guaranteed to return results in a certain order;
92 # sort them numerically so we will compare simply that we received
93 # the expected matches.
99 public function testFullWidth() {
101 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
102 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
103 "Search for normalized from Half-width Upper" );
105 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
106 $this->fetchIds( $this->search
->searchText( 'az' ) ),
107 "Search for normalized from Half-width Lower" );
109 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
110 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
111 "Search for normalized from Full-width Upper" );
113 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
114 $this->fetchIds( $this->search
->searchText( 'az' ) ),
115 "Search for normalized from Full-width Lower" );
118 public function testTextSearch() {
121 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
125 public function testWildcardSearch() {
126 $res = $this->search
->searchText( 'smith*' );
129 $this->fetchIds( $res ),
130 "Search with wildcards" );
132 $res = $this->search
->searchText( 'smithson*' );
135 $this->fetchIds( $res ),
136 "Search with wildcards must not find unrelated articles" );
138 $res = $this->search
->searchText( 'smith* smithee' );
141 $this->fetchIds( $res ),
142 "Search with wildcards can be combined with simple terms" );
144 $res = $this->search
->searchText( 'smith* "one who smiths"' );
147 $this->fetchIds( $res ),
148 "Search with wildcards can be combined with phrase search" );
151 public function testPhraseSearch() {
152 $res = $this->search
->searchText( '"smithee is one who smiths"' );
155 $this->fetchIds( $res ),
158 $res = $this->search
->searchText( '"smithee is who smiths"' );
161 $this->fetchIds( $res ),
162 "Phrase search is not sloppy, search terms must be adjacent" );
164 $res = $this->search
->searchText( '"is smithee one who smiths"' );
167 $this->fetchIds( $res ),
168 "Phrase search is ordered" );
171 public function testPhraseSearchHighlight() {
172 $phrase = "smithee is one who smiths";
173 $res = $this->search
->searchText( "\"$phrase\"" );
174 $match = $res->getIterator()->current();
175 $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
176 $this->assertStringStartsWith( $snippet,
177 $match->getTextSnippet( $res->termMatches() ),
178 "Highlight a phrase search" );
181 public function testTextPowerSearch() {
182 $this->search
->setNamespaces( [ 0, 1, 4 ] );
186 'Talk:Not Main Page',
188 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
192 public function testTitleSearch() {
198 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
202 public function testTextTitlePowerSearch() {
203 $this->search
->setNamespaces( [ 0, 1, 4 ] );
210 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
211 "Title power search" );
215 * @covers SearchEngine::getSearchIndexFields
217 public function testSearchIndexFields() {
219 * @var $mockEngine SearchEngine
221 $mockEngine = $this->getMockBuilder( SearchEngine
::class )
222 ->setMethods( [ 'makeSearchFieldMapping' ] )->getMock();
224 $mockFieldBuilder = function ( $name, $type ) {
226 $this->getMockBuilder( SearchIndexFieldDefinition
::class )->setConstructorArgs( [
231 $mockField->expects( $this->any() )->method( 'getMapping' )->willReturn( [
232 'testData' => 'test',
237 $mockField->expects( $this->any() )
239 ->willReturn( $mockField );
244 $mockEngine->expects( $this->atLeastOnce() )
245 ->method( 'makeSearchFieldMapping' )
246 ->willReturnCallback( $mockFieldBuilder );
248 // Not using mock since PHPUnit mocks do not work properly with references in params
249 $this->setTemporaryHook( 'SearchIndexFields',
250 function ( &$fields, SearchEngine
$engine ) use ( $mockFieldBuilder ) {
251 $fields['testField'] =
252 $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
256 $fields = $mockEngine->getSearchIndexFields();
257 $this->assertArrayHasKey( 'language', $fields );
258 $this->assertArrayHasKey( 'category', $fields );
259 $this->assertInstanceOf( SearchIndexField
::class, $fields['testField'] );
261 $mapping = $fields['testField']->getMapping( $mockEngine );
262 $this->assertArrayHasKey( 'testData', $mapping );
263 $this->assertEquals( 'test', $mapping['testData'] );
266 public function hookSearchIndexFields( $mockFieldBuilder, &$fields, SearchEngine
$engine ) {
267 $fields['testField'] = $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
271 public function testAugmentorSearch() {
272 $this->search
->setNamespaces( [ 0, 1, 4 ] );
273 $resultSet = $this->search
->searchText( 'smithee' );
274 // Not using mock since PHPUnit mocks do not work properly with references in params
275 $this->mergeMwGlobalArrayValue( 'wgHooks',
276 [ 'SearchResultsAugment' => [ [ $this, 'addAugmentors' ] ] ] );
277 $this->search
->augmentSearchResults( $resultSet );
278 foreach ( $resultSet as $result ) {
279 $id = $result->getTitle()->getArticleID();
280 $augmentData = "Result:$id:" . $result->getTitle()->getText();
281 $augmentData2 = "Result2:$id:" . $result->getTitle()->getText();
282 $this->assertEquals( [ 'testSet' => $augmentData, 'testRow' => $augmentData2 ],
283 $result->getExtensionData() );
287 public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
288 $setAugmentor = $this->createMock( ResultSetAugmentor
::class );
289 $setAugmentor->expects( $this->once() )
290 ->method( 'augmentAll' )
291 ->willReturnCallback( function ( SearchResultSet
$resultSet ) {
293 foreach ( $resultSet as $result ) {
294 $id = $result->getTitle()->getArticleID();
295 $data[$id] = "Result:$id:" . $result->getTitle()->getText();
299 $setAugmentors['testSet'] = $setAugmentor;
301 $rowAugmentor = $this->createMock( ResultAugmentor
::class );
302 $rowAugmentor->expects( $this->exactly( 2 ) )
303 ->method( 'augment' )
304 ->willReturnCallback( function ( SearchResult
$result ) {
305 $id = $result->getTitle()->getArticleID();
306 return "Result2:$id:" . $result->getTitle()->getText();
308 $rowAugmentors['testRow'] = $rowAugmentor;