7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
10 class SearchEngineTest
extends MediaWikiLangTestCase
{
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
21 protected function setUp() {
24 // Search tests require MySQL or SQLite with FTS
25 # Get database type and version
26 $dbType = $this->db
->getType();
28 ( $dbType === 'mysql' )
29 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
31 if ( !$dbSupported ) {
32 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
35 $searchType = $this->db
->getSearchEngine();
36 $this->search
= new $searchType( $this->db
);
39 protected function tearDown() {
40 unset( $this->search
);
45 function pageExists( $title ) {
49 function addDBData() {
50 if ( $this->pageExists( 'Not_Main_Page' ) ) {
54 if ( !$this->isWikitextNS( NS_MAIN
) ) {
55 // @todo cover the case of non-wikitext content in the main namespace
59 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
60 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
61 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
62 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
63 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
64 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
65 $this->insertPage( 'Help:Help', 'Help me!', 4 );
66 $this->insertPage( 'Thppt', 'Blah blah', 0 );
67 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
68 $this->insertPage( 'Pages', 'are\'food', 0 );
69 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
70 $this->insertPage( 'FullOneUp', 'AZ', 0 );
71 $this->insertPage( 'HalfTwoLow', 'az', 0 );
72 $this->insertPage( 'FullTwoLow', 'az', 0 );
73 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
74 $this->insertPage( 'FullNumbers', '1234567890', 0 );
75 $this->insertPage( 'DomainName', 'example.com', 0 );
78 function fetchIds( $results ) {
79 if ( !$this->isWikitextNS( NS_MAIN
) ) {
80 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
81 . "in the main namespace" );
84 $this->assertTrue( is_object( $results ) );
87 $row = $results->next();
89 $matches[] = $row->getTitle()->getPrefixedText();
90 $row = $results->next();
93 # Search is not guaranteed to return results in a certain order;
94 # sort them numerically so we will compare simply that we received
95 # the expected matches.
104 * @param $pageName String: page name
105 * @param $text String: page's content
106 * @param $n Integer: unused
108 function insertPage( $pageName, $text, $ns ) {
109 $title = Title
::newFromText( $pageName, $ns );
111 $user = User
::newFromName( 'WikiSysop' );
112 $comment = 'Search Test';
114 // avoid memory leak...?
115 LinkCache
::singleton()->clear();
117 $page = WikiPage
::factory( $title );
118 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
120 $this->pageList
[] = array( $title, $page->getId() );
125 public function testFullWidth() {
127 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
128 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
129 "Search for normalized from Half-width Upper" );
131 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
132 $this->fetchIds( $this->search
->searchText( 'az' ) ),
133 "Search for normalized from Half-width Lower" );
135 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
136 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
137 "Search for normalized from Full-width Upper" );
139 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
140 $this->fetchIds( $this->search
->searchText( 'az' ) ),
141 "Search for normalized from Full-width Lower" );
144 public function testTextSearch() {
147 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
148 "Plain search failed" );
151 public function testTextPowerSearch() {
152 $this->search
->setNamespaces( array( 0, 1, 4 ) );
156 'Talk:Not Main Page',
158 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
159 "Power search failed" );
162 public function testTitleSearch() {
168 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
169 "Title search failed" );
172 public function testTextTitlePowerSearch() {
173 $this->search
->setNamespaces( array( 0, 1, 4 ) );
180 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
181 "Title power search failed" );