Merge "mime.types: allow bzip2 upload"
[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 public function addDBData() {
9 $this->insertPage( 'Sandbox' );
10 $this->insertPage( 'Bar' );
11 $this->insertPage( 'Example' );
12 $this->insertPage( 'Example Bar' );
13 $this->insertPage( 'Example Foo' );
14 $this->insertPage( 'Example Foo/Bar' );
15 $this->insertPage( 'Example/Baz' );
16 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
17 $this->insertPage( 'Redirect Test' );
18 $this->insertPage( 'Redirect Test Worse Result' );
19 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
20 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
21 $this->insertPage( 'Redirect Test2' );
22 $this->insertPage( 'Redirect Test2 Worse Result' );
23
24 $this->insertPage( 'Talk:Sandbox' );
25 $this->insertPage( 'Talk:Example' );
26
27 $this->insertPage( 'User:Example' );
28 }
29
30 protected function setUp() {
31 parent::setUp();
32
33 if ( !$this->isWikitextNS( NS_MAIN ) ) {
34 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
35 }
36
37 // Avoid special pages from extensions interferring with the tests
38 $this->setMwGlobals( 'wgSpecialPages', array() );
39 }
40
41 protected function searchProvision( Array $results = null ) {
42 if ( $results === null ) {
43 $this->setMwGlobals( 'wgHooks', array() );
44 } else {
45 $this->setMwGlobals( 'wgHooks', array(
46 'PrefixSearchBackend' => array(
47 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
48 $srchres = $results;
49 return false;
50 }
51 ),
52 ) );
53 }
54 }
55
56 public static function provideSearch() {
57 return array(
58 array( array(
59 'Empty string',
60 'query' => '',
61 'results' => array(),
62 ) ),
63 array( array(
64 'Main namespace with title prefix',
65 'query' => 'Ex',
66 'results' => array(
67 'Example',
68 'Example/Baz',
69 'Example Bar',
70 ),
71 // Third result when testing offset
72 'offsetresult' => array(
73 'Example Foo',
74 ),
75 ) ),
76 array( array(
77 'Talk namespace prefix',
78 'query' => 'Talk:',
79 'results' => array(
80 'Talk:Example',
81 'Talk:Sandbox',
82 ),
83 ) ),
84 array( array(
85 'User namespace prefix',
86 'query' => 'User:',
87 'results' => array(
88 'User:Example',
89 ),
90 ) ),
91 array( array(
92 'Special namespace prefix',
93 'query' => 'Special:',
94 'results' => array(
95 'Special:ActiveUsers',
96 'Special:AllMessages',
97 'Special:AllMyFiles',
98 ),
99 // Third result when testing offset
100 'offsetresult' => array(
101 'Special:AllMyUploads',
102 ),
103 ) ),
104 array( array(
105 'Special namespace with prefix',
106 'query' => 'Special:Un',
107 'results' => array(
108 'Special:Unblock',
109 'Special:UncategorizedCategories',
110 'Special:UncategorizedFiles',
111 ),
112 // Third result when testing offset
113 'offsetresult' => array(
114 'Special:UncategorizedImages',
115 ),
116 ) ),
117 array( array(
118 'Special page name',
119 'query' => 'Special:EditWatchlist',
120 'results' => array(
121 'Special:EditWatchlist',
122 ),
123 ) ),
124 array( array(
125 'Special page subpages',
126 'query' => 'Special:EditWatchlist/',
127 'results' => array(
128 'Special:EditWatchlist/clear',
129 'Special:EditWatchlist/raw',
130 ),
131 ) ),
132 array( array(
133 'Special page subpages with prefix',
134 'query' => 'Special:EditWatchlist/cl',
135 'results' => array(
136 'Special:EditWatchlist/clear',
137 ),
138 ) ),
139 );
140 }
141
142 /**
143 * @dataProvider provideSearch
144 * @covers PrefixSearch::search
145 * @covers PrefixSearch::searchBackend
146 */
147 public function testSearch( Array $case ) {
148 $this->searchProvision( null );
149 $searcher = new StringPrefixSearch;
150 $results = $searcher->search( $case['query'], 3 );
151 $this->assertEquals(
152 $case['results'],
153 $results,
154 $case[0]
155 );
156 }
157
158 /**
159 * @dataProvider provideSearch
160 * @covers PrefixSearch::search
161 * @covers PrefixSearch::searchBackend
162 */
163 public function testSearchWithOffset( Array $case ) {
164 $this->searchProvision( null );
165 $searcher = new StringPrefixSearch;
166 $results = $searcher->search( $case['query'], 3, array(), 1 );
167
168 // We don't expect the first result when offsetting
169 array_shift( $case['results'] );
170 // And sometimes we expect a different last result
171 $expected = isset( $case['offsetresult'] ) ?
172 array_merge( $case['results'], $case['offsetresult'] ) :
173 $case['results'];
174
175 $this->assertEquals(
176 $expected,
177 $results,
178 $case[0]
179 );
180 }
181
182 public static function provideSearchBackend() {
183 return array(
184 array( array(
185 'Simple case',
186 'provision' => array(
187 'Bar',
188 'Barcelona',
189 'Barbara',
190 ),
191 'query' => 'Bar',
192 'results' => array(
193 'Bar',
194 'Barcelona',
195 'Barbara',
196 ),
197 ) ),
198 array( array(
199 'Exact match not on top (bug 70958)',
200 'provision' => array(
201 'Barcelona',
202 'Bar',
203 'Barbara',
204 ),
205 'query' => 'Bar',
206 'results' => array(
207 'Bar',
208 'Barcelona',
209 'Barbara',
210 ),
211 ) ),
212 array( array(
213 'Exact match missing (bug 70958)',
214 'provision' => array(
215 'Barcelona',
216 'Barbara',
217 'Bart',
218 ),
219 'query' => 'Bar',
220 'results' => array(
221 'Bar',
222 'Barcelona',
223 'Barbara',
224 ),
225 ) ),
226 array( array(
227 'Exact match missing and not existing',
228 'provision' => array(
229 'Exile',
230 'Exist',
231 'External',
232 ),
233 'query' => 'Ex',
234 'results' => array(
235 'Exile',
236 'Exist',
237 'External',
238 ),
239 ) ),
240 array( array(
241 "Exact match shouldn't override already found match if " .
242 "exact is redirect and found isn't",
243 'provision' => array(
244 // Target of the exact match is low in the list
245 'Redirect Test Worse Result',
246 'Redirect Test',
247 ),
248 'query' => 'redirect test',
249 'results' => array(
250 // Redirect target is pulled up and exact match isn't added
251 'Redirect Test',
252 'Redirect Test Worse Result',
253 ),
254 ) ),
255 array( array(
256 "Exact match shouldn't override already found match if " .
257 "both exact match and found match are redirect",
258 'provision' => array(
259 // Another redirect to the same target as the exact match
260 // is low in the list
261 'Redirect Test2 Worse Result',
262 'Redirect test2',
263 ),
264 'query' => 'redirect TEST2',
265 'results' => array(
266 // Found redirect is pulled to the top and exact match isn't
267 // added
268 'Redirect test2',
269 'Redirect Test2 Worse Result',
270 ),
271 ) ),
272 array( array(
273 "Exact match should override any already found matches that " .
274 "are redirects to it",
275 'provision' => array(
276 // Another redirect to the same target as the exact match
277 // is low in the list
278 'Redirect Test Worse Result',
279 'Redirect test',
280 ),
281 'query' => 'Redirect Test',
282 'results' => array(
283 // Found redirect is pulled to the top and exact match isn't
284 // added
285 'Redirect Test',
286 'Redirect Test Worse Result',
287 ),
288 ) ),
289 );
290 }
291
292 /**
293 * @dataProvider provideSearchBackend
294 * @covers PrefixSearch::searchBackend
295 */
296 public function testSearchBackend( Array $case ) {
297 $this->searchProvision( $case['provision'] );
298 $searcher = new StringPrefixSearch;
299 $results = $searcher->search( $case['query'], 3 );
300 $this->assertEquals(
301 $case['results'],
302 $results,
303 $case[0]
304 );
305 }
306 }