Merge "ContribsPager: Factor revision check out of formatRow"
[lhc/web/wiklou.git] / tests / phpunit / mocks / search / MockSearchEngine.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 class MockSearchEngine extends SearchEngine {
6 /** @var SearchResult[][] */
7 private static $results = [];
8 /** @var SearchResultSet[][] */
9 private static $interwikiResults = [];
10
11 public static function clearMockResults() {
12 self::$results = [];
13 self::$interwikiResults = [];
14 }
15
16 /**
17 * @param string $query The query searched for *after* initial
18 * transformations have been applied.
19 * @param SearchResult[] $results The results to return for $query
20 */
21 public static function addMockResults( $query, array $results ) {
22 $lc = MediaWikiServices::getInstance()->getLinkCache();
23 foreach ( $results as &$result ) {
24 // Resolve deferred results; needed to work around T203279
25 if ( is_callable( $result ) ) {
26 $result = $result();
27 }
28
29 // TODO: better page ids? Does it matter?
30 $lc->addGoodLinkObj( mt_rand(), $result->getTitle() );
31 }
32 self::$results[$query] = $results;
33 }
34
35 /**
36 * @param SearchResultSet[][] $interwikiResults
37 */
38 public static function setMockInterwikiResults( array $interwikiResults ) {
39 self::$interwikiResults = $interwikiResults;
40 }
41
42 protected function doSearchText( $term ) {
43 if ( isset( self::$results[ $term ] ) ) {
44 $results = array_slice( self::$results[ $term ], $this->offset, $this->limit );
45 } else {
46 $results = [];
47 }
48 return new MockSearchResultSet( $results, self::$interwikiResults );
49 }
50 }