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