Automatically reset namespace caches when needed
[lhc/web/wiklou.git] / tests / phpunit / includes / PrefixSearchTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group Search
7 * @group Database
8 * @covers PrefixSearch
9 */
10 class PrefixSearchTest extends MediaWikiLangTestCase {
11 const NS_NONCAP = 12346;
12
13 private $originalHandlers;
14
15 public function addDBDataOnce() {
16 if ( !$this->isWikitextNS( NS_MAIN ) ) {
17 // tests are skipped if NS_MAIN is not wikitext
18 return;
19 }
20
21 $this->insertPage( 'Sandbox' );
22 $this->insertPage( 'Bar' );
23 $this->insertPage( 'Example' );
24 $this->insertPage( 'Example Bar' );
25 $this->insertPage( 'Example Foo' );
26 $this->insertPage( 'Example Foo/Bar' );
27 $this->insertPage( 'Example/Baz' );
28 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
29 $this->insertPage( 'Redirect Test' );
30 $this->insertPage( 'Redirect Test Worse Result' );
31 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
32 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
33 $this->insertPage( 'Redirect Test2' );
34 $this->insertPage( 'Redirect Test2 Worse Result' );
35
36 $this->insertPage( 'Talk:Sandbox' );
37 $this->insertPage( 'Talk:Example' );
38
39 $this->insertPage( 'User:Example' );
40
41 $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'Bar' ) );
42 $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'Upper' ) );
43 $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'sandbox' ) );
44 }
45
46 protected function setUp() {
47 parent::setUp();
48
49 if ( !$this->isWikitextNS( NS_MAIN ) ) {
50 $this->markTestSkipped( 'Main namespace does not support wikitext.' );
51 }
52
53 // Avoid special pages from extensions interfering with the tests
54 $this->setMwGlobals( [
55 'wgSpecialPages' => [],
56 'wgHooks' => [],
57 'wgExtraNamespaces' => [ self::NS_NONCAP => 'NonCap' ],
58 'wgCapitalLinkOverrides' => [ self::NS_NONCAP => false ],
59 ] );
60
61 $this->originalHandlers = TestingAccessWrapper::newFromClass( Hooks::class )->handlers;
62 TestingAccessWrapper::newFromClass( Hooks::class )->handlers = [];
63
64 SpecialPageFactory::resetList();
65 }
66
67 public function tearDown() {
68 parent::tearDown();
69
70 TestingAccessWrapper::newFromClass( Hooks::class )->handlers = $this->originalHandlers;
71
72 SpecialPageFactory::resetList();
73 }
74
75 protected function searchProvision( array $results = null ) {
76 if ( $results === null ) {
77 $this->setMwGlobals( 'wgHooks', [] );
78 } else {
79 $this->setMwGlobals( 'wgHooks', [
80 'PrefixSearchBackend' => [
81 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
82 $srchres = $results;
83 return false;
84 }
85 ],
86 ] );
87 }
88 }
89
90 public static function provideSearch() {
91 return [
92 [ [
93 'Empty string',
94 'query' => '',
95 'results' => [],
96 ] ],
97 [ [
98 'Main namespace with title prefix',
99 'query' => 'Ex',
100 'results' => [
101 'Example',
102 'Example/Baz',
103 'Example Bar',
104 ],
105 // Third result when testing offset
106 'offsetresult' => [
107 'Example Foo',
108 ],
109 ] ],
110 [ [
111 'Talk namespace prefix',
112 'query' => 'Talk:',
113 'results' => [
114 'Talk:Example',
115 'Talk:Sandbox',
116 ],
117 ] ],
118 [ [
119 'User namespace prefix',
120 'query' => 'User:',
121 'results' => [
122 'User:Example',
123 ],
124 ] ],
125 [ [
126 'Special namespace prefix',
127 'query' => 'Special:',
128 'results' => [
129 'Special:ActiveUsers',
130 'Special:AllMessages',
131 'Special:AllMyUploads',
132 ],
133 // Third result when testing offset
134 'offsetresult' => [
135 'Special:AllPages',
136 ],
137 ] ],
138 [ [
139 'Special namespace with prefix',
140 'query' => 'Special:Un',
141 'results' => [
142 'Special:Unblock',
143 'Special:UncategorizedCategories',
144 'Special:UncategorizedFiles',
145 ],
146 // Third result when testing offset
147 'offsetresult' => [
148 'Special:UncategorizedPages',
149 ],
150 ] ],
151 [ [
152 'Special page name',
153 'query' => 'Special:EditWatchlist',
154 'results' => [
155 'Special:EditWatchlist',
156 ],
157 ] ],
158 [ [
159 'Special page subpages',
160 'query' => 'Special:EditWatchlist/',
161 'results' => [
162 'Special:EditWatchlist/clear',
163 'Special:EditWatchlist/raw',
164 ],
165 ] ],
166 [ [
167 'Special page subpages with prefix',
168 'query' => 'Special:EditWatchlist/cl',
169 'results' => [
170 'Special:EditWatchlist/clear',
171 ],
172 ] ],
173 [ [
174 'Namespace with case sensitive first letter',
175 'query' => 'NonCap:upper',
176 'results' => []
177 ] ],
178 [ [
179 'Multinamespace search',
180 'query' => 'B',
181 'results' => [
182 'Bar',
183 'NonCap:Bar',
184 ],
185 'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
186 ] ],
187 [ [
188 'Multinamespace search with lowercase first letter',
189 'query' => 'sand',
190 'results' => [
191 'Sandbox',
192 'NonCap:sandbox',
193 ],
194 'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
195 ] ],
196 ];
197 }
198
199 /**
200 * @dataProvider provideSearch
201 * @covers PrefixSearch::search
202 * @covers PrefixSearch::searchBackend
203 */
204 public function testSearch( array $case ) {
205 $this->searchProvision( null );
206
207 $namespaces = $case['namespaces'] ?? [];
208
209 if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
210 // Postgres will sort lexicographically on utf8 code units (" " before "/")
211 sort( $case['results'], SORT_STRING );
212 }
213
214 $searcher = new StringPrefixSearch;
215 $results = $searcher->search( $case['query'], 3, $namespaces );
216 $this->assertEquals(
217 $case['results'],
218 $results,
219 $case[0]
220 );
221 }
222
223 /**
224 * @dataProvider provideSearch
225 * @covers PrefixSearch::search
226 * @covers PrefixSearch::searchBackend
227 */
228 public function testSearchWithOffset( array $case ) {
229 $this->searchProvision( null );
230
231 $namespaces = $case['namespaces'] ?? [];
232
233 $searcher = new StringPrefixSearch;
234 $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
235
236 if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
237 // Postgres will sort lexicographically on utf8 code units (" " before "/")
238 sort( $case['results'], SORT_STRING );
239 }
240
241 // We don't expect the first result when offsetting
242 array_shift( $case['results'] );
243 // And sometimes we expect a different last result
244 $expected = isset( $case['offsetresult'] ) ?
245 array_merge( $case['results'], $case['offsetresult'] ) :
246 $case['results'];
247
248 $this->assertEquals(
249 $expected,
250 $results,
251 $case[0]
252 );
253 }
254
255 public static function provideSearchBackend() {
256 return [
257 [ [
258 'Simple case',
259 'provision' => [
260 'Bar',
261 'Barcelona',
262 'Barbara',
263 ],
264 'query' => 'Bar',
265 'results' => [
266 'Bar',
267 'Barcelona',
268 'Barbara',
269 ],
270 ] ],
271 [ [
272 'Exact match not on top (T72958)',
273 'provision' => [
274 'Barcelona',
275 'Bar',
276 'Barbara',
277 ],
278 'query' => 'Bar',
279 'results' => [
280 'Bar',
281 'Barcelona',
282 'Barbara',
283 ],
284 ] ],
285 [ [
286 'Exact match missing (T72958)',
287 'provision' => [
288 'Barcelona',
289 'Barbara',
290 'Bart',
291 ],
292 'query' => 'Bar',
293 'results' => [
294 'Bar',
295 'Barcelona',
296 'Barbara',
297 ],
298 ] ],
299 [ [
300 'Exact match missing and not existing',
301 'provision' => [
302 'Exile',
303 'Exist',
304 'External',
305 ],
306 'query' => 'Ex',
307 'results' => [
308 'Exile',
309 'Exist',
310 'External',
311 ],
312 ] ],
313 [ [
314 "Exact match shouldn't override already found match if " .
315 "exact is redirect and found isn't",
316 'provision' => [
317 // Target of the exact match is low in the list
318 'Redirect Test Worse Result',
319 'Redirect Test',
320 ],
321 'query' => 'redirect test',
322 'results' => [
323 // Redirect target is pulled up and exact match isn't added
324 'Redirect Test',
325 'Redirect Test Worse Result',
326 ],
327 ] ],
328 [ [
329 "Exact match shouldn't override already found match if " .
330 "both exact match and found match are redirect",
331 'provision' => [
332 // Another redirect to the same target as the exact match
333 // is low in the list
334 'Redirect Test2 Worse Result',
335 'Redirect test2',
336 ],
337 'query' => 'redirect TEST2',
338 'results' => [
339 // Found redirect is pulled to the top and exact match isn't
340 // added
341 'Redirect test2',
342 'Redirect Test2 Worse Result',
343 ],
344 ] ],
345 [ [
346 "Exact match should override any already found matches that " .
347 "are redirects to it",
348 'provision' => [
349 // Another redirect to the same target as the exact match
350 // is low in the list
351 'Redirect Test Worse Result',
352 'Redirect test',
353 ],
354 'query' => 'Redirect Test',
355 'results' => [
356 // Found redirect is pulled to the top and exact match isn't
357 // added
358 'Redirect Test',
359 'Redirect Test Worse Result',
360 ],
361 ] ],
362 ];
363 }
364
365 /**
366 * @dataProvider provideSearchBackend
367 * @covers PrefixSearch::searchBackend
368 */
369 public function testSearchBackend( array $case ) {
370 $this->searchProvision( $case['provision'] );
371 $searcher = new StringPrefixSearch;
372 $results = $searcher->search( $case['query'], 3 );
373 $this->assertEquals(
374 $case['results'],
375 $results,
376 $case[0]
377 );
378 }
379 }