Merge "Add Special:MediaStatistics page for file type stats"
[mediawiki.git] / tests / phpunit / includes / search / SearchEngineTest.php
blob3da1361530f584c1636917d8469d6a3575c9790e
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 protected $pageList;
19 /**
20 * Checks for database type & version.
21 * Will skip current test if DB does not support search.
23 protected function setUp() {
24 parent::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
38 ) );
40 if ( !isset( self::$pageList ) ) {
41 $this->addPages();
44 $this->search = new $searchType( $this->db );
47 protected function tearDown() {
48 unset( $this->search );
50 parent::tearDown();
53 protected function addPages() {
54 if ( !$this->isWikitextNS( NS_MAIN ) ) {
55 // @todo cover the case of non-wikitext content in the main namespace
56 return;
59 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
60 $this->insertPage(
61 'Talk:Not_Main_Page',
62 'This is not a talk page to the main page, see [[smithee]]',
65 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
66 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
67 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
68 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
69 $this->insertPage( 'Help:Help', 'Help me!', 4 );
70 $this->insertPage( 'Thppt', 'Blah blah', 0 );
71 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
72 $this->insertPage( 'Pages', 'are\'food', 0 );
73 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
74 $this->insertPage( 'FullOneUp', 'AZ', 0 );
75 $this->insertPage( 'HalfTwoLow', 'az', 0 );
76 $this->insertPage( 'FullTwoLow', 'az', 0 );
77 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
78 $this->insertPage( 'FullNumbers', '1234567890', 0 );
79 $this->insertPage( 'DomainName', 'example.com', 0 );
82 protected function fetchIds( $results ) {
83 if ( !$this->isWikitextNS( NS_MAIN ) ) {
84 $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
85 . "in the main namespace" );
87 $this->assertTrue( is_object( $results ) );
89 $matches = array();
90 $row = $results->next();
91 while ( $row ) {
92 $matches[] = $row->getTitle()->getPrefixedText();
93 $row = $results->next();
95 $results->free();
96 # Search is not guaranteed to return results in a certain order;
97 # sort them numerically so we will compare simply that we received
98 # the expected matches.
99 sort( $matches );
101 return $matches;
105 * Insert a new page
107 * @param string $pageName Page name
108 * @param string $text Page's content
109 * @param int $ns Unused
111 protected function insertPage( $pageName, $text, $ns ) {
112 $title = Title::newFromText( $pageName, $ns );
114 $user = User::newFromName( 'WikiSysop' );
115 $comment = 'Search Test';
117 // avoid memory leak...?
118 LinkCache::singleton()->clear();
120 $page = WikiPage::factory( $title );
121 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
123 $this->pageList[] = array( $title, $page->getId() );
125 return true;
128 public function testFullWidth() {
129 $this->assertEquals(
130 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
131 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
132 "Search for normalized from Half-width Upper" );
133 $this->assertEquals(
134 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
135 $this->fetchIds( $this->search->searchText( 'az' ) ),
136 "Search for normalized from Half-width Lower" );
137 $this->assertEquals(
138 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
139 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
140 "Search for normalized from Full-width Upper" );
141 $this->assertEquals(
142 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
143 $this->fetchIds( $this->search->searchText( 'az' ) ),
144 "Search for normalized from Full-width Lower" );
147 public function testTextSearch() {
148 $this->assertEquals(
149 array( 'Smithee' ),
150 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
151 "Plain search failed" );
154 public function testTextPowerSearch() {
155 $this->search->setNamespaces( array( 0, 1, 4 ) );
156 $this->assertEquals(
157 array(
158 'Smithee',
159 'Talk:Not Main Page',
161 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
162 "Power search failed" );
165 public function testTitleSearch() {
166 $this->assertEquals(
167 array(
168 'Alan Smithee',
169 'Smithee',
171 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
172 "Title search failed" );
175 public function testTextTitlePowerSearch() {
176 $this->search->setNamespaces( array( 0, 1, 4 ) );
177 $this->assertEquals(
178 array(
179 'Alan Smithee',
180 'Smithee',
181 'Talk:Smithee',
183 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
184 "Title power search failed" );