2014-09-23 00:35:33 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @group Search
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2014-10-15 16:31:29 +00:00
|
|
|
class PrefixSearchTest extends MediaWikiLangTestCase {
|
2014-09-23 00:35:33 +00:00
|
|
|
|
2015-03-23 05:29:55 +00:00
|
|
|
public function addDBData() {
|
2015-08-14 09:32:08 +00:00
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
|
|
|
|
// tests are skipped if NS_MAIN is not wikitext
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 05:29:55 +00:00
|
|
|
$this->insertPage( 'Sandbox' );
|
|
|
|
|
$this->insertPage( 'Bar' );
|
|
|
|
|
$this->insertPage( 'Example' );
|
|
|
|
|
$this->insertPage( 'Example Bar' );
|
|
|
|
|
$this->insertPage( 'Example Foo' );
|
|
|
|
|
$this->insertPage( 'Example Foo/Bar' );
|
|
|
|
|
$this->insertPage( 'Example/Baz' );
|
|
|
|
|
$this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
|
|
|
|
|
$this->insertPage( 'Redirect Test' );
|
|
|
|
|
$this->insertPage( 'Redirect Test Worse Result' );
|
|
|
|
|
$this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
|
|
|
|
|
$this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
|
|
|
|
|
$this->insertPage( 'Redirect Test2' );
|
|
|
|
|
$this->insertPage( 'Redirect Test2 Worse Result' );
|
|
|
|
|
|
|
|
|
|
$this->insertPage( 'Talk:Sandbox' );
|
|
|
|
|
$this->insertPage( 'Talk:Example' );
|
|
|
|
|
|
|
|
|
|
$this->insertPage( 'User:Example' );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 00:35:33 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2014-10-18 12:56:19 +00:00
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
|
|
|
|
$this->markTestSkipped( 'Main namespace does not support wikitext.' );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 00:35:33 +00:00
|
|
|
// Avoid special pages from extensions interferring with the tests
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgSpecialPages', [] );
|
2014-09-23 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function searchProvision( Array $results = null ) {
|
|
|
|
|
if ( $results === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [] );
|
2014-09-23 00:35:33 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [
|
|
|
|
|
'PrefixSearchBackend' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
|
|
|
|
|
$srchres = $results;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] );
|
2014-09-23 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideSearch() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Empty string',
|
|
|
|
|
'query' => '',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Main namespace with title prefix',
|
|
|
|
|
'query' => 'Ex',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Example',
|
|
|
|
|
'Example/Baz',
|
|
|
|
|
'Example Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-26 18:29:15 +00:00
|
|
|
// Third result when testing offset
|
2016-02-17 09:09:32 +00:00
|
|
|
'offsetresult' => [
|
2014-11-26 18:29:15 +00:00
|
|
|
'Example Foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Talk namespace prefix',
|
|
|
|
|
'query' => 'Talk:',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Talk:Example',
|
|
|
|
|
'Talk:Sandbox',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'User namespace prefix',
|
|
|
|
|
'query' => 'User:',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'User:Example',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special namespace prefix',
|
|
|
|
|
'query' => 'Special:',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special:ActiveUsers',
|
|
|
|
|
'Special:AllMessages',
|
|
|
|
|
'Special:AllMyFiles',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-26 18:29:15 +00:00
|
|
|
// Third result when testing offset
|
2016-02-17 09:09:32 +00:00
|
|
|
'offsetresult' => [
|
2014-11-26 18:29:15 +00:00
|
|
|
'Special:AllMyUploads',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special namespace with prefix',
|
|
|
|
|
'query' => 'Special:Un',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special:Unblock',
|
|
|
|
|
'Special:UncategorizedCategories',
|
|
|
|
|
'Special:UncategorizedFiles',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-26 18:29:15 +00:00
|
|
|
// Third result when testing offset
|
2016-02-17 09:09:32 +00:00
|
|
|
'offsetresult' => [
|
2014-11-26 18:29:15 +00:00
|
|
|
'Special:UncategorizedImages',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special page name',
|
|
|
|
|
'query' => 'Special:EditWatchlist',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special:EditWatchlist',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special page subpages',
|
|
|
|
|
'query' => 'Special:EditWatchlist/',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special:EditWatchlist/clear',
|
|
|
|
|
'Special:EditWatchlist/raw',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special page subpages with prefix',
|
|
|
|
|
'query' => 'Special:EditWatchlist/cl',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-23 00:35:33 +00:00
|
|
|
'Special:EditWatchlist/clear',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
];
|
2014-09-23 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSearch
|
|
|
|
|
* @covers PrefixSearch::search
|
|
|
|
|
* @covers PrefixSearch::searchBackend
|
|
|
|
|
*/
|
|
|
|
|
public function testSearch( Array $case ) {
|
|
|
|
|
$this->searchProvision( null );
|
|
|
|
|
$searcher = new StringPrefixSearch;
|
|
|
|
|
$results = $searcher->search( $case['query'], 3 );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$case['results'],
|
|
|
|
|
$results,
|
|
|
|
|
$case[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-09-17 23:24:41 +00:00
|
|
|
|
2014-11-26 18:29:15 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSearch
|
|
|
|
|
* @covers PrefixSearch::search
|
|
|
|
|
* @covers PrefixSearch::searchBackend
|
|
|
|
|
*/
|
|
|
|
|
public function testSearchWithOffset( Array $case ) {
|
|
|
|
|
$this->searchProvision( null );
|
|
|
|
|
$searcher = new StringPrefixSearch;
|
2016-02-17 09:09:32 +00:00
|
|
|
$results = $searcher->search( $case['query'], 3, [], 1 );
|
2014-11-26 18:29:15 +00:00
|
|
|
|
|
|
|
|
// We don't expect the first result when offsetting
|
|
|
|
|
array_shift( $case['results'] );
|
|
|
|
|
// And sometimes we expect a different last result
|
|
|
|
|
$expected = isset( $case['offsetresult'] ) ?
|
2014-11-27 17:03:07 +00:00
|
|
|
array_merge( $case['results'], $case['offsetresult'] ) :
|
2014-11-26 18:29:15 +00:00
|
|
|
$case['results'];
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
|
|
|
|
$results,
|
|
|
|
|
$case[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-17 23:24:41 +00:00
|
|
|
public static function provideSearchBackend() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Simple case',
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Bar',
|
|
|
|
|
'Barcelona',
|
|
|
|
|
'Barbara',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-09-17 23:24:41 +00:00
|
|
|
'query' => 'Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Bar',
|
|
|
|
|
'Barcelona',
|
|
|
|
|
'Barbara',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Exact match not on top (bug 70958)',
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Barcelona',
|
|
|
|
|
'Bar',
|
|
|
|
|
'Barbara',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-09-17 23:24:41 +00:00
|
|
|
'query' => 'Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Bar',
|
|
|
|
|
'Barcelona',
|
|
|
|
|
'Barbara',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Exact match missing (bug 70958)',
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Barcelona',
|
|
|
|
|
'Barbara',
|
|
|
|
|
'Bart',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-09-17 23:24:41 +00:00
|
|
|
'query' => 'Bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Bar',
|
|
|
|
|
'Barcelona',
|
|
|
|
|
'Barbara',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Exact match missing and not existing',
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Exile',
|
|
|
|
|
'Exist',
|
|
|
|
|
'External',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-09-17 23:24:41 +00:00
|
|
|
'query' => 'Ex',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-09-17 23:24:41 +00:00
|
|
|
'Exile',
|
|
|
|
|
'Exist',
|
|
|
|
|
'External',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-11-21 19:47:32 +00:00
|
|
|
"Exact match shouldn't override already found match if " .
|
|
|
|
|
"exact is redirect and found isn't",
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Target of the exact match is low in the list
|
|
|
|
|
'Redirect Test Worse Result',
|
|
|
|
|
'Redirect Test',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-21 19:47:32 +00:00
|
|
|
'query' => 'redirect test',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Redirect target is pulled up and exact match isn't added
|
|
|
|
|
'Redirect Test',
|
|
|
|
|
'Redirect Test Worse Result',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-11-21 19:47:32 +00:00
|
|
|
"Exact match shouldn't override already found match if " .
|
|
|
|
|
"both exact match and found match are redirect",
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Another redirect to the same target as the exact match
|
|
|
|
|
// is low in the list
|
|
|
|
|
'Redirect Test2 Worse Result',
|
|
|
|
|
'Redirect test2',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-21 19:47:32 +00:00
|
|
|
'query' => 'redirect TEST2',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Found redirect is pulled to the top and exact match isn't
|
|
|
|
|
// added
|
|
|
|
|
'Redirect test2',
|
|
|
|
|
'Redirect Test2 Worse Result',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-11-21 19:47:32 +00:00
|
|
|
"Exact match should override any already found matches that " .
|
|
|
|
|
"are redirects to it",
|
2016-02-17 09:09:32 +00:00
|
|
|
'provision' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Another redirect to the same target as the exact match
|
|
|
|
|
// is low in the list
|
|
|
|
|
'Redirect Test Worse Result',
|
|
|
|
|
'Redirect test',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-11-21 19:47:32 +00:00
|
|
|
'query' => 'Redirect Test',
|
2016-02-17 09:09:32 +00:00
|
|
|
'results' => [
|
2014-11-21 19:47:32 +00:00
|
|
|
// Found redirect is pulled to the top and exact match isn't
|
|
|
|
|
// added
|
|
|
|
|
'Redirect Test',
|
|
|
|
|
'Redirect Test Worse Result',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] ],
|
|
|
|
|
];
|
2014-09-17 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSearchBackend
|
|
|
|
|
* @covers PrefixSearch::searchBackend
|
|
|
|
|
*/
|
|
|
|
|
public function testSearchBackend( Array $case ) {
|
|
|
|
|
$this->searchProvision( $case['provision'] );
|
|
|
|
|
$searcher = new StringPrefixSearch;
|
|
|
|
|
$results = $searcher->search( $case['query'], 3 );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$case['results'],
|
|
|
|
|
$results,
|
|
|
|
|
$case[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-09-23 00:35:33 +00:00
|
|
|
}
|