MediaWiki  REL1_21
SearchEngineTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class SearchEngineTest extends MediaWikiLangTestCase {
00008         protected $search, $pageList;
00009 
00014         protected function setUp() {
00015                 parent::setUp();
00016 
00017                 // Search tests require MySQL or SQLite with FTS
00018                 # Get database type and version
00019                 $dbType = $this->db->getType();
00020                 $dbSupported =
00021                         ( $dbType === 'mysql' )
00022                                 || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
00023 
00024                 if ( !$dbSupported ) {
00025                         $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
00026                 }
00027 
00028                 $searchType = $this->db->getSearchEngine();
00029                 $this->search = new $searchType( $this->db );
00030         }
00031 
00032         protected function tearDown() {
00033                 unset( $this->search );
00034 
00035                 parent::tearDown();
00036         }
00037 
00038         function pageExists( $title ) {
00039                 return false;
00040         }
00041 
00042         function addDBData() {
00043                 if ( $this->pageExists( 'Not_Main_Page' ) ) {
00044                         return;
00045                 }
00046 
00047                 if ( !$this->isWikitextNS( NS_MAIN ) ) {
00048                         //@todo: cover the case of non-wikitext content in the main namespace
00049                         return;
00050                 }
00051 
00052                 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
00053                 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
00054                 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
00055                 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
00056                 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
00057                 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
00058                 $this->insertPage( 'Help:Help', 'Help me!', 4 );
00059                 $this->insertPage( 'Thppt', 'Blah blah', 0 );
00060                 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
00061                 $this->insertPage( 'Pages', 'are\'food', 0 );
00062                 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
00063                 $this->insertPage( 'FullOneUp', 'AZ', 0 );
00064                 $this->insertPage( 'HalfTwoLow', 'az', 0 );
00065                 $this->insertPage( 'FullTwoLow', 'az', 0 );
00066                 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
00067                 $this->insertPage( 'FullNumbers', '1234567890', 0 );
00068                 $this->insertPage( 'DomainName', 'example.com', 0 );
00069         }
00070 
00071         function fetchIds( $results ) {
00072                 if ( !$this->isWikitextNS( NS_MAIN ) ) {
00073                         $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
00074                                 . "in the main namespace" );
00075                 }
00076 
00077                 $this->assertTrue( is_object( $results ) );
00078 
00079                 $matches = array();
00080                 $row = $results->next();
00081                 while ( $row ) {
00082                         $matches[] = $row->getTitle()->getPrefixedText();
00083                         $row = $results->next();
00084                 }
00085                 $results->free();
00086                 # Search is not guaranteed to return results in a certain order;
00087                 # sort them numerically so we will compare simply that we received
00088                 # the expected matches.
00089                 sort( $matches );
00090                 return $matches;
00091         }
00092 
00100         function insertPage( $pageName, $text, $ns ) {
00101                 $title = Title::newFromText( $pageName, $ns );
00102 
00103                 $user = User::newFromName( 'WikiSysop' );
00104                 $comment = 'Search Test';
00105 
00106                 // avoid memory leak...?
00107                 LinkCache::singleton()->clear();
00108 
00109                 $page = WikiPage::factory( $title );
00110                 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
00111 
00112                 $this->pageList[] = array( $title, $page->getId() );
00113 
00114                 return true;
00115         }
00116 
00117         function testFullWidth() {
00118                 $this->assertEquals(
00119                         array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00120                         $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00121                         "Search for normalized from Half-width Upper" );
00122                 $this->assertEquals(
00123                         array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00124                         $this->fetchIds( $this->search->searchText( 'az' ) ),
00125                         "Search for normalized from Half-width Lower" );
00126                 $this->assertEquals(
00127                         array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00128                         $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00129                         "Search for normalized from Full-width Upper" );
00130                 $this->assertEquals(
00131                         array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00132                         $this->fetchIds( $this->search->searchText( 'az' ) ),
00133                         "Search for normalized from Full-width Lower" );
00134         }
00135 
00136         function testTextSearch() {
00137                 $this->assertEquals(
00138                         array( 'Smithee' ),
00139                         $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00140                         "Plain search failed" );
00141         }
00142 
00143         function testTextPowerSearch() {
00144                 $this->search->setNamespaces( array( 0, 1, 4 ) );
00145                 $this->assertEquals(
00146                         array(
00147                                 'Smithee',
00148                                 'Talk:Not Main Page',
00149                         ),
00150                         $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00151                         "Power search failed" );
00152         }
00153 
00154         function testTitleSearch() {
00155                 $this->assertEquals(
00156                         array(
00157                                 'Alan Smithee',
00158                                 'Smithee',
00159                         ),
00160                         $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00161                         "Title search failed" );
00162         }
00163 
00164         function testTextTitlePowerSearch() {
00165                 $this->search->setNamespaces( array( 0, 1, 4 ) );
00166                 $this->assertEquals(
00167                         array(
00168                                 'Alan Smithee',
00169                                 'Smithee',
00170                                 'Talk:Smithee',
00171                         ),
00172                         $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00173                         "Title power search failed" );
00174         }
00175 
00176 }