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