MediaWiki  1.29.2
PrefixSearchTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
11  const NS_NONCAP = 12346;
12 
14 
15  public function addDBDataOnce() {
16  if ( !$this->isWikitextNS( NS_MAIN ) ) {
17  // tests are skipped if NS_MAIN is not wikitext
18  return;
19  }
20 
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' );
35 
36  $this->insertPage( 'Talk:Sandbox' );
37  $this->insertPage( 'Talk:Example' );
38 
39  $this->insertPage( 'User:Example' );
40 
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' ) );
44  }
45 
46  protected function setUp() {
47  parent::setUp();
48 
49  if ( !$this->isWikitextNS( NS_MAIN ) ) {
50  $this->markTestSkipped( 'Main namespace does not support wikitext.' );
51  }
52 
53  // Avoid special pages from extensions interferring with the tests
54  $this->setMwGlobals( [
55  'wgSpecialPages' => [],
56  'wgHooks' => [],
57  'wgExtraNamespaces' => [ self::NS_NONCAP => 'NonCap' ],
58  'wgCapitalLinkOverrides' => [ self::NS_NONCAP => false ],
59  ] );
60 
61  $this->originalHandlers = TestingAccessWrapper::newFromClass( 'Hooks' )->handlers;
62  TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
63 
64  // Clear caches so that our new namespace appears
66  Language::factory( 'en' )->resetNamespaces();
67 
69  }
70 
71  public function tearDown() {
72  parent::tearDown();
73 
74  TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = $this->originalHandlers;
75 
77  }
78 
79  protected function searchProvision( array $results = null ) {
80  if ( $results === null ) {
81  $this->setMwGlobals( 'wgHooks', [] );
82  } else {
83  $this->setMwGlobals( 'wgHooks', [
84  'PrefixSearchBackend' => [
85  function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
86  $srchres = $results;
87  return false;
88  }
89  ],
90  ] );
91  }
92  }
93 
94  public static function provideSearch() {
95  return [
96  [ [
97  'Empty string',
98  'query' => '',
99  'results' => [],
100  ] ],
101  [ [
102  'Main namespace with title prefix',
103  'query' => 'Ex',
104  'results' => [
105  'Example',
106  'Example/Baz',
107  'Example Bar',
108  ],
109  // Third result when testing offset
110  'offsetresult' => [
111  'Example Foo',
112  ],
113  ] ],
114  [ [
115  'Talk namespace prefix',
116  'query' => 'Talk:',
117  'results' => [
118  'Talk:Example',
119  'Talk:Sandbox',
120  ],
121  ] ],
122  [ [
123  'User namespace prefix',
124  'query' => 'User:',
125  'results' => [
126  'User:Example',
127  ],
128  ] ],
129  [ [
130  'Special namespace prefix',
131  'query' => 'Special:',
132  'results' => [
133  'Special:ActiveUsers',
134  'Special:AllMessages',
135  'Special:AllMyUploads',
136  ],
137  // Third result when testing offset
138  'offsetresult' => [
139  'Special:AllPages',
140  ],
141  ] ],
142  [ [
143  'Special namespace with prefix',
144  'query' => 'Special:Un',
145  'results' => [
146  'Special:Unblock',
147  'Special:UncategorizedCategories',
148  'Special:UncategorizedFiles',
149  ],
150  // Third result when testing offset
151  'offsetresult' => [
152  'Special:UncategorizedPages',
153  ],
154  ] ],
155  [ [
156  'Special page name',
157  'query' => 'Special:EditWatchlist',
158  'results' => [
159  'Special:EditWatchlist',
160  ],
161  ] ],
162  [ [
163  'Special page subpages',
164  'query' => 'Special:EditWatchlist/',
165  'results' => [
166  'Special:EditWatchlist/clear',
167  'Special:EditWatchlist/raw',
168  ],
169  ] ],
170  [ [
171  'Special page subpages with prefix',
172  'query' => 'Special:EditWatchlist/cl',
173  'results' => [
174  'Special:EditWatchlist/clear',
175  ],
176  ] ],
177  [ [
178  'Namespace with case sensitive first letter',
179  'query' => 'NonCap:upper',
180  'results' => []
181  ] ],
182  [ [
183  'Multinamespace search',
184  'query' => 'B',
185  'results' => [
186  'Bar',
187  'NonCap:Bar',
188  ],
189  'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
190  ] ],
191  [ [
192  'Multinamespace search with lowercase first letter',
193  'query' => 'sand',
194  'results' => [
195  'Sandbox',
196  'NonCap:sandbox',
197  ],
198  'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
199  ] ],
200  ];
201  }
202 
208  public function testSearch( array $case ) {
209  $this->searchProvision( null );
210 
211  $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
212 
213  $searcher = new StringPrefixSearch;
214  $results = $searcher->search( $case['query'], 3, $namespaces );
215  $this->assertEquals(
216  $case['results'],
217  $results,
218  $case[0]
219  );
220  }
221 
227  public function testSearchWithOffset( array $case ) {
228  $this->searchProvision( null );
229 
230  $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
231 
232  $searcher = new StringPrefixSearch;
233  $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
234 
235  // We don't expect the first result when offsetting
236  array_shift( $case['results'] );
237  // And sometimes we expect a different last result
238  $expected = isset( $case['offsetresult'] ) ?
239  array_merge( $case['results'], $case['offsetresult'] ) :
240  $case['results'];
241 
242  $this->assertEquals(
243  $expected,
244  $results,
245  $case[0]
246  );
247  }
248 
249  public static function provideSearchBackend() {
250  return [
251  [ [
252  'Simple case',
253  'provision' => [
254  'Bar',
255  'Barcelona',
256  'Barbara',
257  ],
258  'query' => 'Bar',
259  'results' => [
260  'Bar',
261  'Barcelona',
262  'Barbara',
263  ],
264  ] ],
265  [ [
266  'Exact match not on top (T72958)',
267  'provision' => [
268  'Barcelona',
269  'Bar',
270  'Barbara',
271  ],
272  'query' => 'Bar',
273  'results' => [
274  'Bar',
275  'Barcelona',
276  'Barbara',
277  ],
278  ] ],
279  [ [
280  'Exact match missing (T72958)',
281  'provision' => [
282  'Barcelona',
283  'Barbara',
284  'Bart',
285  ],
286  'query' => 'Bar',
287  'results' => [
288  'Bar',
289  'Barcelona',
290  'Barbara',
291  ],
292  ] ],
293  [ [
294  'Exact match missing and not existing',
295  'provision' => [
296  'Exile',
297  'Exist',
298  'External',
299  ],
300  'query' => 'Ex',
301  'results' => [
302  'Exile',
303  'Exist',
304  'External',
305  ],
306  ] ],
307  [ [
308  "Exact match shouldn't override already found match if " .
309  "exact is redirect and found isn't",
310  'provision' => [
311  // Target of the exact match is low in the list
312  'Redirect Test Worse Result',
313  'Redirect Test',
314  ],
315  'query' => 'redirect test',
316  'results' => [
317  // Redirect target is pulled up and exact match isn't added
318  'Redirect Test',
319  'Redirect Test Worse Result',
320  ],
321  ] ],
322  [ [
323  "Exact match shouldn't override already found match if " .
324  "both exact match and found match are redirect",
325  'provision' => [
326  // Another redirect to the same target as the exact match
327  // is low in the list
328  'Redirect Test2 Worse Result',
329  'Redirect test2',
330  ],
331  'query' => 'redirect TEST2',
332  'results' => [
333  // Found redirect is pulled to the top and exact match isn't
334  // added
335  'Redirect test2',
336  'Redirect Test2 Worse Result',
337  ],
338  ] ],
339  [ [
340  "Exact match should override any already found matches that " .
341  "are redirects to it",
342  'provision' => [
343  // Another redirect to the same target as the exact match
344  // is low in the list
345  'Redirect Test Worse Result',
346  'Redirect test',
347  ],
348  'query' => 'Redirect Test',
349  'results' => [
350  // Found redirect is pulled to the top and exact match isn't
351  // added
352  'Redirect Test',
353  'Redirect Test Worse Result',
354  ],
355  ] ],
356  ];
357  }
358 
363  public function testSearchBackend( array $case ) {
364  $this->searchProvision( $case['provision'] );
365  $searcher = new StringPrefixSearch;
366  $results = $searcher->search( $case['query'], 3 );
367  $this->assertEquals(
368  $case['results'],
369  $results,
370  $case[0]
371  );
372  }
373 }
PrefixSearchTest\addDBDataOnce
addDBDataOnce()
Stub.
Definition: PrefixSearchTest.php:15
MediaWikiTestCase\isWikitextNS
isWikitextNS( $ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
Definition: MediaWikiTestCase.php:1622
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:934
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
PrefixSearchTest\tearDown
tearDown()
Definition: PrefixSearchTest.php:71
PrefixSearchTest\provideSearch
static provideSearch()
Definition: PrefixSearchTest.php:94
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:62
PrefixSearchTest\$originalHandlers
$originalHandlers
Definition: PrefixSearchTest.php:13
PrefixSearchTest\NS_NONCAP
const NS_NONCAP
Definition: PrefixSearchTest.php:11
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
PrefixSearch\search
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:55
$limit
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers please use GetContentModels hook to make them known to core if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1049
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
PrefixSearchTest\testSearchWithOffset
testSearchWithOffset(array $case)
provideSearch PrefixSearch::search PrefixSearch::searchBackend
Definition: PrefixSearchTest.php:227
PrefixSearchTest\testSearchBackend
testSearchBackend(array $case)
provideSearchBackend PrefixSearch::searchBackend
Definition: PrefixSearchTest.php:363
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
PrefixSearchTest\setUp
setUp()
Definition: PrefixSearchTest.php:46
MediaWikiTestCase\insertPage
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null)
Insert a new page.
Definition: MediaWikiTestCase.php:953
SpecialPageFactory\resetList
static resetList()
Reset the internal list of special pages.
Definition: SpecialPageFactory.php:198
MWNamespace\getCanonicalNamespaces
static getCanonicalNamespaces( $rebuild=false)
Returns array of all defined namespaces with their canonical (English) names.
Definition: MWNamespace.php:207
PrefixSearchTest\provideSearchBackend
static provideSearchBackend()
Definition: PrefixSearchTest.php:249
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
PrefixSearchTest
Search Database PrefixSearch.
Definition: PrefixSearchTest.php:10
StringPrefixSearch
Performs prefix search, returning strings.
Definition: PrefixSearch.php:395
array
the array() calling protocol came about after MediaWiki 1.4rc1.
PrefixSearchTest\searchProvision
searchProvision(array $results=null)
Definition: PrefixSearchTest.php:79
PrefixSearchTest\testSearch
testSearch(array $case)
provideSearch PrefixSearch::search PrefixSearch::searchBackend
Definition: PrefixSearchTest.php:208