Add DROP INDEX support to DatabaseSqlite::replaceVars method
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
1 <?php
2
3 /**
4 * @group Search
5 * @group Database
6 */
7 class SearchEngineTest extends MediaWikiLangTestCase {
8 protected $search, $pageList;
9
10 /**
11 * Checks for database type & version.
12 * Will skip current test if DB does not support search.
13 */
14 protected function setUp() {
15 parent::setUp();
16
17 // Search tests require MySQL or SQLite with FTS
18 # Get database type and version
19 $dbType = $this->db->getType();
20 $dbSupported =
21 ( $dbType === 'mysql' )
22 || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
23
24 if ( !$dbSupported ) {
25 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
26 }
27
28 $searchType = $this->db->getSearchEngine();
29 $this->search = new $searchType( $this->db );
30 }
31
32 protected function tearDown() {
33 unset( $this->search );
34
35 parent::tearDown();
36 }
37
38 function pageExists( $title ) {
39 return false;
40 }
41
42 function addDBData() {
43 if ( $this->pageExists( 'Not_Main_Page' ) ) {
44 return;
45 }
46
47 if ( !$this->isWikitextNS( NS_MAIN ) ) {
48 // @todo cover the case of non-wikitext content in the main namespace
49 return;
50 }
51
52 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
53 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
54 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
55 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
56 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
57 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
58 $this->insertPage( 'Help:Help', 'Help me!', 4 );
59 $this->insertPage( 'Thppt', 'Blah blah', 0 );
60 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
61 $this->insertPage( 'Pages', 'are\'food', 0 );
62 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
63 $this->insertPage( 'FullOneUp', 'AZ', 0 );
64 $this->insertPage( 'HalfTwoLow', 'az', 0 );
65 $this->insertPage( 'FullTwoLow', 'az', 0 );
66 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
67 $this->insertPage( 'FullNumbers', '1234567890', 0 );
68 $this->insertPage( 'DomainName', 'example.com', 0 );
69 }
70
71 function fetchIds( $results ) {
72 if ( !$this->isWikitextNS( NS_MAIN ) ) {
73 $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
74 . "in the main namespace" );
75 }
76
77 $this->assertTrue( is_object( $results ) );
78
79 $matches = array();
80 $row = $results->next();
81 while ( $row ) {
82 $matches[] = $row->getTitle()->getPrefixedText();
83 $row = $results->next();
84 }
85 $results->free();
86 # Search is not guaranteed to return results in a certain order;
87 # sort them numerically so we will compare simply that we received
88 # the expected matches.
89 sort( $matches );
90
91 return $matches;
92 }
93
94 /**
95 * Insert a new page
96 *
97 * @param $pageName String: page name
98 * @param $text String: page's content
99 * @param $n Integer: unused
100 */
101 function insertPage( $pageName, $text, $ns ) {
102 $title = Title::newFromText( $pageName, $ns );
103
104 $user = User::newFromName( 'WikiSysop' );
105 $comment = 'Search Test';
106
107 // avoid memory leak...?
108 LinkCache::singleton()->clear();
109
110 $page = WikiPage::factory( $title );
111 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
112
113 $this->pageList[] = array( $title, $page->getId() );
114
115 return true;
116 }
117
118 function testFullWidth() {
119 $this->assertEquals(
120 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
121 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
122 "Search for normalized from Half-width Upper" );
123 $this->assertEquals(
124 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
125 $this->fetchIds( $this->search->searchText( 'az' ) ),
126 "Search for normalized from Half-width Lower" );
127 $this->assertEquals(
128 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
129 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
130 "Search for normalized from Full-width Upper" );
131 $this->assertEquals(
132 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
133 $this->fetchIds( $this->search->searchText( 'az' ) ),
134 "Search for normalized from Full-width Lower" );
135 }
136
137 function testTextSearch() {
138 $this->assertEquals(
139 array( 'Smithee' ),
140 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
141 "Plain search failed" );
142 }
143
144 function testTextPowerSearch() {
145 $this->search->setNamespaces( array( 0, 1, 4 ) );
146 $this->assertEquals(
147 array(
148 'Smithee',
149 'Talk:Not Main Page',
150 ),
151 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
152 "Power search failed" );
153 }
154
155 function testTitleSearch() {
156 $this->assertEquals(
157 array(
158 'Alan Smithee',
159 'Smithee',
160 ),
161 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
162 "Title search failed" );
163 }
164
165 function testTextTitlePowerSearch() {
166 $this->search->setNamespaces( array( 0, 1, 4 ) );
167 $this->assertEquals(
168 array(
169 'Alan Smithee',
170 'Smithee',
171 'Talk:Smithee',
172 ),
173 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
174 "Title power search failed" );
175 }
176 }