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