Make phpunit on Ubuntu work out-of-the-box (at least for me) and prepare for some...
[lhc/web/wiklou.git] / tests / SearchEngineTest.php
1 <?php
2
3 require_once 'MediaWiki_TestCase.php';
4
5 /** @todo document
6 * @group Broken
7 */
8
9 class SearchEngineTest extends MediaWiki_TestCase {
10 var $db, $search;
11
12 function insertSearchData() {
13 $this->db->safeQuery( <<<SQL
14 INSERT INTO ! (page_id,page_namespace,page_title,page_latest)
15 VALUES (1, 0, 'Main_Page', 1),
16 (2, 1, 'Main_Page', 2),
17 (3, 0, 'Smithee', 3),
18 (4, 1, 'Smithee', 4),
19 (5, 0, 'Unrelated_page', 5),
20 (6, 0, 'Another_page', 6),
21 (7, 4, 'Help', 7),
22 (8, 0, 'Thppt', 8),
23 (9, 0, 'Alan_Smithee', 9),
24 (10, 0, 'Pages', 10)
25 SQL
26 , $this->db->tableName( 'page' ) );
27 $this->db->safeQuery( <<<SQL
28 INSERT INTO ! (rev_id,rev_page)
29 VALUES (1, 1),
30 (2, 2),
31 (3, 3),
32 (4, 4),
33 (5, 5),
34 (6, 6),
35 (7, 7),
36 (8, 8),
37 (9, 9),
38 (10, 10)
39 SQL
40 , $this->db->tableName( 'revision' ) );
41 $this->db->safeQuery( <<<SQL
42 INSERT INTO ! (old_id,old_text)
43 VALUES (1, 'This is a main page'),
44 (2, 'This is a talk page to the main page, see [[smithee]]'),
45 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
46 (4, 'This article sucks.'),
47 (5, 'Nothing in this page is about the S word.'),
48 (6, 'This page also is unrelated.'),
49 (7, 'Help me!'),
50 (8, 'Blah blah'),
51 (9, 'yum'),
52 (10,'are food')
53 SQL
54 , $this->db->tableName( 'text' ) );
55 $this->db->safeQuery( <<<SQL
56 INSERT INTO ! (si_page,si_title,si_text)
57 VALUES (1, 'main page', 'this is a main page'),
58 (2, 'main page', 'this is a talk page to the main page, see smithee'),
59 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
60 (4, 'smithee', 'this article sucks'),
61 (5, 'unrelated page', 'nothing in this page is about the s word'),
62 (6, 'another page', 'this page also is unrelated'),
63 (7, 'help', 'help me'),
64 (8, 'thppt', 'blah blah'),
65 (9, 'alan smithee', 'yum'),
66 (10, 'pages', 'are food')
67 SQL
68 , $this->db->tableName( 'searchindex' ) );
69 }
70
71 function fetchIds( $results ) {
72 $matches = array();
73 while( $row = $results->next() ) {
74 $matches[] = $row->getTitle()->getPrefixedText();
75 }
76 $results->free();
77 # Search is not guaranteed to return results in a certain order;
78 # sort them numerically so we will compare simply that we received
79 # the expected matches.
80 sort( $matches );
81 return $matches;
82 }
83
84 function testTextSearch() {
85 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
86 if( !is_null( $this->db ) ) {
87 $this->assertEquals(
88 array( 'Smithee' ),
89 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
90 "Plain search failed" );
91 }
92 }
93
94 function testTextPowerSearch() {
95 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
96 if( !is_null( $this->db ) ) {
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
108 function testTitleSearch() {
109 $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." );
110 if( !is_null( $this->db ) ) {
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
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(
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
139