Debian Codegraph @

tests/phpunit/mocks/search/MockCompletionSearchEngine.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Title\Title;
4
5 /**
6 * SearchEngine implementation for returning mocked completion search results.
7 */
8 class MockCompletionSearchEngine extends SearchEngine {
9 /** @var string[][] */
10 private static $results = [];
11
12 /**
13 * Reset any mocked results
14 */
15 public static function clearMockResults() {
16 self::$results = [];
17 }
18
19 /**
20 * Allows returning arbitrary lists of titles for completion search.
21 * Provided results will be sliced based on offset/limit of query.
22 *
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.
26 *
27 * @param string $query Search term as seen in completionSearchBackend
28 * @param string[] $result List of titles to respond to query with
29 */
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;
34 }
35
36 public function completionSearchBackend( $search ) {
37 $search = mb_strtolower( $search );
38 if ( !isset( self::$results[$search] ) ) {
39 return SearchSuggestionSet::emptySuggestionSet();
40 }
41 $results = array_slice( self::$results[$search], $this->offset, $this->limit );
42
43 return SearchSuggestionSet::fromStrings( $results );
44 }
45 }