This reduces the runtime of database-bound tests by about 40% (on my system, from 4:55 to 2:47; results from Jenkins are inconclusive). The basic idea is to call addCoreDBData() only once, and have a addDBDataOnce() that is called once per test class, not for every test method lie addDBData() is. Most tests could be trivially be changed to implement addDBDataOnce() instead of addDBData(). The ones for which this did not work immediately were left out for now. A closer look at the tests that still implement addDBData() may reveal additional potential for improvement. TODO: Once this is merged, try to change addDBData() to addDBDataOnce() where possible in extensions. Change-Id: Iec4ed4c8419fb4ad87e6710de808863ede9998b7
156 lines
4.4 KiB
PHP
156 lines
4.4 KiB
PHP
<?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;
|
||
|
||
/**
|
||
* 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( [
|
||
'wgSearchType' => $searchType
|
||
] );
|
||
|
||
$this->search = new $searchType( $this->db );
|
||
}
|
||
|
||
protected function tearDown() {
|
||
unset( $this->search );
|
||
|
||
parent::tearDown();
|
||
}
|
||
|
||
public function addDBDataOnce() {
|
||
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' );
|
||
$this->insertPage(
|
||
'Talk:Not_Main_Page',
|
||
'This is not a talk page to the main page, see [[smithee]]'
|
||
);
|
||
$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' );
|
||
}
|
||
|
||
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 = [];
|
||
$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;
|
||
}
|
||
|
||
public function testFullWidth() {
|
||
$this->assertEquals(
|
||
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
||
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
||
"Search for normalized from Half-width Upper" );
|
||
$this->assertEquals(
|
||
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
||
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
||
"Search for normalized from Half-width Lower" );
|
||
$this->assertEquals(
|
||
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
||
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
||
"Search for normalized from Full-width Upper" );
|
||
$this->assertEquals(
|
||
[ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
|
||
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
||
"Search for normalized from Full-width Lower" );
|
||
}
|
||
|
||
public function testTextSearch() {
|
||
$this->assertEquals(
|
||
[ 'Smithee' ],
|
||
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
||
"Plain search failed" );
|
||
}
|
||
|
||
public function testTextPowerSearch() {
|
||
$this->search->setNamespaces( [ 0, 1, 4 ] );
|
||
$this->assertEquals(
|
||
[
|
||
'Smithee',
|
||
'Talk:Not Main Page',
|
||
],
|
||
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
||
"Power search failed" );
|
||
}
|
||
|
||
public function testTitleSearch() {
|
||
$this->assertEquals(
|
||
[
|
||
'Alan Smithee',
|
||
'Smithee',
|
||
],
|
||
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
||
"Title search failed" );
|
||
}
|
||
|
||
public function testTextTitlePowerSearch() {
|
||
$this->search->setNamespaces( [ 0, 1, 4 ] );
|
||
$this->assertEquals(
|
||
[
|
||
'Alan Smithee',
|
||
'Smithee',
|
||
'Talk:Smithee',
|
||
],
|
||
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
||
"Title power search failed" );
|
||
}
|
||
|
||
}
|