Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / search / SearchEnginePrefixTest.php
blob86b963f26c6e6712e42458f7af819d25bb234bc0
1 <?php
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Title\Title;
6 /**
7 * @group Search
8 * @group Database
9 */
10 class SearchEnginePrefixTest extends MediaWikiLangTestCase {
11 /**
12 * @var SearchEngine
14 private $search;
16 public function addDBDataOnce() {
17 if ( !$this->isWikitextNS( NS_MAIN ) ) {
18 // tests are skipped if NS_MAIN is not wikitext
19 return;
22 $this->insertPage( 'Sandbox' );
23 $this->insertPage( 'Bar' );
24 $this->insertPage( 'Example' );
25 $this->insertPage( 'Example Bar' );
26 $this->insertPage( 'Example Foo' );
27 $this->insertPage( 'Example Foo/Bar' );
28 $this->insertPage( 'Example/Baz' );
29 $this->insertPage( 'Sample' );
30 $this->insertPage( 'Sample Ban' );
31 $this->insertPage( 'Sample Eat' );
32 $this->insertPage( 'Sample Who' );
33 $this->insertPage( 'Sample Zoo' );
34 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
35 $this->insertPage( 'Redirect Test' );
36 $this->insertPage( 'Redirect Test Worse Result' );
37 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
38 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
39 $this->insertPage( 'Redirect Test2' );
40 $this->insertPage( 'Redirect Test2 Worse Result' );
42 $this->insertPage( 'Talk:Sandbox' );
43 $this->insertPage( 'Talk:Example' );
45 $this->insertPage( 'User:Example' );
46 $this->insertPage( 'Barcelona' );
47 $this->insertPage( 'Barbara' );
48 $this->insertPage( 'External' );
51 protected function setUp(): void {
52 parent::setUp();
54 if ( !$this->isWikitextNS( NS_MAIN ) ) {
55 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
58 // Avoid special pages from extensions interferring with the tests
59 $this->overrideConfigValues( [
60 MainConfigNames::SpecialPages => [],
61 MainConfigNames::Hooks => [],
62 ] );
64 $this->search = $this->getServiceContainer()->newSearchEngine();
65 $this->search->setNamespaces( [] );
68 protected function searchProvision( ?array $results = null ) {
69 if ( $results === null ) {
70 $this->overrideConfigValue( MainConfigNames::Hooks, [] );
71 } else {
72 $this->setTemporaryHook(
73 'PrefixSearchBackend',
74 static function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
75 $srchres = $results;
76 return false;
82 public static function provideSearch() {
83 return [
84 [ [
85 'Empty string',
86 'query' => '',
87 'results' => [],
88 ] ],
89 [ [
90 'All invalid characters, effectively empty',
91 'query' => '[',
92 'results' => [],
93 ] ],
94 [ [
95 'Main namespace with title prefix',
96 'query' => 'Sa',
97 'results' => [
98 'Sample',
99 'Sample Ban',
100 'Sample Eat',
102 // Third result when testing offset
103 'offsetresult' => [
104 'Sample Who',
106 ] ],
108 'Some invalid characters',
109 'query' => '[[Sa]]',
110 'results' => [
111 'Sample',
112 'Sample Ban',
113 'Sample Eat',
115 'offsetresult' => [ 'Sample Who' ],
116 ] ],
118 'Talk namespace prefix',
119 'query' => 'Talk:',
120 'results' => [
121 'Talk:Example',
122 'Talk:Sandbox',
124 ] ],
126 'User namespace prefix',
127 'query' => 'User:',
128 'results' => [
129 'User:Example',
131 ] ],
133 'Special namespace prefix',
134 'query' => 'Special:',
135 'results' => [
136 'Special:ActiveUsers',
137 'Special:AllMessages',
138 'Special:AllPages',
140 // Third result when testing offset
141 'offsetresult' => [
142 'Special:AncientPages',
144 ] ],
146 'Special namespace with prefix',
147 'query' => 'Special:Un',
148 'results' => [
149 'Special:Unblock',
150 'Special:UncategorizedCategories',
151 'Special:UncategorizedFiles',
153 // Third result when testing offset
154 'offsetresult' => [
155 'Special:UncategorizedPages',
157 ] ],
159 'Special page name',
160 'query' => 'Special:EditWatchlist',
161 'results' => [
163 ] ],
165 'Special page subpages',
166 'query' => 'Special:EditWatchlist/',
167 'results' => [
168 'Special:EditWatchlist/clear',
169 'Special:EditWatchlist/raw',
171 ] ],
173 'Special page subpages with prefix',
174 'query' => 'Special:EditWatchlist/cl',
175 'results' => [
176 'Special:EditWatchlist/clear',
178 ] ],
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( static function ( Title $t ) {
190 return $t->getPrefixedText();
191 }, $results );
193 $this->assertEquals(
194 $case['results'],
195 $results,
196 $case[0]
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( static function ( Title $t ) {
208 return $t->getPrefixedText();
209 }, $results );
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'] ) :
216 $case['results'];
218 $this->assertEquals(
219 $expected,
220 $results,
221 $case[0]
225 public static function provideSearchBackend() {
226 return [
228 'Simple case',
229 'provision' => [
230 'Bar',
231 'Barcelona',
232 'Barbara',
234 'query' => 'Bar',
235 'results' => [
236 'Bar',
237 'Barcelona',
238 'Barbara',
240 ] ],
242 'Exact match not in first result should be moved to the first result (T72958)',
243 'provision' => [
244 'Barcelona',
245 'Bar',
246 'Barbara',
248 'query' => 'Bar',
249 'results' => [
250 'Bar',
251 'Barcelona',
252 'Barbara',
254 ] ],
256 'Exact match missing from results should be added as first result (T72958)',
257 'provision' => [
258 'Barcelona',
259 'Barbara',
260 'Bart',
262 'query' => 'Bar',
263 'results' => [
264 'Bar',
265 'Barcelona',
266 'Barbara',
268 ] ],
270 'Exact match missing and not existing pages should be dropped',
271 'provision' => [
272 'Exile',
273 'Exist',
274 'External',
276 'query' => 'Ex',
277 'results' => [
278 'External',
280 ] ],
282 "Exact match shouldn't override already found match if " .
283 "exact is redirect and found isn't",
284 'provision' => [
285 // Target of the exact match is low in the list
286 'Redirect Test Worse Result',
287 'Redirect Test',
289 'query' => 'redirect test',
290 'results' => [
291 // Redirect target is pulled up and exact match isn't added
292 'Redirect Test',
293 'Redirect Test Worse Result',
295 ] ],
297 "Exact match should override already found match if " .
298 "both exact match and found match are redirect",
299 'provision' => [
300 // Another redirect to the same target as the exact match
301 // is low in the list
302 'Redirect Test2 Worse Result',
303 'Redirect test2',
305 'query' => 'redirect TEST2',
306 'results' => [
307 // Found redirect is pulled to the top and exact match isn't
308 // added
309 'Redirect TEST2',
310 'Redirect Test2 Worse Result',
312 ] ],
314 "Exact match should override any already found matches that " .
315 "are redirects to it",
316 'provision' => [
317 // Another redirect to the same target as the exact match
318 // is low in the list
319 'Redirect Test Worse Result',
320 'Redirect test',
322 'query' => 'Redirect Test',
323 'results' => [
324 // Found redirect is pulled to the top and exact match isn't
325 // added
326 'Redirect Test',
327 'Redirect Test Worse Result',
328 'Redirect test',
330 ] ],
332 "Extra results must not be returned",
333 'provision' => [
334 'Example',
335 'Example Bar',
336 'Example Foo',
337 'Example Foo/Bar'
339 'query' => 'foo',
340 'results' => [
341 'Example',
342 'Example Bar',
343 'Example Foo',
345 ] ],
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( static function ( SearchSuggestion $s ) {
358 return $s->getText();
359 } );
361 $this->assertEquals(
362 $case['results'],
363 $results,
364 $case[0]
368 public static function paginationProvider() {
369 $res = [ 'Example', 'Example Bar', 'Example Foo', 'Example Foo/Bar' ];
370 return [
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' => [
378 true, $res,
384 * @dataProvider paginationProvider
385 * @covers \SearchSuggestionSet::hasMoreResults
387 public function testPagination( $hasMoreResults, $provision ) {
388 $search = $this->mockSearchWithResults( $provision );
389 $results = $search->completionSearch( 'irrelevant' );
391 $this->assertEquals( $hasMoreResults, $results->hasMoreResults() );
394 private function mockSearchWithResults( $titleStrings, $limit = 3 ) {
395 $search = $this->getMockBuilder( SearchEngine::class )
396 ->onlyMethods( [ 'completionSearchBackend' ] )->getMock();
398 $return = SearchSuggestionSet::fromStrings( $titleStrings );
400 $search->method( 'completionSearchBackend' )
401 ->willReturn( $return );
403 $search->setLimitOffset( $limit );
404 return $search;