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