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