Update OOjs UI to v0.2.4
[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 $this->insertPage( 'Redirect test', '#REDIRECT [[Redirect Test]]' );
45 $this->insertPage( 'Redirect Test');
46 $this->insertPage( 'Redirect Test Worse Result');
47 $this->insertPage( 'Redirect test2', '#REDIRECT [[Redirect Test2]]' );
48 $this->insertPage( 'Redirect TEST2', '#REDIRECT [[Redirect Test2]]' );
49 $this->insertPage( 'Redirect Test2');
50 $this->insertPage( 'Redirect Test2 Worse Result');
51
52 $this->insertPage( 'Talk:Sandbox' );
53 $this->insertPage( 'Talk:Example' );
54
55 $this->insertPage( 'User:Example' );
56 }
57
58 public static function provideSearch() {
59 return array(
60 array( array(
61 'Empty string',
62 'query' => '',
63 'results' => array(),
64 ) ),
65 array( array(
66 'Main namespace with title prefix',
67 'query' => 'Ex',
68 'results' => array(
69 'Example',
70 'Example/Baz',
71 'Example Bar',
72 ),
73 ) ),
74 array( array(
75 'Talk namespace prefix',
76 'query' => 'Talk:',
77 'results' => array(
78 'Talk:Example',
79 'Talk:Sandbox',
80 ),
81 ) ),
82 array( array(
83 'User namespace prefix',
84 'query' => 'User:',
85 'results' => array(
86 'User:Example',
87 ),
88 ) ),
89 array( array(
90 'Special namespace prefix',
91 'query' => 'Special:',
92 'results' => array(
93 'Special:ActiveUsers',
94 'Special:AllMessages',
95 'Special:AllMyFiles',
96 ),
97 ) ),
98 array( array(
99 'Special namespace with prefix',
100 'query' => 'Special:Un',
101 'results' => array(
102 'Special:Unblock',
103 'Special:UncategorizedCategories',
104 'Special:UncategorizedFiles',
105 ),
106 ) ),
107 array( array(
108 'Special page name',
109 'query' => 'Special:EditWatchlist',
110 'results' => array(
111 'Special:EditWatchlist',
112 ),
113 ) ),
114 array( array(
115 'Special page subpages',
116 'query' => 'Special:EditWatchlist/',
117 'results' => array(
118 'Special:EditWatchlist/clear',
119 'Special:EditWatchlist/raw',
120 ),
121 ) ),
122 array( array(
123 'Special page subpages with prefix',
124 'query' => 'Special:EditWatchlist/cl',
125 'results' => array(
126 'Special:EditWatchlist/clear',
127 ),
128 ) ),
129 );
130 }
131
132 /**
133 * @dataProvider provideSearch
134 * @covers PrefixSearch::search
135 * @covers PrefixSearch::searchBackend
136 */
137 public function testSearch( Array $case ) {
138 $this->searchProvision( null );
139 $searcher = new StringPrefixSearch;
140 $results = $searcher->search( $case['query'], 3 );
141 $this->assertEquals(
142 $case['results'],
143 $results,
144 $case[0]
145 );
146 }
147
148 public static function provideSearchBackend() {
149 return array(
150 array( array(
151 'Simple case',
152 'provision' => array(
153 'Bar',
154 'Barcelona',
155 'Barbara',
156 ),
157 'query' => 'Bar',
158 'results' => array(
159 'Bar',
160 'Barcelona',
161 'Barbara',
162 ),
163 ) ),
164 array( array(
165 'Exact match not on top (bug 70958)',
166 'provision' => array(
167 'Barcelona',
168 'Bar',
169 'Barbara',
170 ),
171 'query' => 'Bar',
172 'results' => array(
173 'Bar',
174 'Barcelona',
175 'Barbara',
176 ),
177 ) ),
178 array( array(
179 'Exact match missing (bug 70958)',
180 'provision' => array(
181 'Barcelona',
182 'Barbara',
183 'Bart',
184 ),
185 'query' => 'Bar',
186 'results' => array(
187 'Bar',
188 'Barcelona',
189 'Barbara',
190 ),
191 ) ),
192 array( array(
193 'Exact match missing and not existing',
194 'provision' => array(
195 'Exile',
196 'Exist',
197 'External',
198 ),
199 'query' => 'Ex',
200 'results' => array(
201 'Exile',
202 'Exist',
203 'External',
204 ),
205 ) ),
206 array( array(
207 "Exact match shouldn't override already found match if " .
208 "exact is redirect and found isn't",
209 'provision' => array(
210 // Target of the exact match is low in the list
211 'Redirect Test Worse Result',
212 'Redirect Test',
213 ),
214 'query' => 'redirect test',
215 'results' => array(
216 // Redirect target is pulled up and exact match isn't added
217 'Redirect Test',
218 'Redirect Test Worse Result',
219 ),
220 ) ),
221 array( array(
222 "Exact match shouldn't override already found match if " .
223 "both exact match and found match are redirect",
224 'provision' => array(
225 // Another redirect to the same target as the exact match
226 // is low in the list
227 'Redirect Test2 Worse Result',
228 'Redirect test2',
229 ),
230 'query' => 'redirect TEST2',
231 'results' => array(
232 // Found redirect is pulled to the top and exact match isn't
233 // added
234 'Redirect test2',
235 'Redirect Test2 Worse Result',
236 ),
237 ) ),
238 array( array(
239 "Exact match should override any already found matches that " .
240 "are redirects to it",
241 'provision' => array(
242 // Another redirect to the same target as the exact match
243 // is low in the list
244 'Redirect Test Worse Result',
245 'Redirect test',
246 ),
247 'query' => 'Redirect Test',
248 'results' => array(
249 // Found redirect is pulled to the top and exact match isn't
250 // added
251 'Redirect Test',
252 'Redirect Test Worse Result',
253 ),
254 ) ),
255 );
256 }
257
258 /**
259 * @dataProvider provideSearchBackend
260 * @covers PrefixSearch::searchBackend
261 */
262 public function testSearchBackend( Array $case ) {
263 $this->searchProvision( $case['provision'] );
264 $searcher = new StringPrefixSearch;
265 $results = $searcher->search( $case['query'], 3 );
266 $this->assertEquals(
267 $case['results'],
268 $results,
269 $case[0]
270 );
271 }
272 }