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