6 class SearchEnginePrefixTest
extends MediaWikiLangTestCase
{
13 public function addDBData() {
14 if ( !$this->isWikitextNS( NS_MAIN
) ) {
15 // tests are skipped if NS_MAIN is not wikitext
19 $this->insertPage( 'Sandbox' );
20 $this->insertPage( 'Bar' );
21 $this->insertPage( 'Example' );
22 $this->insertPage( 'Example Bar' );
23 $this->insertPage( 'Example Foo' );
24 $this->insertPage( 'Example Foo/Bar' );
25 $this->insertPage( 'Example/Baz' );
26 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
27 $this->insertPage( 'Redirect Test' );
28 $this->insertPage( 'Redirect Test Worse Result' );
29 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
30 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
31 $this->insertPage( 'Redirect Test2' );
32 $this->insertPage( 'Redirect Test2 Worse Result' );
34 $this->insertPage( 'Talk:Sandbox' );
35 $this->insertPage( 'Talk:Example' );
37 $this->insertPage( 'User:Example' );
40 protected function setUp() {
43 if ( !$this->isWikitextNS( NS_MAIN
) ) {
44 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
47 // Avoid special pages from extensions interferring with the tests
48 $this->setMwGlobals( 'wgSpecialPages', array() );
49 $this->search
= SearchEngine
::create();
50 $this->search
->setNamespaces( array() );
53 protected function searchProvision( Array $results = null ) {
54 if ( $results === null ) {
55 $this->setMwGlobals( 'wgHooks', array() );
57 $this->setMwGlobals( 'wgHooks', array(
58 'PrefixSearchBackend' => array(
59 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
68 public static function provideSearch() {
76 'Main namespace with title prefix',
83 // Third result when testing offset
84 'offsetresult' => array(
89 'Talk namespace prefix',
97 'User namespace prefix',
104 'Special namespace prefix',
105 'query' => 'Special:',
107 'Special:ActiveUsers',
108 'Special:AllMessages',
109 'Special:AllMyFiles',
111 // Third result when testing offset
112 'offsetresult' => array(
113 'Special:AllMyUploads',
117 'Special namespace with prefix',
118 'query' => 'Special:Un',
121 'Special:UncategorizedCategories',
122 'Special:UncategorizedFiles',
124 // Third result when testing offset
125 'offsetresult' => array(
126 'Special:UncategorizedImages',
131 'query' => 'Special:EditWatchlist',
133 'Special:EditWatchlist',
137 'Special page subpages',
138 'query' => 'Special:EditWatchlist/',
140 'Special:EditWatchlist/clear',
141 'Special:EditWatchlist/raw',
145 'Special page subpages with prefix',
146 'query' => 'Special:EditWatchlist/cl',
148 'Special:EditWatchlist/clear',
155 * @dataProvider provideSearch
156 * @covers SearchEngine::defaultPrefixSearch
158 public function testSearch( Array $case ) {
159 $this->search
->setLimitOffset( 3 );
160 $results = $this->search
->defaultPrefixSearch( $case['query'] );
161 $results = array_map( function( Title
$t ) {
162 return $t->getPrefixedText();
172 * @dataProvider provideSearch
173 * @covers SearchEngine::defaultPrefixSearch
175 public function testSearchWithOffset( Array $case ) {
176 $this->search
->setLimitOffset( 3, 1 );
177 $results = $this->search
->defaultPrefixSearch( $case['query'] );
178 $results = array_map( function( Title
$t ) {
179 return $t->getPrefixedText();
182 // We don't expect the first result when offsetting
183 array_shift( $case['results'] );
184 // And sometimes we expect a different last result
185 $expected = isset( $case['offsetresult'] ) ?
186 array_merge( $case['results'], $case['offsetresult'] ) :
196 public static function provideSearchBackend() {
200 'provision' => array(
213 'Exact match not on top (bug 70958)',
214 'provision' => array(
227 'Exact match missing (bug 70958)',
228 'provision' => array(
241 'Exact match missing and not existing',
242 'provision' => array(
255 "Exact match shouldn't override already found match if " .
256 "exact is redirect and found isn't",
257 'provision' => array(
258 // Target of the exact match is low in the list
259 'Redirect Test Worse Result',
262 'query' => 'redirect test',
264 // Redirect target is pulled up and exact match isn't added
266 'Redirect Test Worse Result',
270 "Exact match shouldn't override already found match if " .
271 "both exact match and found match are redirect",
272 'provision' => array(
273 // Another redirect to the same target as the exact match
274 // is low in the list
275 'Redirect Test2 Worse Result',
278 'query' => 'redirect TEST2',
280 // Found redirect is pulled to the top and exact match isn't
283 'Redirect Test2 Worse Result',
287 "Exact match should override any already found matches that " .
288 "are redirects to it",
289 'provision' => array(
290 // Another redirect to the same target as the exact match
291 // is low in the list
292 'Redirect Test Worse Result',
295 'query' => 'Redirect Test',
297 // Found redirect is pulled to the top and exact match isn't
300 'Redirect Test Worse Result',
308 * @dataProvider provideSearchBackend
309 * @covers PrefixSearch::searchBackend
311 public function testSearchBackend( Array $case ) {
312 $search = $stub = $this->getMockBuilder( 'SearchEngine' )
313 ->setMethods( array( 'completionSearchBackend' ) )->getMock();
315 $return = SearchSuggestionSet
::fromStrings( $case['provision'] );
317 $search->expects( $this->any() )
318 ->method( 'completionSearchBackend' )
319 ->will( $this->returnValue( $return ) );
321 $search->setLimitOffset( 3 );
322 $results = $search->completionSearch( $case['query'] );
324 $results = $results->map( function( SearchSuggestion
$s ) {
325 return $s->getText();