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