6 class PrefixSearchTest
extends MediaWikiLangTestCase
{
8 public function addDBData() {
9 $this->insertPage( 'Sandbox' );
10 $this->insertPage( 'Bar' );
11 $this->insertPage( 'Example' );
12 $this->insertPage( 'Example Bar' );
13 $this->insertPage( 'Example Foo' );
14 $this->insertPage( 'Example Foo/Bar' );
15 $this->insertPage( 'Example/Baz' );
16 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
17 $this->insertPage( 'Redirect Test' );
18 $this->insertPage( 'Redirect Test Worse Result' );
19 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
20 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
21 $this->insertPage( 'Redirect Test2' );
22 $this->insertPage( 'Redirect Test2 Worse Result' );
24 $this->insertPage( 'Talk:Sandbox' );
25 $this->insertPage( 'Talk:Example' );
27 $this->insertPage( 'User:Example' );
30 protected function setUp() {
33 if ( !$this->isWikitextNS( NS_MAIN
) ) {
34 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
37 // Avoid special pages from extensions interferring with the tests
38 $this->setMwGlobals( 'wgSpecialPages', array() );
41 protected function searchProvision( Array $results = null ) {
42 if ( $results === null ) {
43 $this->setMwGlobals( 'wgHooks', array() );
45 $this->setMwGlobals( 'wgHooks', array(
46 'PrefixSearchBackend' => array(
47 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
56 public static function provideSearch() {
64 'Main namespace with title prefix',
71 // Third result when testing offset
72 'offsetresult' => array(
77 'Talk namespace prefix',
85 'User namespace prefix',
92 'Special namespace prefix',
93 'query' => 'Special:',
95 'Special:ActiveUsers',
96 'Special:AllMessages',
99 // Third result when testing offset
100 'offsetresult' => array(
101 'Special:AllMyUploads',
105 'Special namespace with prefix',
106 'query' => 'Special:Un',
109 'Special:UncategorizedCategories',
110 'Special:UncategorizedFiles',
112 // Third result when testing offset
113 'offsetresult' => array(
114 'Special:UncategorizedImages',
119 'query' => 'Special:EditWatchlist',
121 'Special:EditWatchlist',
125 'Special page subpages',
126 'query' => 'Special:EditWatchlist/',
128 'Special:EditWatchlist/clear',
129 'Special:EditWatchlist/raw',
133 'Special page subpages with prefix',
134 'query' => 'Special:EditWatchlist/cl',
136 'Special:EditWatchlist/clear',
143 * @dataProvider provideSearch
144 * @covers PrefixSearch::search
145 * @covers PrefixSearch::searchBackend
147 public function testSearch( Array $case ) {
148 $this->searchProvision( null );
149 $searcher = new StringPrefixSearch
;
150 $results = $searcher->search( $case['query'], 3 );
159 * @dataProvider provideSearch
160 * @covers PrefixSearch::search
161 * @covers PrefixSearch::searchBackend
163 public function testSearchWithOffset( Array $case ) {
164 $this->searchProvision( null );
165 $searcher = new StringPrefixSearch
;
166 $results = $searcher->search( $case['query'], 3, array(), 1 );
168 // We don't expect the first result when offsetting
169 array_shift( $case['results'] );
170 // And sometimes we expect a different last result
171 $expected = isset( $case['offsetresult'] ) ?
172 array_merge( $case['results'], $case['offsetresult'] ) :
182 public static function provideSearchBackend() {
186 'provision' => array(
199 'Exact match not on top (bug 70958)',
200 'provision' => array(
213 'Exact match missing (bug 70958)',
214 'provision' => array(
227 'Exact match missing and not existing',
228 'provision' => array(
241 "Exact match shouldn't override already found match if " .
242 "exact is redirect and found isn't",
243 'provision' => array(
244 // Target of the exact match is low in the list
245 'Redirect Test Worse Result',
248 'query' => 'redirect test',
250 // Redirect target is pulled up and exact match isn't added
252 'Redirect Test Worse Result',
256 "Exact match shouldn't override already found match if " .
257 "both exact match and found match are redirect",
258 'provision' => array(
259 // Another redirect to the same target as the exact match
260 // is low in the list
261 'Redirect Test2 Worse Result',
264 'query' => 'redirect TEST2',
266 // Found redirect is pulled to the top and exact match isn't
269 'Redirect Test2 Worse Result',
273 "Exact match should override any already found matches that " .
274 "are redirects to it",
275 'provision' => array(
276 // Another redirect to the same target as the exact match
277 // is low in the list
278 'Redirect Test Worse Result',
281 'query' => 'Redirect Test',
283 // Found redirect is pulled to the top and exact match isn't
286 'Redirect Test Worse Result',
293 * @dataProvider provideSearchBackend
294 * @covers PrefixSearch::searchBackend
296 public function testSearchBackend( Array $case ) {
297 $this->searchProvision( $case['provision'] );
298 $searcher = new StringPrefixSearch
;
299 $results = $searcher->search( $case['query'], 3 );