Merge "Mention translatewiki.net on edits only, when edit a default message"
[lhc/web/wiklou.git] / tests / phpunit / includes / PrefixSearchTest.php
1 <?php
2 /**
3 * @group Search
4 * @group Database
5 */
6 class PrefixSearchTest extends MediaWikiLangTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 if ( !$this->isWikitextNS( NS_MAIN ) ) {
12 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
13 }
14
15 $this->insertPages();
16
17 // Avoid special pages from extensions interferring with the tests
18 $this->setMwGlobals( 'wgSpecialPages', array() );
19 }
20
21 protected function searchProvision( Array $results = null ) {
22 if ( $results === null ) {
23 $this->setMwGlobals( 'wgHooks', array() );
24 } else {
25 $this->setMwGlobals( 'wgHooks', array(
26 'PrefixSearchBackend' => array(
27 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
28 $srchres = $results;
29 return false;
30 }
31 ),
32 ) );
33 }
34 }
35
36 public function insertPages() {
37 $this->insertPage( 'Sandbox' );
38 $this->insertPage( 'Bar' );
39 $this->insertPage( 'Example' );
40 $this->insertPage( 'Example Bar' );
41 $this->insertPage( 'Example Foo' );
42 $this->insertPage( 'Example Foo/Bar' );
43 $this->insertPage( 'Example/Baz' );
44
45 $this->insertPage( 'Talk:Sandbox' );
46 $this->insertPage( 'Talk:Example' );
47
48 $this->insertPage( 'User:Example' );
49 }
50
51 public static function provideSearch() {
52 return array(
53 array( array(
54 'Empty string',
55 'query' => '',
56 'results' => array(),
57 ) ),
58 array( array(
59 'Main namespace with title prefix',
60 'query' => 'Ex',
61 'results' => array(
62 'Example',
63 'Example/Baz',
64 'Example Bar',
65 ),
66 ) ),
67 array( array(
68 'Talk namespace prefix',
69 'query' => 'Talk:',
70 'results' => array(
71 'Talk:Example',
72 'Talk:Sandbox',
73 ),
74 ) ),
75 array( array(
76 'User namespace prefix',
77 'query' => 'User:',
78 'results' => array(
79 'User:Example',
80 ),
81 ) ),
82 array( array(
83 'Special namespace prefix',
84 'query' => 'Special:',
85 'results' => array(
86 'Special:ActiveUsers',
87 'Special:AllMessages',
88 'Special:AllMyFiles',
89 ),
90 ) ),
91 array( array(
92 'Special namespace with prefix',
93 'query' => 'Special:Un',
94 'results' => array(
95 'Special:Unblock',
96 'Special:UncategorizedCategories',
97 'Special:UncategorizedFiles',
98 ),
99 ) ),
100 array( array(
101 'Special page name',
102 'query' => 'Special:EditWatchlist',
103 'results' => array(
104 'Special:EditWatchlist',
105 ),
106 ) ),
107 array( array(
108 'Special page subpages',
109 'query' => 'Special:EditWatchlist/',
110 'results' => array(
111 'Special:EditWatchlist/clear',
112 'Special:EditWatchlist/raw',
113 ),
114 ) ),
115 array( array(
116 'Special page subpages with prefix',
117 'query' => 'Special:EditWatchlist/cl',
118 'results' => array(
119 'Special:EditWatchlist/clear',
120 ),
121 ) ),
122 );
123 }
124
125 /**
126 * @dataProvider provideSearch
127 * @covers PrefixSearch::search
128 * @covers PrefixSearch::searchBackend
129 */
130 public function testSearch( Array $case ) {
131 $this->searchProvision( null );
132 $searcher = new StringPrefixSearch;
133 $results = $searcher->search( $case['query'], 3 );
134 $this->assertEquals(
135 $case['results'],
136 $results,
137 $case[0]
138 );
139 }
140
141 public static function provideSearchBackend() {
142 return array(
143 array( array(
144 'Simple case',
145 'provision' => array(
146 'Bar',
147 'Barcelona',
148 'Barbara',
149 ),
150 'query' => 'Bar',
151 'results' => array(
152 'Bar',
153 'Barcelona',
154 'Barbara',
155 ),
156 ) ),
157 array( array(
158 'Exact match not on top (bug 70958)',
159 'provision' => array(
160 'Barcelona',
161 'Bar',
162 'Barbara',
163 ),
164 'query' => 'Bar',
165 'results' => array(
166 'Bar',
167 'Barcelona',
168 'Barbara',
169 ),
170 ) ),
171 array( array(
172 'Exact match missing (bug 70958)',
173 'provision' => array(
174 'Barcelona',
175 'Barbara',
176 'Bart',
177 ),
178 'query' => 'Bar',
179 'results' => array(
180 'Bar',
181 'Barcelona',
182 'Barbara',
183 ),
184 ) ),
185 array( array(
186 'Exact match missing and not existing',
187 'provision' => array(
188 'Exile',
189 'Exist',
190 'External',
191 ),
192 'query' => 'Ex',
193 'results' => array(
194 'Exile',
195 'Exist',
196 'External',
197 ),
198 ) ),
199 );
200 }
201
202 /**
203 * @dataProvider provideSearchBackend
204 * @covers PrefixSearch::searchBackend
205 */
206 public function testSearchBackend( Array $case ) {
207 $this->searchProvision( $case['provision'] );
208 $searcher = new StringPrefixSearch;
209 $results = $searcher->search( $case['query'], 3 );
210 $this->assertEquals(
211 $case['results'],
212 $results,
213 $case[0]
214 );
215 }
216 }