MediaWiki  REL1_22
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 
00091         return $matches;
00092     }
00093 
00101     function insertPage( $pageName, $text, $ns ) {
00102         $title = Title::newFromText( $pageName, $ns );
00103 
00104         $user = User::newFromName( 'WikiSysop' );
00105         $comment = 'Search Test';
00106 
00107         // avoid memory leak...?
00108         LinkCache::singleton()->clear();
00109 
00110         $page = WikiPage::factory( $title );
00111         $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
00112 
00113         $this->pageList[] = array( $title, $page->getId() );
00114 
00115         return true;
00116     }
00117 
00118     public function testFullWidth() {
00119         $this->assertEquals(
00120             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00121             $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00122             "Search for normalized from Half-width Upper" );
00123         $this->assertEquals(
00124             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00125             $this->fetchIds( $this->search->searchText( 'az' ) ),
00126             "Search for normalized from Half-width Lower" );
00127         $this->assertEquals(
00128             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00129             $this->fetchIds( $this->search->searchText( 'AZ' ) ),
00130             "Search for normalized from Full-width Upper" );
00131         $this->assertEquals(
00132             array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
00133             $this->fetchIds( $this->search->searchText( 'az' ) ),
00134             "Search for normalized from Full-width Lower" );
00135     }
00136 
00137     public function testTextSearch() {
00138         $this->assertEquals(
00139             array( 'Smithee' ),
00140             $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00141             "Plain search failed" );
00142     }
00143 
00144     public function testTextPowerSearch() {
00145         $this->search->setNamespaces( array( 0, 1, 4 ) );
00146         $this->assertEquals(
00147             array(
00148                 'Smithee',
00149                 'Talk:Not Main Page',
00150             ),
00151             $this->fetchIds( $this->search->searchText( 'smithee' ) ),
00152             "Power search failed" );
00153     }
00154 
00155     public function testTitleSearch() {
00156         $this->assertEquals(
00157             array(
00158                 'Alan Smithee',
00159                 'Smithee',
00160             ),
00161             $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00162             "Title search failed" );
00163     }
00164 
00165     public function testTextTitlePowerSearch() {
00166         $this->search->setNamespaces( array( 0, 1, 4 ) );
00167         $this->assertEquals(
00168             array(
00169                 'Alan Smithee',
00170                 'Smithee',
00171                 'Talk:Smithee',
00172             ),
00173             $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
00174             "Title power search failed" );
00175     }
00176 }