3 use MediaWiki\Title\Title
;
6 * SearchEngine implementation for returning mocked completion search results.
8 class MockCompletionSearchEngine
extends SearchEngine
{
10 private static $results = [];
13 * Reset any mocked results
15 public static function clearMockResults() {
20 * Allows returning arbitrary lists of titles for completion search.
21 * Provided results will be sliced based on offset/limit of query.
23 * For results to exit the search engine they must pass Title::isKnown.
24 * Injecting into link cache is not enough, as LinkBatch will mark them
25 * bad, they need to be injected into the DB.
27 * @param string $query Search term as seen in completionSearchBackend
28 * @param string[] $result List of titles to respond to query with
30 public static function addMockResults( $query, array $result ) {
31 // Leading : ensures we don't treat another : as a namespace separator
32 $normalized = mb_strtolower( Title
::newFromText( ":$query" )->getText() );
33 self
::$results[$normalized] = $result;
36 public function completionSearchBackend( $search ) {
37 $search = mb_strtolower( $search );
38 if ( !isset( self
::$results[$search] ) ) {
39 return SearchSuggestionSet
::emptySuggestionSet();
41 $results = array_slice( self
::$results[$search], $this->offset
, $this->limit
);
43 return SearchSuggestionSet
::fromStrings( $results );