Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / search / SearchEnginePrefixTest.php
bloba88264bb78de825f02f14105316a6b48cde0127c
1 <?php
2 use MediaWiki\MediaWikiServices;
4 /**
5 * @group Search
6 * @group Database
7 */
8 class SearchEnginePrefixTest extends MediaWikiLangTestCase {
9 private $originalHandlers;
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( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
30 $this->insertPage( 'Redirect Test' );
31 $this->insertPage( 'Redirect Test Worse Result' );
32 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
33 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
34 $this->insertPage( 'Redirect Test2' );
35 $this->insertPage( 'Redirect Test2 Worse Result' );
37 $this->insertPage( 'Talk:Sandbox' );
38 $this->insertPage( 'Talk:Example' );
40 $this->insertPage( 'User:Example' );
43 protected function setUp() {
44 parent::setUp();
46 if ( !$this->isWikitextNS( NS_MAIN ) ) {
47 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
50 // Avoid special pages from extensions interferring with the tests
51 $this->setMwGlobals( [
52 'wgSpecialPages' => [],
53 'wgHooks' => [],
54 ] );
56 $this->search = MediaWikiServices::getInstance()->newSearchEngine();
57 $this->search->setNamespaces( [] );
59 $this->originalHandlers = TestingAccessWrapper::newFromClass( 'Hooks' )->handlers;
60 TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
62 SpecialPageFactory::resetList();
65 public function tearDown() {
66 parent::tearDown();
68 TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = $this->originalHandlers;
70 SpecialPageFactory::resetList();
73 protected function searchProvision( array $results = null ) {
74 if ( $results === null ) {
75 $this->setMwGlobals( 'wgHooks', [] );
76 } else {
77 $this->setMwGlobals( 'wgHooks', [
78 'PrefixSearchBackend' => [
79 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
80 $srchres = $results;
81 return false;
84 ] );
88 public static function provideSearch() {
89 return [
90 [ [
91 'Empty string',
92 'query' => '',
93 'results' => [],
94 ] ],
95 [ [
96 'Main namespace with title prefix',
97 'query' => 'Ex',
98 'results' => [
99 'Example',
100 'Example/Baz',
101 'Example Bar',
103 // Third result when testing offset
104 'offsetresult' => [
105 'Example Foo',
107 ] ],
109 'Talk namespace prefix',
110 'query' => 'Talk:',
111 'results' => [
112 'Talk:Example',
113 'Talk:Sandbox',
115 ] ],
117 'User namespace prefix',
118 'query' => 'User:',
119 'results' => [
120 'User:Example',
122 ] ],
124 'Special namespace prefix',
125 'query' => 'Special:',
126 'results' => [
127 'Special:ActiveUsers',
128 'Special:AllMessages',
129 'Special:AllMyUploads',
131 // Third result when testing offset
132 'offsetresult' => [
133 'Special:AllPages',
135 ] ],
137 'Special namespace with prefix',
138 'query' => 'Special:Un',
139 'results' => [
140 'Special:Unblock',
141 'Special:UncategorizedCategories',
142 'Special:UncategorizedFiles',
144 // Third result when testing offset
145 'offsetresult' => [
146 'Special:UncategorizedPages',
148 ] ],
150 'Special page name',
151 'query' => 'Special:EditWatchlist',
152 'results' => [
153 'Special:EditWatchlist',
155 ] ],
157 'Special page subpages',
158 'query' => 'Special:EditWatchlist/',
159 'results' => [
160 'Special:EditWatchlist/clear',
161 'Special:EditWatchlist/raw',
163 ] ],
165 'Special page subpages with prefix',
166 'query' => 'Special:EditWatchlist/cl',
167 'results' => [
168 'Special:EditWatchlist/clear',
170 ] ],
175 * @dataProvider provideSearch
176 * @covers SearchEngine::defaultPrefixSearch
178 public function testSearch( array $case ) {
179 $this->search->setLimitOffset( 3 );
180 $results = $this->search->defaultPrefixSearch( $case['query'] );
181 $results = array_map( function( Title $t ) {
182 return $t->getPrefixedText();
183 }, $results );
184 $this->assertEquals(
185 $case['results'],
186 $results,
187 $case[0]
192 * @dataProvider provideSearch
193 * @covers SearchEngine::defaultPrefixSearch
195 public function testSearchWithOffset( array $case ) {
196 $this->search->setLimitOffset( 3, 1 );
197 $results = $this->search->defaultPrefixSearch( $case['query'] );
198 $results = array_map( function( Title $t ) {
199 return $t->getPrefixedText();
200 }, $results );
202 // We don't expect the first result when offsetting
203 array_shift( $case['results'] );
204 // And sometimes we expect a different last result
205 $expected = isset( $case['offsetresult'] ) ?
206 array_merge( $case['results'], $case['offsetresult'] ) :
207 $case['results'];
209 $this->assertEquals(
210 $expected,
211 $results,
212 $case[0]
216 public static function provideSearchBackend() {
217 return [
219 'Simple case',
220 'provision' => [
221 'Bar',
222 'Barcelona',
223 'Barbara',
225 'query' => 'Bar',
226 'results' => [
227 'Bar',
228 'Barcelona',
229 'Barbara',
231 ] ],
233 'Exact match not on top (bug 70958)',
234 'provision' => [
235 'Barcelona',
236 'Bar',
237 'Barbara',
239 'query' => 'Bar',
240 'results' => [
241 'Bar',
242 'Barcelona',
243 'Barbara',
245 ] ],
247 'Exact match missing (bug 70958)',
248 'provision' => [
249 'Barcelona',
250 'Barbara',
251 'Bart',
253 'query' => 'Bar',
254 'results' => [
255 'Bar',
256 'Barcelona',
257 'Barbara',
259 ] ],
261 'Exact match missing and not existing',
262 'provision' => [
263 'Exile',
264 'Exist',
265 'External',
267 'query' => 'Ex',
268 'results' => [
269 'Exile',
270 'Exist',
271 'External',
273 ] ],
275 "Exact match shouldn't override already found match if " .
276 "exact is redirect and found isn't",
277 'provision' => [
278 // Target of the exact match is low in the list
279 'Redirect Test Worse Result',
280 'Redirect Test',
282 'query' => 'redirect test',
283 'results' => [
284 // Redirect target is pulled up and exact match isn't added
285 'Redirect Test',
286 'Redirect Test Worse Result',
288 ] ],
290 "Exact match shouldn't override already found match if " .
291 "both exact match and found match are redirect",
292 'provision' => [
293 // Another redirect to the same target as the exact match
294 // is low in the list
295 'Redirect Test2 Worse Result',
296 'Redirect test2',
298 'query' => 'redirect TEST2',
299 'results' => [
300 // Found redirect is pulled to the top and exact match isn't
301 // added
302 'Redirect test2',
303 'Redirect Test2 Worse Result',
305 ] ],
307 "Exact match should override any already found matches that " .
308 "are redirects to it",
309 'provision' => [
310 // Another redirect to the same target as the exact match
311 // is low in the list
312 'Redirect Test Worse Result',
313 'Redirect test',
315 'query' => 'Redirect Test',
316 'results' => [
317 // Found redirect is pulled to the top and exact match isn't
318 // added
319 'Redirect Test',
320 'Redirect Test Worse Result',
321 'Redirect test',
323 ] ],
328 * @dataProvider provideSearchBackend
329 * @covers PrefixSearch::searchBackend
331 public function testSearchBackend( array $case ) {
332 $search = $stub = $this->getMockBuilder( 'SearchEngine' )
333 ->setMethods( [ 'completionSearchBackend' ] )->getMock();
335 $return = SearchSuggestionSet::fromStrings( $case['provision'] );
337 $search->expects( $this->any() )
338 ->method( 'completionSearchBackend' )
339 ->will( $this->returnValue( $return ) );
341 $search->setLimitOffset( 3 );
342 $results = $search->completionSearch( $case['query'] );
344 $results = $results->map( function( SearchSuggestion $s ) {
345 return $s->getText();
346 } );
348 $this->assertEquals(
349 $case['results'],
350 $results,
351 $case[0]