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' ) ) {
44 if ( !$this->isWikitextNS( NS_MAIN
) ) {
45 //@todo: cover the case of non-wikitext content in the main namespace
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 );
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");
74 $this->assertTrue( is_object( $results ) );
77 $row = $results->next();
79 $matches[] = $row->getTitle()->getPrefixedText();
80 $row = $results->next();
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.
93 * @param $pageName String: page name
94 * @param $text String: page's content
95 * @param $n Integer: unused
97 function insertPage( $pageName, $text, $ns ) {
98 $title = Title
::newFromText( $pageName, $ns );
100 $user = User
::newFromName( 'WikiSysop' );
101 $comment = 'Search Test';
103 // avoid memory leak...?
104 LinkCache
::singleton()->clear();
106 $page = WikiPage
::factory( $title );
107 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
109 $this->pageList
[] = array( $title, $page->getId() );
114 function testFullWidth() {
116 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
117 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
118 "Search for normalized from Half-width Upper" );
120 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
121 $this->fetchIds( $this->search
->searchText( 'az' ) ),
122 "Search for normalized from Half-width Lower" );
124 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
125 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
126 "Search for normalized from Full-width Upper" );
128 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
129 $this->fetchIds( $this->search
->searchText( 'az' ) ),
130 "Search for normalized from Full-width Lower" );
133 function testTextSearch() {
136 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
137 "Plain search failed" );
140 function testTextPowerSearch() {
141 $this->search
->setNamespaces( array( 0, 1, 4 ) );
145 'Talk:Not Main Page',
147 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
148 "Power search failed" );
151 function testTitleSearch() {
157 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
158 "Title search failed" );
161 function testTextTitlePowerSearch() {
162 $this->search
->setNamespaces( array( 0, 1, 4 ) );
169 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
170 "Title power search failed" );