6 class PrefixSearchTest
extends MediaWikiLangTestCase
{
8 public function addDBData() {
9 if ( !$this->isWikitextNS( NS_MAIN
) ) {
10 // tests are skipped if NS_MAIN is not wikitext
14 $this->insertPage( 'Sandbox' );
15 $this->insertPage( 'Bar' );
16 $this->insertPage( 'Example' );
17 $this->insertPage( 'Example Bar' );
18 $this->insertPage( 'Example Foo' );
19 $this->insertPage( 'Example Foo/Bar' );
20 $this->insertPage( 'Example/Baz' );
21 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
22 $this->insertPage( 'Redirect Test' );
23 $this->insertPage( 'Redirect Test Worse Result' );
24 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
25 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
26 $this->insertPage( 'Redirect Test2' );
27 $this->insertPage( 'Redirect Test2 Worse Result' );
29 $this->insertPage( 'Talk:Sandbox' );
30 $this->insertPage( 'Talk:Example' );
32 $this->insertPage( 'User:Example' );
35 protected function setUp() {
38 if ( !$this->isWikitextNS( NS_MAIN
) ) {
39 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
42 // Avoid special pages from extensions interferring with the tests
43 $this->setMwGlobals( 'wgSpecialPages', array() );
46 protected function searchProvision( Array $results = null ) {
47 if ( $results === null ) {
48 $this->setMwGlobals( 'wgHooks', array() );
50 $this->setMwGlobals( 'wgHooks', array(
51 'PrefixSearchBackend' => array(
52 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
61 public static function provideSearch() {
69 'Main namespace with title prefix',
76 // Third result when testing offset
77 'offsetresult' => array(
82 'Talk namespace prefix',
90 'User namespace prefix',
97 'Special namespace prefix',
98 'query' => 'Special:',
100 'Special:ActiveUsers',
101 'Special:AllMessages',
102 'Special:AllMyFiles',
104 // Third result when testing offset
105 'offsetresult' => array(
106 'Special:AllMyUploads',
110 'Special namespace with prefix',
111 'query' => 'Special:Un',
114 'Special:UncategorizedCategories',
115 'Special:UncategorizedFiles',
117 // Third result when testing offset
118 'offsetresult' => array(
119 'Special:UncategorizedImages',
124 'query' => 'Special:EditWatchlist',
126 'Special:EditWatchlist',
130 'Special page subpages',
131 'query' => 'Special:EditWatchlist/',
133 'Special:EditWatchlist/clear',
134 'Special:EditWatchlist/raw',
138 'Special page subpages with prefix',
139 'query' => 'Special:EditWatchlist/cl',
141 'Special:EditWatchlist/clear',
148 * @dataProvider provideSearch
149 * @covers PrefixSearch::search
150 * @covers PrefixSearch::searchBackend
152 public function testSearch( Array $case ) {
153 $this->searchProvision( null );
154 $searcher = new StringPrefixSearch
;
155 $results = $searcher->search( $case['query'], 3 );
164 * @dataProvider provideSearch
165 * @covers PrefixSearch::search
166 * @covers PrefixSearch::searchBackend
168 public function testSearchWithOffset( Array $case ) {
169 $this->searchProvision( null );
170 $searcher = new StringPrefixSearch
;
171 $results = $searcher->search( $case['query'], 3, array(), 1 );
173 // We don't expect the first result when offsetting
174 array_shift( $case['results'] );
175 // And sometimes we expect a different last result
176 $expected = isset( $case['offsetresult'] ) ?
177 array_merge( $case['results'], $case['offsetresult'] ) :
187 public static function provideSearchBackend() {
191 'provision' => array(
204 'Exact match not on top (bug 70958)',
205 'provision' => array(
218 'Exact match missing (bug 70958)',
219 'provision' => array(
232 'Exact match missing and not existing',
233 'provision' => array(
246 "Exact match shouldn't override already found match if " .
247 "exact is redirect and found isn't",
248 'provision' => array(
249 // Target of the exact match is low in the list
250 'Redirect Test Worse Result',
253 'query' => 'redirect test',
255 // Redirect target is pulled up and exact match isn't added
257 'Redirect Test Worse Result',
261 "Exact match shouldn't override already found match if " .
262 "both exact match and found match are redirect",
263 'provision' => array(
264 // Another redirect to the same target as the exact match
265 // is low in the list
266 'Redirect Test2 Worse Result',
269 'query' => 'redirect TEST2',
271 // Found redirect is pulled to the top and exact match isn't
274 'Redirect Test2 Worse Result',
278 "Exact match should override any already found matches that " .
279 "are redirects to it",
280 'provision' => array(
281 // Another redirect to the same target as the exact match
282 // is low in the list
283 'Redirect Test Worse Result',
286 'query' => 'Redirect Test',
288 // Found redirect is pulled to the top and exact match isn't
291 'Redirect Test Worse Result',
298 * @dataProvider provideSearchBackend
299 * @covers PrefixSearch::searchBackend
301 public function testSearchBackend( Array $case ) {
302 $this->searchProvision( $case['provision'] );
303 $searcher = new StringPrefixSearch
;
304 $results = $searcher->search( $case['query'], 3 );