6d4e3711df5b5d856f16e59d7d42223e2ee3449b
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' );
29 /** These tests validate basic functionality of the api query module
35 class ApiQueryBasicTest
extends ApiQueryTestBase
{
37 * Create a set of pages. These must not change, otherwise the tests might give wrong results.
38 * @see MediaWikiTestCase::addDBData()
40 function addDBData() {
42 if ( Title
::newFromText( 'AQBT-All' )->exists() ) {
46 // Ordering is important, as it will be returned in the same order as stored in the index
47 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
48 $this->editPage( 'AQBT-Categories', '[[Category:AQBT-Cat]]' );
49 $this->editPage( 'AQBT-Links', '[[AQBT-All]] [[AQBT-Categories]] [[AQBT-Templates]]' );
50 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
51 $this->editPage( 'AQBT-T', 'Content', '', NS_TEMPLATE
);
53 // Refresh due to the bug with listing transclusions as links if they don't exist
54 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' );
55 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' );
56 } catch ( Exception
$e ) {
57 $this->exceptionFromAddDBData
= $e;
61 private static $links = array(
62 array( 'prop' => 'links', 'titles' => 'AQBT-All' ),
63 array( 'pages' => array(
67 'title' => 'AQBT-All',
69 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
72 private static $templates = array(
73 array( 'prop' => 'templates', 'titles' => 'AQBT-All' ),
74 array( 'pages' => array(
78 'title' => 'AQBT-All',
80 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
83 private static $categories = array(
84 array( 'prop' => 'categories', 'titles' => 'AQBT-All' ),
85 array( 'pages' => array(
89 'title' => 'AQBT-All',
90 'categories' => array(
91 array( 'ns' => 14, 'title' => 'Category:AQBT-Cat' ),
94 private static $allpages = array(
95 array( 'list' => 'allpages', 'apprefix' => 'AQBT-' ),
96 array( 'allpages' => array(
97 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
98 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
99 array( 'pageid' => 3, 'ns' => 0, 'title' => 'AQBT-Links' ),
100 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
103 private static $alllinks = array(
104 array( 'list' => 'alllinks', 'alprefix' => 'AQBT-' ),
105 array( 'alllinks' => array(
106 array( 'ns' => 0, 'title' => 'AQBT-All' ),
107 array( 'ns' => 0, 'title' => 'AQBT-Categories' ),
108 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
109 array( 'ns' => 0, 'title' => 'AQBT-Templates' ),
112 private static $alltransclusions = array(
113 array( 'list' => 'alltransclusions', 'atprefix' => 'AQBT-' ),
114 array( 'alltransclusions' => array(
115 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
116 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
119 private static $allcategories = array(
120 array( 'list' => 'allcategories', 'acprefix' => 'AQBT-' ),
121 array( 'allcategories' => array(
122 array( '*' => 'AQBT-Cat' ),
125 private static $backlinks = array(
126 array( 'list' => 'backlinks', 'bltitle' => 'AQBT-Links' ),
127 array( 'backlinks' => array(
128 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
131 private static $embeddedin = array(
132 array( 'list' => 'embeddedin', 'eititle' => 'Template:AQBT-T' ),
133 array( 'embeddedin' => array(
134 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
135 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
138 private static $categorymembers = array(
139 array( 'list' => 'categorymembers', 'cmtitle' => 'Category:AQBT-Cat' ),
140 array( 'categorymembers' => array(
141 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
142 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
145 private static $generatorAllpages = array(
146 array( 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ),
147 array( 'pages' => array(
151 'title' => 'AQBT-All' ),
155 'title' => 'AQBT-Categories' ),
159 'title' => 'AQBT-Links' ),
163 'title' => 'AQBT-Templates' ),
166 private static $generatorLinks = array(
167 array( 'generator' => 'links', 'titles' => 'AQBT-Links' ),
168 array( 'pages' => array(
172 'title' => 'AQBT-All' ),
176 'title' => 'AQBT-Categories' ),
180 'title' => 'AQBT-Templates' ),
183 private static $generatorLinksPropLinks = array(
184 array( 'prop' => 'links' ),
185 array( 'pages' => array(
186 '1' => array( 'links' => array(
187 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
190 private static $generatorLinksPropTemplates = array(
191 array( 'prop' => 'templates' ),
192 array( 'pages' => array(
193 '1' => array( 'templates' => array(
194 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
195 '4' => array( 'templates' => array(
196 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
202 public function testProps() {
203 $this->check( self
::$links );
204 $this->check( self
::$templates );
205 $this->check( self
::$categories );
211 public function testLists() {
212 $this->check( self
::$allpages );
213 $this->check( self
::$alllinks );
214 $this->check( self
::$alltransclusions );
215 // This test is temporarily disabled until a sqlite bug is fixed
216 // $this->check( self::$allcategories );
217 $this->check( self
::$backlinks );
218 $this->check( self
::$embeddedin );
219 $this->check( self
::$categorymembers );
225 public function testAllTogether() {
227 // All props together
228 $this->check( $this->merge(
234 // All lists together
235 $this->check( $this->merge(
238 self
::$alltransclusions,
239 // This test is temporarily disabled until a sqlite bug is fixed
240 // self::$allcategories,
243 self
::$categorymembers
246 // All props+lists together
247 $this->check( $this->merge(
253 self
::$alltransclusions,
254 // This test is temporarily disabled until a sqlite bug is fixed
255 // self::$allcategories,
258 self
::$categorymembers
265 public function testGenerator() {
266 // generator=allpages
267 $this->check( self
::$generatorAllpages );
268 // generator=allpages & list=allpages
269 $this->check( $this->merge(
270 self
::$generatorAllpages,
273 $this->check( self
::$generatorLinks );
274 // generator=links & prop=links
275 $this->check( $this->merge(
276 self
::$generatorLinks,
277 self
::$generatorLinksPropLinks ) );
278 // generator=links & prop=templates
279 $this->check( $this->merge(
280 self
::$generatorLinks,
281 self
::$generatorLinksPropTemplates ) );
282 // generator=links & prop=links|templates
283 $this->check( $this->merge(
284 self
::$generatorLinks,
285 self
::$generatorLinksPropLinks,
286 self
::$generatorLinksPropTemplates ) );
287 // generator=links & prop=links|templates & list=allpages|...
288 $this->check( $this->merge(
289 self
::$generatorLinks,
290 self
::$generatorLinksPropLinks,
291 self
::$generatorLinksPropTemplates,
294 self
::$alltransclusions,
295 // This test is temporarily disabled until a sqlite bug is fixed
296 // self::$allcategories,
299 self
::$categorymembers ) );
303 * Recursively merges the expected values in the $item into the $all
305 private function mergeExpected( &$all, $item ) {
306 foreach ( $item as $k => $v ) {
307 if ( array_key_exists( $k, $all ) ) {
308 if ( is_array( $all[$k] ) ) {
309 $this->mergeExpected( $all[$k], $v );
311 $this->assertEquals( $all[$k], $v );
320 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
321 * @param $expected array expected values
322 * @param $result array returned values
324 private function assertQueryResults( $expected, $result ) {
328 $e = each( $expected );
329 $r = each( $result );
330 // If either of the arrays is shorter, abort. If both are done, success.
331 $this->assertEquals( (bool)$e, (bool)$r );
335 // continue only if keys are identical or both keys are numeric
336 $this->assertTrue( $e['key'] === $r['key'] ||
( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
337 // don't compare pageids
338 if ( $e['key'] !== 'pageid' ) {
339 // If values are arrays, compare recursively, otherwise compare with ===
340 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
341 $this->assertQueryResults( $e['value'], $r['value'] );
343 $this->assertEquals( $e['value'], $r['value'] );