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