wiki.techinc.nl/tests/phpunit/includes/search/SearchEngineTest.php

177 lines
5 KiB
PHP
Raw Normal View History

<?php
/**
* @group Search
* @group Database
*/
class SearchEngineTest extends MediaWikiLangTestCase {
protected $search, $pageList;
/**
* Checks for database type & version.
* Will skip current test if DB does not support search.
*/
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
// Search tests require MySQL or SQLite with FTS
# Get database type and version
2011-06-16 20:57:31 +00:00
$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->search = new $searchType( $this->db );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function tearDown() {
unset( $this->search );
parent::tearDown();
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
}
function pageExists( $title ) {
return false;
}
function addDBData() {
if ( $this->pageExists( 'Not_Main_Page' ) ) {
return;
}
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 );
}
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;
}
2011-05-13 09:30:19 +00:00
/**
* Insert a new page
*
* @param $pageName String: page name
* @param $text String: page's content
* @param $n Integer: unused
*/
function insertPage( $pageName, $text, $ns ) {
$title = Title::newFromText( $pageName, $ns );
2011-06-16 20:57:31 +00:00
$user = User::newFromName( 'WikiSysop' );
$comment = 'Search Test';
2011-06-16 20:57:31 +00:00
// 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() );
2011-06-16 20:57:31 +00:00
return true;
}
function testFullWidth() {
2011-06-16 20:57:31 +00:00
$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" );
}
function testTextSearch() {
$this->assertEquals(
array( 'Smithee' ),
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
"Plain search failed" );
}
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" );
}
function testTitleSearch() {
$this->assertEquals(
array(
'Alan Smithee',
'Smithee',
),
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
"Title search failed" );
}
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" );
}
}