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 interferring 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' )->handlers
;
62 TestingAccessWrapper
::newFromClass( 'Hooks' )->handlers
= [];
64 // Clear caches so that our new namespace appears
65 MWNamespace
::getCanonicalNamespaces( true );
66 Language
::factory( 'en' )->resetNamespaces();
68 SpecialPageFactory
::resetList();
71 public function tearDown() {
74 TestingAccessWrapper
::newFromClass( 'Hooks' )->handlers
= $this->originalHandlers
;
76 SpecialPageFactory
::resetList();
79 protected function searchProvision( array $results = null ) {
80 if ( $results === null ) {
81 $this->setMwGlobals( 'wgHooks', [] );
83 $this->setMwGlobals( 'wgHooks', [
84 'PrefixSearchBackend' => [
85 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
94 public static function provideSearch() {
102 'Main namespace with title prefix',
109 // Third result when testing offset
115 'Talk namespace prefix',
123 'User namespace prefix',
130 'Special namespace prefix',
131 'query' => 'Special:',
133 'Special:ActiveUsers',
134 'Special:AllMessages',
135 'Special:AllMyUploads',
137 // Third result when testing offset
143 'Special namespace with prefix',
144 'query' => 'Special:Un',
147 'Special:UncategorizedCategories',
148 'Special:UncategorizedFiles',
150 // Third result when testing offset
152 'Special:UncategorizedPages',
157 'query' => 'Special:EditWatchlist',
159 'Special:EditWatchlist',
163 'Special page subpages',
164 'query' => 'Special:EditWatchlist/',
166 'Special:EditWatchlist/clear',
167 'Special:EditWatchlist/raw',
171 'Special page subpages with prefix',
172 'query' => 'Special:EditWatchlist/cl',
174 'Special:EditWatchlist/clear',
178 'Namespace with case sensitive first letter',
179 'query' => 'NonCap:upper',
183 'Multinamespace search',
189 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
192 'Multinamespace search with lowercase first letter',
198 'namespaces' => [ NS_MAIN
, self
::NS_NONCAP
],
204 * @dataProvider provideSearch
205 * @covers PrefixSearch::search
206 * @covers PrefixSearch::searchBackend
208 public function testSearch( array $case ) {
209 $this->searchProvision( null );
211 $namespaces = isset( $case['namespaces'] ) ?
$case['namespaces'] : [];
213 if ( wfGetDB( DB_REPLICA
)->getType() === 'postgres' ) {
214 // Postgres will sort lexicographically on utf8 code units (" " before "/")
215 sort( $case['results'], SORT_STRING
);
218 $searcher = new StringPrefixSearch
;
219 $results = $searcher->search( $case['query'], 3, $namespaces );
228 * @dataProvider provideSearch
229 * @covers PrefixSearch::search
230 * @covers PrefixSearch::searchBackend
232 public function testSearchWithOffset( array $case ) {
233 $this->searchProvision( null );
235 $namespaces = isset( $case['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 );