MediaWiki REL1_31
PrefixSearchTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
11 const NS_NONCAP = 12346;
12
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 interferring 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 // Clear caches so that our new namespace appears
65 MWNamespace::clearCaches();
66 Language::factory( 'en' )->resetNamespaces();
67
69 }
70
71 public function tearDown() {
72 MWNamespace::clearCaches();
73 Language::factory( 'en' )->resetNamespaces();
74
75 parent::tearDown();
76
77 TestingAccessWrapper::newFromClass( Hooks::class )->handlers = $this->originalHandlers;
78
80 }
81
82 protected function searchProvision( array $results = null ) {
83 if ( $results === null ) {
84 $this->setMwGlobals( 'wgHooks', [] );
85 } else {
86 $this->setMwGlobals( 'wgHooks', [
87 'PrefixSearchBackend' => [
88 function ( $namespaces, $search, $limit, &$srchres ) use ( $results ) {
89 $srchres = $results;
90 return false;
91 }
92 ],
93 ] );
94 }
95 }
96
97 public static function provideSearch() {
98 return [
99 [ [
100 'Empty string',
101 'query' => '',
102 'results' => [],
103 ] ],
104 [ [
105 'Main namespace with title prefix',
106 'query' => 'Ex',
107 'results' => [
108 'Example',
109 'Example/Baz',
110 'Example Bar',
111 ],
112 // Third result when testing offset
113 'offsetresult' => [
114 'Example Foo',
115 ],
116 ] ],
117 [ [
118 'Talk namespace prefix',
119 'query' => 'Talk:',
120 'results' => [
121 'Talk:Example',
122 'Talk:Sandbox',
123 ],
124 ] ],
125 [ [
126 'User namespace prefix',
127 'query' => 'User:',
128 'results' => [
129 'User:Example',
130 ],
131 ] ],
132 [ [
133 'Special namespace prefix',
134 'query' => 'Special:',
135 'results' => [
136 'Special:ActiveUsers',
137 'Special:AllMessages',
138 'Special:AllMyUploads',
139 ],
140 // Third result when testing offset
141 'offsetresult' => [
142 'Special:AllPages',
143 ],
144 ] ],
145 [ [
146 'Special namespace with prefix',
147 'query' => 'Special:Un',
148 'results' => [
149 'Special:Unblock',
150 'Special:UncategorizedCategories',
151 'Special:UncategorizedFiles',
152 ],
153 // Third result when testing offset
154 'offsetresult' => [
155 'Special:UncategorizedPages',
156 ],
157 ] ],
158 [ [
159 'Special page name',
160 'query' => 'Special:EditWatchlist',
161 'results' => [
162 'Special:EditWatchlist',
163 ],
164 ] ],
165 [ [
166 'Special page subpages',
167 'query' => 'Special:EditWatchlist/',
168 'results' => [
169 'Special:EditWatchlist/clear',
170 'Special:EditWatchlist/raw',
171 ],
172 ] ],
173 [ [
174 'Special page subpages with prefix',
175 'query' => 'Special:EditWatchlist/cl',
176 'results' => [
177 'Special:EditWatchlist/clear',
178 ],
179 ] ],
180 [ [
181 'Namespace with case sensitive first letter',
182 'query' => 'NonCap:upper',
183 'results' => []
184 ] ],
185 [ [
186 'Multinamespace search',
187 'query' => 'B',
188 'results' => [
189 'Bar',
190 'NonCap:Bar',
191 ],
192 'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
193 ] ],
194 [ [
195 'Multinamespace search with lowercase first letter',
196 'query' => 'sand',
197 'results' => [
198 'Sandbox',
199 'NonCap:sandbox',
200 ],
201 'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
202 ] ],
203 ];
204 }
205
211 public function testSearch( array $case ) {
212 $this->searchProvision( null );
213
214 $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
215
216 if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
217 // Postgres will sort lexicographically on utf8 code units (" " before "/")
218 sort( $case['results'], SORT_STRING );
219 }
220
221 $searcher = new StringPrefixSearch;
222 $results = $searcher->search( $case['query'], 3, $namespaces );
223 $this->assertEquals(
224 $case['results'],
225 $results,
226 $case[0]
227 );
228 }
229
235 public function testSearchWithOffset( array $case ) {
236 $this->searchProvision( null );
237
238 $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
239
240 $searcher = new StringPrefixSearch;
241 $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
242
243 if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
244 // Postgres will sort lexicographically on utf8 code units (" " before "/")
245 sort( $case['results'], SORT_STRING );
246 }
247
248 // We don't expect the first result when offsetting
249 array_shift( $case['results'] );
250 // And sometimes we expect a different last result
251 $expected = isset( $case['offsetresult'] ) ?
252 array_merge( $case['results'], $case['offsetresult'] ) :
253 $case['results'];
254
255 $this->assertEquals(
256 $expected,
257 $results,
258 $case[0]
259 );
260 }
261
262 public static function provideSearchBackend() {
263 return [
264 [ [
265 'Simple case',
266 'provision' => [
267 'Bar',
268 'Barcelona',
269 'Barbara',
270 ],
271 'query' => 'Bar',
272 'results' => [
273 'Bar',
274 'Barcelona',
275 'Barbara',
276 ],
277 ] ],
278 [ [
279 'Exact match not on top (T72958)',
280 'provision' => [
281 'Barcelona',
282 'Bar',
283 'Barbara',
284 ],
285 'query' => 'Bar',
286 'results' => [
287 'Bar',
288 'Barcelona',
289 'Barbara',
290 ],
291 ] ],
292 [ [
293 'Exact match missing (T72958)',
294 'provision' => [
295 'Barcelona',
296 'Barbara',
297 'Bart',
298 ],
299 'query' => 'Bar',
300 'results' => [
301 'Bar',
302 'Barcelona',
303 'Barbara',
304 ],
305 ] ],
306 [ [
307 'Exact match missing and not existing',
308 'provision' => [
309 'Exile',
310 'Exist',
311 'External',
312 ],
313 'query' => 'Ex',
314 'results' => [
315 'Exile',
316 'Exist',
317 'External',
318 ],
319 ] ],
320 [ [
321 "Exact match shouldn't override already found match if " .
322 "exact is redirect and found isn't",
323 'provision' => [
324 // Target of the exact match is low in the list
325 'Redirect Test Worse Result',
326 'Redirect Test',
327 ],
328 'query' => 'redirect test',
329 'results' => [
330 // Redirect target is pulled up and exact match isn't added
331 'Redirect Test',
332 'Redirect Test Worse Result',
333 ],
334 ] ],
335 [ [
336 "Exact match shouldn't override already found match if " .
337 "both exact match and found match are redirect",
338 'provision' => [
339 // Another redirect to the same target as the exact match
340 // is low in the list
341 'Redirect Test2 Worse Result',
342 'Redirect test2',
343 ],
344 'query' => 'redirect TEST2',
345 'results' => [
346 // Found redirect is pulled to the top and exact match isn't
347 // added
348 'Redirect test2',
349 'Redirect Test2 Worse Result',
350 ],
351 ] ],
352 [ [
353 "Exact match should override any already found matches that " .
354 "are redirects to it",
355 'provision' => [
356 // Another redirect to the same target as the exact match
357 // is low in the list
358 'Redirect Test Worse Result',
359 'Redirect test',
360 ],
361 'query' => 'Redirect Test',
362 'results' => [
363 // Found redirect is pulled to the top and exact match isn't
364 // added
365 'Redirect Test',
366 'Redirect Test Worse Result',
367 ],
368 ] ],
369 ];
370 }
371
376 public function testSearchBackend( array $case ) {
377 $this->searchProvision( $case['provision'] );
378 $searcher = new StringPrefixSearch;
379 $results = $searcher->search( $case['query'], 3 );
380 $this->assertEquals(
381 $case['results'],
382 $results,
383 $case[0]
384 );
385 }
386}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Base class that store and restore the Language objects.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null)
Insert a new page.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
isWikitextNS( $ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
Search Database PrefixSearch.
testSearch(array $case)
provideSearch PrefixSearch::search PrefixSearch::searchBackend
testSearchBackend(array $case)
provideSearchBackend PrefixSearch::searchBackend
searchProvision(array $results=null)
testSearchWithOffset(array $case)
provideSearch PrefixSearch::search PrefixSearch::searchBackend
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
static resetList()
Reset the internal list of special pages.
Performs prefix search, returning strings.
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:934
const NS_MAIN
Definition Defines.php:74
const DB_REPLICA
Definition defines.php:25