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