6 class PrefixSearchTest
extends MediaWikiLangTestCase
{
7 private $originalHandlers;
9 public function addDBDataOnce() {
10 if ( !$this->isWikitextNS( NS_MAIN
) ) {
11 // tests are skipped if NS_MAIN is not wikitext
15 $this->insertPage( 'Sandbox' );
16 $this->insertPage( 'Bar' );
17 $this->insertPage( 'Example' );
18 $this->insertPage( 'Example Bar' );
19 $this->insertPage( 'Example Foo' );
20 $this->insertPage( 'Example Foo/Bar' );
21 $this->insertPage( 'Example/Baz' );
22 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
23 $this->insertPage( 'Redirect Test' );
24 $this->insertPage( 'Redirect Test Worse Result' );
25 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
26 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
27 $this->insertPage( 'Redirect Test2' );
28 $this->insertPage( 'Redirect Test2 Worse Result' );
30 $this->insertPage( 'Talk:Sandbox' );
31 $this->insertPage( 'Talk:Example' );
33 $this->insertPage( 'User:Example' );
36 protected function setUp() {
39 if ( !$this->isWikitextNS( NS_MAIN
) ) {
40 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
43 // Avoid special pages from extensions interferring with the tests
44 $this->setMwGlobals( [
45 'wgSpecialPages' => [],
49 $this->originalHandlers
= TestingAccessWrapper
::newFromClass( 'Hooks' )->handlers
;
50 TestingAccessWrapper
::newFromClass( 'Hooks' )->handlers
= [];
52 SpecialPageFactory
::resetList();
55 public function tearDown() {
58 TestingAccessWrapper
::newFromClass( 'Hooks' )->handlers
= $this->originalHandlers
;
60 SpecialPageFactory
::resetList();
63 protected function searchProvision( array $results = null ) {
64 if ( $results === null ) {
65 $this->setMwGlobals( 'wgHooks', [] );
67 $this->setMwGlobals( 'wgHooks', [
68 'PrefixSearchBackend' => [
69 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
78 public static function provideSearch() {
86 'Main namespace with title prefix',
93 // Third result when testing offset
99 'Talk namespace prefix',
107 'User namespace prefix',
114 'Special namespace prefix',
115 'query' => 'Special:',
117 'Special:ActiveUsers',
118 'Special:AllMessages',
119 'Special:AllMyFiles',
121 // Third result when testing offset
123 'Special:AllMyUploads',
127 'Special namespace with prefix',
128 'query' => 'Special:Un',
131 'Special:UncategorizedCategories',
132 'Special:UncategorizedFiles',
134 // Third result when testing offset
136 'Special:UncategorizedImages',
141 'query' => 'Special:EditWatchlist',
143 'Special:EditWatchlist',
147 'Special page subpages',
148 'query' => 'Special:EditWatchlist/',
150 'Special:EditWatchlist/clear',
151 'Special:EditWatchlist/raw',
155 'Special page subpages with prefix',
156 'query' => 'Special:EditWatchlist/cl',
158 'Special:EditWatchlist/clear',
165 * @dataProvider provideSearch
166 * @covers PrefixSearch::search
167 * @covers PrefixSearch::searchBackend
169 public function testSearch( array $case ) {
170 $this->searchProvision( null );
171 $searcher = new StringPrefixSearch
;
172 $results = $searcher->search( $case['query'], 3 );
181 * @dataProvider provideSearch
182 * @covers PrefixSearch::search
183 * @covers PrefixSearch::searchBackend
185 public function testSearchWithOffset( array $case ) {
186 $this->searchProvision( null );
187 $searcher = new StringPrefixSearch
;
188 $results = $searcher->search( $case['query'], 3, [], 1 );
190 // We don't expect the first result when offsetting
191 array_shift( $case['results'] );
192 // And sometimes we expect a different last result
193 $expected = isset( $case['offsetresult'] ) ?
194 array_merge( $case['results'], $case['offsetresult'] ) :
204 public static function provideSearchBackend() {
221 'Exact match not on top (bug 70958)',
235 'Exact match missing (bug 70958)',
249 'Exact match missing and not existing',
263 "Exact match shouldn't override already found match if " .
264 "exact is redirect and found isn't",
266 // Target of the exact match is low in the list
267 'Redirect Test Worse Result',
270 'query' => 'redirect test',
272 // Redirect target is pulled up and exact match isn't added
274 'Redirect Test Worse Result',
278 "Exact match shouldn't override already found match if " .
279 "both exact match and found match are redirect",
281 // Another redirect to the same target as the exact match
282 // is low in the list
283 'Redirect Test2 Worse Result',
286 'query' => 'redirect TEST2',
288 // Found redirect is pulled to the top and exact match isn't
291 'Redirect Test2 Worse Result',
295 "Exact match should override any already found matches that " .
296 "are redirects to it",
298 // Another redirect to the same target as the exact match
299 // is low in the list
300 'Redirect Test Worse Result',
303 'query' => 'Redirect Test',
305 // Found redirect is pulled to the top and exact match isn't
308 'Redirect Test Worse Result',
315 * @dataProvider provideSearchBackend
316 * @covers PrefixSearch::searchBackend
318 public function testSearchBackend( array $case ) {
319 $this->searchProvision( $case['provision'] );
320 $searcher = new StringPrefixSearch
;
321 $results = $searcher->search( $case['query'], 3 );