Reorganization of SearchEngine for legibility
[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 return $matches;
55 }
56
57 function testTextSearch() {
58 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
59 if( !is_null( $this->db ) ) {
60 $this->assertEquals(
61 array( 3 ),
62 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
63 "Plain search failed" );
64 }
65 }
66
67 function testTextPowerSearch() {
68 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
69 if( !is_null( $this->db ) ) {
70 $this->search->setNamespaces( array( 0, 1, 4 ) );
71 $this->assertEquals(
72 array( 2, 3 ),
73 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
74 "Power search failed" );
75 }
76 }
77
78 function testTitleSearch() {
79 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
80 if( !is_null( $this->db ) ) {
81 $this->assertEquals(
82 array( 3, 9 ),
83 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
84 "Title search failed" );
85 }
86 }
87
88 function testTextTitlePowerSearch() {
89 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
90 if( !is_null( $this->db ) ) {
91 $this->search->setNamespaces( array( 0, 1, 4 ) );
92 $this->assertEquals(
93 array( 3, 4, 9 ),
94 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
95 "Title power search failed" );
96 }
97 }
98
99 }
100
101
102 ?>