Merge "Add countUnreadNotifications to WatchedItemStore"
[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 /**
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
20 */
21 protected function setUp() {
22 parent::setUp();
23
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' );
28
29 if ( !$dbSupported ) {
30 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
31 }
32
33 $searchType = $this->db->getSearchEngine();
34 $this->setMwGlobals( [
35 'wgSearchType' => $searchType
36 ] );
37
38 $this->search = new $searchType( $this->db );
39 }
40
41 protected function tearDown() {
42 unset( $this->search );
43
44 parent::tearDown();
45 }
46
47 public function addDBDataOnce() {
48 if ( !$this->isWikitextNS( NS_MAIN ) ) {
49 // @todo cover the case of non-wikitext content in the main namespace
50 return;
51 }
52
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]]'
57 );
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' );
73 }
74
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" );
79 }
80 $this->assertTrue( is_object( $results ) );
81
82 $matches = [];
83 $row = $results->next();
84 while ( $row ) {
85 $matches[] = $row->getTitle()->getPrefixedText();
86 $row = $results->next();
87 }
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 );
93
94 return $matches;
95 }
96
97 public function testFullWidth() {
98 $this->assertEquals(
99 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
100 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
101 "Search for normalized from Half-width Upper" );
102 $this->assertEquals(
103 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
104 $this->fetchIds( $this->search->searchText( 'az' ) ),
105 "Search for normalized from Half-width Lower" );
106 $this->assertEquals(
107 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
108 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
109 "Search for normalized from Full-width Upper" );
110 $this->assertEquals(
111 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
112 $this->fetchIds( $this->search->searchText( 'az' ) ),
113 "Search for normalized from Full-width Lower" );
114 }
115
116 public function testTextSearch() {
117 $this->assertEquals(
118 [ 'Smithee' ],
119 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
120 "Plain search failed" );
121 }
122
123 public function testTextPowerSearch() {
124 $this->search->setNamespaces( [ 0, 1, 4 ] );
125 $this->assertEquals(
126 [
127 'Smithee',
128 'Talk:Not Main Page',
129 ],
130 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
131 "Power search failed" );
132 }
133
134 public function testTitleSearch() {
135 $this->assertEquals(
136 [
137 'Alan Smithee',
138 'Smithee',
139 ],
140 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
141 "Title search failed" );
142 }
143
144 public function testTextTitlePowerSearch() {
145 $this->search->setNamespaces( [ 0, 1, 4 ] );
146 $this->assertEquals(
147 [
148 'Alan Smithee',
149 'Smithee',
150 'Talk:Smithee',
151 ],
152 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
153 "Title power search failed" );
154 }
155
156 }