Part 2 at solving conflict with vector and js edit
[mediawiki.git] / t / Search.inc
blob5f784c67b583677cb3640a8fce77af9d9881dfaa
1 <?php
3 $wgCommandLineMode = true;
4 $self = 'Search.t';
5 define( 'MEDIAWIKI', true );
6 require 't/Test.php';
7 require 'includes/Defines.php';
8 require 'includes/ProfilerStub.php';
9 require 'LocalSettings.php';
10 require 'includes/Setup.php';
12 function buildTestDatabase( $tables ) {
13         global $wgDBprefix, $wgDBserver, $wgDBname, $wgDBtype;
14         $oldPrefix = $wgDBprefix;
15         $wgDBprefix = 'parsertest_';
17         $db = wfGetDB ( DB_SLAVE );
19         if( $db->isOpen() ) {
20                 if ( !( stristr( $db->getSoftwareLink(), 'MySQL') && version_compare( $db->getServerVersion(), '4.1', '<' ) ) ) {
21                         # Database that supports CREATE TABLE ... LIKE
22                         foreach ($tables as $tbl) {
23                                 $newTableName = $db->tableName( $tbl );
24                                 $tableName = $oldPrefix . $tbl;
25                                 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)");
26                         }
27                 } else {
28                         # Hack for MySQL versions < 4.1, which don't support
29                         # "CREATE TABLE ... LIKE". Note that
30                         # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
31                         # would not create the indexes we need....
32                         foreach ($tables as $tbl) {
33                                 $res = $db->query("SHOW CREATE TABLE $tbl");
34                                 $row = $db->fetchRow($res);
35                                 $create = $row[1];
36                                 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
37                                         . $wgDBprefix . '\\1`', $create);
38                                 if ($create === $create_tmp) {
39                                         # Couldn't do replacement
40                                         wfDie( "could not create temporary table $tbl" );
41                                 }
42                                 $db->query($create_tmp);
43                         }
45                 }
46                 return $db;
47         } else {
48                 // Something amiss
49                 return null;
50         }
53 class SearchEngineTest {
54         var $db, $search;
56         function __construct( SearchEngine $search ){
57                 $this->search = $search;
58                 $this->db = $this->search->db;
59         }
61         function insertSearchData() {
62                 $this->db->safeQuery( <<<END
63                 INSERT INTO ! (page_id,page_namespace,page_title,page_latest)
64                 VALUES (1, 0, 'Main_Page', 1),
65                            (2, 1, 'Main_Page', 2),
66                            (3, 0, 'Smithee', 3),
67                            (4, 1, 'Smithee', 4),
68                            (5, 0, 'Unrelated_page', 5),
69                            (6, 0, 'Another_page', 6),
70                            (7, 4, 'Help', 7),
71                            (8, 0, 'Thppt', 8),
72                            (9, 0, 'Alan_Smithee', 9),
73                            (10, 0, 'Pages', 10)
74 END
75                         , $this->db->tableName( 'page' ) );
76                 $this->db->safeQuery( <<<END
77                 INSERT INTO ! (rev_id,rev_page)
78                 VALUES (1, 1),
79                        (2, 2),
80                        (3, 3),
81                        (4, 4),
82                        (5, 5),
83                        (6, 6),
84                        (7, 7),
85                        (8, 8),
86                        (9, 9),
87                        (10, 10)
88 END
89                         , $this->db->tableName( 'revision' ) );
90                 $this->db->safeQuery( <<<END
91                 INSERT INTO ! (old_id,old_text)
92                 VALUES (1, 'This is a main page'),
93                            (2, 'This is a talk page to the main page, see [[smithee]]'),
94                            (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
95                            (4, 'This article sucks.'),
96                            (5, 'Nothing in this page is about the S word.'),
97                            (6, 'This page also is unrelated.'),
98                            (7, 'Help me!'),
99                            (8, 'Blah blah'),
100                            (9, 'yum'),
101                            (10,'are food')
103                         , $this->db->tableName( 'text' ) );
104                 $this->db->safeQuery( <<<END
105                 INSERT INTO ! (si_page,si_title,si_text)
106                 VALUES (1, 'main page', 'this is a main page'),
107                            (2, 'main page', 'this is a talk page to the main page, see smithee'),
108                            (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
109                            (4, 'smithee', 'this article sucks'),
110                            (5, 'unrelated page', 'nothing in this page is about the s word'),
111                            (6, 'another page', 'this page also is unrelated'),
112                            (7, 'help', 'help me'),
113                            (8, 'thppt', 'blah blah'),
114                            (9, 'alan smithee', 'yum'),
115                            (10, 'pages', 'are food')
117                         , $this->db->tableName( 'searchindex' ) );
118         }
120         function fetchIds( $results ) {
121                 $matches = array();
122                 while( $row = $results->next() ) {
123                         $matches[] = $row->getTitle()->getPrefixedText();
124                 }
125                 $results->free();
126                 # Search is not guaranteed to return results in a certain order;
127                 # sort them numerically so we will compare simply that we received
128                 # the expected matches.
129                 sort( $matches );
130                 return $matches;
131         }
133         function run(){
134                 if( is_null( $this->db ) ){
135                         fail( "Can't find a database to test with." );
136                         return;
137                 }
138                 
139                 $this->insertSearchData();
140                 plan( 4 );
141                 
142                 $exp = array( 'Smithee' );
143                 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
144                 is( $got, $exp, "Plain search" );
146                 $exp = array( 'Alan Smithee', 'Smithee' );
147                 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
148                 is( $got, $exp, "Title search" );
150                 $this->search->setNamespaces( array( 0, 1, 4 ) );
152                 $exp = array( 'Smithee', 'Talk:Main Page', );
153                 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
154                 is( $got, $exp, "Power search" );
156                 $exp = array( 'Alan Smithee', 'Smithee', 'Talk:Smithee', );
157                 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
158                 is( $got, $exp, "Title power search" );
159         }