Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryBasicTest.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 6, 2013
6 *
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 require_once 'ApiQueryTestBase.php';
28
29 /**
30 * These tests validate basic functionality of the api query module
31 *
32 * @group API
33 * @group Database
34 * @group medium
35 * @covers ApiQuery
36 */
37 class ApiQueryBasicTest extends ApiQueryTestBase {
38 /**
39 * Create a set of pages. These must not change, otherwise the tests might give wrong results.
40 * @see MediaWikiTestCase::addDBData()
41 */
42 function addDBData() {
43 try {
44 if ( Title::newFromText( 'AQBT-All' )->exists() ) {
45 return;
46 }
47
48 // Ordering is important, as it will be returned in the same order as stored in the index
49 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
50 $this->editPage( 'AQBT-Categories', '[[Category:AQBT-Cat]]' );
51 $this->editPage( 'AQBT-Links', '[[AQBT-All]] [[AQBT-Categories]] [[AQBT-Templates]]' );
52 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
53 $this->editPage( 'AQBT-T', 'Content', '', NS_TEMPLATE );
54
55 // Refresh due to the bug with listing transclusions as links if they don't exist
56 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
57 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
58 } catch ( Exception $e ) {
59 $this->exceptionFromAddDBData = $e;
60 }
61 }
62
63 private static $links = array(
64 array( 'prop' => 'links', 'titles' => 'AQBT-All' ),
65 array( 'pages' => array(
66 '1' => array(
67 'pageid' => 1,
68 'ns' => 0,
69 'title' => 'AQBT-All',
70 'links' => array(
71 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
72 )
73 )
74 ) )
75 );
76
77 private static $templates = array(
78 array( 'prop' => 'templates', 'titles' => 'AQBT-All' ),
79 array( 'pages' => array(
80 '1' => array(
81 'pageid' => 1,
82 'ns' => 0,
83 'title' => 'AQBT-All',
84 'templates' => array(
85 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
86 )
87 )
88 ) )
89 );
90
91 private static $categories = array(
92 array( 'prop' => 'categories', 'titles' => 'AQBT-All' ),
93 array( 'pages' => array(
94 '1' => array(
95 'pageid' => 1,
96 'ns' => 0,
97 'title' => 'AQBT-All',
98 'categories' => array(
99 array( 'ns' => 14, 'title' => 'Category:AQBT-Cat' ),
100 )
101 )
102 ) )
103 );
104
105 private static $allpages = array(
106 array( 'list' => 'allpages', 'apprefix' => 'AQBT-' ),
107 array( 'allpages' => array(
108 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
109 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
110 array( 'pageid' => 3, 'ns' => 0, 'title' => 'AQBT-Links' ),
111 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
112 ) )
113 );
114
115 private static $alllinks = array(
116 array( 'list' => 'alllinks', 'alprefix' => 'AQBT-' ),
117 array( 'alllinks' => array(
118 array( 'ns' => 0, 'title' => 'AQBT-All' ),
119 array( 'ns' => 0, 'title' => 'AQBT-Categories' ),
120 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
121 array( 'ns' => 0, 'title' => 'AQBT-Templates' ),
122 ) )
123 );
124
125 private static $alltransclusions = array(
126 array( 'list' => 'alltransclusions', 'atprefix' => 'AQBT-' ),
127 array( 'alltransclusions' => array(
128 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
129 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
130 ) )
131 );
132
133 private static $allcategories = array(
134 array( 'list' => 'allcategories', 'acprefix' => 'AQBT-' ),
135 array( 'allcategories' => array(
136 array( '*' => 'AQBT-Cat' ),
137 ) )
138 );
139
140 private static $backlinks = array(
141 array( 'list' => 'backlinks', 'bltitle' => 'AQBT-Links' ),
142 array( 'backlinks' => array(
143 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
144 ) )
145 );
146
147 private static $embeddedin = array(
148 array( 'list' => 'embeddedin', 'eititle' => 'Template:AQBT-T' ),
149 array( 'embeddedin' => array(
150 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
151 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
152 ) )
153 );
154
155 private static $categorymembers = array(
156 array( 'list' => 'categorymembers', 'cmtitle' => 'Category:AQBT-Cat' ),
157 array( 'categorymembers' => array(
158 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
159 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
160 ) )
161 );
162
163 private static $generatorAllpages = array(
164 array( 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ),
165 array( 'pages' => array(
166 '1' => array(
167 'pageid' => 1,
168 'ns' => 0,
169 'title' => 'AQBT-All' ),
170 '2' => array(
171 'pageid' => 2,
172 'ns' => 0,
173 'title' => 'AQBT-Categories' ),
174 '3' => array(
175 'pageid' => 3,
176 'ns' => 0,
177 'title' => 'AQBT-Links' ),
178 '4' => array(
179 'pageid' => 4,
180 'ns' => 0,
181 'title' => 'AQBT-Templates' ),
182 ) )
183 );
184
185 private static $generatorLinks = array(
186 array( 'generator' => 'links', 'titles' => 'AQBT-Links' ),
187 array( 'pages' => array(
188 '1' => array(
189 'pageid' => 1,
190 'ns' => 0,
191 'title' => 'AQBT-All' ),
192 '2' => array(
193 'pageid' => 2,
194 'ns' => 0,
195 'title' => 'AQBT-Categories' ),
196 '4' => array(
197 'pageid' => 4,
198 'ns' => 0,
199 'title' => 'AQBT-Templates' ),
200 ) )
201 );
202
203 private static $generatorLinksPropLinks = array(
204 array( 'prop' => 'links' ),
205 array( 'pages' => array(
206 '1' => array( 'links' => array(
207 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
208 ) )
209 ) )
210 );
211
212 private static $generatorLinksPropTemplates = array(
213 array( 'prop' => 'templates' ),
214 array( 'pages' => array(
215 '1' => array( 'templates' => array(
216 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
217 '4' => array( 'templates' => array(
218 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
219 ) )
220 );
221
222 /**
223 * Test basic props
224 */
225 public function testProps() {
226 $this->check( self::$links );
227 $this->check( self::$templates );
228 $this->check( self::$categories );
229 }
230
231 /**
232 * Test basic lists
233 */
234 public function testLists() {
235 $this->check( self::$allpages );
236 $this->check( self::$alllinks );
237 $this->check( self::$alltransclusions );
238 // This test is temporarily disabled until a sqlite bug is fixed
239 // $this->check( self::$allcategories );
240 $this->check( self::$backlinks );
241 $this->check( self::$embeddedin );
242 $this->check( self::$categorymembers );
243 }
244
245 /**
246 * Test basic lists
247 */
248 public function testAllTogether() {
249
250 // All props together
251 $this->check( $this->merge(
252 self::$links,
253 self::$templates,
254 self::$categories
255 ) );
256
257 // All lists together
258 $this->check( $this->merge(
259 self::$allpages,
260 self::$alllinks,
261 self::$alltransclusions,
262 // This test is temporarily disabled until a sqlite bug is fixed
263 // self::$allcategories,
264 self::$backlinks,
265 self::$embeddedin,
266 self::$categorymembers
267 ) );
268
269 // All props+lists together
270 $this->check( $this->merge(
271 self::$links,
272 self::$templates,
273 self::$categories,
274 self::$allpages,
275 self::$alllinks,
276 self::$alltransclusions,
277 // This test is temporarily disabled until a sqlite bug is fixed
278 // self::$allcategories,
279 self::$backlinks,
280 self::$embeddedin,
281 self::$categorymembers
282 ) );
283 }
284
285 /**
286 * Test basic lists
287 */
288 public function testGenerator() {
289 // generator=allpages
290 $this->check( self::$generatorAllpages );
291 // generator=allpages & list=allpages
292 $this->check( $this->merge(
293 self::$generatorAllpages,
294 self::$allpages ) );
295 // generator=links
296 $this->check( self::$generatorLinks );
297 // generator=links & prop=links
298 $this->check( $this->merge(
299 self::$generatorLinks,
300 self::$generatorLinksPropLinks ) );
301 // generator=links & prop=templates
302 $this->check( $this->merge(
303 self::$generatorLinks,
304 self::$generatorLinksPropTemplates ) );
305 // generator=links & prop=links|templates
306 $this->check( $this->merge(
307 self::$generatorLinks,
308 self::$generatorLinksPropLinks,
309 self::$generatorLinksPropTemplates ) );
310 // generator=links & prop=links|templates & list=allpages|...
311 $this->check( $this->merge(
312 self::$generatorLinks,
313 self::$generatorLinksPropLinks,
314 self::$generatorLinksPropTemplates,
315 self::$allpages,
316 self::$alllinks,
317 self::$alltransclusions,
318 // This test is temporarily disabled until a sqlite bug is fixed
319 // self::$allcategories,
320 self::$backlinks,
321 self::$embeddedin,
322 self::$categorymembers ) );
323 }
324
325 /**
326 * Test bug 51821
327 */
328 public function testGeneratorRedirects() {
329 $this->editPage( 'AQBT-Target', 'test' );
330 $this->editPage( 'AQBT-Redir', '#REDIRECT [[AQBT-Target]]' );
331 $this->check( array(
332 array( 'generator' => 'backlinks', 'gbltitle' => 'AQBT-Target', 'redirects' => '1' ),
333 array(
334 'redirects' => array(
335 array(
336 'from' => 'AQBT-Redir',
337 'to' => 'AQBT-Target',
338 )
339 ),
340 'pages' => array(
341 '6' => array(
342 'pageid' => 6,
343 'ns' => 0,
344 'title' => 'AQBT-Target',
345 )
346 ),
347 )
348 ) );
349 }
350
351 /**
352 * Recursively merges the expected values in the $item into the $all
353 */
354 private function mergeExpected( &$all, $item ) {
355 foreach ( $item as $k => $v ) {
356 if ( array_key_exists( $k, $all ) ) {
357 if ( is_array( $all[$k] ) ) {
358 $this->mergeExpected( $all[$k], $v );
359 } else {
360 $this->assertEquals( $all[$k], $v );
361 }
362 } else {
363 $all[$k] = $v;
364 }
365 }
366 }
367
368 /**
369 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
370 * @param $expected array expected values
371 * @param $result array returned values
372 */
373 private function assertQueryResults( $expected, $result ) {
374 reset( $expected );
375 reset( $result );
376 while ( true ) {
377 $e = each( $expected );
378 $r = each( $result );
379 // If either of the arrays is shorter, abort. If both are done, success.
380 $this->assertEquals( (bool)$e, (bool)$r );
381 if ( !$e ) {
382 break; // done
383 }
384 // continue only if keys are identical or both keys are numeric
385 $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
386 // don't compare pageids
387 if ( $e['key'] !== 'pageid' ) {
388 // If values are arrays, compare recursively, otherwise compare with ===
389 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
390 $this->assertQueryResults( $e['value'], $r['value'] );
391 } else {
392 $this->assertEquals( $e['value'], $r['value'] );
393 }
394 }
395 }
396 }
397 }