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
= [];
64 SpecialPageFactory
::resetList();
67 public function tearDown() {
70 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= $this->originalHandlers
;
72 SpecialPageFactory
::resetList();
75 protected function searchProvision( array $results = null ) {
76 if ( $results === null ) {
77 $this->setMwGlobals( 'wgHooks', [] );
79 $this->setMwGlobals( 'wgHooks', [
80 'PrefixSearchBackend' => [
81 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
90 public static function provideSearch() {
98 'Main namespace with title prefix',
105 // Third result when testing offset
111 'Talk namespace prefix',
119 'User namespace prefix',
126 'Special namespace prefix',
127 'query' => 'Special:',
129 'Special:ActiveUsers',
130 'Special:AllMessages',
131 'Special:AllMyUploads',
133 // Third result when testing offset
139 'Special namespace with prefix',
140 'query' => 'Special:Un',
143 'Special:UncategorizedCategories',
144 'Special:UncategorizedFiles',
146 // Third result when testing offset
148 'Special:UncategorizedPages',
153 'query' => 'Special:EditWatchlist',
155 'Special:EditWatchlist',
159 'Special page subpages',
160 'query' => 'Special:EditWatchlist/',
162 'Special:EditWatchlist/clear',
163 'Special:EditWatchlist/raw',
167 'Special page subpages with prefix',
168 'query' => 'Special:EditWatchlist/cl',
170 'Special:EditWatchlist/clear',
174 'Namespace with case sensitive first letter',
175 'query' => 'NonCap:upper',
179 'Multinamespace search',
185 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
188 'Multinamespace search with lowercase first letter',
194 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
200 * @dataProvider provideSearch
201 * @covers PrefixSearch::search
202 * @covers PrefixSearch::searchBackend
204 public function testSearch( array $case ) {
205 // FIXME: fails under postgres
206 $this->markTestSkippedIfDbType( 'postgres' );
207 $this->searchProvision( null );
209 $namespaces = $case['namespaces'] ??
[];
211 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
212 // Postgres will sort lexicographically on utf8 code units (" " before "/")
213 sort( $case['results'], SORT_STRING
);
216 $searcher = new StringPrefixSearch
;
217 $results = $searcher->search( $case['query'], 3, $namespaces );
226 * @dataProvider provideSearch
227 * @covers PrefixSearch::search
228 * @covers PrefixSearch::searchBackend
230 public function testSearchWithOffset( array $case ) {
231 // FIXME: fails under postgres
232 $this->markTestSkippedIfDbType( 'postgres' );
233 $this->searchProvision( null );
235 $namespaces = $case['namespaces'] ??
[];
237 $searcher = new StringPrefixSearch
;
238 $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
240 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
241 // Postgres will sort lexicographically on utf8 code units (" " before "/")
242 sort( $case['results'], SORT_STRING
);
245 // We don't expect the first result when offsetting
246 array_shift( $case['results'] );
247 // And sometimes we expect a different last result
248 $expected = isset( $case['offsetresult'] ) ?
249 array_merge( $case['results'], $case['offsetresult'] ) :
259 public static function provideSearchBackend() {
276 'Exact match not on top (T72958)',
290 'Exact match missing (T72958)',
304 'Exact match missing and not existing',
318 "Exact match shouldn't override already found match if " .
319 "exact is redirect and found isn't",
321 // Target of the exact match is low in the list
322 'Redirect Test Worse Result',
325 'query' => 'redirect test',
327 // Redirect target is pulled up and exact match isn't added
329 'Redirect Test Worse Result',
333 "Exact match shouldn't override already found match if " .
334 "both exact match and found match are redirect",
336 // Another redirect to the same target as the exact match
337 // is low in the list
338 'Redirect Test2 Worse Result',
341 'query' => 'redirect TEST2',
343 // Found redirect is pulled to the top and exact match isn't
346 'Redirect Test2 Worse Result',
350 "Exact match should override any already found matches that " .
351 "are redirects to it",
353 // Another redirect to the same target as the exact match
354 // is low in the list
355 'Redirect Test Worse Result',
358 'query' => 'Redirect Test',
360 // Found redirect is pulled to the top and exact match isn't
363 'Redirect Test Worse Result',
370 * @dataProvider provideSearchBackend
371 * @covers PrefixSearch::searchBackend
373 public function testSearchBackend( array $case ) {
374 $this->searchProvision( $case['provision'] );
375 $searcher = new StringPrefixSearch
;
376 $results = $searcher->search( $case['query'], 3 );