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