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
);
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.
96 * @param $pageName String: page name
97 * @param $text String: page's content
98 * @param $n Integer: unused
100 function insertPage( $pageName, $text, $ns ) {
101 $title = Title
::newFromText( $pageName, $ns );
103 $user = User
::newFromName( 'WikiSysop' );
104 $comment = 'Search Test';
106 // avoid memory leak...?
107 LinkCache
::singleton()->clear();
109 $page = WikiPage
::factory( $title );
110 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
112 $this->pageList
[] = array( $title, $page->getId() );
117 function testFullWidth() {
119 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
120 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
121 "Search for normalized from Half-width Upper" );
123 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
124 $this->fetchIds( $this->search
->searchText( 'az' ) ),
125 "Search for normalized from Half-width Lower" );
127 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
128 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
129 "Search for normalized from Full-width Upper" );
131 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
132 $this->fetchIds( $this->search
->searchText( 'az' ) ),
133 "Search for normalized from Full-width Lower" );
136 function testTextSearch() {
139 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
140 "Plain search failed" );
143 function testTextPowerSearch() {
144 $this->search
->setNamespaces( array( 0, 1, 4 ) );
148 'Talk:Not Main Page',
150 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
151 "Power search failed" );
154 function testTitleSearch() {
160 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
161 "Title search failed" );
164 function testTextTitlePowerSearch() {
165 $this->search
->setNamespaces( array( 0, 1, 4 ) );
172 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
173 "Title power search failed" );