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