7 class SearchEngineTest
extends MediaWikiLangTestCase
{
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() {
17 // Search tests require MySQL or SQLite with FTS
18 # Get database type and version
19 $dbType = $this->db
->getType();
21 ( $dbType === 'mysql' )
22 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
24 if ( !$dbSupported ) {
25 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
28 $searchType = $this->db
->getSearchEngine();
29 $this->search
= new $searchType( $this->db
);
32 protected function tearDown() {
33 unset( $this->search
);
38 function pageExists( $title ) {
42 function addDBData() {
43 if ( $this->pageExists( 'Not_Main_Page' ) ) {
47 if ( !$this->isWikitextNS( NS_MAIN
) ) {
48 // @todo cover the case of non-wikitext content in the main namespace
52 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
53 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
54 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
55 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
56 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
57 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
58 $this->insertPage( 'Help:Help', 'Help me!', 4 );
59 $this->insertPage( 'Thppt', 'Blah blah', 0 );
60 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
61 $this->insertPage( 'Pages', 'are\'food', 0 );
62 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
63 $this->insertPage( 'FullOneUp', 'AZ', 0 );
64 $this->insertPage( 'HalfTwoLow', 'az', 0 );
65 $this->insertPage( 'FullTwoLow', 'az', 0 );
66 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
67 $this->insertPage( 'FullNumbers', '1234567890', 0 );
68 $this->insertPage( 'DomainName', 'example.com', 0 );
71 function fetchIds( $results ) {
72 if ( !$this->isWikitextNS( NS_MAIN
) ) {
73 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
74 . "in the main namespace" );
77 $this->assertTrue( is_object( $results ) );
80 $row = $results->next();
82 $matches[] = $row->getTitle()->getPrefixedText();
83 $row = $results->next();
86 # Search is not guaranteed to return results in a certain order;
87 # sort them numerically so we will compare simply that we received
88 # the expected matches.
97 * @param $pageName String: page name
98 * @param $text String: page's content
99 * @param $n Integer: unused
101 function insertPage( $pageName, $text, $ns ) {
102 $title = Title
::newFromText( $pageName, $ns );
104 $user = User
::newFromName( 'WikiSysop' );
105 $comment = 'Search Test';
107 // avoid memory leak...?
108 LinkCache
::singleton()->clear();
110 $page = WikiPage
::factory( $title );
111 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
113 $this->pageList
[] = array( $title, $page->getId() );
118 function testFullWidth() {
120 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
121 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
122 "Search for normalized from Half-width Upper" );
124 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
125 $this->fetchIds( $this->search
->searchText( 'az' ) ),
126 "Search for normalized from Half-width Lower" );
128 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
129 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
130 "Search for normalized from Full-width Upper" );
132 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
133 $this->fetchIds( $this->search
->searchText( 'az' ) ),
134 "Search for normalized from Full-width Lower" );
137 function testTextSearch() {
140 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
141 "Plain search failed" );
144 function testTextPowerSearch() {
145 $this->search
->setNamespaces( array( 0, 1, 4 ) );
149 'Talk:Not Main Page',
151 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
152 "Power search failed" );
155 function testTitleSearch() {
161 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
162 "Title search failed" );
165 function testTextTitlePowerSearch() {
166 $this->search
->setNamespaces( array( 0, 1, 4 ) );
173 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
174 "Title power search failed" );