Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / search / SearchEngineTest.php
blobd0cbfa0a32a50a6c8dbb2405b12e671bf9f34640
1 <?php
3 /**
4 * @group Search
5 * @group Database
7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
9 */
10 class SearchEngineTest extends MediaWikiLangTestCase {
12 /**
13 * @var SearchEngine
15 protected $search;
17 /**
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
21 protected function setUp() {
22 parent::setUp();
24 // Search tests require MySQL or SQLite with FTS
25 $dbType = $this->db->getType();
26 $dbSupported = ( $dbType === 'mysql' )
27 || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
29 if ( !$dbSupported ) {
30 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
33 $searchType = $this->db->getSearchEngine();
34 $this->setMwGlobals( array(
35 'wgSearchType' => $searchType
36 ) );
38 $this->search = new $searchType( $this->db );
41 protected function tearDown() {
42 unset( $this->search );
44 parent::tearDown();
47 public function addDBData() {
48 if ( !$this->isWikitextNS( NS_MAIN ) ) {
49 // @todo cover the case of non-wikitext content in the main namespace
50 return;
53 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
54 $this->insertPage(
55 'Talk:Not_Main_Page',
56 'This is not a talk page to the main page, see [[smithee]]'
58 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
59 $this->insertPage( 'Talk:Smithee', 'This article sucks.' );
60 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
61 $this->insertPage( 'Another_page', 'This page also is unrelated.' );
62 $this->insertPage( 'Help:Help', 'Help me!' );
63 $this->insertPage( 'Thppt', 'Blah blah' );
64 $this->insertPage( 'Alan_Smithee', 'yum' );
65 $this->insertPage( 'Pages', 'are\'food' );
66 $this->insertPage( 'HalfOneUp', 'AZ' );
67 $this->insertPage( 'FullOneUp', 'AZ' );
68 $this->insertPage( 'HalfTwoLow', 'az' );
69 $this->insertPage( 'FullTwoLow', 'az' );
70 $this->insertPage( 'HalfNumbers', '1234567890' );
71 $this->insertPage( 'FullNumbers', '1234567890' );
72 $this->insertPage( 'DomainName', 'example.com' );
75 protected function fetchIds( $results ) {
76 if ( !$this->isWikitextNS( NS_MAIN ) ) {
77 $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
78 . "in the main namespace" );
80 $this->assertTrue( is_object( $results ) );
82 $matches = array();
83 $row = $results->next();
84 while ( $row ) {
85 $matches[] = $row->getTitle()->getPrefixedText();
86 $row = $results->next();
88 $results->free();
89 # Search is not guaranteed to return results in a certain order;
90 # sort them numerically so we will compare simply that we received
91 # the expected matches.
92 sort( $matches );
94 return $matches;
97 public function testFullWidth() {
98 $this->assertEquals(
99 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
100 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
101 "Search for normalized from Half-width Upper" );
102 $this->assertEquals(
103 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
104 $this->fetchIds( $this->search->searchText( 'az' ) ),
105 "Search for normalized from Half-width Lower" );
106 $this->assertEquals(
107 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
108 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
109 "Search for normalized from Full-width Upper" );
110 $this->assertEquals(
111 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
112 $this->fetchIds( $this->search->searchText( 'az' ) ),
113 "Search for normalized from Full-width Lower" );
116 public function testTextSearch() {
117 $this->assertEquals(
118 array( 'Smithee' ),
119 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
120 "Plain search failed" );
123 public function testTextPowerSearch() {
124 $this->search->setNamespaces( array( 0, 1, 4 ) );
125 $this->assertEquals(
126 array(
127 'Smithee',
128 'Talk:Not Main Page',
130 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
131 "Power search failed" );
134 public function testTitleSearch() {
135 $this->assertEquals(
136 array(
137 'Alan Smithee',
138 'Smithee',
140 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
141 "Title search failed" );
144 public function testTextTitlePowerSearch() {
145 $this->search->setNamespaces( array( 0, 1, 4 ) );
146 $this->assertEquals(
147 array(
148 'Alan Smithee',
149 'Smithee',
150 'Talk:Smithee',
152 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
153 "Title power search failed" );