Make sure wfProfileIn is available for testing, in case some silly function uses it
[lhc/web/wiklou.git] / tests / SearchEngineTest.php
1 <?php
2
3 $IP = '..';
4 require_once( 'PHPUnit.php' );
5 require_once( '../includes/Defines.php' );
6 require_once( '../includes/DefaultSettings.php' );
7 require_once( '../includes/Profiling.php' );
8 require_once( '../includes/MagicWord.php' );
9 require_once( '../languages/Language.php' );
10 require_once( '../languages/LanguageUtf8.php' );
11
12 require_once( '../includes/SearchEngine.php' );
13
14 class SearchEngine_TestCase extends PHPUnit_TestCase {
15 var $db, $search;
16
17 function insertSearchData() {
18 $this->db->safeQuery( <<<END
19 INSERT INTO ! (cur_id,cur_namespace,cur_title,cur_text)
20 VALUES (1, 0, 'Main_Page', 'This is a main page'),
21 (2, 1, 'Main_Page', 'This is a talk page to the main page, see [[smithee]]'),
22 (3, 0, 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]'),
23 (4, 1, 'Smithee', 'This article sucks.'),
24 (5, 0, 'Unrelated_page', 'Nothing in this page is about the S word.'),
25 (6, 0, 'Another_page', 'This page also is unrelated.'),
26 (7, 4, 'Help', 'Help me!'),
27 (8, 0, 'Thppt', 'Blah blah'),
28 (9, 0, 'Alan_Smithee', 'yum'),
29 (10, 0, 'Pages', 'are food')
30 END
31 , $this->db->tableName( 'cur' ) );
32 $this->db->safeQuery( <<<END
33 INSERT INTO ! (si_page,si_title,si_text)
34 VALUES (1, 'main page', 'this is a main page'),
35 (2, 'main page', 'this is a talk page to the main page, see smithee'),
36 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
37 (4, 'smithee', 'this article sucks'),
38 (5, 'unrelated page', 'nothing in this page is about the s word'),
39 (6, 'another page', 'this page also is unrelated'),
40 (7, 'help', 'help me'),
41 (8, 'thppt', 'blah blah'),
42 (9, 'alan smithee', 'yum'),
43 (10, 'pages', 'are food')
44 END
45 , $this->db->tableName( 'searchindex' ) );
46 }
47
48 function fetchIds( &$results ) {
49 $matches = array();
50 while( $row = $results->fetchObject() ) {
51 $matches[] = IntVal( $row->cur_id );
52 }
53 $results->free();
54 # Search is not guaranteed to return results in a certain order;
55 # sort them numerically so we will compare simply that we received
56 # the expected matches.
57 sort( $matches );
58 return $matches;
59 }
60
61 function testTextSearch() {
62 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
63 if( !is_null( $this->db ) ) {
64 $this->assertEquals(
65 array( 3 ),
66 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
67 "Plain search failed" );
68 }
69 }
70
71 function testTextPowerSearch() {
72 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
73 if( !is_null( $this->db ) ) {
74 $this->search->setNamespaces( array( 0, 1, 4 ) );
75 $this->assertEquals(
76 array( 2, 3 ),
77 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
78 "Power search failed" );
79 }
80 }
81
82 function testTitleSearch() {
83 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
84 if( !is_null( $this->db ) ) {
85 $this->assertEquals(
86 array( 3, 9 ),
87 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
88 "Title search failed" );
89 }
90 }
91
92 function testTextTitlePowerSearch() {
93 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
94 if( !is_null( $this->db ) ) {
95 $this->search->setNamespaces( array( 0, 1, 4 ) );
96 $this->assertEquals(
97 array( 3, 4, 9 ),
98 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
99 "Title power search failed" );
100 }
101 }
102
103 }
104
105
106 ?>