MediaWiki  REL1_24
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(
00061             'Talk:Not_Main_Page',
00062             'This is not a talk page to the main page, see [[smithee]]',
00063             1
00064         );
00065         $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
00066         $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
00067         $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
00068         $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
00069         $this->insertPage( 'Help:Help', 'Help me!', 4 );
00070         $this->insertPage( 'Thppt', 'Blah blah', 0 );
00071         $this->insertPage( 'Alan_Smithee', 'yum', 0 );
00072         $this->insertPage( 'Pages', 'are\'food', 0 );
00073         $this->insertPage( 'HalfOneUp', 'AZ', 0 );
00074         $this->insertPage( 'FullOneUp', 'AZ', 0 );
00075         $this->insertPage( 'HalfTwoLow', 'az', 0 );
00076         $this->insertPage( 'FullTwoLow', 'az', 0 );
00077         $this->insertPage( 'HalfNumbers', '1234567890', 0 );
00078         $this->insertPage( 'FullNumbers', '1234567890', 0 );
00079         $this->insertPage( 'DomainName', 'example.com', 0 );
00080     }
00081 
00082     protected function fetchIds( $results ) {
00083         if ( !$this->isWikitextNS( NS_MAIN ) ) {
00084             $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
00085                 . "in the main namespace" );
00086         }
00087         $this->assertTrue( is_object( $results ) );
00088 
00089         $matches = array();
00090         $row = $results->next();
00091         while ( $row ) {
00092             $matches[] = $row->getTitle()->getPrefixedText();
00093             $row = $results->next();
00094         }
00095         $results->free();
00096         # Search is not guaranteed to return results in a certain order;
00097         # sort them numerically so we will compare simply that we received
00098         # the expected matches.
00099         sort( $matches );
00100 
00101         return $matches;
00102     }
00103 
00111     protected function insertPage( $pageName, $text, $ns ) {
00112         $title = Title::newFromText( $pageName, $ns );
00113 
00114         $user = User::newFromName( 'WikiSysop' );
00115         $comment = 'Search Test';
00116 
00117         // avoid memory leak...?
00118         LinkCache::singleton()->clear();
00119 
00120         $page = WikiPage::factory( $title );
00121         $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
00122 
00123         $this->pageList[] = array( $title, $page->getId() );
00124 
00125         return true;
00126     }
00127 
00128     public function testFullWidth() {
00129         $this->assertEquals(
00130             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00131             $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00132             "Search for normalized from Half-width Upper" );
00133         $this->assertEquals(
00134             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00135             $this->fetchIds( $this->search->searchText( 'az' ) ),
00136             "Search for normalized from Half-width Lower" );
00137         $this->assertEquals(
00138             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00139             $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00140             "Search for normalized from Full-width Upper" );
00141         $this->assertEquals(
00142             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00143             $this->fetchIds( $this->search->searchText( 'az' ) ),
00144             "Search for normalized from Full-width Lower" );
00145     }
00146 
00147     public function testTextSearch() {
00148         $this->assertEquals(
00149             array( 'Smithee' ),
00150             $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00151             "Plain search failed" );
00152     }
00153 
00154     public function testTextPowerSearch() {
00155         $this->search->setNamespaces( array( 0, 1, 4 ) );
00156         $this->assertEquals(
00157             array(
00158                 'Smithee',
00159                 'Talk:Not Main Page',
00160             ),
00161             $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00162             "Power search failed" );
00163     }
00164 
00165     public function testTitleSearch() {
00166         $this->assertEquals(
00167             array(
00168                 'Alan Smithee',
00169                 'Smithee',
00170             ),
00171             $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00172             "Title search failed" );
00173     }
00174 
00175     public function testTextTitlePowerSearch() {
00176         $this->search->setNamespaces( array( 0, 1, 4 ) );
00177         $this->assertEquals(
00178             array(
00179                 'Alan Smithee',
00180                 'Smithee',
00181                 'Talk:Smithee',
00182             ),
00183             $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00184             "Title power search failed" );
00185     }
00186 
00187 }