3 use Wikimedia\TestingAccessWrapper
;
10 class PrefixSearchTest
extends MediaWikiLangTestCase
{
11 const NS_NONCAP
= 12346;
13 private $originalHandlers;
15 public function addDBDataOnce() {
16 if ( !$this->isWikitextNS( NS_MAIN
) ) {
17 // tests are skipped if NS_MAIN is not wikitext
21 $this->insertPage( 'Sandbox' );
22 $this->insertPage( 'Bar' );
23 $this->insertPage( 'Example' );
24 $this->insertPage( 'Example Bar' );
25 $this->insertPage( 'Example Foo' );
26 $this->insertPage( 'Example Foo/Bar' );
27 $this->insertPage( 'Example/Baz' );
28 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
29 $this->insertPage( 'Redirect Test' );
30 $this->insertPage( 'Redirect Test Worse Result' );
31 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
32 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
33 $this->insertPage( 'Redirect Test2' );
34 $this->insertPage( 'Redirect Test2 Worse Result' );
36 $this->insertPage( 'Talk:Sandbox' );
37 $this->insertPage( 'Talk:Example' );
39 $this->insertPage( 'User:Example' );
41 $this->insertPage( Title
::makeTitle( self
::NS_NONCAP
, 'Bar' ) );
42 $this->insertPage( Title
::makeTitle( self
::NS_NONCAP
, 'Upper' ) );
43 $this->insertPage( Title
::makeTitle( self
::NS_NONCAP
, 'sandbox' ) );
46 protected function setUp() {
49 if ( !$this->isWikitextNS( NS_MAIN
) ) {
50 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
53 // Avoid special pages from extensions interfering with the tests
54 $this->setMwGlobals( [
55 'wgSpecialPages' => [],
57 'wgExtraNamespaces' => [ self
::NS_NONCAP
=> 'NonCap' ],
58 'wgCapitalLinkOverrides' => [ self
::NS_NONCAP
=> false ],
61 $this->originalHandlers
= TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
;
62 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= [];
65 public function tearDown() {
68 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= $this->originalHandlers
;
71 protected function searchProvision( array $results = null ) {
72 if ( $results === null ) {
73 $this->setMwGlobals( 'wgHooks', [] );
75 $this->setMwGlobals( 'wgHooks', [
76 'PrefixSearchBackend' => [
77 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
86 public static function provideSearch() {
94 'Main namespace with title prefix',
101 // Third result when testing offset
107 'Talk namespace prefix',
115 'User namespace prefix',
122 'Special namespace prefix',
123 'query' => 'Special:',
125 'Special:ActiveUsers',
126 'Special:AllMessages',
127 'Special:AllMyUploads',
129 // Third result when testing offset
135 'Special namespace with prefix',
136 'query' => 'Special:Un',
139 'Special:UncategorizedCategories',
140 'Special:UncategorizedFiles',
142 // Third result when testing offset
144 'Special:UncategorizedPages',
149 'query' => 'Special:EditWatchlist',
151 'Special:EditWatchlist',
155 'Special page subpages',
156 'query' => 'Special:EditWatchlist/',
158 'Special:EditWatchlist/clear',
159 'Special:EditWatchlist/raw',
163 'Special page subpages with prefix',
164 'query' => 'Special:EditWatchlist/cl',
166 'Special:EditWatchlist/clear',
170 'Namespace with case sensitive first letter',
171 'query' => 'NonCap:upper',
175 'Multinamespace search',
181 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
184 'Multinamespace search with lowercase first letter',
190 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
196 * @dataProvider provideSearch
197 * @covers PrefixSearch::search
198 * @covers PrefixSearch::searchBackend
200 public function testSearch( array $case ) {
201 // FIXME: fails under postgres
202 $this->markTestSkippedIfDbType( 'postgres' );
203 $this->searchProvision( null );
205 $namespaces = $case['namespaces'] ??
[];
207 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
208 // Postgres will sort lexicographically on utf8 code units (" " before "/")
209 sort( $case['results'], SORT_STRING
);
212 $searcher = new StringPrefixSearch
;
213 $results = $searcher->search( $case['query'], 3, $namespaces );
222 * @dataProvider provideSearch
223 * @covers PrefixSearch::search
224 * @covers PrefixSearch::searchBackend
226 public function testSearchWithOffset( array $case ) {
227 // FIXME: fails under postgres
228 $this->markTestSkippedIfDbType( 'postgres' );
229 $this->searchProvision( null );
231 $namespaces = $case['namespaces'] ??
[];
233 $searcher = new StringPrefixSearch
;
234 $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
236 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
237 // Postgres will sort lexicographically on utf8 code units (" " before "/")
238 sort( $case['results'], SORT_STRING
);
241 // We don't expect the first result when offsetting
242 array_shift( $case['results'] );
243 // And sometimes we expect a different last result
244 $expected = isset( $case['offsetresult'] ) ?
245 array_merge( $case['results'], $case['offsetresult'] ) :
255 public static function provideSearchBackend() {
272 'Exact match not on top (T72958)',
286 'Exact match missing (T72958)',
300 'Exact match missing and not existing',
314 "Exact match shouldn't override already found match if " .
315 "exact is redirect and found isn't",
317 // Target of the exact match is low in the list
318 'Redirect Test Worse Result',
321 'query' => 'redirect test',
323 // Redirect target is pulled up and exact match isn't added
325 'Redirect Test Worse Result',
329 "Exact match shouldn't override already found match if " .
330 "both exact match and found match are redirect",
332 // Another redirect to the same target as the exact match
333 // is low in the list
334 'Redirect Test2 Worse Result',
337 'query' => 'redirect TEST2',
339 // Found redirect is pulled to the top and exact match isn't
342 'Redirect Test2 Worse Result',
346 "Exact match should override any already found matches that " .
347 "are redirects to it",
349 // Another redirect to the same target as the exact match
350 // is low in the list
351 'Redirect Test Worse Result',
354 'query' => 'Redirect Test',
356 // Found redirect is pulled to the top and exact match isn't
359 'Redirect Test Worse Result',
366 * @dataProvider provideSearchBackend
367 * @covers PrefixSearch::searchBackend
369 public function testSearchBackend( array $case ) {
370 $this->searchProvision( $case['provision'] );
371 $searcher = new StringPrefixSearch
;
372 $results = $searcher->search( $case['query'], 3 );