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