wiki.techinc.nl/tests/phpunit/includes/search/SearchEngineTest.php
aude 73bc21bb1c Fix SearchEngineTest when $wgSearchType is set to non-default
$wgSearchType needs to be controlled for at time the pages get created.
This patch sets the $wgSearchType global.

Also, the 'singleton' is removed from SearchEngine::getSearchTypes, which
appears to provide little or no benefit while binding all tests to the
global variable.

SearchEngine::getSearchTypes() gets called in SearchUpdate which is
run on page edits. This means the first page edit in the entire
test suite causes the global variable to be set and subsequent tests
cannot override it.

These changes allow SearchEngineTest to pass, even if one has CirrusSearch
or other such extensions enabled.

Change-Id: I39050da8659dc69db31125f469f494a5fb4b8fca
2014-01-03 11:28:41 +01:00

183 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @group Search
* @group Database
*
* @covers SearchEngine<extended>
* @note Coverage will only ever show one of on of the Search* classes
*/
class SearchEngineTest extends MediaWikiLangTestCase {
/**
* @var SearchEngine
*/
protected $search;
protected $pageList;
/**
* Checks for database type & version.
* Will skip current test if DB does not support search.
*/
protected function setUp() {
parent::setUp();
// Search tests require MySQL or SQLite with FTS
$dbType = $this->db->getType();
$dbSupported = ( $dbType === 'mysql' )
|| ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
if ( !$dbSupported ) {
$this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
}
$searchType = $this->db->getSearchEngine();
$this->setMwGlobals( array(
'wgSearchType' => $searchType
) );
if ( !isset( self::$pageList ) ) {
$this->addPages();
}
$this->search = new $searchType( $this->db );
}
protected function tearDown() {
unset( $this->search );
parent::tearDown();
}
protected function addPages() {
if ( !$this->isWikitextNS( NS_MAIN ) ) {
// @todo cover the case of non-wikitext content in the main namespace
return;
}
$this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
$this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
$this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
$this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
$this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
$this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
$this->insertPage( 'Help:Help', 'Help me!', 4 );
$this->insertPage( 'Thppt', 'Blah blah', 0 );
$this->insertPage( 'Alan_Smithee', 'yum', 0 );
$this->insertPage( 'Pages', 'are\'food', 0 );
$this->insertPage( 'HalfOneUp', 'AZ', 0 );
$this->insertPage( 'FullOneUp', '', 0 );
$this->insertPage( 'HalfTwoLow', 'az', 0 );
$this->insertPage( 'FullTwoLow', '', 0 );
$this->insertPage( 'HalfNumbers', '1234567890', 0 );
$this->insertPage( 'FullNumbers', '', 0 );
$this->insertPage( 'DomainName', 'example.com', 0 );
}
protected function fetchIds( $results ) {
if ( !$this->isWikitextNS( NS_MAIN ) ) {
$this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
. "in the main namespace" );
}
$this->assertTrue( is_object( $results ) );
$matches = array();
$row = $results->next();
while ( $row ) {
$matches[] = $row->getTitle()->getPrefixedText();
$row = $results->next();
}
$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 );
return $matches;
}
/**
* Insert a new page
*
* @param $pageName String: page name
* @param $text String: page's content
* @param $n Integer: unused
*/
protected function insertPage( $pageName, $text, $ns ) {
$title = Title::newFromText( $pageName, $ns );
$user = User::newFromName( 'WikiSysop' );
$comment = 'Search Test';
// avoid memory leak...?
LinkCache::singleton()->clear();
$page = WikiPage::factory( $title );
$page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
$this->pageList[] = array( $title, $page->getId() );
return true;
}
public function testFullWidth() {
$this->assertEquals(
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
"Search for normalized from Half-width Upper" );
$this->assertEquals(
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
$this->fetchIds( $this->search->searchText( 'az' ) ),
"Search for normalized from Half-width Lower" );
$this->assertEquals(
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
$this->fetchIds( $this->search->searchText( '' ) ),
"Search for normalized from Full-width Upper" );
$this->assertEquals(
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
$this->fetchIds( $this->search->searchText( '' ) ),
"Search for normalized from Full-width Lower" );
}
public function testTextSearch() {
$this->assertEquals(
array( 'Smithee' ),
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
"Plain search failed" );
}
public function testTextPowerSearch() {
$this->search->setNamespaces( array( 0, 1, 4 ) );
$this->assertEquals(
array(
'Smithee',
'Talk:Not Main Page',
),
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
"Power search failed" );
}
public function testTitleSearch() {
$this->assertEquals(
array(
'Alan Smithee',
'Smithee',
),
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
"Title search failed" );
}
public function testTextTitlePowerSearch() {
$this->search->setNamespaces( array( 0, 1, 4 ) );
$this->assertEquals(
array(
'Alan Smithee',
'Smithee',
'Talk:Smithee',
),
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
"Title power search failed" );
}
}