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