Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / mocks / search / MockCompletionSearchEngine.php
blobec4016d3d6a1909981680861df31e7729c840b94
1 <?php
3 use MediaWiki\Title\Title;
5 /**
6 * SearchEngine implementation for returning mocked completion search results.
7 */
8 class MockCompletionSearchEngine extends SearchEngine {
9 /** @var string[][] */
10 private static $results = [];
12 /**
13 * Reset any mocked results
15 public static function clearMockResults() {
16 self::$results = [];
19 /**
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 );