5 * Created on Feb 6, 2013
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
27 require_once 'ApiQueryTestBase.php';
30 * These tests validate basic functionality of the api query module
37 class ApiQueryBasicTest
extends ApiQueryTestBase
{
39 * Create a set of pages. These must not change, otherwise the tests might give wrong results.
40 * @see MediaWikiTestCase::addDBData()
42 function addDBData() {
44 if ( Title
::newFromText( 'AQBT-All' )->exists() ) {
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
);
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;
63 private static $links = array(
64 array( 'prop' => 'links', 'titles' => 'AQBT-All' ),
65 array( 'pages' => array(
69 'title' => 'AQBT-All',
71 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
77 private static $templates = array(
78 array( 'prop' => 'templates', 'titles' => 'AQBT-All' ),
79 array( 'pages' => array(
83 'title' => 'AQBT-All',
85 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
91 private static $categories = array(
92 array( 'prop' => 'categories', 'titles' => 'AQBT-All' ),
93 array( 'pages' => array(
97 'title' => 'AQBT-All',
98 'categories' => array(
99 array( 'ns' => 14, 'title' => 'Category:AQBT-Cat' ),
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' ),
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' ),
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' ),
133 private static $allcategories = array(
134 array( 'list' => 'allcategories', 'acprefix' => 'AQBT-' ),
135 array( 'allcategories' => array(
136 array( '*' => 'AQBT-Cat' ),
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' ),
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' ),
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' ),
163 private static $generatorAllpages = array(
164 array( 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ),
165 array( 'pages' => array(
169 'title' => 'AQBT-All' ),
173 'title' => 'AQBT-Categories' ),
177 'title' => 'AQBT-Links' ),
181 'title' => 'AQBT-Templates' ),
185 private static $generatorLinks = array(
186 array( 'generator' => 'links', 'titles' => 'AQBT-Links' ),
187 array( 'pages' => array(
191 'title' => 'AQBT-All' ),
195 'title' => 'AQBT-Categories' ),
199 'title' => 'AQBT-Templates' ),
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' ),
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' ) ) ),
225 public function testProps() {
226 $this->check( self
::$links );
227 $this->check( self
::$templates );
228 $this->check( self
::$categories );
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 );
248 public function testAllTogether() {
250 // All props together
251 $this->check( $this->merge(
257 // All lists together
258 $this->check( $this->merge(
261 self
::$alltransclusions,
262 // This test is temporarily disabled until a sqlite bug is fixed
263 // self::$allcategories,
266 self
::$categorymembers
269 // All props+lists together
270 $this->check( $this->merge(
276 self
::$alltransclusions,
277 // This test is temporarily disabled until a sqlite bug is fixed
278 // self::$allcategories,
281 self
::$categorymembers
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,
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,
317 self
::$alltransclusions,
318 // This test is temporarily disabled until a sqlite bug is fixed
319 // self::$allcategories,
322 self
::$categorymembers ) );
328 public function testGeneratorRedirects() {
329 $this->editPage( 'AQBT-Target', 'test' );
330 $this->editPage( 'AQBT-Redir', '#REDIRECT [[AQBT-Target]]' );
332 array( 'generator' => 'backlinks', 'gbltitle' => 'AQBT-Target', 'redirects' => '1' ),
334 'redirects' => array(
336 'from' => 'AQBT-Redir',
337 'to' => 'AQBT-Target',
344 'title' => 'AQBT-Target',
352 * Recursively merges the expected values in the $item into the $all
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 );
360 $this->assertEquals( $all[$k], $v );
369 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
370 * @param $expected array expected values
371 * @param $result array returned values
373 private function assertQueryResults( $expected, $result ) {
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 );
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'] );
392 $this->assertEquals( $e['value'], $r['value'] );