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