00001 <?php
00002
00003 $wgCommandLineMode = true;
00004 $self = 'Search.t';
00005 define( 'MEDIAWIKI', true );
00006 require 't/Test.php';
00007 require 'includes/Defines.php';
00008 require 'includes/ProfilerStub.php';
00009 require 'LocalSettings.php';
00010 require 'AdminSettings.php';
00011 require 'includes/Setup.php';
00012
00013 function buildTestDatabase( $tables ) {
00014 global $wgDBprefix, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname, $wgDBtype;
00015 $oldPrefix = $wgDBprefix;
00016 $wgDBprefix = 'parsertest';
00017
00018 $db = wfGetDB ( DB_SLAVE );
00019
00020 if( $db->isOpen() ) {
00021 if ( !( stristr( $db->getSoftwareLink(), 'MySQL') && version_compare( $db->getServerVersion(), '4.1', '<' ) ) ) {
00022 # Database that supports CREATE TABLE ... LIKE
00023 foreach ($tables as $tbl) {
00024 $newTableName = $db->tableName( $tbl );
00025 $tableName = $oldPrefix . $tbl;
00026 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)");
00027 }
00028 } else {
00029 # Hack for MySQL versions < 4.1, which don't support
00030 # "CREATE TABLE ... LIKE". Note that
00031 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
00032 # would not create the indexes we need....
00033 foreach ($tables as $tbl) {
00034 $res = $db->query("SHOW CREATE TABLE $tbl");
00035 $row = $db->fetchRow($res);
00036 $create = $row[1];
00037 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
00038 . $wgDBprefix . '\\1`', $create);
00039 if ($create === $create_tmp) {
00040 # Couldn't do replacement
00041 wfDie( "could not create temporary table $tbl" );
00042 }
00043 $db->query($create_tmp);
00044 }
00045
00046 }
00047 return $db;
00048 } else {
00049
00050 return null;
00051 }
00052 }
00053
00054 class SearchEngineTest {
00055 var $db, $search;
00056
00057 function __construct( SearchEngine $search ){
00058 $this->search = $search;
00059 $this->db = $this->search->db;
00060 }
00061
00062 function insertSearchData() {
00063 $this->db->safeQuery( <<<END
00064 INSERT INTO ! (page_id,page_namespace,page_title,page_latest)
00065 VALUES (1, 0, 'Main_Page', 1),
00066 (2, 1, 'Main_Page', 2),
00067 (3, 0, 'Smithee', 3),
00068 (4, 1, 'Smithee', 4),
00069 (5, 0, 'Unrelated_page', 5),
00070 (6, 0, 'Another_page', 6),
00071 (7, 4, 'Help', 7),
00072 (8, 0, 'Thppt', 8),
00073 (9, 0, 'Alan_Smithee', 9),
00074 (10, 0, 'Pages', 10)
00075 END
00076 , $this->db->tableName( 'page' ) );
00077 $this->db->safeQuery( <<<END
00078 INSERT INTO ! (rev_id,rev_page)
00079 VALUES (1, 1),
00080 (2, 2),
00081 (3, 3),
00082 (4, 4),
00083 (5, 5),
00084 (6, 6),
00085 (7, 7),
00086 (8, 8),
00087 (9, 9),
00088 (10, 10)
00089 END
00090 , $this->db->tableName( 'revision' ) );
00091 $this->db->safeQuery( <<<END
00092 INSERT INTO ! (old_id,old_text)
00093 VALUES (1, 'This is a main page'),
00094 (2, 'This is a talk page to the main page, see [[smithee]]'),
00095 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
00096 (4, 'This article sucks.'),
00097 (5, 'Nothing in this page is about the S word.'),
00098 (6, 'This page also is unrelated.'),
00099 (7, 'Help me!'),
00100 (8, 'Blah blah'),
00101 (9, 'yum'),
00102 (10,'are food')
00103 END
00104 , $this->db->tableName( 'text' ) );
00105 $this->db->safeQuery( <<<END
00106 INSERT INTO ! (si_page,si_title,si_text)
00107 VALUES (1, 'main page', 'this is a main page'),
00108 (2, 'main page', 'this is a talk page to the main page, see smithee'),
00109 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
00110 (4, 'smithee', 'this article sucks'),
00111 (5, 'unrelated page', 'nothing in this page is about the s word'),
00112 (6, 'another page', 'this page also is unrelated'),
00113 (7, 'help', 'help me'),
00114 (8, 'thppt', 'blah blah'),
00115 (9, 'alan smithee', 'yum'),
00116 (10, 'pages', 'are food')
00117 END
00118 , $this->db->tableName( 'searchindex' ) );
00119 }
00120
00121 function fetchIds( $results ) {
00122 $matches = array();
00123 while( $row = $results->next() ) {
00124 $matches[] = $row->getTitle()->getPrefixedText();
00125 }
00126 $results->free();
00127 # Search is not guaranteed to return results in a certain order;
00128 # sort them numerically so we will compare simply that we received
00129 # the expected matches.
00130 sort( $matches );
00131 return $matches;
00132 }
00133
00134 function run(){
00135 if( is_null( $this->db ) ){
00136 fail( "Can't find a database to test with." );
00137 return;
00138 }
00139
00140 $this->insertSearchData();
00141 plan( 4 );
00142
00143 $exp = array( 'Smithee' );
00144 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
00145 is( $got, $exp, "Plain search" );
00146
00147 $exp = array( 'Alan Smithee', 'Smithee' );
00148 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
00149 is( $got, $exp, "Title search" );
00150
00151 $this->search->setNamespaces( array( 0, 1, 4 ) );
00152
00153 $exp = array( 'Smithee', 'Talk:Main Page', );
00154 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
00155 is( $got, $exp, "Power search" );
00156
00157 $exp = array( 'Alan Smithee', 'Smithee', 'Talk:Smithee', );
00158 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
00159 is( $got, $exp, "Title power search" );
00160 }
00161 }