nicer format for static groups record
[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 ! (page_id,page_namespace,page_title,page_latest)
20 VALUES (1, 0, 'Main_Page', 1),
21 (2, 1, 'Main_Page', 2),
22 (3, 0, 'Smithee', 3),
23 (4, 1, 'Smithee', 4),
24 (5, 0, 'Unrelated_page', 5),
25 (6, 0, 'Another_page', 6),
26 (7, 4, 'Help', 7),
27 (8, 0, 'Thppt', 8),
28 (9, 0, 'Alan_Smithee', 9),
29 (10, 0, 'Pages', 10)
30 END
31 , $this->db->tableName( 'page' ) );
32 $this->db->safeQuery( <<<END
33 INSERT INTO ! (rev_id,rev_page)
34 VALUES (1, 1),
35 (2, 2),
36 (3, 3),
37 (4, 4),
38 (5, 5),
39 (6, 6),
40 (7, 7),
41 (8, 8),
42 (9, 9),
43 (10, 10)
44 END
45 , $this->db->tableName( 'revision' ) );
46 $this->db->safeQuery( <<<END
47 INSERT INTO ! (old_id,old_text)
48 VALUES (1, 'This is a main page'),
49 (2, 'This is a talk page to the main page, see [[smithee]]'),
50 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
51 (4, 'This article sucks.'),
52 (5, 'Nothing in this page is about the S word.'),
53 (6, 'This page also is unrelated.'),
54 (7, 'Help me!'),
55 (8, 'Blah blah'),
56 (9, 'yum'),
57 (10,'are food')
58 END
59 , $this->db->tableName( 'text' ) );
60 $this->db->safeQuery( <<<END
61 INSERT INTO ! (si_page,si_title,si_text)
62 VALUES (1, 'main page', 'this is a main page'),
63 (2, 'main page', 'this is a talk page to the main page, see smithee'),
64 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
65 (4, 'smithee', 'this article sucks'),
66 (5, 'unrelated page', 'nothing in this page is about the s word'),
67 (6, 'another page', 'this page also is unrelated'),
68 (7, 'help', 'help me'),
69 (8, 'thppt', 'blah blah'),
70 (9, 'alan smithee', 'yum'),
71 (10, 'pages', 'are food')
72 END
73 , $this->db->tableName( 'searchindex' ) );
74 }
75
76 function fetchIds( &$results ) {
77 $matches = array();
78 while( $row = $results->fetchObject() ) {
79 $matches[] = IntVal( $row->page_id );
80 }
81 $results->free();
82 # Search is not guaranteed to return results in a certain order;
83 # sort them numerically so we will compare simply that we received
84 # the expected matches.
85 sort( $matches );
86 return $matches;
87 }
88
89 function testTextSearch() {
90 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
91 if( !is_null( $this->db ) ) {
92 $this->assertEquals(
93 array( 3 ),
94 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
95 "Plain search failed" );
96 }
97 }
98
99 function testTextPowerSearch() {
100 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
101 if( !is_null( $this->db ) ) {
102 $this->search->setNamespaces( array( 0, 1, 4 ) );
103 $this->assertEquals(
104 array( 2, 3 ),
105 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
106 "Power search failed" );
107 }
108 }
109
110 function testTitleSearch() {
111 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
112 if( !is_null( $this->db ) ) {
113 $this->assertEquals(
114 array( 3, 9 ),
115 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
116 "Title search failed" );
117 }
118 }
119
120 function testTextTitlePowerSearch() {
121 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
122 if( !is_null( $this->db ) ) {
123 $this->search->setNamespaces( array( 0, 1, 4 ) );
124 $this->assertEquals(
125 array( 3, 4, 9 ),
126 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
127 "Title power search failed" );
128 }
129 }
130
131 }
132
133
134 ?>