MediaWiki  1.28.0
SearchEnginePrefixTest.php
Go to the documentation of this file.
1 <?php
3 
10 
14  private $search;
15 
16  public function addDBDataOnce() {
17  if ( !$this->isWikitextNS( NS_MAIN ) ) {
18  // tests are skipped if NS_MAIN is not wikitext
19  return;
20  }
21 
22  $this->insertPage( 'Sandbox' );
23  $this->insertPage( 'Bar' );
24  $this->insertPage( 'Example' );
25  $this->insertPage( 'Example Bar' );
26  $this->insertPage( 'Example Foo' );
27  $this->insertPage( 'Example Foo/Bar' );
28  $this->insertPage( 'Example/Baz' );
29  $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
30  $this->insertPage( 'Redirect Test' );
31  $this->insertPage( 'Redirect Test Worse Result' );
32  $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
33  $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
34  $this->insertPage( 'Redirect Test2' );
35  $this->insertPage( 'Redirect Test2 Worse Result' );
36 
37  $this->insertPage( 'Talk:Sandbox' );
38  $this->insertPage( 'Talk:Example' );
39 
40  $this->insertPage( 'User:Example' );
41  }
42 
43  protected function setUp() {
44  parent::setUp();
45 
46  if ( !$this->isWikitextNS( NS_MAIN ) ) {
47  $this->markTestSkipped( 'Main namespace does not support wikitext.' );
48  }
49 
50  // Avoid special pages from extensions interferring with the tests
51  $this->setMwGlobals( [
52  'wgSpecialPages' => [],
53  'wgHooks' => [],
54  ] );
55 
56  $this->search = MediaWikiServices::getInstance()->newSearchEngine();
57  $this->search->setNamespaces( [] );
58 
59  $this->originalHandlers = TestingAccessWrapper::newFromClass( 'Hooks' )->handlers;
60  TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
61 
63  }
64 
65  public function tearDown() {
66  parent::tearDown();
67 
69 
71  }
72 
73  protected function searchProvision( array $results = null ) {
74  if ( $results === null ) {
75  $this->setMwGlobals( 'wgHooks', [] );
76  } else {
77  $this->setMwGlobals( 'wgHooks', [
78  'PrefixSearchBackend' => [
79  function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
80  $srchres = $results;
81  return false;
82  }
83  ],
84  ] );
85  }
86  }
87 
88  public static function provideSearch() {
89  return [
90  [ [
91  'Empty string',
92  'query' => '',
93  'results' => [],
94  ] ],
95  [ [
96  'Main namespace with title prefix',
97  'query' => 'Ex',
98  'results' => [
99  'Example',
100  'Example/Baz',
101  'Example Bar',
102  ],
103  // Third result when testing offset
104  'offsetresult' => [
105  'Example Foo',
106  ],
107  ] ],
108  [ [
109  'Talk namespace prefix',
110  'query' => 'Talk:',
111  'results' => [
112  'Talk:Example',
113  'Talk:Sandbox',
114  ],
115  ] ],
116  [ [
117  'User namespace prefix',
118  'query' => 'User:',
119  'results' => [
120  'User:Example',
121  ],
122  ] ],
123  [ [
124  'Special namespace prefix',
125  'query' => 'Special:',
126  'results' => [
127  'Special:ActiveUsers',
128  'Special:AllMessages',
129  'Special:AllMyFiles',
130  ],
131  // Third result when testing offset
132  'offsetresult' => [
133  'Special:AllMyUploads',
134  ],
135  ] ],
136  [ [
137  'Special namespace with prefix',
138  'query' => 'Special:Un',
139  'results' => [
140  'Special:Unblock',
141  'Special:UncategorizedCategories',
142  'Special:UncategorizedFiles',
143  ],
144  // Third result when testing offset
145  'offsetresult' => [
146  'Special:UncategorizedImages',
147  ],
148  ] ],
149  [ [
150  'Special page name',
151  'query' => 'Special:EditWatchlist',
152  'results' => [
153  'Special:EditWatchlist',
154  ],
155  ] ],
156  [ [
157  'Special page subpages',
158  'query' => 'Special:EditWatchlist/',
159  'results' => [
160  'Special:EditWatchlist/clear',
161  'Special:EditWatchlist/raw',
162  ],
163  ] ],
164  [ [
165  'Special page subpages with prefix',
166  'query' => 'Special:EditWatchlist/cl',
167  'results' => [
168  'Special:EditWatchlist/clear',
169  ],
170  ] ],
171  ];
172  }
173 
178  public function testSearch( array $case ) {
179  $this->search->setLimitOffset( 3 );
180  $results = $this->search->defaultPrefixSearch( $case['query'] );
181  $results = array_map( function( Title $t ) {
182  return $t->getPrefixedText();
183  }, $results );
184  $this->assertEquals(
185  $case['results'],
186  $results,
187  $case[0]
188  );
189  }
190 
195  public function testSearchWithOffset( array $case ) {
196  $this->search->setLimitOffset( 3, 1 );
197  $results = $this->search->defaultPrefixSearch( $case['query'] );
198  $results = array_map( function( Title $t ) {
199  return $t->getPrefixedText();
200  }, $results );
201 
202  // We don't expect the first result when offsetting
203  array_shift( $case['results'] );
204  // And sometimes we expect a different last result
205  $expected = isset( $case['offsetresult'] ) ?
206  array_merge( $case['results'], $case['offsetresult'] ) :
207  $case['results'];
208 
209  $this->assertEquals(
210  $expected,
211  $results,
212  $case[0]
213  );
214  }
215 
216  public static function provideSearchBackend() {
217  return [
218  [ [
219  'Simple case',
220  'provision' => [
221  'Bar',
222  'Barcelona',
223  'Barbara',
224  ],
225  'query' => 'Bar',
226  'results' => [
227  'Bar',
228  'Barcelona',
229  'Barbara',
230  ],
231  ] ],
232  [ [
233  'Exact match not on top (bug 70958)',
234  'provision' => [
235  'Barcelona',
236  'Bar',
237  'Barbara',
238  ],
239  'query' => 'Bar',
240  'results' => [
241  'Bar',
242  'Barcelona',
243  'Barbara',
244  ],
245  ] ],
246  [ [
247  'Exact match missing (bug 70958)',
248  'provision' => [
249  'Barcelona',
250  'Barbara',
251  'Bart',
252  ],
253  'query' => 'Bar',
254  'results' => [
255  'Bar',
256  'Barcelona',
257  'Barbara',
258  ],
259  ] ],
260  [ [
261  'Exact match missing and not existing',
262  'provision' => [
263  'Exile',
264  'Exist',
265  'External',
266  ],
267  'query' => 'Ex',
268  'results' => [
269  'Exile',
270  'Exist',
271  'External',
272  ],
273  ] ],
274  [ [
275  "Exact match shouldn't override already found match if " .
276  "exact is redirect and found isn't",
277  'provision' => [
278  // Target of the exact match is low in the list
279  'Redirect Test Worse Result',
280  'Redirect Test',
281  ],
282  'query' => 'redirect test',
283  'results' => [
284  // Redirect target is pulled up and exact match isn't added
285  'Redirect Test',
286  'Redirect Test Worse Result',
287  ],
288  ] ],
289  [ [
290  "Exact match shouldn't override already found match if " .
291  "both exact match and found match are redirect",
292  'provision' => [
293  // Another redirect to the same target as the exact match
294  // is low in the list
295  'Redirect Test2 Worse Result',
296  'Redirect test2',
297  ],
298  'query' => 'redirect TEST2',
299  'results' => [
300  // Found redirect is pulled to the top and exact match isn't
301  // added
302  'Redirect test2',
303  'Redirect Test2 Worse Result',
304  ],
305  ] ],
306  [ [
307  "Exact match should override any already found matches that " .
308  "are redirects to it",
309  'provision' => [
310  // Another redirect to the same target as the exact match
311  // is low in the list
312  'Redirect Test Worse Result',
313  'Redirect test',
314  ],
315  'query' => 'Redirect Test',
316  'results' => [
317  // Found redirect is pulled to the top and exact match isn't
318  // added
319  'Redirect Test',
320  'Redirect Test Worse Result',
321  'Redirect test',
322  ],
323  ] ],
324  ];
325  }
326 
331  public function testSearchBackend( array $case ) {
332  $search = $stub = $this->getMockBuilder( 'SearchEngine' )
333  ->setMethods( [ 'completionSearchBackend' ] )->getMock();
334 
335  $return = SearchSuggestionSet::fromStrings( $case['provision'] );
336 
337  $search->expects( $this->any() )
338  ->method( 'completionSearchBackend' )
339  ->will( $this->returnValue( $return ) );
340 
341  $search->setLimitOffset( 3 );
342  $results = $search->completionSearch( $case['query'] );
343 
344  $results = $results->map( function( SearchSuggestion $s ) {
345  return $s->getText();
346  } );
347 
348  $this->assertEquals(
349  $case['results'],
350  $results,
351  $case[0]
352  );
353  }
354 }
the array() calling protocol came about after MediaWiki 1.4rc1.
Search suggestion.
const NS_MAIN
Definition: Defines.php:56
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testSearch(array $case)
provideSearch SearchEngine::defaultPrefixSearch
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 MediaWikiServices
Definition: injection.txt:23
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1455
static fromStrings(array $titles)
Builds a new set of suggestion based on a string array.
static newFromClass($className)
Allow access to non-public static methods and properties of the class.
testSearchBackend(array $case)
provideSearchBackend PrefixSearch::searchBackend
searchProvision(array $results=null)
getText()
The suggestion text.
setLimitOffset($limit, $offset=0)
Set the maximum number of results to return and how many to skip before returning the first...
Base class that store and restore the Language objects.
static resetList()
Reset the internal list of special pages.
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:953
testSearchWithOffset(array $case)
provideSearch SearchEngine::defaultPrefixSearch
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
insertPage($pageName, $text= 'Sample page for unit test.', $namespace=null)
Insert a new page.
completionSearch($search)
Perform a completion search.
isWikitextNS($ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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 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:1046
setMwGlobals($pairs, $value=null)