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 $this->overrideMwServices();
67 public function tearDown() {
70 TestingAccessWrapper
::newFromClass( Hooks
::class )->handlers
= $this->originalHandlers
;
73 protected function searchProvision( array $results = null ) {
74 if ( $results === null ) {
75 $this->setMwGlobals( 'wgHooks', [] );
77 $this->setMwGlobals( 'wgHooks', [
78 'PrefixSearchBackend' => [
79 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
88 public static function provideSearch() {
96 'Main namespace with title prefix',
103 // Third result when testing offset
109 'Talk namespace prefix',
117 'User namespace prefix',
124 'Special namespace prefix',
125 'query' => 'Special:',
127 'Special:ActiveUsers',
128 'Special:AllMessages',
129 'Special:AllMyUploads',
131 // Third result when testing offset
137 'Special namespace with prefix',
138 'query' => 'Special:Un',
141 'Special:UncategorizedCategories',
142 'Special:UncategorizedFiles',
144 // Third result when testing offset
146 'Special:UncategorizedPages',
151 'query' => 'Special:EditWatchlist',
153 'Special:EditWatchlist',
157 'Special page subpages',
158 'query' => 'Special:EditWatchlist/',
160 'Special:EditWatchlist/clear',
161 'Special:EditWatchlist/raw',
165 'Special page subpages with prefix',
166 'query' => 'Special:EditWatchlist/cl',
168 'Special:EditWatchlist/clear',
172 'Namespace with case sensitive first letter',
173 'query' => 'NonCap:upper',
177 'Multinamespace search',
183 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
186 'Multinamespace search with lowercase first letter',
192 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
198 * @dataProvider provideSearch
199 * @covers PrefixSearch::search
200 * @covers PrefixSearch::searchBackend
202 public function testSearch( array $case ) {
203 // FIXME: fails under postgres
204 $this->markTestSkippedIfDbType( 'postgres' );
205 $this->searchProvision( null );
207 $namespaces = $case['namespaces'] ??
[];
209 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
210 // Postgres will sort lexicographically on utf8 code units (" " before "/")
211 sort( $case['results'], SORT_STRING
);
214 $searcher = new StringPrefixSearch
;
215 $results = $searcher->search( $case['query'], 3, $namespaces );
224 * @dataProvider provideSearch
225 * @covers PrefixSearch::search
226 * @covers PrefixSearch::searchBackend
228 public function testSearchWithOffset( array $case ) {
229 // FIXME: fails under postgres
230 $this->markTestSkippedIfDbType( 'postgres' );
231 $this->searchProvision( null );
233 $namespaces = $case['namespaces'] ??
[];
235 $searcher = new StringPrefixSearch
;
236 $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
238 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
239 // Postgres will sort lexicographically on utf8 code units (" " before "/")
240 sort( $case['results'], SORT_STRING
);
243 // We don't expect the first result when offsetting
244 array_shift( $case['results'] );
245 // And sometimes we expect a different last result
246 $expected = isset( $case['offsetresult'] ) ?
247 array_merge( $case['results'], $case['offsetresult'] ) :
257 public static function provideSearchBackend() {
274 'Exact match not on top (T72958)',
288 'Exact match missing (T72958)',
302 'Exact match missing and not existing',
316 "Exact match shouldn't override already found match if " .
317 "exact is redirect and found isn't",
319 // Target of the exact match is low in the list
320 'Redirect Test Worse Result',
323 'query' => 'redirect test',
325 // Redirect target is pulled up and exact match isn't added
327 'Redirect Test Worse Result',
331 "Exact match shouldn't override already found match if " .
332 "both exact match and found match are redirect",
334 // Another redirect to the same target as the exact match
335 // is low in the list
336 'Redirect Test2 Worse Result',
339 'query' => 'redirect TEST2',
341 // Found redirect is pulled to the top and exact match isn't
344 'Redirect Test2 Worse Result',
348 "Exact match should override any already found matches that " .
349 "are redirects to it",
351 // Another redirect to the same target as the exact match
352 // is low in the list
353 'Redirect Test Worse Result',
356 'query' => 'Redirect Test',
358 // Found redirect is pulled to the top and exact match isn't
361 'Redirect Test Worse Result',
368 * @dataProvider provideSearchBackend
369 * @covers PrefixSearch::searchBackend
371 public function testSearchBackend( array $case ) {
372 $this->searchProvision( $case['provision'] );
373 $searcher = new StringPrefixSearch
;
374 $results = $searcher->search( $case['query'], 3 );