Merge "mediawiki.language: Fix infinite loop in commafy() when pattern has no grouping"
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
1 <?php
2
3 /**
4 * @group Search
5 * @group Database
6 *
7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
9 */
10 class SearchEngineTest extends MediaWikiLangTestCase {
11
12 /**
13 * @var SearchEngine
14 */
15 protected $search;
16
17 protected $pageList;
18
19 /**
20 * Checks for database type & version.
21 * Will skip current test if DB does not support search.
22 */
23 protected function setUp() {
24 parent::setUp();
25
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' );
30
31 if ( !$dbSupported ) {
32 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
33 }
34
35 $searchType = $this->db->getSearchEngine();
36 $this->setMwGlobals( array(
37 'wgSearchType' => $searchType
38 ) );
39
40 if ( !isset( self::$pageList ) ) {
41 $this->addPages();
42 }
43
44 $this->search = new $searchType( $this->db );
45 }
46
47 protected function tearDown() {
48 unset( $this->search );
49
50 parent::tearDown();
51 }
52
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;
57 }
58
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]]',
63 1
64 );
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 );
80 }
81
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" );
86 }
87 $this->assertTrue( is_object( $results ) );
88
89 $matches = array();
90 $row = $results->next();
91 while ( $row ) {
92 $matches[] = $row->getTitle()->getPrefixedText();
93 $row = $results->next();
94 }
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 );
100
101 return $matches;
102 }
103
104 /**
105 * Insert a new page
106 *
107 * @param string $pageName Page name
108 * @param string $text Page's content
109 * @param int $ns Unused
110 */
111 protected function insertPage( $pageName, $text, $ns ) {
112 $title = Title::newFromText( $pageName, $ns );
113
114 $user = User::newFromName( 'WikiSysop' );
115 $comment = 'Search Test';
116
117 // avoid memory leak...?
118 LinkCache::singleton()->clear();
119
120 $page = WikiPage::factory( $title );
121 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
122
123 $this->pageList[] = array( $title, $page->getId() );
124
125 return true;
126 }
127
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" );
145 }
146
147 public function testTextSearch() {
148 $this->assertEquals(
149 array( 'Smithee' ),
150 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
151 "Plain search failed" );
152 }
153
154 public function testTextPowerSearch() {
155 $this->search->setNamespaces( array( 0, 1, 4 ) );
156 $this->assertEquals(
157 array(
158 'Smithee',
159 'Talk:Not Main Page',
160 ),
161 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
162 "Power search failed" );
163 }
164
165 public function testTitleSearch() {
166 $this->assertEquals(
167 array(
168 'Alan Smithee',
169 'Smithee',
170 ),
171 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
172 "Title search failed" );
173 }
174
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',
182 ),
183 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
184 "Title power search failed" );
185 }
186
187 }