3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\TestingAccessWrapper
;
10 class SearchEnginePrefixTest
extends MediaWikiLangTestCase
{
11 private $originalHandlers;
18 public function addDBDataOnce() {
19 if ( !$this->isWikitextNS( NS_MAIN
) ) {
20 // tests are skipped if NS_MAIN is not wikitext
24 $this->insertPage( 'Sandbox' );
25 $this->insertPage( 'Bar' );
26 $this->insertPage( 'Example' );
27 $this->insertPage( 'Example Bar' );
28 $this->insertPage( 'Example Foo' );
29 $this->insertPage( 'Example Foo/Bar' );
30 $this->insertPage( 'Example/Baz' );
31 $this->insertPage( 'Sample' );
32 $this->insertPage( 'Sample Ban' );
33 $this->insertPage( 'Sample Eat' );
34 $this->insertPage( 'Sample Who' );
35 $this->insertPage( 'Sample Zoo' );
36 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
37 $this->insertPage( 'Redirect Test' );
38 $this->insertPage( 'Redirect Test Worse Result' );
39 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
40 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
41 $this->insertPage( 'Redirect Test2' );
42 $this->insertPage( 'Redirect Test2 Worse Result' );
44 $this->insertPage( 'Talk:Sandbox' );
45 $this->insertPage( 'Talk:Example' );
47 $this->insertPage( 'User:Example' );
48 $this->insertPage( 'Barcelona' );
49 $this->insertPage( 'Barbara' );
50 $this->insertPage( 'External' );
53 protected function setUp() {
56 if ( !$this->isWikitextNS( NS_MAIN
) ) {
57 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
60 // Avoid special pages from extensions interferring with the tests
61 $this->setMwGlobals( [
62 'wgSpecialPages' => [],
66 $this->search
= MediaWikiServices
::getInstance()->newSearchEngine();
67 $this->search
->setNamespaces( [] );
69 $this->originalHandlers
= TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
;
70 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= [];
72 $this->overrideMwServices();
75 public function tearDown() {
78 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= $this->originalHandlers
;
81 protected function searchProvision( array $results = null ) {
82 if ( $results === null ) {
83 $this->setMwGlobals( 'wgHooks', [] );
85 $this->setMwGlobals( 'wgHooks', [
86 'PrefixSearchBackend' => [
87 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
96 public static function provideSearch() {
104 'Main namespace with title prefix',
111 // Third result when testing offset
117 'Talk namespace prefix',
125 'User namespace prefix',
132 'Special namespace prefix',
133 'query' => 'Special:',
135 'Special:ActiveUsers',
136 'Special:AllMessages',
137 'Special:AllMyUploads',
139 // Third result when testing offset
145 'Special namespace with prefix',
146 'query' => 'Special:Un',
149 'Special:UncategorizedCategories',
150 'Special:UncategorizedFiles',
152 // Third result when testing offset
154 'Special:UncategorizedPages',
159 'query' => 'Special:EditWatchlist',
161 'Special:EditWatchlist',
165 'Special page subpages',
166 'query' => 'Special:EditWatchlist/',
168 'Special:EditWatchlist/clear',
169 'Special:EditWatchlist/raw',
173 'Special page subpages with prefix',
174 'query' => 'Special:EditWatchlist/cl',
176 'Special:EditWatchlist/clear',
183 * @dataProvider provideSearch
184 * @covers SearchEngine::defaultPrefixSearch
186 public function testSearch( array $case ) {
187 $this->search
->setLimitOffset( 3 );
188 $results = $this->search
->defaultPrefixSearch( $case['query'] );
189 $results = array_map( function ( Title
$t ) {
190 return $t->getPrefixedText();
201 * @dataProvider provideSearch
202 * @covers SearchEngine::defaultPrefixSearch
204 public function testSearchWithOffset( array $case ) {
205 $this->search
->setLimitOffset( 3, 1 );
206 $results = $this->search
->defaultPrefixSearch( $case['query'] );
207 $results = array_map( function ( Title
$t ) {
208 return $t->getPrefixedText();
211 // We don't expect the first result when offsetting
212 array_shift( $case['results'] );
213 // And sometimes we expect a different last result
214 $expected = isset( $case['offsetresult'] ) ?
215 array_merge( $case['results'], $case['offsetresult'] ) :
225 public static function provideSearchBackend() {
242 'Exact match not in first result should be moved to the first result (T72958)',
256 'Exact match missing from results should be added as first result (T72958)',
270 'Exact match missing and not existing pages should be dropped',
282 "Exact match shouldn't override already found match if " .
283 "exact is redirect and found isn't",
285 // Target of the exact match is low in the list
286 'Redirect Test Worse Result',
289 'query' => 'redirect test',
291 // Redirect target is pulled up and exact match isn't added
293 'Redirect Test Worse Result',
297 "Exact match shouldn't override already found match if " .
298 "both exact match and found match are redirect",
300 // Another redirect to the same target as the exact match
301 // is low in the list
302 'Redirect Test2 Worse Result',
305 'query' => 'redirect TEST2',
307 // Found redirect is pulled to the top and exact match isn't
310 'Redirect Test2 Worse Result',
314 "Exact match should override any already found matches that " .
315 "are redirects to it",
317 // Another redirect to the same target as the exact match
318 // is low in the list
319 'Redirect Test Worse Result',
322 'query' => 'Redirect Test',
324 // Found redirect is pulled to the top and exact match isn't
327 'Redirect Test Worse Result',
332 "Extra results must not be returned",
350 * @dataProvider provideSearchBackend
351 * @covers PrefixSearch::searchBackend
353 public function testSearchBackend( array $case ) {
354 $search = $this->mockSearchWithResults( $case['provision'] );
355 $results = $search->completionSearch( $case['query'] );
357 $results = $results->map( function ( SearchSuggestion
$s ) {
358 return $s->getText();
368 public function paginationProvider() {
369 $res = [ 'Example', 'Example Bar', 'Example Foo', 'Example Foo/Bar' ];
371 'With less than requested results no pagination' => [
372 false, array_slice( $res, 0, 2 ),
374 'With same as requested results no pagination' => [
375 false, array_slice( $res, 0, 3 ),
377 'With extra result returned offer pagination' => [
384 * @dataProvider paginationProvider
386 public function testPagination( $hasMoreResults, $provision ) {
387 $search = $this->mockSearchWithResults( $provision );
388 $results = $search->completionSearch( 'irrelevant' );
390 $this->assertEquals( $hasMoreResults, $results->hasMoreResults() );
393 private function mockSearchWithResults( $titleStrings, $limit = 3 ) {
394 $search = $stub = $this->getMockBuilder( SearchEngine
::class )
395 ->setMethods( [ 'completionSearchBackend' ] )->getMock();
397 $return = SearchSuggestionSet
::fromStrings( $titleStrings );
399 $search->expects( $this->any() )
400 ->method( 'completionSearchBackend' )
401 ->will( $this->returnValue( $return ) );
403 $search->setLimitOffset( $limit );