Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / search / SearchEngineTest.php
blob3fb4bbbb1fb084e0be8118955a6e33073bc06a03
1 <?php
3 /**
4 * @group Search
5 * @group Database
7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
9 */
10 class SearchEngineTest extends MediaWikiLangTestCase {
12 /**
13 * @var SearchEngine
15 protected $search;
17 /**
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
21 protected function setUp() {
22 parent::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
36 ] );
38 $this->search = new $searchType( $this->db );
41 protected function tearDown() {
42 unset( $this->search );
44 parent::tearDown();
47 public function addDBDataOnce() {
48 if ( !$this->isWikitextNS( NS_MAIN ) ) {
49 // @todo cover the case of non-wikitext content in the main namespace
50 return;
53 // Reset the search type back to default - some extensions may have
54 // overridden it.
55 $this->setMwGlobals( [ 'wgSearchType' => null ] );
57 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
58 $this->insertPage(
59 'Talk:Not_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 ) );
86 $matches = [];
87 $row = $results->next();
88 while ( $row ) {
89 $matches[] = $row->getTitle()->getPrefixedText();
90 $row = $results->next();
92 $results->free();
93 # Search is not guaranteed to return results in a certain order;
94 # sort them numerically so we will compare simply that we received
95 # the expected matches.
96 sort( $matches );
98 return $matches;
101 public function testFullWidth() {
102 $this->assertEquals(
103 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
104 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
105 "Search for normalized from Half-width Upper" );
106 $this->assertEquals(
107 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
108 $this->fetchIds( $this->search->searchText( 'az' ) ),
109 "Search for normalized from Half-width Lower" );
110 $this->assertEquals(
111 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
112 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
113 "Search for normalized from Full-width Upper" );
114 $this->assertEquals(
115 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
116 $this->fetchIds( $this->search->searchText( 'az' ) ),
117 "Search for normalized from Full-width Lower" );
120 public function testTextSearch() {
121 $this->assertEquals(
122 [ 'Smithee' ],
123 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
124 "Plain search failed" );
127 public function testTextPowerSearch() {
128 $this->search->setNamespaces( [ 0, 1, 4 ] );
129 $this->assertEquals(
131 'Smithee',
132 'Talk:Not Main Page',
134 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
135 "Power search failed" );
138 public function testTitleSearch() {
139 $this->assertEquals(
141 'Alan Smithee',
142 'Smithee',
144 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
145 "Title search failed" );
148 public function testTextTitlePowerSearch() {
149 $this->search->setNamespaces( [ 0, 1, 4 ] );
150 $this->assertEquals(
152 'Alan Smithee',
153 'Smithee',
154 'Talk:Smithee',
156 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
157 "Title power search failed" );
161 * @covers SearchEngine::getSearchIndexFields
163 public function testSearchIndexFields() {
165 * @var $mockEngine SearchEngine
167 $mockEngine = $this->getMock( 'SearchEngine', [ 'makeSearchFieldMapping' ] );
169 $mockFieldBuilder = function ( $name, $type ) {
170 $mockField =
171 $this->getMockBuilder( 'SearchIndexFieldDefinition' )->setConstructorArgs( [
172 $name,
173 $type
174 ] )->getMock();
176 $mockField->expects( $this->any() )->method( 'getMapping' )->willReturn( [
177 'testData' => 'test',
178 'name' => $name,
179 'type' => $type,
180 ] );
182 $mockField->expects( $this->any() )
183 ->method( 'merge' )
184 ->willReturn( $mockField );
186 return $mockField;
189 $mockEngine->expects( $this->atLeastOnce() )
190 ->method( 'makeSearchFieldMapping' )
191 ->willReturnCallback( $mockFieldBuilder );
193 // Not using mock since PHPUnit mocks do not work properly with references in params
194 $this->setTemporaryHook( 'SearchIndexFields',
195 function ( &$fields, SearchEngine $engine ) use ( $mockFieldBuilder ) {
196 $fields['testField'] =
197 $mockFieldBuilder( "testField", SearchIndexField::INDEX_TYPE_TEXT );
198 return true;
199 } );
201 $fields = $mockEngine->getSearchIndexFields();
202 $this->assertArrayHasKey( 'language', $fields );
203 $this->assertArrayHasKey( 'category', $fields );
204 $this->assertInstanceOf( 'SearchIndexField', $fields['testField'] );
206 $mapping = $fields['testField']->getMapping( $mockEngine );
207 $this->assertArrayHasKey( 'testData', $mapping );
208 $this->assertEquals( 'test', $mapping['testData'] );
211 public function hookSearchIndexFields( $mockFieldBuilder, &$fields, SearchEngine $engine ) {
212 $fields['testField'] = $mockFieldBuilder( "testField", SearchIndexField::INDEX_TYPE_TEXT );
213 return true;
216 public function testAugmentorSearch() {
217 $this->search->setNamespaces( [ 0, 1, 4 ] );
218 $resultSet = $this->search->searchText( 'smithee' );
219 // Not using mock since PHPUnit mocks do not work properly with references in params
220 $this->mergeMwGlobalArrayValue( 'wgHooks',
221 [ 'SearchResultsAugment' => [ [ $this, 'addAugmentors' ] ] ] );
222 $this->search->augmentSearchResults( $resultSet );
223 for ( $result = $resultSet->next(); $result; $result = $resultSet->next() ) {
224 $id = $result->getTitle()->getArticleID();
225 $augmentData = "Result:$id:" . $result->getTitle()->getText();
226 $augmentData2 = "Result2:$id:" . $result->getTitle()->getText();
227 $this->assertEquals( [ 'testSet' => $augmentData, 'testRow' => $augmentData2 ],
228 $result->getExtensionData() );
232 public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
233 $setAugmentor = $this->getMock( 'ResultSetAugmentor' );
234 $setAugmentor->expects( $this->once() )
235 ->method( 'augmentAll' )
236 ->willReturnCallback( function ( SearchResultSet $resultSet ) {
237 $data = [];
238 for ( $result = $resultSet->next(); $result; $result = $resultSet->next() ) {
239 $id = $result->getTitle()->getArticleID();
240 $data[$id] = "Result:$id:" . $result->getTitle()->getText();
242 $resultSet->rewind();
243 return $data;
244 } );
245 $setAugmentors['testSet'] = $setAugmentor;
247 $rowAugmentor = $this->getMock( 'ResultAugmentor' );
248 $rowAugmentor->expects( $this->exactly( 2 ) )
249 ->method( 'augment' )
250 ->willReturnCallback( function ( SearchResult $result ) {
251 $id = $result->getTitle()->getArticleID();
252 return "Result2:$id:" . $result->getTitle()->getText();
253 } );
254 $rowAugmentors['testRow'] = $rowAugmentor;