2010-12-14 16:26:35 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @group Search
|
2011-06-28 22:26:22 +00:00
|
|
|
|
* @group Database
|
2013-10-24 19:35:04 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @covers SearchEngine<extended>
|
|
|
|
|
|
* @note Coverage will only ever show one of on of the Search* classes
|
2010-12-14 16:26:35 +00:00
|
|
|
|
*/
|
2012-11-11 07:13:21 +00:00
|
|
|
|
class SearchEngineTest extends MediaWikiLangTestCase {
|
2014-01-03 01:23:16 +00:00
|
|
|
|
|
2013-10-24 19:35:04 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @var SearchEngine
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected $search;
|
2014-01-03 01:23:16 +00:00
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
|
/**
|
2010-12-14 16:26:35 +00:00
|
|
|
|
* Checks for database type & version.
|
|
|
|
|
|
* Will skip current test if DB does not support search.
|
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
|
protected function setUp() {
|
2011-06-28 22:26:22 +00:00
|
|
|
|
parent::setUp();
|
2012-11-11 07:13:21 +00:00
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
// Search tests require MySQL or SQLite with FTS
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$dbType = $this->db->getType();
|
2014-01-03 01:23:16 +00:00
|
|
|
|
$dbSupported = ( $dbType === 'mysql' )
|
|
|
|
|
|
|| ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2013-02-15 10:24:31 +00:00
|
|
|
|
if ( !$dbSupported ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
|
|
|
|
|
|
}
|
2011-06-28 22:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
$searchType = $this->db->getSearchEngine();
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->setMwGlobals( [
|
2014-01-03 01:23:16 +00:00
|
|
|
|
'wgSearchType' => $searchType
|
2016-02-17 09:09:32 +00:00
|
|
|
|
] );
|
2014-01-03 01:23:16 +00:00
|
|
|
|
|
2011-06-28 22:26:22 +00:00
|
|
|
|
$this->search = new $searchType( $this->db );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
|
protected function tearDown() {
|
|
|
|
|
|
unset( $this->search );
|
2012-12-31 12:54:06 +00:00
|
|
|
|
|
|
|
|
|
|
parent::tearDown();
|
2012-10-08 10:56:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-07 17:26:25 +00:00
|
|
|
|
public function addDBDataOnce() {
|
2012-10-12 11:09:08 +00:00
|
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
2013-05-15 01:12:35 +00:00
|
|
|
|
// @todo cover the case of non-wikitext content in the main namespace
|
2012-10-12 11:09:08 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-09-18 00:02:24 +00:00
|
|
|
|
$this->insertPage( 'Not_Main_Page', 'This is not a main page' );
|
2014-04-24 17:52:34 +00:00
|
|
|
|
$this->insertPage(
|
|
|
|
|
|
'Talk:Not_Main_Page',
|
2014-09-18 00:02:24 +00:00
|
|
|
|
'This is not a talk page to the main page, see [[smithee]]'
|
2014-04-24 17:52:34 +00:00
|
|
|
|
);
|
2014-09-18 00:02:24 +00:00
|
|
|
|
$this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
|
|
|
|
|
|
$this->insertPage( 'Talk:Smithee', 'This article sucks.' );
|
|
|
|
|
|
$this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
|
|
|
|
|
|
$this->insertPage( 'Another_page', 'This page also is unrelated.' );
|
|
|
|
|
|
$this->insertPage( 'Help:Help', 'Help me!' );
|
|
|
|
|
|
$this->insertPage( 'Thppt', 'Blah blah' );
|
|
|
|
|
|
$this->insertPage( 'Alan_Smithee', 'yum' );
|
|
|
|
|
|
$this->insertPage( 'Pages', 'are\'food' );
|
|
|
|
|
|
$this->insertPage( 'HalfOneUp', 'AZ' );
|
|
|
|
|
|
$this->insertPage( 'FullOneUp', 'AZ' );
|
|
|
|
|
|
$this->insertPage( 'HalfTwoLow', 'az' );
|
|
|
|
|
|
$this->insertPage( 'FullTwoLow', 'az' );
|
|
|
|
|
|
$this->insertPage( 'HalfNumbers', '1234567890' );
|
|
|
|
|
|
$this->insertPage( 'FullNumbers', '1234567890' );
|
|
|
|
|
|
$this->insertPage( 'DomainName', 'example.com' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-03 01:23:16 +00:00
|
|
|
|
protected function fetchIds( $results ) {
|
2012-10-12 11:09:08 +00:00
|
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
|
|
|
|
|
$this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
|
2013-02-15 10:24:31 +00:00
|
|
|
|
. "in the main namespace" );
|
2012-10-12 11:09:08 +00:00
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertTrue( is_object( $results ) );
|
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$matches = [];
|
2011-10-16 03:27:12 +00:00
|
|
|
|
$row = $results->next();
|
|
|
|
|
|
while ( $row ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$matches[] = $row->getTitle()->getPrefixedText();
|
2011-10-16 03:27:12 +00:00
|
|
|
|
$row = $results->next();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
$results->free();
|
|
|
|
|
|
# Search is not guaranteed to return results in a certain order;
|
|
|
|
|
|
# sort them numerically so we will compare simply that we received
|
|
|
|
|
|
# the expected matches.
|
|
|
|
|
|
sort( $matches );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
return $matches;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testFullWidth() {
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
|
|
|
|
|
"Search for normalized from Half-width Upper" );
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
|
|
|
|
|
"Search for normalized from Half-width Lower" );
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
|
|
|
|
|
"Search for normalized from Full-width Upper" );
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
|
|
|
|
|
"Search for normalized from Full-width Lower" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testTextSearch() {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ 'Smithee' ],
|
2013-02-15 10:24:31 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
|
|
|
|
|
"Plain search failed" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testTextPowerSearch() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->search->setNamespaces( [ 0, 1, 4 ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2010-12-14 16:26:35 +00:00
|
|
|
|
'Smithee',
|
|
|
|
|
|
'Talk:Not Main Page',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
|
|
|
|
|
"Power search failed" );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testTitleSearch() {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2010-12-14 16:26:35 +00:00
|
|
|
|
'Alan Smithee',
|
|
|
|
|
|
'Smithee',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
|
|
|
|
|
"Title search failed" );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testTextTitlePowerSearch() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->search->setNamespaces( [ 0, 1, 4 ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2010-12-14 16:26:35 +00:00
|
|
|
|
'Alan Smithee',
|
|
|
|
|
|
'Smithee',
|
|
|
|
|
|
'Talk:Smithee',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
|
|
|
|
|
"Title power search failed" );
|
|
|
|
|
|
}
|
2014-01-03 01:23:16 +00:00
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|