Merge "DifferenceEngine: use a fake title when there's no real title"
[lhc/web/wiklou.git] / tests / phpunit / mocks / search / MockCompletionSearchEngine.php
1 <?php
2
3 /**
4 * SearchEngine implementation for returning mocked completion search results.
5 */
6 class MockCompletionSearchEngine extends SearchEngine {
7 /** @var string[][] */
8 private static $results = [];
9
10 /**
11 * Reset any mocked results
12 */
13 public static function clearMockResults() {
14 self::$results = [];
15 }
16
17 /**
18 * Allows returning arbitrary lists of titles for completion search.
19 * Provided results will be sliced based on offset/limit of query.
20 *
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.
24 *
25 * @param string $query Search term as seen in completionSearchBackend
26 * @param string[] $result List of titles to respond to query with
27 */
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;
32 }
33
34 public function completionSearchBackend( $search ) {
35 $search = mb_strtolower( $search );
36 if ( !isset( self::$results[$search] ) ) {
37 return SearchSuggestionSet::emptySuggestionSet();
38 }
39 $results = array_slice( self::$results[$search], $this->offset, $this->limit );
40
41 return SearchSuggestionSet::fromStrings( $results );
42 }
43 }