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();
22 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
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
);
36 function pageExists( $title ) {
40 function addDBData() {
41 if ( $this->pageExists( 'Not_Main_Page' ) ) {
45 if ( !$this->isWikitextNS( NS_MAIN
) ) {
46 //@todo: cover the case of non-wikitext content in the main namespace
50 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
51 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
52 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
53 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
54 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
55 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
56 $this->insertPage( 'Help:Help', 'Help me!', 4 );
57 $this->insertPage( 'Thppt', 'Blah blah', 0 );
58 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
59 $this->insertPage( 'Pages', 'are\'food', 0 );
60 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
61 $this->insertPage( 'FullOneUp', 'AZ', 0 );
62 $this->insertPage( 'HalfTwoLow', 'az', 0 );
63 $this->insertPage( 'FullTwoLow', 'az', 0 );
64 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
65 $this->insertPage( 'FullNumbers', '1234567890', 0 );
66 $this->insertPage( 'DomainName', 'example.com', 0 );
69 function fetchIds( $results ) {
70 if ( !$this->isWikitextNS( NS_MAIN
) ) {
71 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
72 . "in the main namespace");
75 $this->assertTrue( is_object( $results ) );
78 $row = $results->next();
80 $matches[] = $row->getTitle()->getPrefixedText();
81 $row = $results->next();
84 # Search is not guaranteed to return results in a certain order;
85 # sort them numerically so we will compare simply that we received
86 # the expected matches.
94 * @param $pageName String: page name
95 * @param $text String: page's content
96 * @param $n Integer: unused
98 function insertPage( $pageName, $text, $ns ) {
99 $title = Title
::newFromText( $pageName, $ns );
101 $user = User
::newFromName( 'WikiSysop' );
102 $comment = 'Search Test';
104 // avoid memory leak...?
105 LinkCache
::singleton()->clear();
107 $page = WikiPage
::factory( $title );
108 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
110 $this->pageList
[] = array( $title, $page->getId() );
115 function testFullWidth() {
117 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
118 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
119 "Search for normalized from Half-width Upper" );
121 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
122 $this->fetchIds( $this->search
->searchText( 'az' ) ),
123 "Search for normalized from Half-width Lower" );
125 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
126 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
127 "Search for normalized from Full-width Upper" );
129 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
130 $this->fetchIds( $this->search
->searchText( 'az' ) ),
131 "Search for normalized from Full-width Lower" );
134 function testTextSearch() {
137 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
138 "Plain search failed" );
141 function testTextPowerSearch() {
142 $this->search
->setNamespaces( array( 0, 1, 4 ) );
146 'Talk:Not Main Page',
148 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
149 "Power search failed" );
152 function testTitleSearch() {
158 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
159 "Title search failed" );
162 function testTextTitlePowerSearch() {
163 $this->search
->setNamespaces( array( 0, 1, 4 ) );
170 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
171 "Title power search failed" );