Merge "(Bug 44987) Allow n=form in plural syntax"
[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 return $matches;
91 }
92
93 /**
94 * Insert a new page
95 *
96 * @param $pageName String: page name
97 * @param $text String: page's content
98 * @param $n Integer: unused
99 */
100 function insertPage( $pageName, $text, $ns ) {
101 $title = Title::newFromText( $pageName, $ns );
102
103 $user = User::newFromName( 'WikiSysop' );
104 $comment = 'Search Test';
105
106 // avoid memory leak...?
107 LinkCache::singleton()->clear();
108
109 $page = WikiPage::factory( $title );
110 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
111
112 $this->pageList[] = array( $title, $page->getId() );
113
114 return true;
115 }
116
117 function testFullWidth() {
118 $this->assertEquals(
119 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
120 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
121 "Search for normalized from Half-width Upper" );
122 $this->assertEquals(
123 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
124 $this->fetchIds( $this->search->searchText( 'az' ) ),
125 "Search for normalized from Half-width Lower" );
126 $this->assertEquals(
127 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
128 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
129 "Search for normalized from Full-width Upper" );
130 $this->assertEquals(
131 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
132 $this->fetchIds( $this->search->searchText( 'az' ) ),
133 "Search for normalized from Full-width Lower" );
134 }
135
136 function testTextSearch() {
137 $this->assertEquals(
138 array( 'Smithee' ),
139 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
140 "Plain search failed" );
141 }
142
143 function testTextPowerSearch() {
144 $this->search->setNamespaces( array( 0, 1, 4 ) );
145 $this->assertEquals(
146 array(
147 'Smithee',
148 'Talk:Not Main Page',
149 ),
150 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
151 "Power search failed" );
152 }
153
154 function testTitleSearch() {
155 $this->assertEquals(
156 array(
157 'Alan Smithee',
158 'Smithee',
159 ),
160 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
161 "Title search failed" );
162 }
163
164 function testTextTitlePowerSearch() {
165 $this->search->setNamespaces( array( 0, 1, 4 ) );
166 $this->assertEquals(
167 array(
168 'Alan Smithee',
169 'Smithee',
170 'Talk:Smithee',
171 ),
172 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
173 "Title power search failed" );
174 }
175
176 }