00001 <?php 00002 00003 require_once 'MediaWiki_TestCase.php'; 00004 00006 class SearchEngineTest extends MediaWiki_TestCase { 00007 var $db, $search; 00008 00009 function insertSearchData() { 00010 $this->db->safeQuery( <<<END 00011 INSERT INTO ! (page_id,page_namespace,page_title,page_latest) 00012 VALUES (1, 0, 'Main_Page', 1), 00013 (2, 1, 'Main_Page', 2), 00014 (3, 0, 'Smithee', 3), 00015 (4, 1, 'Smithee', 4), 00016 (5, 0, 'Unrelated_page', 5), 00017 (6, 0, 'Another_page', 6), 00018 (7, 4, 'Help', 7), 00019 (8, 0, 'Thppt', 8), 00020 (9, 0, 'Alan_Smithee', 9), 00021 (10, 0, 'Pages', 10) 00022 END 00023 , $this->db->tableName( 'page' ) ); 00024 $this->db->safeQuery( <<<END 00025 INSERT INTO ! (rev_id,rev_page) 00026 VALUES (1, 1), 00027 (2, 2), 00028 (3, 3), 00029 (4, 4), 00030 (5, 5), 00031 (6, 6), 00032 (7, 7), 00033 (8, 8), 00034 (9, 9), 00035 (10, 10) 00036 END 00037 , $this->db->tableName( 'revision' ) ); 00038 $this->db->safeQuery( <<<END 00039 INSERT INTO ! (old_id,old_text) 00040 VALUES (1, 'This is a main page'), 00041 (2, 'This is a talk page to the main page, see [[smithee]]'), 00042 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'), 00043 (4, 'This article sucks.'), 00044 (5, 'Nothing in this page is about the S word.'), 00045 (6, 'This page also is unrelated.'), 00046 (7, 'Help me!'), 00047 (8, 'Blah blah'), 00048 (9, 'yum'), 00049 (10,'are food') 00050 END 00051 , $this->db->tableName( 'text' ) ); 00052 $this->db->safeQuery( <<<END 00053 INSERT INTO ! (si_page,si_title,si_text) 00054 VALUES (1, 'main page', 'this is a main page'), 00055 (2, 'main page', 'this is a talk page to the main page, see smithee'), 00056 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'), 00057 (4, 'smithee', 'this article sucks'), 00058 (5, 'unrelated page', 'nothing in this page is about the s word'), 00059 (6, 'another page', 'this page also is unrelated'), 00060 (7, 'help', 'help me'), 00061 (8, 'thppt', 'blah blah'), 00062 (9, 'alan smithee', 'yum'), 00063 (10, 'pages', 'are food') 00064 END 00065 , $this->db->tableName( 'searchindex' ) ); 00066 } 00067 00068 function fetchIds( &$results ) { 00069 $matches = array(); 00070 while( $row = $results->next() ) { 00071 $matches[] = $row->getTitle()->getPrefixedText(); 00072 } 00073 $results->free(); 00074 # Search is not guaranteed to return results in a certain order; 00075 # sort them numerically so we will compare simply that we received 00076 # the expected matches. 00077 sort( $matches ); 00078 return $matches; 00079 } 00080 00081 function testTextSearch() { 00082 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); 00083 if( !is_null( $this->db ) ) { 00084 $this->assertEquals( 00085 array( 'Smithee' ), 00086 $this->fetchIds( $this->search->searchText( 'smithee' ) ), 00087 "Plain search failed" ); 00088 } 00089 } 00090 00091 function testTextPowerSearch() { 00092 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); 00093 if( !is_null( $this->db ) ) { 00094 $this->search->setNamespaces( array( 0, 1, 4 ) ); 00095 $this->assertEquals( 00096 array( 00097 'Smithee', 00098 'Talk:Main Page', 00099 ), 00100 $this->fetchIds( $this->search->searchText( 'smithee' ) ), 00101 "Power search failed" ); 00102 } 00103 } 00104 00105 function testTitleSearch() { 00106 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); 00107 if( !is_null( $this->db ) ) { 00108 $this->assertEquals( 00109 array( 00110 'Alan Smithee', 00111 'Smithee', 00112 ), 00113 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ), 00114 "Title search failed" ); 00115 } 00116 } 00117 00118 function testTextTitlePowerSearch() { 00119 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); 00120 if( !is_null( $this->db ) ) { 00121 $this->search->setNamespaces( array( 0, 1, 4 ) ); 00122 $this->assertEquals( 00123 array( 00124 'Alan Smithee', 00125 'Smithee', 00126 'Talk:Smithee', 00127 ), 00128 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ), 00129 "Title power search failed" ); 00130 } 00131 } 00132 00133 } 00134 00135 00136