7 class SearchEngineTest
extends MediaWikiTestCase
{
8 protected $search, $pageList;
11 * Checks for database type & version.
12 * Will skip current test if DB does not support search.
14 protected function setUp() {
16 // Search tests require MySQL or SQLite with FTS
17 # Get database type and version
18 $dbType = $this->db
->getType();
21 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
24 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
27 $searchType = $this->db
->getSearchEngine();
28 $this->search
= new $searchType( $this->db
);
31 protected function tearDown() {
32 unset( $this->search
);
35 function pageExists( $title ) {
39 function addDBData() {
40 if ( $this->pageExists( 'Not_Main_Page' ) ) {
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 );
62 function fetchIds( $results ) {
63 $this->assertTrue( is_object( $results ) );
66 $row = $results->next();
68 $matches[] = $row->getTitle()->getPrefixedText();
69 $row = $results->next();
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.
82 * @param $pageName String: page name
83 * @param $text String: page's content
84 * @param $n Integer: unused
86 function insertPage( $pageName, $text, $ns ) {
87 $title = Title
::newFromText( $pageName );
89 $user = User
::newFromName( 'WikiSysop' );
90 $comment = 'Search Test';
92 // avoid memory leak...?
93 LinkCache
::singleton()->clear();
95 $page = WikiPage
::factory( $title );
96 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
98 $this->pageList
[] = array( $title, $page->getId() );
103 function testFullWidth() {
105 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
106 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
107 "Search for normalized from Half-width Upper" );
109 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
110 $this->fetchIds( $this->search
->searchText( 'az' ) ),
111 "Search for normalized from Half-width Lower" );
113 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
114 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
115 "Search for normalized from Full-width Upper" );
117 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
118 $this->fetchIds( $this->search
->searchText( 'az' ) ),
119 "Search for normalized from Full-width Lower" );
122 function testTextSearch() {
125 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
126 "Plain search failed" );
129 function testTextPowerSearch() {
130 $this->search
->setNamespaces( array( 0, 1, 4 ) );
134 'Talk:Not Main Page',
136 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
137 "Power search failed" );
140 function testTitleSearch() {
146 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
147 "Title search failed" );
150 function testTextTitlePowerSearch() {
151 $this->search
->setNamespaces( array( 0, 1, 4 ) );
158 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
159 "Title power search failed" );