3 use MediaWiki\MainConfigNames
;
4 use MediaWiki\Title\Title
;
10 class SearchEnginePrefixTest
extends MediaWikiLangTestCase
{
11 private SearchEngine
$search;
13 public function addDBDataOnce() {
14 if ( !$this->isWikitextNS( NS_MAIN
) ) {
15 // tests are skipped if NS_MAIN is not wikitext
19 $this->insertPage( 'Sandbox' );
20 $this->insertPage( 'Bar' );
21 $this->insertPage( 'Example' );
22 $this->insertPage( 'Example Bar' );
23 $this->insertPage( 'Example Foo' );
24 $this->insertPage( 'Example Foo/Bar' );
25 $this->insertPage( 'Example/Baz' );
26 $this->insertPage( 'Sample' );
27 $this->insertPage( 'Sample Ban' );
28 $this->insertPage( 'Sample Eat' );
29 $this->insertPage( 'Sample Who' );
30 $this->insertPage( 'Sample Zoo' );
31 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
32 $this->insertPage( 'Redirect Test' );
33 $this->insertPage( 'Redirect Test Worse Result' );
34 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
35 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
36 $this->insertPage( 'Redirect Test2' );
37 $this->insertPage( 'Redirect Test2 Worse Result' );
39 $this->insertPage( 'Talk:Sandbox' );
40 $this->insertPage( 'Talk:Example' );
42 $this->insertPage( 'User:Example' );
43 $this->insertPage( 'Barcelona' );
44 $this->insertPage( 'Barbara' );
45 $this->insertPage( 'External' );
48 protected function setUp(): void
{
51 if ( !$this->isWikitextNS( NS_MAIN
) ) {
52 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
55 // Avoid special pages from extensions interferring with the tests
56 $this->overrideConfigValues( [
57 MainConfigNames
::SpecialPages
=> [],
58 MainConfigNames
::Hooks
=> [],
61 $this->search
= $this->getServiceContainer()->newSearchEngine();
62 $this->search
->setNamespaces( [] );
65 protected function searchProvision( ?
array $results = null ) {
66 if ( $results === null ) {
67 $this->overrideConfigValue( MainConfigNames
::Hooks
, [] );
69 $this->setTemporaryHook(
70 'PrefixSearchBackend',
71 static function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
79 public static function provideSearch() {
87 'All invalid characters, effectively empty',
92 'Main namespace with title prefix',
99 // Third result when testing offset
105 'Some invalid characters',
112 'offsetresult' => [ 'Sample Who' ],
115 'Talk namespace prefix',
123 'User namespace prefix',
130 'Special namespace prefix',
131 'query' => 'Special:',
133 'Special:ActiveUsers',
134 'Special:AllMessages',
137 // Third result when testing offset
139 'Special:AncientPages',
143 'Special namespace with prefix',
144 'query' => 'Special:Un',
147 'Special:UncategorizedCategories',
148 'Special:UncategorizedFiles',
150 // Third result when testing offset
152 'Special:UncategorizedPages',
157 'query' => 'Special:EditWatchlist',
162 'Special page subpages',
163 'query' => 'Special:EditWatchlist/',
165 'Special:EditWatchlist/clear',
166 'Special:EditWatchlist/raw',
170 'Special page subpages with prefix',
171 'query' => 'Special:EditWatchlist/cl',
173 'Special:EditWatchlist/clear',
180 * @dataProvider provideSearch
181 * @covers \SearchEngine::defaultPrefixSearch
183 public function testSearch( array $case ) {
184 $this->search
->setLimitOffset( 3 );
185 $results = $this->search
->defaultPrefixSearch( $case['query'] );
186 $results = array_map( static function ( Title
$t ) {
187 return $t->getPrefixedText();
198 * @dataProvider provideSearch
199 * @covers \SearchEngine::defaultPrefixSearch
201 public function testSearchWithOffset( array $case ) {
202 $this->search
->setLimitOffset( 3, 1 );
203 $results = $this->search
->defaultPrefixSearch( $case['query'] );
204 $results = array_map( static function ( Title
$t ) {
205 return $t->getPrefixedText();
208 // We don't expect the first result when offsetting
209 array_shift( $case['results'] );
210 // And sometimes we expect a different last result
211 $expected = isset( $case['offsetresult'] ) ?
212 array_merge( $case['results'], $case['offsetresult'] ) :
222 public static function provideSearchBackend() {
239 'Exact match not in first result should be moved to the first result (T72958)',
253 'Exact match missing from results should be added as first result (T72958)',
267 'Exact match missing and not existing pages should be dropped',
279 "Exact match shouldn't override already found match if " .
280 "exact is redirect and found isn't",
282 // Target of the exact match is low in the list
283 'Redirect Test Worse Result',
286 'query' => 'redirect test',
288 // Redirect target is pulled up and exact match isn't added
290 'Redirect Test Worse Result',
294 "Exact match should override already found match if " .
295 "both exact match and found match are redirect",
297 // Another redirect to the same target as the exact match
298 // is low in the list
299 'Redirect Test2 Worse Result',
302 'query' => 'redirect TEST2',
304 // Found redirect is pulled to the top and exact match isn't
307 'Redirect Test2 Worse Result',
311 "Exact match should override any already found matches that " .
312 "are redirects to it",
314 // Another redirect to the same target as the exact match
315 // is low in the list
316 'Redirect Test Worse Result',
319 'query' => 'Redirect Test',
321 // Found redirect is pulled to the top and exact match isn't
324 'Redirect Test Worse Result',
329 "Extra results must not be returned",
347 * @dataProvider provideSearchBackend
348 * @covers \PrefixSearch::searchBackend
350 public function testSearchBackend( array $case ) {
351 $search = $this->mockSearchWithResults( $case['provision'] );
352 $results = $search->completionSearch( $case['query'] );
354 $results = $results->map( static function ( SearchSuggestion
$s ) {
355 return $s->getText();
365 public static function paginationProvider() {
366 $res = [ 'Example', 'Example Bar', 'Example Foo', 'Example Foo/Bar' ];
368 'With less than requested results no pagination' => [
369 false, array_slice( $res, 0, 2 ),
371 'With same as requested results no pagination' => [
372 false, array_slice( $res, 0, 3 ),
374 'With extra result returned offer pagination' => [
381 * @dataProvider paginationProvider
382 * @covers \SearchSuggestionSet::hasMoreResults
384 public function testPagination( $hasMoreResults, $provision ) {
385 $search = $this->mockSearchWithResults( $provision );
386 $results = $search->completionSearch( 'irrelevant' );
388 $this->assertEquals( $hasMoreResults, $results->hasMoreResults() );
391 private function mockSearchWithResults( $titleStrings, $limit = 3 ) {
392 $search = $this->getMockBuilder( SearchEngine
::class )
393 ->onlyMethods( [ 'completionSearchBackend' ] )->getMock();
395 $return = SearchSuggestionSet
::fromStrings( $titleStrings );
397 $search->method( 'completionSearchBackend' )
398 ->willReturn( $return );
400 $search->setLimitOffset( $limit );