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