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