7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
10 class SearchEngineTest
extends MediaWikiLangTestCase
{
20 * Checks for database type & version.
21 * Will skip current test if DB does not support search.
23 protected function setUp() {
26 // Search tests require MySQL or SQLite with FTS
27 $dbType = $this->db
->getType();
28 $dbSupported = ( $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->setMwGlobals( array(
37 'wgSearchType' => $searchType
40 if ( !isset( self
::$pageList ) ) {
44 $this->search
= new $searchType( $this->db
);
47 protected function tearDown() {
48 unset( $this->search
);
53 protected function addPages() {
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 protected 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" );
83 $this->assertTrue( is_object( $results ) );
86 $row = $results->next();
88 $matches[] = $row->getTitle()->getPrefixedText();
89 $row = $results->next();
92 # Search is not guaranteed to return results in a certain order;
93 # sort them numerically so we will compare simply that we received
94 # the expected matches.
103 * @param $pageName String: page name
104 * @param $text String: page's content
105 * @param $n Integer: unused
107 protected function insertPage( $pageName, $text, $ns ) {
108 $title = Title
::newFromText( $pageName, $ns );
110 $user = User
::newFromName( 'WikiSysop' );
111 $comment = 'Search Test';
113 // avoid memory leak...?
114 LinkCache
::singleton()->clear();
116 $page = WikiPage
::factory( $title );
117 $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $comment, 0, false, $user );
119 $this->pageList
[] = array( $title, $page->getId() );
124 public function testFullWidth() {
126 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
127 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
128 "Search for normalized from Half-width Upper" );
130 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
131 $this->fetchIds( $this->search
->searchText( 'az' ) ),
132 "Search for normalized from Half-width Lower" );
134 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
135 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
136 "Search for normalized from Full-width Upper" );
138 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
139 $this->fetchIds( $this->search
->searchText( 'az' ) ),
140 "Search for normalized from Full-width Lower" );
143 public function testTextSearch() {
146 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
147 "Plain search failed" );
150 public function testTextPowerSearch() {
151 $this->search
->setNamespaces( array( 0, 1, 4 ) );
155 'Talk:Not Main Page',
157 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
158 "Power search failed" );
161 public function testTitleSearch() {
167 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
168 "Title search failed" );
171 public function testTextTitlePowerSearch() {
172 $this->search
->setNamespaces( array( 0, 1, 4 ) );
179 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
180 "Title power search failed" );