Merge "(bug 37158) display personal menu on top of page tabs on Vector"
[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
36 function pageExists( $title ) {
37 return false;
38 }
39
40 function addDBData() {
41 if ( $this->pageExists( 'Not_Main_Page' ) ) {
42 return;
43 }
44
45 if ( !$this->isWikitextNS( NS_MAIN ) ) {
46 //@todo: cover the case of non-wikitext content in the main namespace
47 return;
48 }
49
50 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
51 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
52 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
53 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
54 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
55 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
56 $this->insertPage( 'Help:Help', 'Help me!', 4 );
57 $this->insertPage( 'Thppt', 'Blah blah', 0 );
58 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
59 $this->insertPage( 'Pages', 'are\'food', 0 );
60 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
61 $this->insertPage( 'FullOneUp', 'AZ', 0 );
62 $this->insertPage( 'HalfTwoLow', 'az', 0 );
63 $this->insertPage( 'FullTwoLow', 'az', 0 );
64 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
65 $this->insertPage( 'FullNumbers', '1234567890', 0 );
66 $this->insertPage( 'DomainName', 'example.com', 0 );
67 }
68
69 function fetchIds( $results ) {
70 if ( !$this->isWikitextNS( NS_MAIN ) ) {
71 $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
72 . "in the main namespace");
73 }
74
75 $this->assertTrue( is_object( $results ) );
76
77 $matches = array();
78 $row = $results->next();
79 while ( $row ) {
80 $matches[] = $row->getTitle()->getPrefixedText();
81 $row = $results->next();
82 }
83 $results->free();
84 # Search is not guaranteed to return results in a certain order;
85 # sort them numerically so we will compare simply that we received
86 # the expected matches.
87 sort( $matches );
88 return $matches;
89 }
90
91 /**
92 * Insert a new page
93 *
94 * @param $pageName String: page name
95 * @param $text String: page's content
96 * @param $n Integer: unused
97 */
98 function insertPage( $pageName, $text, $ns ) {
99 $title = Title::newFromText( $pageName, $ns );
100
101 $user = User::newFromName( 'WikiSysop' );
102 $comment = 'Search Test';
103
104 // avoid memory leak...?
105 LinkCache::singleton()->clear();
106
107 $page = WikiPage::factory( $title );
108 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
109
110 $this->pageList[] = array( $title, $page->getId() );
111
112 return true;
113 }
114
115 function testFullWidth() {
116 $this->assertEquals(
117 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
118 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
119 "Search for normalized from Half-width Upper" );
120 $this->assertEquals(
121 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
122 $this->fetchIds( $this->search->searchText( 'az' ) ),
123 "Search for normalized from Half-width Lower" );
124 $this->assertEquals(
125 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
126 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
127 "Search for normalized from Full-width Upper" );
128 $this->assertEquals(
129 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
130 $this->fetchIds( $this->search->searchText( 'az' ) ),
131 "Search for normalized from Full-width Lower" );
132 }
133
134 function testTextSearch() {
135 $this->assertEquals(
136 array( 'Smithee' ),
137 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
138 "Plain search failed" );
139 }
140
141 function testTextPowerSearch() {
142 $this->search->setNamespaces( array( 0, 1, 4 ) );
143 $this->assertEquals(
144 array(
145 'Smithee',
146 'Talk:Not Main Page',
147 ),
148 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
149 "Power search failed" );
150 }
151
152 function testTitleSearch() {
153 $this->assertEquals(
154 array(
155 'Alan Smithee',
156 'Smithee',
157 ),
158 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
159 "Title search failed" );
160 }
161
162 function testTextTitlePowerSearch() {
163 $this->search->setNamespaces( array( 0, 1, 4 ) );
164 $this->assertEquals(
165 array(
166 'Alan Smithee',
167 'Smithee',
168 'Talk:Smithee',
169 ),
170 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
171 "Title power search failed" );
172 }
173
174 }